src/video/SDL_shape.c
author egottlieb
Sat, 14 Aug 2010 20:29:51 -0400
changeset 4850 14870d46ee2d
parent 4849 0b918c186938
child 4851 5624fb0190b5
permissions -rw-r--r--
Mostly seem to have fixed SDL_CalculateShapeTree. Mostly. There still seem to be some rendering artifacts, stepping effects, and such on Windows -- which indicates subtle logic errors in SDL_CalculateShapeTree().

Oh well, back into the salt mine.
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) {
egottlieb@4849
    35
    SDL_Window *result = SDL_CreateWindow(title,x,y,w,h,flags | SDL_WINDOW_BORDERLESS & !SDL_WINDOW_FULLSCREEN & !SDL_WINDOW_SHOWN);
eligottlieb@4843
    36
    if(result != NULL) {
eligottlieb@4843
    37
        result->shaper = result->display->device->shape_driver.CreateShaper(result);
eligottlieb@4843
    38
        if(result->shaper != NULL) {
eligottlieb@4843
    39
            result->shaper->usershownflag = flags & SDL_WINDOW_SHOWN;
eligottlieb@4843
    40
            result->shaper->mode.mode = ShapeModeDefault;
eligottlieb@4843
    41
            result->shaper->mode.parameters.binarizationCutoff = 1;
eligottlieb@4843
    42
            result->shaper->hasshape = SDL_FALSE;
eligottlieb@4843
    43
            return result;
eligottlieb@4843
    44
        }
eligottlieb@4843
    45
        else {
eligottlieb@4843
    46
            SDL_DestroyWindow(result);
eligottlieb@4843
    47
            return NULL;
eligottlieb@4843
    48
        }
eligottlieb@4843
    49
    }
eligottlieb@4843
    50
    else
eligottlieb@4843
    51
        return NULL;
eligottlieb@4766
    52
}
eligottlieb@4766
    53
eligottlieb@4843
    54
SDL_bool
eligottlieb@4843
    55
SDL_IsShapedWindow(const SDL_Window *window) {
eligottlieb@4843
    56
    if(window == NULL)
eligottlieb@4843
    57
        return SDL_FALSE;
eligottlieb@4843
    58
    else
eligottlieb@4843
    59
        return (SDL_bool)(window->shaper != NULL);
eligottlieb@4782
    60
}
eligottlieb@4782
    61
eligottlieb@4813
    62
/* REQUIRES that bitmap point to a w-by-h bitmap with ppb pixels-per-byte. */
eligottlieb@4843
    63
void
eligottlieb@4843
    64
SDL_CalculateShapeBitmap(SDL_WindowShapeMode mode,SDL_Surface *shape,Uint8* bitmap,Uint8 ppb) {
eligottlieb@4843
    65
    int x = 0;
eligottlieb@4843
    66
    int y = 0;
eligottlieb@4843
    67
    Uint8 r = 0,g = 0,b = 0,alpha = 0;
eligottlieb@4843
    68
    Uint8* pixel = NULL;
eligottlieb@4843
    69
    Uint32 bitmap_pixel,pixel_value = 0,mask_value = 0;
eligottlieb@4843
    70
    SDL_Color key;
eligottlieb@4843
    71
    if(SDL_MUSTLOCK(shape))
eligottlieb@4843
    72
        SDL_LockSurface(shape);
eligottlieb@4843
    73
    pixel = (Uint8*)shape->pixels;
eligottlieb@4843
    74
    for(y = 0;y<shape->h;y++) {
eligottlieb@4843
    75
        for(x=0;x<shape->w;x++) {
eligottlieb@4843
    76
            alpha = 0;
eligottlieb@4843
    77
            pixel_value = 0;
eligottlieb@4843
    78
            pixel = (Uint8 *)(shape->pixels) + (y*shape->pitch) + (x*shape->format->BytesPerPixel);
eligottlieb@4843
    79
            switch(shape->format->BytesPerPixel) {
eligottlieb@4843
    80
                case(1):
eligottlieb@4843
    81
                    pixel_value = *(Uint8*)pixel;
eligottlieb@4843
    82
                    break;
eligottlieb@4843
    83
                case(2):
eligottlieb@4843
    84
                    pixel_value = *(Uint16*)pixel;
eligottlieb@4843
    85
                    break;
eligottlieb@4843
    86
                case(3):
eligottlieb@4843
    87
                    pixel_value = *(Uint32*)pixel & (~shape->format->Amask);
eligottlieb@4843
    88
                    break;
eligottlieb@4843
    89
                case(4):
eligottlieb@4843
    90
                    pixel_value = *(Uint32*)pixel;
eligottlieb@4843
    91
                    break;
eligottlieb@4843
    92
            }
eligottlieb@4843
    93
            SDL_GetRGBA(pixel_value,shape->format,&r,&g,&b,&alpha);
eligottlieb@4843
    94
            bitmap_pixel = y*shape->w + x;
eligottlieb@4843
    95
            switch(mode.mode) {
eligottlieb@4843
    96
                case(ShapeModeDefault):
eligottlieb@4843
    97
                    mask_value = (alpha >= 1 ? 1 : 0);
eligottlieb@4843
    98
                    break;
eligottlieb@4843
    99
                case(ShapeModeBinarizeAlpha):
eligottlieb@4843
   100
                    mask_value = (alpha >= mode.parameters.binarizationCutoff ? 1 : 0);
eligottlieb@4843
   101
                    break;
eligottlieb@4843
   102
                case(ShapeModeReverseBinarizeAlpha):
eligottlieb@4843
   103
                    mask_value = (alpha <= mode.parameters.binarizationCutoff ? 1 : 0);
eligottlieb@4843
   104
                    break;
eligottlieb@4843
   105
                case(ShapeModeColorKey):
eligottlieb@4843
   106
                    key = mode.parameters.colorKey;
eligottlieb@4846
   107
                    mask_value = ((key.r != r || key.g != g || key.b != b) ? 1 : 0);
eligottlieb@4843
   108
                    break;
eligottlieb@4843
   109
            }
eligottlieb@4843
   110
            bitmap[bitmap_pixel / ppb] |= mask_value << (7 - ((ppb - 1) - (bitmap_pixel % ppb)));
eligottlieb@4843
   111
        }
eligottlieb@4843
   112
    }
eligottlieb@4843
   113
    if(SDL_MUSTLOCK(shape))
eligottlieb@4843
   114
        SDL_UnlockSurface(shape);
eligottlieb@4769
   115
}
eligottlieb@4769
   116
