Commit f294cf6a authored by Eli Gottlieb's avatar Eli Gottlieb

Fixed formatting of SDL_shape.c post-merge again.

parent 4e4cf1ce
...@@ -30,237 +30,247 @@ ...@@ -30,237 +30,247 @@
#include "SDL_shape.h" #include "SDL_shape.h"
#include "SDL_shape_internals.h" #include "SDL_shape_internals.h"
SDL_Window* SDL_CreateShapedWindow(const char *title,unsigned int x,unsigned int y,unsigned int w,unsigned int h,Uint32 flags) { SDL_Window*
SDL_Window *result = SDL_CreateWindow(title,x,y,w,h,SDL_WINDOW_BORDERLESS | flags & !SDL_WINDOW_FULLSCREEN & !SDL_WINDOW_SHOWN); SDL_CreateShapedWindow(const char *title,unsigned int x,unsigned int y,unsigned int w,unsigned int h,Uint32 flags) {
if(result != NULL) { SDL_Window *result = SDL_CreateWindow(title,x,y,w,h,SDL_WINDOW_BORDERLESS | flags & !SDL_WINDOW_FULLSCREEN & !SDL_WINDOW_SHOWN);
result->shaper = result->display->device->shape_driver.CreateShaper(result); if(result != NULL) {
if(result->shaper != NULL) { result->shaper = result->display->device->shape_driver.CreateShaper(result);
result->shaper->usershownflag = flags & SDL_WINDOW_SHOWN; if(result->shaper != NULL) {
result->shaper->mode.mode = ShapeModeDefault; result->shaper->usershownflag = flags & SDL_WINDOW_SHOWN;
result->shaper->mode.parameters.binarizationCutoff = 1; result->shaper->mode.mode = ShapeModeDefault;
result->shaper->hasshape = SDL_FALSE; result->shaper->mode.parameters.binarizationCutoff = 1;
return result; result->shaper->hasshape = SDL_FALSE;
} return result;
else { }
SDL_DestroyWindow(result); else {
return NULL; SDL_DestroyWindow(result);
} return NULL;
} }
else }
return NULL; else
return NULL;
} }
SDL_bool SDL_IsShapedWindow(const SDL_Window *window) { SDL_bool
if(window == NULL) SDL_IsShapedWindow(const SDL_Window *window) {
return SDL_FALSE; if(window == NULL)
else return SDL_FALSE;
return (SDL_bool)(window->shaper != NULL); else
return (SDL_bool)(window->shaper != NULL);
} }
/* REQUIRES that bitmap point to a w-by-h bitmap with ppb pixels-per-byte. */ /* REQUIRES that bitmap point to a w-by-h bitmap with ppb pixels-per-byte. */
void SDL_CalculateShapeBitmap(SDL_WindowShapeMode mode,SDL_Surface *shape,Uint8* bitmap,Uint8 ppb) { void
int x = 0; SDL_CalculateShapeBitmap(SDL_WindowShapeMode mode,SDL_Surface *shape,Uint8* bitmap,Uint8 ppb) {
int y = 0; int x = 0;
Uint8 r = 0,g = 0,b = 0,alpha = 0; int y = 0;
Uint8* pixel = NULL; Uint8 r = 0,g = 0,b = 0,alpha = 0;
Uint32 bitmap_pixel,pixel_value = 0,mask_value = 0; Uint8* pixel = NULL;
SDL_Color key; Uint32 bitmap_pixel,pixel_value = 0,mask_value = 0;
if(SDL_MUSTLOCK(shape)) SDL_Color key;
SDL_LockSurface(shape); if(SDL_MUSTLOCK(shape))
pixel = (Uint8*)shape->pixels; SDL_LockSurface(shape);
for(y = 0;y<shape->h;y++) { pixel = (Uint8*)shape->pixels;
for(x=0;x<shape->w;x++) { for(y = 0;y<shape->h;y++) {
alpha = 0; for(x=0;x<shape->w;x++) {
pixel_value = 0; alpha = 0;
pixel = (Uint8 *)(shape->pixels) + (y*shape->pitch) + (x*shape->format->BytesPerPixel); pixel_value = 0;
switch(shape->format->BytesPerPixel) { pixel = (Uint8 *)(shape->pixels) + (y*shape->pitch) + (x*shape->format->BytesPerPixel);
case(1): switch(shape->format->BytesPerPixel) {
pixel_value = *(Uint8*)pixel; case(1):
break; pixel_value = *(Uint8*)pixel;
case(2): break;
pixel_value = *(Uint16*)pixel; case(2):
break; pixel_value = *(Uint16*)pixel;
case(3): break;
pixel_value = *(Uint32*)pixel & (~shape->format->Amask); case(3):
break; pixel_value = *(Uint32*)pixel & (~shape->format->Amask);
case(4): break;
pixel_value = *(Uint32*)pixel; case(4):
break; pixel_value = *(Uint32*)pixel;
} break;
SDL_GetRGBA(pixel_value,shape->format,&r,&g,&b,&alpha); }
bitmap_pixel = y*shape->w + x; SDL_GetRGBA(pixel_value,shape->format,&r,&g,&b,&alpha);
switch(mode.mode) { bitmap_pixel = y*shape->w + x;
case(ShapeModeDefault): switch(mode.mode) {
mask_value = (alpha >= 1 ? 1 : 0); case(ShapeModeDefault):
break; mask_value = (alpha >= 1 ? 1 : 0);
case(ShapeModeBinarizeAlpha): break;
mask_value = (alpha >= mode.parameters.binarizationCutoff ? 1 : 0); case(ShapeModeBinarizeAlpha):
break; mask_value = (alpha >= mode.parameters.binarizationCutoff ? 1 : 0);
case(ShapeModeReverseBinarizeAlpha): break;
mask_value = (alpha <= mode.parameters.binarizationCutoff ? 1 : 0); case(ShapeModeReverseBinarizeAlpha):
break; mask_value = (alpha <= mode.parameters.binarizationCutoff ? 1 : 0);
case(ShapeModeColorKey): break;
key = mode.parameters.colorKey; case(ShapeModeColorKey):
mask_value = ((key.r != r && key.g != g && key.b != b) ? 1 : 0); key = mode.parameters.colorKey;
break; mask_value = ((key.r != r && key.g != g && key.b != b) ? 1 : 0);
} break;
bitmap[bitmap_pixel / ppb] |= mask_value << (7 - ((ppb - 1) - (bitmap_pixel % ppb))); }
} bitmap[bitmap_pixel / ppb] |= mask_value << (7 - ((ppb - 1) - (bitmap_pixel % ppb)));
} }
if(SDL_MUSTLOCK(shape)) }
SDL_UnlockSurface(shape); if(SDL_MUSTLOCK(shape))
SDL_UnlockSurface(shape);
} }
SDL_ShapeTree* RecursivelyCalculateShapeTree(SDL_WindowShapeMode mode,SDL_Surface* mask,SDL_Rect dimensions) { SDL_ShapeTree*
int x = 0,y = 0; RecursivelyCalculateShapeTree(SDL_WindowShapeMode mode,SDL_Surface* mask,SDL_Rect dimensions) {
Uint8* pixel = NULL; int x = 0,y = 0;
Uint32 pixel_value = 0; Uint8* pixel = NULL;
Uint8 r = 0,g = 0,b = 0,a = 0; Uint32 pixel_value = 0;
SDL_bool pixel_opaque = SDL_FALSE; Uint8 r = 0,g = 0,b = 0,a = 0;
int last_opaque = -1; SDL_bool pixel_opaque = SDL_FALSE;
SDL_Color key; int last_opaque = -1;
SDL_ShapeTree* result = (SDL_ShapeTree*)SDL_malloc(sizeof(SDL_ShapeTree)); SDL_Color key;
SDL_Rect next = {0,0,0,0}; SDL_ShapeTree* result = (SDL_ShapeTree*)SDL_malloc(sizeof(SDL_ShapeTree));
for(y=dimensions.y;y<dimensions.y + dimensions.h;y++) SDL_Rect next = {0,0,0,0};
for(x=dimensions.x;x<dimensions.x + dimensions.w;x++) { for(y=dimensions.y;y<dimensions.y + dimensions.h;y++)
pixel_value = 0; for(x=dimensions.x;x<dimensions.x + dimensions.w;x++) {
pixel = (Uint8 *)(mask->pixels) + (y*mask->pitch) + (x*mask->format->BytesPerPixel); pixel_value = 0;
switch(mask->format->BytesPerPixel) { pixel = (Uint8 *)(mask->pixels) + (y*mask->pitch) + (x*mask->format->BytesPerPixel);
case(1): switch(mask->format->BytesPerPixel) {
pixel_value = *(Uint8*)pixel; case(1):
break; pixel_value = *(Uint8*)pixel;
case(2): break;
pixel_value = *(Uint16*)pixel; case(2):
break; pixel_value = *(Uint16*)pixel;
case(3): break;
pixel_value = *(Uint32*)pixel & (~mask->format->Amask); case(3):
break; pixel_value = *(Uint32*)pixel & (~mask->format->Amask);
case(4): break;
pixel_value = *(Uint32*)pixel; case(4):
break; pixel_value = *(Uint32*)pixel;
} break;
SDL_GetRGBA(pixel_value,mask->format,&r,&g,&b,&a); }
switch(mode.mode) { SDL_GetRGBA(pixel_value,mask->format,&r,&g,&b,&a);
case(ShapeModeDefault): switch(mode.mode) {
pixel_opaque = (a >= 1 ? SDL_TRUE : SDL_FALSE); case(ShapeModeDefault):
break; pixel_opaque = (a >= 1 ? SDL_TRUE : SDL_FALSE);
case(ShapeModeBinarizeAlpha): break;
pixel_opaque = (a >= mode.parameters.binarizationCutoff ? SDL_TRUE : SDL_FALSE); case(ShapeModeBinarizeAlpha):
break; pixel_opaque = (a >= mode.parameters.binarizationCutoff ? SDL_TRUE : SDL_FALSE);
case(ShapeModeReverseBinarizeAlpha): break;
pixel_opaque = (a <= mode.parameters.binarizationCutoff ? SDL_TRUE : SDL_FALSE); case(ShapeModeReverseBinarizeAlpha):
break; pixel_opaque = (a <= mode.parameters.binarizationCutoff ? SDL_TRUE : SDL_FALSE);
case(ShapeModeColorKey): break;
key = mode.parameters.colorKey; case(ShapeModeColorKey):
pixel_opaque = ((key.r == r && key.g == g && key.b == b) ? SDL_TRUE : SDL_FALSE); key = mode.parameters.colorKey;
break; pixel_opaque = ((key.r == r && key.g == g && key.b == b) ? SDL_TRUE : SDL_FALSE);
} break;
if(last_opaque == -1) }
last_opaque = pixel_opaque; if(last_opaque == -1)
if(last_opaque != pixel_opaque) { last_opaque = pixel_opaque;
result->kind = QuadShape; if(last_opaque != pixel_opaque) {
//These will stay the same. result->kind = QuadShape;
next.w = dimensions.w / 2; //These will stay the same.
next.h = dimensions.h / 2; next.w = dimensions.w / 2;
//These will change from recursion to recursion. next.h = dimensions.h / 2;
next.x = dimensions.x; //These will change from recursion to recursion.
next.y = dimensions.y; next.x = dimensions.x;
result->data.children.upleft = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next); next.y = dimensions.y;
next.x = dimensions.w / 2; result->data.children.upleft = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next);
//Unneeded: next.y = dimensions.y; next.x = dimensions.w / 2;
result->data.children.upright = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next); //Unneeded: next.y = dimensions.y;
next.x = dimensions.x; result->data.children.upright = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next);
next.y = dimensions.h / 2; next.x = dimensions.x;
result->data.children.downleft = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next); next.y = dimensions.h / 2;
next.x = dimensions.w / 2; result->data.children.downleft = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next);
//Unneeded: next.y = dimensions.h / 2 + 1; next.x = dimensions.w / 2;
result->data.children.downright = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next); //Unneeded: next.y = dimensions.h / 2 + 1;
return result; result->data.children.downright = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next);
} return result;
} }
//If we never recursed, all the pixels in this quadrant have the same "value". }
result->kind = (last_opaque == SDL_TRUE ? OpaqueShape : TransparentShape); //If we never recursed, all the pixels in this quadrant have the same "value".
result->data.shape = dimensions; result->kind = (last_opaque == SDL_TRUE ? OpaqueShape : TransparentShape);
return result; result->data.shape = dimensions;
return result;
} }
SDL_ShapeTree* SDL_CalculateShapeTree(SDL_WindowShapeMode mode,SDL_Surface* shape) { SDL_ShapeTree*
SDL_Rect dimensions = {0,0,shape->w,shape->h}; SDL_CalculateShapeTree(SDL_WindowShapeMode mode,SDL_Surface* shape) {
SDL_ShapeTree* result = NULL; SDL_Rect dimensions = {0,0,shape->w,shape->h};
if(SDL_MUSTLOCK(shape)) SDL_ShapeTree* result = NULL;
SDL_LockSurface(shape); if(SDL_MUSTLOCK(shape))
result = RecursivelyCalculateShapeTree(mode,shape,dimensions); SDL_LockSurface(shape);
if(SDL_MUSTLOCK(shape)) result = RecursivelyCalculateShapeTree(mode,shape,dimensions);
SDL_UnlockSurface(shape); if(SDL_MUSTLOCK(shape))
return result; SDL_UnlockSurface(shape);
return result;
} }
void SDL_TraverseShapeTree(SDL_ShapeTree *tree,SDL_TraversalFunction function,void* closure) { void
SDL_assert(tree != NULL); SDL_TraverseShapeTree(SDL_ShapeTree *tree,SDL_TraversalFunction function,void* closure) {
if(tree->kind == QuadShape) { SDL_assert(tree != NULL);
SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.upleft,function,closure); if(tree->kind == QuadShape) {
SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.upright,function,closure); SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.upleft,function,closure);
SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.downleft,function,closure); SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.upright,function,closure);
SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.downright,function,closure); SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.downleft,function,closure);
} SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.downright,function,closure);
else }
function(tree,closure); else
function(tree,closure);
} }
void SDL_FreeShapeTree(SDL_ShapeTree** shapeTree) { void
if((*shapeTree)->kind == QuadShape) { SDL_FreeShapeTree(SDL_ShapeTree** shapeTree) {
SDL_FreeShapeTree((SDL_ShapeTree **)&(*shapeTree)->data.children.upleft); if((*shapeTree)->kind == QuadShape) {
SDL_FreeShapeTree((SDL_ShapeTree **)&(*shapeTree)->data.children.upright); SDL_FreeShapeTree((SDL_ShapeTree **)&(*shapeTree)->data.children.upleft);
SDL_FreeShapeTree((SDL_ShapeTree **)&(*shapeTree)->data.children.downleft); SDL_FreeShapeTree((SDL_ShapeTree **)&(*shapeTree)->data.children.upright);
SDL_FreeShapeTree((SDL_ShapeTree **)&(*shapeTree)->data.children.downright); SDL_FreeShapeTree((SDL_ShapeTree **)&(*shapeTree)->data.children.downleft);
} SDL_FreeShapeTree((SDL_ShapeTree **)&(*shapeTree)->data.children.downright);
SDL_free(*shapeTree); }
*shapeTree = NULL; SDL_free(*shapeTree);
*shapeTree = NULL;
} }
int SDL_SetWindowShape(SDL_Window *window,SDL_Surface *shape,SDL_WindowShapeMode *shapeMode) { int
int result; SDL_SetWindowShape(SDL_Window *window,SDL_Surface *shape,SDL_WindowShapeMode *shapeMode) {
if(window == NULL || !SDL_IsShapedWindow(window)) int result;
//The window given was not a shapeable window. if(window == NULL || !SDL_IsShapedWindow(window))
return SDL_NONSHAPEABLE_WINDOW; //The window given was not a shapeable window.
if(shape == NULL) return SDL_NONSHAPEABLE_WINDOW;
//Invalid shape argument. if(shape == NULL)
return SDL_INVALID_SHAPE_ARGUMENT; //Invalid shape argument.
return SDL_INVALID_SHAPE_ARGUMENT;
if(shapeMode != NULL)
window->shaper->mode = *shapeMode; if(shapeMode != NULL)
//TODO: Platform-specific implementations of SetWindowShape. X11 is finished. Win32 is finished. Debugging is in progress on both. window->shaper->mode = *shapeMode;
result = window->display->device->shape_driver.SetWindowShape(window->shaper,shape,shapeMode); //TODO: Platform-specific implementations of SetWindowShape. X11 is finished. Win32 is finished. Debugging is in progress on both.
window->shaper->hasshape = SDL_TRUE; result = window->display->device->shape_driver.SetWindowShape(window->shaper,shape,shapeMode);
if((window->shaper->usershownflag & SDL_WINDOW_SHOWN) == SDL_WINDOW_SHOWN) { window->shaper->hasshape = SDL_TRUE;
SDL_ShowWindow(window); if((window->shaper->usershownflag & SDL_WINDOW_SHOWN) == SDL_WINDOW_SHOWN) {
window->shaper->usershownflag &= !SDL_WINDOW_SHOWN; SDL_ShowWindow(window);
} window->shaper->usershownflag &= !SDL_WINDOW_SHOWN;
return result; }
return result;
} }
SDL_bool SDL_WindowHasAShape(SDL_Window *window) { SDL_bool
if (window == NULL || !SDL_IsShapedWindow(window)) SDL_WindowHasAShape(SDL_Window *window) {
return SDL_FALSE; if (window == NULL || !SDL_IsShapedWindow(window))
return window->shaper->hasshape; return SDL_FALSE;
return window->shaper->hasshape;
} }
int SDL_GetShapedWindowMode(SDL_Window *window,SDL_WindowShapeMode *shapeMode) { int
if(window != NULL && SDL_IsShapedWindow(window)) { SDL_GetShapedWindowMode(SDL_Window *window,SDL_WindowShapeMode *shapeMode) {
if(shapeMode == NULL) { if(window != NULL && SDL_IsShapedWindow(window)) {
if(SDL_WindowHasAShape(window)) if(shapeMode == NULL) {
//The window given has a shape. if(SDL_WindowHasAShape(window))
return 0; //The window given has a shape.
else return 0;
//The window given is shapeable but lacks a shape. else
return SDL_WINDOW_LACKS_SHAPE; //The window given is shapeable but lacks a shape.
} return SDL_WINDOW_LACKS_SHAPE;
else { }
*shapeMode = window->shaper->mode; else {
return 0; *shapeMode = window->shaper->mode;
} return 0;
} }
else }
//The window given is not a valid shapeable window. else
return SDL_NONSHAPEABLE_WINDOW; //The window given is not a valid shapeable window.
return SDL_NONSHAPEABLE_WINDOW;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment