src/video/SDL_shape.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 08 Feb 2011 16:50:51 -0800
changeset 5229 c015d3e63631
parent 4861 b67815cf9f25
child 5246 58265e606e4e
permissions -rw-r--r--
Fixed setting the texture unit, still doesn't work.
eligottlieb@4765
     1
/*
eligottlieb@4765
     2
    SDL - Simple DirectMedia Layer
eligottlieb@4765
     3
    Copyright (C) 2010 Eli Gottlieb
eligottlieb@4765
     4
eligottlieb@4765
     5
    This library is free software; you can redistribute it and/or
eligottlieb@4765
     6
    modify it under the terms of the GNU Lesser General Public
eligottlieb@4765
     7
    License as published by the Free Software Foundation; either
eligottlieb@4765
     8
    version 2.1 of the License, or (at your option) any later version.
eligottlieb@4765
     9
eligottlieb@4765
    10
    This library is distributed in the hope that it will be useful,
eligottlieb@4765
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
eligottlieb@4765
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
eligottlieb@4765
    13
    Lesser General Public License for more details.
eligottlieb@4765
    14
eligottlieb@4765
    15
    You should have received a copy of the GNU Lesser General Public
eligottlieb@4765
    16
    License along with this library; if not, write to the Free Software
eligottlieb@4765
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
eligottlieb@4765
    18
eligottlieb@4765
    19
    Eli Gottlieb
eligottlieb@4765
    20
    eligottlieb@gmail.com
eligottlieb@4765
    21
*/
eligottlieb@4769
    22
#include "SDL_config.h"
eligottlieb@4776
    23
eligottlieb@4769
    24
#include "SDL.h"
eligottlieb@4841
    25
#include "SDL_assert.h"
eligottlieb@4769
    26
#include "SDL_video.h"
eligottlieb@4769
    27
#include "SDL_sysvideo.h"
eligottlieb@4782
    28
#include "SDL_pixels.h"
eligottlieb@4782
    29
#include "SDL_surface.h"
eligottlieb@4765
    30
#include "SDL_shape.h"
eligottlieb@4841
    31
#include "SDL_shape_internals.h"
eligottlieb@4766
    32
eligottlieb@4843
    33
SDL_Window*
eligottlieb@4843
    34
SDL_CreateShapedWindow(const char *title,unsigned int x,unsigned int y,unsigned int w,unsigned int h,Uint32 flags) {
eligottlieb@4855
    35
    SDL_Window *result = NULL;
egottlieb@4860
    36
    result = SDL_CreateWindow(title,-1000,-1000,w,h,(flags | SDL_WINDOW_BORDERLESS) & (~SDL_WINDOW_FULLSCREEN) & (~SDL_WINDOW_RESIZABLE) /*& (~SDL_WINDOW_SHOWN)*/);
eligottlieb@4843
    37
    if(result != NULL) {
eligottlieb@4843
    38
        result->shaper = result->display->device->shape_driver.CreateShaper(result);
eligottlieb@4843
    39
        if(result->shaper != NULL) {
eligottlieb@4851
    40
            result->shaper->userx = x;
eligottlieb@4851
    41
            result->shaper->usery = y;
eligottlieb@4843
    42
            result->shaper->mode.mode = ShapeModeDefault;
eligottlieb@4843
    43
            result->shaper->mode.parameters.binarizationCutoff = 1;
eligottlieb@4843
    44
            result->shaper->hasshape = SDL_FALSE;
eligottlieb@4843
    45
            return result;
eligottlieb@4843
    46
        }
eligottlieb@4843
    47
        else {
eligottlieb@4843
    48
            SDL_DestroyWindow(result);
eligottlieb@4843
    49
            return NULL;
eligottlieb@4843
    50
        }
eligottlieb@4843
    51
    }
eligottlieb@4843
    52
    else
eligottlieb@4843
    53
        return NULL;
eligottlieb@4766
    54
}
eligottlieb@4766
    55
eligottlieb@4843
    56
SDL_bool
eligottlieb@4843
    57