eligottlieb@4843
   117
SDL_ShapeTree*
eligottlieb@4843
   118
RecursivelyCalculateShapeTree(SDL_WindowShapeMode mode,SDL_Surface* mask,SDL_Rect dimensions) {
eligottlieb@4843
   119
    int x = 0,y = 0;
eligottlieb@4843
   120
    Uint8* pixel = NULL;
eligottlieb@4843
   121
    Uint32 pixel_value = 0;
eligottlieb@4843
   122
    Uint8 r = 0,g = 0,b = 0,a = 0;
eligottlieb@4843
   123
    SDL_bool pixel_opaque = SDL_FALSE;
eligottlieb@4843
   124
    int last_opaque = -1;
eligottlieb@4843
   125
    SDL_Color key;
eligottlieb@4843
   126
    SDL_ShapeTree* result = (SDL_ShapeTree*)SDL_malloc(sizeof(SDL_ShapeTree));
eligottlieb@4843
   127
    SDL_Rect next = {0,0,0,0};
eligottlieb@4843
   128
    for(y=dimensions.y;y<dimensions.y + dimensions.h;y++)
eligottlieb@4843
   129
        for(x=dimensions.x;x<dimensions.x + dimensions.w;x++) {
eligottlieb@4843
   130
            pixel_value = 0;
eligottlieb@4843
   131
            pixel = (Uint8 *)(mask->pixels) + (y*mask->pitch) + (x*mask->format->BytesPerPixel);
eligottlieb@4843
   132
            switch(mask->format->BytesPerPixel) {
eligottlieb@4843
   133
                case(1):
eligottlieb@4843
   134
                    pixel_value = *(Uint8*)pixel;
eligottlieb@4843
   135
                    break;
eligottlieb@4843
   136
                case(2):
eligottlieb@4843
   137
                    pixel_value = *(Uint16*)pixel;
eligottlieb@4843
   138
                    break;
eligottlieb@4843
   139
                case(3):
eligottlieb@4843
   140
                    pixel_value = *(Uint32*)pixel & (~mask->format->Amask);
eligottlieb@4843
   141
                    break;
eligottlieb@4843
   142
                case(4):
eligottlieb@4843
   143
                    pixel_value = *(Uint32*)pixel;
eligottlieb@4843
   144
                    break;
eligottlieb@4843
   145
            }
eligottlieb@4843
   146
            SDL_GetRGBA(pixel_value,mask->format,&r,&g,&b,&a);
eligottlieb@4843
   147
            switch(mode.mode) {
eligottlieb@4843
   148
                case(ShapeModeDefault):
eligottlieb@4843
   149
                    pixel_opaque = (a >= 1 ? SDL_TRUE : SDL_FALSE);
eligottlieb@4843
   150
                    break;
eligottlieb@4843
   151
                case(ShapeModeBinarizeAlpha):
eligottlieb@4843
   152
                    pixel_opaque = (a >= mode.parameters.binarizationCutoff ? SDL_TRUE : SDL_FALSE);
eligottlieb@4843
   153
                    break;
eligottlieb@4843
   154
                case(ShapeModeReverseBinarizeAlpha):
eligottlieb@4843
   155
                    pixel_opaque = (a <= mode.parameters.binarizationCutoff ? SDL_TRUE : SDL_FALSE);
eligottlieb@4843
   156
                    break;
eligottlieb@4843
   157
                case(ShapeModeColorKey):
eligottlieb@4843
   158
                    key = mode.parameters.colorKey;
egottlieb@4850
   159
                    pixel_opaque = ((key.r != r || key.g != g || key.b != b) ? SDL_TRUE : SDL_FALSE);
eligottlieb@4843
   160
                    break;
eligottlieb@4843
   161
            }
eligottlieb@4843
   162
            if(last_opaque == -1)
eligottlieb@4843
   163
                last_opaque = pixel_opaque;
eligottlieb@4843
   164
            if(last_opaque != pixel_opaque) {
eligottlieb@4843
   165
                result->kind = QuadShape;
eligottlieb@4843
   166
                //These will stay the same.
eligottlieb@4843
   167
                next.w = dimensions.w / 2;
eligottlieb@4843
   168
                next.h = dimensions.h / 2;
eligottlieb@4843
   169
                //These will change from recursion to recursion.
eligottlieb@4843
   170
                next.x = dimensions.x;
eligottlieb@4843
   171
                next.y = dimensions.y;
eligottlieb@4843
   172
                result->data.children.upleft = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next);
egottlieb@4850
   173
                next.x += next.w;
eligottlieb@4843
   174
                //Unneeded: next.y = dimensions.y;
eligottlieb@4843
   175
                result->data.children.upright = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next);
eligottlieb@4843
   176
                next.x = dimensions.x;
egottlieb@4850
   177
                next.y += next.h;
eligottlieb@4843
   178
                result->data.children.downleft = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next);
