src/video/SDL_shape.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 28 May 2015 01:27:24 -0400
changeset 9663 647180a6caad
parent 9619 b94b6d0bff0f
child 9998 f67cf37e9cd4
permissions -rw-r--r--
I think this will be the time...
eligottlieb@4765
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@9619
     3
  Copyright (C) 1997-2015 Sam Lantinga <slouken@libsdl.org>
eligottlieb@4765
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
eligottlieb@4765
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
eligottlieb@4765
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
eligottlieb@4765
    20
*/
icculus@8093
    21
#include "../SDL_internal.h"
eligottlieb@4776
    22
eligottlieb@4769
    23
#include "SDL.h"
eligottlieb@4841
    24
#include "SDL_assert.h"
eligottlieb@4769
    25
#include "SDL_video.h"
eligottlieb@4769
    26
#include "SDL_sysvideo.h"
eligottlieb@4782
    27
#include "SDL_pixels.h"
eligottlieb@4782
    28
#include "SDL_surface.h"
eligottlieb@4765
    29
#include "SDL_shape.h"
eligottlieb@4841
    30
#include "SDL_shape_internals.h"
eligottlieb@4766
    31
eligottlieb@4843
    32
SDL_Window*
slouken@7191
    33
SDL_CreateShapedWindow(const char *title,unsigned int x,unsigned int y,unsigned int w,unsigned int h,Uint32 flags)
slouken@7191
    34
{
eligottlieb@4855
    35
    SDL_Window *result = NULL;
gabomdq@7678
    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) {
slouken@5246
    38
        result->shaper = SDL_GetVideoDevice()->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
slouken@7191
    57
SDL_IsShapedWindow(const SDL_Window *window)
slouken@7191
    58
{
eligottlieb@4843
    59
    if(window == NULL)
eligottlieb@4843
    60
        return SDL_FALSE;
eligottlieb@4843
    61
    else
eligottlieb@4843
    62
        return (SDL_bool)(window->shaper != NULL);
eligottlieb@4782
    63
}
eligottlieb@4782
    64
eligottlieb@4813
    65
/* REQUIRES that bitmap point to a w-by-h bitmap with ppb pixels-per-byte. */
eligottlieb@4843
    66
void
slouken@7191
    67
SDL_CalculateShapeBitmap(SDL_WindowShapeMode mode,SDL_Surface *shape,Uint8* bitmap,Uint8 ppb)
slouken@7191
    68
{
eligottlieb@4843
    69
    int x = 0;
eligottlieb@4843
    70
    int y = 0;
eligottlieb@4843
    71
    Uint8 r = 0,g = 0,b = 0,alpha = 0;
eligottlieb@4843
    72
    Uint8* pixel = NULL;
eligottlieb@4843
    73
    Uint32 bitmap_pixel,pixel_value = 0,mask_value = 0;
eligottlieb@4843
    74
    SDL_Color key;
eligottlieb@4843
    75
    if(SDL_MUSTLOCK(shape))
eligottlieb@4843
    76
        SDL_LockSurface(shape);
eligottlieb@4843
    77
    for(y = 0;y<shape->h;y++) {
eligottlieb@4843
    78
        for(x=0;x<shape->w;x++) {
eligottlieb@4843
    79
            alpha = 0;
eligottlieb@4843
    80
            pixel_value = 0;
eligottlieb@4843
    81
            pixel = (Uint8 *)(shape->pixels) + (y*shape->pitch) + (x*shape->format->BytesPerPixel);
eligottlieb@4843
    82
            switch(shape->format->BytesPerPixel) {
eligottlieb@4843
    83
                case(1):
eligottlieb@4843
    84
                    pixel_value = *(Uint8*)pixel;
eligottlieb@4843
    85
                    break;
eligottlieb@4843
    86
                case(2):
eligottlieb@4843
    87
                    pixel_value = *(Uint16*)pixel;
eligottlieb@4843
    88
                    break;
eligottlieb@4843
    89
                case(3):
eligottlieb@4843
    90
                    pixel_value = *(Uint32*)pixel & (~shape->format->Amask);
eligottlieb@4843
    91
                    break;
eligottlieb@4843
    92
                case(4):
eligottlieb@4843
    93
                    pixel_value = *(Uint32*)pixel;
eligottlieb@4843
    94
                    break;
eligottlieb@4843
    95
            }
eligottlieb@4843
    96
            SDL_GetRGBA(pixel_value,shape->format,&r,&g,&b,&alpha);
eligottlieb@4843
    97
            bitmap_pixel = y*shape->w + x;
eligottlieb@4843
    98
            switch(mode.mode) {
eligottlieb@4843
    99
                case(ShapeModeDefault):
eligottlieb@4843
   100
                    mask_value = (alpha >= 1 ? 1 : 0);
eligottlieb@4843
   101
                    break;
eligottlieb@4843
   102
                case(ShapeModeBinarizeAlpha):
eligottlieb@4843
   103
                    mask_value = (alpha >= mode.parameters.binarizationCutoff ? 1 : 0);
eligottlieb@4843
   104
                    break;
eligottlieb@4843
   105
                case(ShapeModeReverseBinarizeAlpha):
eligottlieb@4843
   106
                    mask_value = (alpha <= mode.parameters.binarizationCutoff ? 1 : 0);
eligottlieb@4843
   107
                    break;
eligottlieb@4843
   108
                case(ShapeModeColorKey):
eligottlieb@4843
   109
                    key = mode.parameters.colorKey;
eligottlieb@4846
   110
                    mask_value = ((key.r != r || key.g != g || key.b != b) ? 1 : 0);
eligottlieb@4843
   111
                    break;
eligottlieb@4843
   112
            }
eligottlieb@4843
   113
            bitmap[bitmap_pixel / ppb] |= mask_value << (7 - ((ppb - 1) - (bitmap_pixel % ppb)));
eligottlieb@4843
   114
        }
eligottlieb@4843
   115
    }
eligottlieb@4843
   116
    if(SDL_MUSTLOCK(shape))
eligottlieb@4843
   117
        SDL_UnlockSurface(shape);
eligottlieb@4766
   118
}
eligottlieb@4766
   119
slouken@6044
   120
static SDL_ShapeTree*
eligottlieb@4843
   121
RecursivelyCalculateShapeTree(SDL_WindowShapeMode mode,SDL_Surface* mask,SDL_Rect dimensions) {
eligottlieb@4843
   122
    int x = 0,y = 0;
eligottlieb@4843
   123
    Uint8* pixel = NULL;
eligottlieb@4843
   124
    Uint32 pixel_value = 0;
eligottlieb@4843
   125
    Uint8 r = 0,g = 0,b = 0,a = 0;
eligottlieb@4843
   126
    SDL_bool pixel_opaque = SDL_FALSE;
eligottlieb@4843
   127
    int last_opaque = -1;
eligottlieb@4843
   128
    SDL_Color key;
eligottlieb@4843
   129
    SDL_ShapeTree* result = (SDL_ShapeTree*)SDL_malloc(sizeof(SDL_ShapeTree));
eligottlieb@4843
   130
    SDL_Rect next = {0,0,0,0};
jorgen@9122
   131
egottlieb@4852
   132
    for(y=dimensions.y;y<dimensions.y + dimensions.h;y++) {
eligottlieb@4843
   133
        for(x=dimensions.x;x<dimensions.x + dimensions.w;x++) {
eligottlieb@4843
   134
            pixel_value = 0;
eligottlieb@4843
   135
            pixel = (Uint8 *)(mask->pixels) + (y*mask->pitch) + (x*mask->format->BytesPerPixel);
eligottlieb@4843
   136
            switch(mask->format->BytesPerPixel) {
eligottlieb@4843
   137
                case(1):
eligottlieb@4843
   138
                    pixel_value = *(Uint8*)pixel;
eligottlieb@4843
   139
                    break;
eligottlieb@4843
   140
                case(2):
eligottlieb@4843
   141
                    pixel_value = *(Uint16*)pixel;
eligottlieb@4843
   142
                    break;
eligottlieb@4843
   143
                case(3):
eligottlieb@4843
   144
                    pixel_value = *(Uint32*)pixel & (~mask->format->Amask);
eligottlieb@4843
   145
                    break;
eligottlieb@4843
   146
                case(4):
eligottlieb@4843
   147
                    pixel_value = *(Uint32*)pixel;
eligottlieb@4843
   148
                    break;
eligottlieb@4843
   149
            }
eligottlieb@4843
   150
            SDL_GetRGBA(pixel_value,mask->format,&r,&g,&b,&a);
eligottlieb@4843
   151
            switch(mode.mode) {
eligottlieb@4843
   152
                case(ShapeModeDefault):
eligottlieb@4843
   153
                    pixel_opaque = (a >= 1 ? SDL_TRUE : SDL_FALSE);
eligottlieb@4843
   154
                    break;
eligottlieb@4843
   155
                case(ShapeModeBinarizeAlpha):
eligottlieb@4843
   156
                    pixel_opaque = (a >= mode.parameters.binarizationCutoff ? SDL_TRUE : SDL_FALSE);
eligottlieb@4843
   157
                    break;
eligottlieb@4843
   158
                case(ShapeModeReverseBinarizeAlpha):
eligottlieb@4843
   159
                    pixel_opaque = (a <= mode.parameters.binarizationCutoff ? SDL_TRUE : SDL_FALSE);
eligottlieb@4843
   160
                    break;
eligottlieb@4843
   161
                case(ShapeModeColorKey):
eligottlieb@4843
   162
                    key = mode.parameters.colorKey;
egottlieb@4850
   163
                    pixel_opaque = ((key.r != r || key.g != g || key.b != b) ? SDL_TRUE : SDL_FALSE);
eligottlieb@4843
   164
                    break;
eligottlieb@4843
   165
            }
eligottlieb@4843
   166
            if(last_opaque == -1)
eligottlieb@4843
   167
                last_opaque = pixel_opaque;
eligottlieb@4843
   168
            if(last_opaque != pixel_opaque) {
jorgen@9122
   169
                const int halfwidth = dimensions.w / 2;
jorgen@9122
   170
                const int halfheight = dimensions.h / 2;
jorgen@9122
   171
eligottlieb@4843
   172
                result->kind = QuadShape;
jorgen@9122
   173
eligottlieb@4843
   174
                next.x = dimensions.x;
eligottlieb@4843
   175
                next.y = dimensions.y;
jorgen@9122
   176
                next.w = halfwidth;
jorgen@9122
   177
                next.h = halfheight;
eligottlieb@4843
   178
                result->data.children.upleft = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next);
jorgen@9122
   179
jorgen@9122
   180
                next.x = dimensions.x + halfwidth;
jorgen@9122
   181
                next.w = dimensions.w - halfwidth;
eligottlieb@4843
   182
                result->data.children.upright = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next);
jorgen@9122
   183
eligottlieb@4843
   184
                next.x = dimensions.x;
jorgen@9122
   185
                next.w = halfwidth;
jorgen@9122
   186
                next.y = dimensions.y + halfheight;
jorgen@9122
   187
                next.h = dimensions.h - halfheight;
eligottlieb@4843
   188
                result->data.children.downleft = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next);
jorgen@9122
   189
jorgen@9122
   190
                next.x = dimensions.x + halfwidth;
jorgen@9122
   191
                next.w = dimensions.w - halfwidth;
eligottlieb@4843
   192
                result->data.children.downright = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next);
jorgen@9122
   193
eligottlieb@4843
   194
                return result;
eligottlieb@4843
   195
            }