SDL_IsShapedWindow(const SDL_Window *window) {
eligottlieb@4843
    58
    if(window == NULL)
eligottlieb@4843
    59
        return SDL_FALSE;
eligottlieb@4843
    60
    else
eligottlieb@4843
    61
        return (SDL_bool)(window->shaper != NULL);
eligottlieb@4782
    62
}
eligottlieb@4782
    63
eligottlieb@4813
    64
/* REQUIRES that bitmap point to a w-by-h bitmap with ppb pixels-per-byte. */
eligottlieb@4843
    65
void
eligottlieb@4843
    66
SDL_CalculateShapeBitmap(SDL_WindowShapeMode mode,SDL_Surface *shape,Uint8* bitmap,Uint8 ppb) {
eligottlieb@4843
    67
    int x = 0;
eligottlieb@4843
    68
    int y = 0;
eligottlieb@4843
    69
    Uint8 r = 0,g = 0,b = 0,alpha = 0;
eligottlieb@4843
    70
    Uint8* pixel = NULL;
eligottlieb@4843
    71
    Uint32 bitmap_pixel,pixel_value = 0,mask_value = 0;
eligottlieb@4843
    72
    SDL_Color key;
eligottlieb@4843
    73
    if(SDL_MUSTLOCK(shape))
eligottlieb@4843
    74
        SDL_LockSurface(shape);
eligottlieb@4843
    75
    pixel = (Uint8*)shape->pixels;
eligottlieb@4843
    76
    for(y = 0;y<shape->h;y++) {
eligottlieb@4843
    77
        for(x=0;x<shape->w;x++) {
eligottlieb@4843
    78
            alpha = 0;
eligottlieb@4843
    79
            pixel_value = 0;
eligottlieb@4843
    80
            pixel = (Uint8 *)(shape->pixels) + (y*shape->pitch) + (x*shape->format->BytesPerPixel);
eligottlieb@4843
    81
            switch(shape->format->BytesPerPixel) {
eligottlieb@4843
    82
                case(1):
eligottlieb@4843
    83
                    pixel_value = *(Uint8*)pixel;
eligottlieb@4843
    84
                    break;
eligottlieb@4843
    85
                case(2):
eligottlieb@4843
    86
                    pixel_value = *(Uint16*)pixel;
eligottlieb@4843
    87
                    break;
eligottlieb@4843
    88
                case(3):
eligottlieb@4843
    89
                    pixel_value = *(Uint32*)pixel & (~shape->format->Amask);
eligottlieb@4843
    90
                    break;
eligottlieb@4843
    91
                case(4):
eligottlieb@4843
    92
                    pixel_value = *(Uint32*)pixel;
eligottlieb@4843
    93
                    break;
eligottlieb@4843
    94
            }
eligottlieb@4843
    95
            SDL_GetRGBA(pixel_value,shape->format,&r,&g,&b,&alpha);
eligottlieb@4843
    96
            bitmap_pixel = y*shape->w + x;
eligottlieb@4843
    97
            switch(mode.mode) {
eligottlieb@4843
    98
                case(ShapeModeDefault):
eligottlieb@4843
    99
                    mask_value = (alpha >= 1 ? 1 : 0);
eligottlieb@4843
   100
                    break;
eligottlieb@4843
   101
                case(ShapeModeBinarizeAlpha):
eligottlieb@4843
   102
                    mask_value = (alpha >= mode.parameters.binarizationCutoff ? 1 : 0);
eligottlieb@4843
   103
                    break;
eligottlieb@4843
   104
                case(ShapeModeReverseBinarizeAlpha):
eligottlieb@4843
   105
                    mask_value = (alpha <= mode.parameters.binarizationCutoff ? 1 : 0);
eligottlieb@4843
   106
                    break;
eligottlieb@4843
   107
                case(ShapeModeColorKey):
eligottlieb@4843
   108
                    key = mode.parameters.colorKey;
eligottlieb@4846
   109
                    mask_value = ((key.r != r || key.g != g || key.b != b) ? 1 : 0);
eligottlieb@4843
   110
                    break;
eligottlieb@4843
   111
            }
eligottlieb@4843
   112
            bitmap[bitmap_pixel / ppb] |= mask_value << (7 - ((ppb - 1) - (bitmap_pixel % ppb)));
eligottlieb@4843
   113
        }
eligottlieb@4843
   114
    }
eligottlieb@4843
   115
    if(SDL_MUSTLOCK(shape))
eligottlieb@4843
   116
        SDL_UnlockSurface(shape);
eligottlieb@4766
   117
}
eligottlieb@4766
   118