egottlieb@4850
   179
                next.x += next.w;
egottlieb@4850
   180
                //Unneeded: next.y = dimensions.y + dimensions.h /2;
eligottlieb@4843
   181
                result->data.children.downright = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next);
eligottlieb@4843
   182
                return result;
eligottlieb@4843
   183
            }
eligottlieb@4843
   184
        }
eligottlieb@4843
   185
    //If we never recursed, all the pixels in this quadrant have the same "value".
eligottlieb@4843
   186
    result->kind = (last_opaque == SDL_TRUE ? OpaqueShape : TransparentShape);
eligottlieb@4843
   187
    result->data.shape = dimensions;
eligottlieb@4843
   188
    return result;
eligottlieb@4813
   189
}
eligottlieb@4813
   190
eligottlieb@4843
   191
SDL_ShapeTree*
eligottlieb@4843
   192
SDL_CalculateShapeTree(SDL_WindowShapeMode mode,SDL_Surface* shape) {
eligottlieb@4843
   193
    SDL_Rect dimensions = {0,0,shape->w,shape->h};
eligottlieb@4843
   194
    SDL_ShapeTree* result = NULL;
eligottlieb@4843
   195
    if(SDL_MUSTLOCK(shape))
eligottlieb@4843
   196
        SDL_LockSurface(shape);
eligottlieb@4843
   197
    result = RecursivelyCalculateShapeTree(mode,shape,dimensions);
eligottlieb@4843
   198
    if(SDL_MUSTLOCK(shape))
eligottlieb@4843
   199
        SDL_UnlockSurface(shape);
eligottlieb@4843
   200
    return result;
eligottlieb@4813
   201
}
eligottlieb@4813
   202
eligottlieb@4843
   203
void
eligottlieb@4843
   204
SDL_TraverseShapeTree(SDL_ShapeTree *tree,SDL_TraversalFunction function,void* closure) {
eligottlieb@4843
   205
    SDL_assert(tree != NULL);
eligottlieb@4843
   206
    if(tree->kind == QuadShape) {
eligottlieb@4843
   207
        SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.upleft,function,closure);
eligottlieb@4843
   208
        SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.upright,function,closure);
eligottlieb@4843
   209
        SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.downleft,function,closure);
eligottlieb@4843
   210
        SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.downright,function,closure);