eligottlieb@4843
   196
        }
egottlieb@4852
   197
    }
jorgen@9122
   198
jorgen@9122
   199
slouken@7191
   200
    /* If we never recursed, all the pixels in this quadrant have the same "value". */
eligottlieb@4843
   201
    result->kind = (last_opaque == SDL_TRUE ? OpaqueShape : TransparentShape);
eligottlieb@4843
   202
    result->data.shape = dimensions;
eligottlieb@4843
   203
    return result;
eligottlieb@4813
   204
}
eligottlieb@4813
   205
eligottlieb@4843
   206
SDL_ShapeTree*
slouken@7191
   207
SDL_CalculateShapeTree(SDL_WindowShapeMode mode,SDL_Surface* shape)
slouken@7191
   208
{
eligottlieb@4843
   209
    SDL_Rect dimensions = {0,0,shape->w,shape->h};
eligottlieb@4843
   210
    SDL_ShapeTree* result = NULL;
eligottlieb@4843
   211
    if(SDL_MUSTLOCK(shape))
eligottlieb@4843
   212
        SDL_LockSurface(shape);
eligottlieb@4843
   213
    result = RecursivelyCalculateShapeTree(mode,shape,dimensions);
eligottlieb@4843
   214
    if(SDL_MUSTLOCK(shape))
eligottlieb@4843
   215
        SDL_UnlockSurface(shape);
eligottlieb@4843
   216
    return result;
eligottlieb@4813
   217
}
eligottlieb@4813
   218