eligottlieb@4843
   119
SDL_ShapeTree*
eligottlieb@4843
   120
RecursivelyCalculateShapeTree(SDL_WindowShapeMode mode,SDL_Surface* mask,SDL_Rect dimensions) {
eligottlieb@4843
   121
    int x = 0,y = 0;
eligottlieb@4843
   122
    Uint8* pixel = NULL;
eligottlieb@4843
   123
    Uint32 pixel_value = 0;
eligottlieb@4843
   124
    Uint8 r = 0,g = 0,b = 0,a = 0;
eligottlieb@4843
   125
    SDL_bool pixel_opaque = SDL_FALSE;
eligottlieb@4843
   126
    int last_opaque = -1;
eligottlieb@4843
   127
    SDL_Color key;
eligottlieb@4843
   128
    SDL_ShapeTree* result = (SDL_ShapeTree*)SDL_malloc(sizeof(SDL_ShapeTree));
eligottlieb@4843
   129
    SDL_Rect next = {0,0,0,0};
egottlieb@4852
   130
    for(y=dimensions.y;y<dimensions.y + dimensions.h;y++) {
eligottlieb@4843
   131
        for(x=dimensions.x;x<dimensions.x + dimensions.w;x++) {
eligottlieb@4843
   132
            pixel_value = 0;
eligottlieb@4843
   133
            pixel = (Uint8 *)(mask->pixels) + (y*mask->pitch) + (x*mask->format->BytesPerPixel);
eligottlieb@4843
   134
            switch(mask->format->BytesPerPixel) {
eligottlieb@4843
   135
                case(1):
eligottlieb@4843
   136
                    pixel_value = *(Uint8*)pixel;
eligottlieb@4843
   137
                    break;
eligottlieb@4843
   138
                case(2):
eligottlieb@4843
   139
                    pixel_value = *(Uint16*)pixel;
eligottlieb@4843
   140
                    break;
eligottlieb@4843
   141
                case(3):
eligottlieb@4843
   142
                    pixel_value = *(Uint32*)pixel & (~mask->format->Amask);
eligottlieb@4843
   143
                    break;
eligottlieb@4843
   144
                case(4):
eligottlieb@4843
   145
                    pixel_value = *(Uint32*)pixel;
eligottlieb@4843
   146
                    break;
eligottlieb@4843
   147
            }
eligottlieb@4843
   148
            SDL_GetRGBA(pixel_value,mask->format,&r,&g,&b,&a);
eligottlieb@4843
   149
            switch(mode.mode) {
eligottlieb@4843
   150
                case(ShapeModeDefault):
eligottlieb@4843
   151
                    pixel_opaque = (a >= 1 ? SDL_TRUE : SDL_FALSE);
eligottlieb@4843
   152
                    break;
eligottlieb@4843
   153
                case(ShapeModeBinarizeAlpha):
eligottlieb@4843
   154
                    pixel_opaque = (a >= mode.parameters.binarizationCutoff ? SDL_TRUE : SDL_FALSE);
eligottlieb@4843
   155
                    break;
eligottlieb@4843
   156
                case(ShapeModeReverseBinarizeAlpha):
eligottlieb@4843
   157
                    pixel_opaque = (a <= mode.parameters.binarizationCutoff ? SDL_TRUE : SDL_FALSE);
eligottlieb@4843
   158
                    break;
eligottlieb@4843
   159
                case(ShapeModeColorKey):
eligottlieb@4843
   160
                    key = mode.parameters.colorKey;
egottlieb@4850
   161
                    pixel_opaque = ((key.r != r || key.g != g || key.b != b) ? SDL_TRUE : SDL_FALSE);
eligottlieb@4843
   162
                    break;
eligottlieb@4843
   163
            }
eligottlieb@4843
   164
            if(last_opaque == -1)
eligottlieb@4843
   165
                last_opaque = pixel_opaque;
eligottlieb@4843
   166
            if(last_opaque != pixel_opaque) {
eligottlieb@4843
   167
                result->kind = QuadShape;
eligottlieb@4843
   168
                //These will stay the same.
eligottlieb@4843
   169
                next.w = dimensions.w / 2;
eligottlieb@4843
   170
                next.h = dimensions.h / 2;
eligottlieb@4843
   171
                //These will change from recursion to recursion.
eligottlieb@4843
   172
                next.x = dimensions.x;
eligottlieb@4843
   173
                next.y = dimensions.y;
eligottlieb@4843
   174
                result->data.children.upleft = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next);
egottlieb@4850
   175
                next.x += next.w;
eligottlieb@4843
   176
                //Unneeded: next.y = dimensions.y;
eligottlieb@4843
   177
                result->data.children.upright = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next);
eligottlieb@4843
   178
                next.x = dimensions.x;
egottlieb@4850
   179
                next.y += next.h;
eligottlieb@4843
   180
                result->data.children.downleft = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next);
egottlieb@4850
   181
                next.x += next.w;
egottlieb@4850
   182
                //Unneeded: next.y = dimensions.y + dimensions.h /2;
eligottlieb@4843
   183
                result->data.children.downright = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next);