eligottlieb@4843
   211
    }
eligottlieb@4843
   212
    else
eligottlieb@4843
   213
        function(tree,closure);
eligottlieb@4813
   214
}
eligottlieb@4813
   215
eligottlieb@4843
   216
void
egottlieb@4850
   217
SDL_FreeShapeTree(SDL_ShapeTree** shape_tree) {
egottlieb@4850
   218
    if((*shape_tree)->kind == QuadShape) {
egottlieb@4850
   219
        SDL_FreeShapeTree((SDL_ShapeTree **)&(*shape_tree)->data.children.upleft);
egottlieb@4850
   220
        SDL_FreeShapeTree((SDL_ShapeTree **)&(*shape_tree)->data.children.upright);
egottlieb@4850
   221
        SDL_FreeShapeTree((SDL_ShapeTree **)&(*shape_tree)->data.children.downleft);
egottlieb@4850
   222
        SDL_FreeShapeTree((SDL_ShapeTree **)&(*shape_tree)->data.children.downright);
eligottlieb@4843
   223
    }
egottlieb@4850
   224
    SDL_free(*shape_tree);
egottlieb@4850
   225
    *shape_tree = NULL;
eligottlieb@4813
   226
}
eligottlieb@4813
   227
eligottlieb@4843
   228
int
egottlieb@4849
   229
SDL_SetWindowShape(SDL_Window *window,SDL_Surface *shape,SDL_WindowShapeMode *shape_mode) {
eligottlieb@4843
   230
    int result;
eligottlieb@4843
   231
    if(window == NULL || !SDL_IsShapedWindow(window))
eligottlieb@4843
   232
        //The window given was not a shapeable window.
eligottlieb@4843
   233
        return SDL_NONSHAPEABLE_WINDOW;
eligottlieb@4843
   234
    if(shape == NULL)
eligottlieb@4843
   235
        //Invalid shape argument.
eligottlieb@4843
   236
        return SDL_INVALID_SHAPE_ARGUMENT;
eligottlieb@4843
   237
    
egottlieb@4849
   238
    if(shape_mode != NULL)
egottlieb@4849
   239
        window->shaper->mode = *shape_mode;
egottlieb@4849
   240
    result = window->display->device->shape_driver.SetWindowShape(window->shaper,shape,shape_mode);
eligottlieb@4843
   241
    window->shaper->hasshape = SDL_TRUE;
eligottlieb@4843
   242
    if((window->shaper->usershownflag & SDL_WINDOW_SHOWN) == SDL_WINDOW_SHOWN) {
eligottlieb@4843
   243
        SDL_ShowWindow(window);
eligottlieb@4843
   244
        window->shaper->usershownflag &= !SDL_WINDOW_SHOWN;
eligottlieb@4843
   245
    }
eligottlieb@4843
   246
    return result;
eligottlieb@4782
   247
}
eligottlieb@4782
   248
eligottlieb@4843
   249
SDL_bool
eligottlieb@4843
   250
SDL_WindowHasAShape(SDL_Window *window) {
eligottlieb@4843
   251
    if (window == NULL || !SDL_IsShapedWindow(window))
eligottlieb@4843
   252
        return SDL_FALSE;
eligottlieb@4843
   253
    return window->shaper->hasshape;
eligottlieb@4778
   254
}
eligottlieb@4778
   255
eligottlieb@4843
   256
int
egottlieb@4849
   257
SDL_GetShapedWindowMode(SDL_Window *window,SDL_WindowShapeMode *shape_mode) {
eligottlieb@4843
   258
    if(window != NULL && SDL_IsShapedWindow(window)) {
egottlieb@4849
   259
        if(shape_mode == NULL) {
eligottlieb@4843
   260
            if(SDL_WindowHasAShape(window))
eligottlieb@4843
   261
                //The window given has a shape.
eligottlieb@4843
   262
                return 0;
eligottlieb@4843
   263
            else
eligottlieb@4843
   264
                //The window given is shapeable but lacks a shape.
eligottlieb@4843
   265
                return SDL_WINDOW_LACKS_SHAPE;
eligottlieb@4843
   266
        }
eligottlieb@4843
   267
        else {
egottlieb@4849
   268
            *shape_mode = window->shaper->mode;
eligottlieb@4843
   269
            return 0;
eligottlieb@4843
   270
        }
eligottlieb@4843
   271
    }
eligottlieb@4843
   272
    else
eligottlieb@4843
   273
        //The window given is not a valid shapeable window.
eligottlieb@4843
   274
        return SDL_NONSHAPEABLE_WINDOW;
eligottlieb@4778
   275
}