eligottlieb@4843
   219
void
slouken@7191
   220
SDL_TraverseShapeTree(SDL_ShapeTree *tree,SDL_TraversalFunction function,void* closure)
slouken@7191
   221
{
eligottlieb@4843
   222
    SDL_assert(tree != NULL);
eligottlieb@4843
   223
    if(tree->kind == QuadShape) {
eligottlieb@4843
   224
        SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.upleft,function,closure);
eligottlieb@4843
   225
        SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.upright,function,closure);
eligottlieb@4843
   226
        SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.downleft,function,closure);
eligottlieb@4843
   227
        SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.downright,function,closure);
eligottlieb@4843
   228
    }
eligottlieb@4843
   229
    else
eligottlieb@4843
   230
        function(tree,closure);
eligottlieb@4813
   231
}
eligottlieb@4813
   232
eligottlieb@4843
   233
void
slouken@7191
   234
SDL_FreeShapeTree(SDL_ShapeTree** shape_tree)
slouken@7191
   235
{
egottlieb@4850
   236
    if((*shape_tree)->kind == QuadShape) {
egottlieb@4850
   237
        SDL_FreeShapeTree((SDL_ShapeTree **)&(*shape_tree)->data.children.upleft);
egottlieb@4850
   238
        SDL_FreeShapeTree((SDL_ShapeTree **)&(*shape_tree)->data.children.upright);
egottlieb@4850
   239
        SDL_FreeShapeTree((SDL_ShapeTree **)&(*shape_tree)->data.children.downleft);
egottlieb@4850
   240
        SDL_FreeShapeTree((SDL_ShapeTree **)&(*shape_tree)->data.children.downright);
eligottlieb@4843
   241
    }
egottlieb@4850
   242
    SDL_free(*shape_tree);
egottlieb@4850
   243
    *shape_tree = NULL;
eligottlieb@4813
   244
}
eligottlieb@4813
   245