eligottlieb@4843
   184
                return result;
eligottlieb@4843
   185
            }
eligottlieb@4843
   186
        }
egottlieb@4852
   187
    }
eligottlieb@4843
   188
    //If we never recursed, all the pixels in this quadrant have the same "value".
eligottlieb@4843
   189
    result->kind = (last_opaque == SDL_TRUE ? OpaqueShape : TransparentShape);
eligottlieb@4843
   190
    result->data.shape = dimensions;
eligottlieb@4843
   191
    return result;
eligottlieb@4813
   192
}
eligottlieb@4813
   193
eligottlieb@4843
   194
SDL_ShapeTree*
eligottlieb@4843
   195
SDL_CalculateShapeTree(SDL_WindowShapeMode mode,SDL_Surface* shape) {
eligottlieb@4843
   196
    SDL_Rect dimensions = {0,0,shape->w,shape->h};
eligottlieb@4843
   197
    SDL_ShapeTree* result = NULL;
eligottlieb@4843
   198
    if(SDL_MUSTLOCK(shape))
eligottlieb@4843
   199
        SDL_LockSurface(shape);
eligottlieb@4843
   200
    result = RecursivelyCalculateShapeTree(mode,shape,dimensions);
eligottlieb@4843
   201
    if(SDL_MUSTLOCK(shape))
eligottlieb@4843
   202
        SDL_UnlockSurface(shape);
eligottlieb@4843
   203
    return result;
eligottlieb@4813
   204
}
eligottlieb@4813
   205
eligottlieb@4843
   206
void
eligottlieb@4843
   207
SDL_TraverseShapeTree(SDL_ShapeTree *tree,SDL_TraversalFunction function,void* closure) {
eligottlieb@4843
   208
    SDL_assert(tree != NULL);
eligottlieb@4843
   209
    if(tree->kind == QuadShape) {
eligottlieb@4843
   210
        SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.upleft,function,closure);
eligottlieb@4843
   211
        SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.upright,function,closure);
eligottlieb@4843
   212
        SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.downleft,function,closure);
eligottlieb@4843
   213
        SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.downright,function,closure);
eligottlieb@4843
   214
    }
eligottlieb@4843
   215
    else
eligottlieb@4843
   216
        function(tree,closure);
eligottlieb@4813
   217
}
eligottlieb@4813
   218
eligottlieb@4843
   219
void
egottlieb@4850
   220