eligottlieb@4843
   246
int
slouken@7191
   247
SDL_SetWindowShape(SDL_Window *window,SDL_Surface *shape,SDL_WindowShapeMode *shape_mode)
slouken@7191
   248
{
eligottlieb@4843
   249
    int result;
eligottlieb@4843
   250
    if(window == NULL || !SDL_IsShapedWindow(window))
slouken@7191
   251
        /* The window given was not a shapeable window. */
eligottlieb@4843
   252
        return SDL_NONSHAPEABLE_WINDOW;
eligottlieb@4843
   253
    if(shape == NULL)
slouken@7191
   254
        /* Invalid shape argument. */
eligottlieb@4843
   255
        return SDL_INVALID_SHAPE_ARGUMENT;
slouken@7191
   256
egottlieb@4847
   257
    if(shape_mode != NULL)
egottlieb@4847
   258
        window->shaper->mode = *shape_mode;
slouken@5246
   259
    result = SDL_GetVideoDevice()->shape_driver.SetWindowShape(window->shaper,shape,shape_mode);
eligottlieb@4843
   260
    window->shaper->hasshape = SDL_TRUE;
egottlieb@4852
   261
    if(window->shaper->userx != 0 && window->shaper->usery != 0) {
eligottlieb@4851
   262
        SDL_SetWindowPosition(window,window->shaper->userx,window->shaper->usery);
eligottlieb@4851
   263
        window->shaper->userx = 0;
eligottlieb@4851
   264
        window->shaper->usery = 0;
eligottlieb@4843
   265
    }
eligottlieb@4843
   266
    return result;
eligottlieb@4782
   267
}
eligottlieb@4782
   268
slouken@6044
   269
static SDL_bool
slouken@7191
   270
SDL_WindowHasAShape(SDL_Window *window)
slouken@7191
   271
{
eligottlieb@4843
   272
    if (window == NULL || !SDL_IsShapedWindow(window))
eligottlieb@4843
   273
        return SDL_FALSE;
eligottlieb@4843
   274
    return window->shaper->hasshape;
eligottlieb@4778
   275
}
eligottlieb@4778
   276
eligottlieb@4843
   277
int
slouken@7191
   278
SDL_GetShapedWindowMode(SDL_Window *window,SDL_WindowShapeMode *shape_mode)
slouken@7191
   279
{
eligottlieb@4843
   280
    if(window != NULL && SDL_IsShapedWindow(window)) {
egottlieb@4847
   281
        if(shape_mode == NULL) {
eligottlieb@4843
   282
            if(SDL_WindowHasAShape(window))
slouken@7191
   283
                /* The window given has a shape. */
eligottlieb@4843
   284
                return 0;
eligottlieb@4843
   285
            else
slouken@7191
   286
                /* The window given is shapeable but lacks a shape. */
eligottlieb@4843
   287
                return SDL_WINDOW_LACKS_SHAPE;
eligottlieb@4843
   288
        }
eligottlieb@4843
   289
        else {
egottlieb@4847
   290
            *shape_mode = window->shaper->mode;
eligottlieb@4843
   291
            return 0;
eligottlieb@4843
   292
        }
eligottlieb@4843
   293
    }
eligottlieb@4843
   294
    else
slouken@7191
   295
        /* The window given is not a valid shapeable window. */
eligottlieb@4843
   296
        return SDL_NONSHAPEABLE_WINDOW;
eligottlieb@4778
   297
}