SDL_FreeShapeTree(SDL_ShapeTree** shape_tree) {
egottlieb@4850
   221
    if((*shape_tree)->kind == QuadShape) {
egottlieb@4850
   222
        SDL_FreeShapeTree((SDL_ShapeTree **)&(*shape_tree)->data.children.upleft);
egottlieb@4850
   223
        SDL_FreeShapeTree((SDL_ShapeTree **)&(*shape_tree)->data.children.upright);
egottlieb@4850
   224
        SDL_FreeShapeTree((SDL_ShapeTree **)&(*shape_tree)->data.children.downleft);
egottlieb@4850
   225
        SDL_FreeShapeTree((SDL_ShapeTree **)&(*shape_tree)->data.children.downright);
eligottlieb@4843
   226
    }
egottlieb@4850
   227
    SDL_free(*shape_tree);
egottlieb@4850
   228
    *shape_tree = NULL;
eligottlieb@4813
   229
}
eligottlieb@4813
   230
eligottlieb@4843
   231
int
egottlieb@4847
   232
SDL_SetWindowShape(SDL_Window *window,SDL_Surface *shape,SDL_WindowShapeMode *shape_mode) {
eligottlieb@4843
   233
    int result;
eligottlieb@4843
   234
    if(window == NULL || !SDL_IsShapedWindow(window))
eligottlieb@4843
   235
        //The window given was not a shapeable window.
eligottlieb@4843
   236
        return SDL_NONSHAPEABLE_WINDOW;
eligottlieb@4843
   237
    if(shape == NULL)
eligottlieb@4843
   238
        //Invalid shape argument.
eligottlieb@4843
   239
        return SDL_INVALID_SHAPE_ARGUMENT;
eligottlieb@4843
   240
    
egottlieb@4847
   241
    if(shape_mode != NULL)
egottlieb@4847
   242
        window->shaper->mode = *shape_mode;
egottlieb@4847
   243
    result = window->display->device->shape_driver.SetWindowShape(window->shaper,shape,shape_mode);
eligottlieb@4843
   244
    window->shaper->hasshape = SDL_TRUE;
egottlieb@4852
   245
    if(window->shaper->userx != 0 && window->shaper->usery != 0) {
eligottlieb@4851
   246
        SDL_SetWindowPosition(window,window->shaper->userx,window->shaper->usery);
eligottlieb@4851
   247
        window->shaper->userx = 0;
eligottlieb@4851
   248
        window->shaper->usery = 0;
eligottlieb@4843
   249
    }
eligottlieb@4843
   250
    return result;
eligottlieb@4782
   251
}
eligottlieb@4782
   252
eligottlieb@4843
   253
SDL_bool
eligottlieb@4843
   254
SDL_WindowHasAShape(SDL_Window *window) {
eligottlieb@4843
   255
    if (window == NULL || !SDL_IsShapedWindow(window))
eligottlieb@4843
   256
        return SDL_FALSE;
eligottlieb@4843
   257
    return window->shaper->hasshape;
eligottlieb@4778
   258
}
eligottlieb@4778
   259
eligottlieb@4843
   260
int
egottlieb@4847
   261
SDL_GetShapedWindowMode(SDL_Window *window,SDL_WindowShapeMode *shape_mode) {
eligottlieb@4843
   262
    if(window != NULL && SDL_IsShapedWindow(window)) {
egottlieb@4847
   263
        if(shape_mode == NULL) {
eligottlieb@4843
   264
            if(SDL_WindowHasAShape(window))
eligottlieb@4843
   265
                //The window given has a shape.
eligottlieb@4843
   266
                return 0;
eligottlieb@4843
   267
            else
eligottlieb@4843
   268
                //The window given is shapeable but lacks a shape.
eligottlieb@4843
   269
                return SDL_WINDOW_LACKS_SHAPE;
eligottlieb@4843
   270
        }
eligottlieb@4843
   271
        else {
egottlieb@4847
   272
            *shape_mode = window->shaper->mode;
eligottlieb@4843
   273
            return 0;
eligottlieb@4843
   274
        }
eligottlieb@4843
   275
    }
eligottlieb@4843
   276
    else
eligottlieb@4843
   277
        //The window given is not a valid shapeable window.
eligottlieb@4843
   278
        return SDL_NONSHAPEABLE_WINDOW;
eligottlieb@4778
   279
}