Commit 30693e91 authored by Eli Gottlieb's avatar Eli Gottlieb

Reformatting code to match the rest of SDL. Variable names seem OK; replaced...

Reformatting code to match the rest of SDL.  Variable names seem OK; replaced tabs with 4 spaces each for indentation.
parent 34bbabf0
...@@ -29,238 +29,248 @@ ...@@ -29,238 +29,248 @@
#include "SDL_shape.h" #include "SDL_shape.h"
#include "../src/video/SDL_shape_internals.h" #include "../src/video/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_bool invert,SDL_Rect dimensions) { SDL_ShapeTree*
int x = 0,y = 0; RecursivelyCalculateShapeTree(SDL_WindowShapeMode mode,SDL_Surface* mask,SDL_bool invert,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_transparent = SDL_FALSE; Uint8 r = 0,g = 0,b = 0,a = 0;
int last_transparent = -1; SDL_bool pixel_transparent = SDL_FALSE;
SDL_Color key; int last_transparent = -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.h;y++) SDL_Rect next = {0,0,0,0};
for(x=dimensions.x;x<dimensions.w;x++) { for(y=dimensions.y;y<dimensions.h;y++)
pixel_value = 0; for(x=dimensions.x;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_transparent = (SDL_bool)(a >= 1 ? !invert : invert); case(ShapeModeDefault):
break; pixel_transparent = (SDL_bool)(a >= 1 ? !invert : invert);
case(ShapeModeBinarizeAlpha): break;
pixel_transparent = (SDL_bool)(a >= mode.parameters.binarizationCutoff ? !invert : invert); case(ShapeModeBinarizeAlpha):
break; pixel_transparent = (SDL_bool)(a >= mode.parameters.binarizationCutoff ? !invert : invert);
case(ShapeModeReverseBinarizeAlpha): break;
pixel_transparent = (SDL_bool)(a <= mode.parameters.binarizationCutoff ? !invert : invert); case(ShapeModeReverseBinarizeAlpha):
break; pixel_transparent = (SDL_bool)(a <= mode.parameters.binarizationCutoff ? !invert : invert);
case(ShapeModeColorKey): break;
key = mode.parameters.colorKey; case(ShapeModeColorKey):
pixel_transparent = (SDL_bool)((key.r == r && key.g == g && key.b == b) ? !invert : invert); key = mode.parameters.colorKey;
break; pixel_transparent = (SDL_bool)((key.r == r && key.g == g && key.b == b) ? !invert : invert);
} break;
if(last_transparent == -1) { }
last_transparent = pixel_transparent; if(last_transparent == -1) {
break; last_transparent = pixel_transparent;
} break;
if(last_transparent != pixel_transparent) { }
result->kind = QuadShape; if(last_transparent != pixel_transparent) {
//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,invert,next); next.y = dimensions.y;
next.x = dimensions.w / 2 + 1; result->data.children.upleft = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,invert,next);
//Unneeded: next.y = dimensions.y; next.x = dimensions.w / 2 + 1;
result->data.children.upright = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,invert,next); //Unneeded: next.y = dimensions.y;
next.x = dimensions.x; result->data.children.upright = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,invert,next);
next.y = dimensions.h / 2 + 1; next.x = dimensions.x;
result->data.children.downleft = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,invert,next); next.y = dimensions.h / 2 + 1;
next.x = dimensions.w / 2 + 1; result->data.children.downleft = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,invert,next);
//Unneeded: next.y = dimensions.h / 2 + 1; next.x = dimensions.w / 2 + 1;
result->data.children.downright = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,invert,next); //Unneeded: next.y = dimensions.h / 2 + 1;
return result; result->data.children.downright = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,invert,next);
} return result;
} }
//If we never recursed, all the pixels in this quadrant have the same "value". }
result->kind = (last_transparent == SDL_FALSE ? OpaqueShape : TransparentShape); //If we never recursed, all the pixels in this quadrant have the same "value".
result->data.shape = dimensions; result->kind = (last_transparent == SDL_FALSE ? OpaqueShape : TransparentShape);
return result; result->data.shape = dimensions;
return result;
} }
SDL_ShapeTree* SDL_CalculateShapeTree(SDL_WindowShapeMode mode,SDL_Surface* shape,SDL_bool invert) { SDL_ShapeTree*
SDL_Rect dimensions = {0,0,shape->w,shape->h}; SDL_CalculateShapeTree(SDL_WindowShapeMode mode,SDL_Surface* shape,SDL_bool invert) {
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,invert,dimensions); SDL_LockSurface(shape);
if(SDL_MUSTLOCK(shape)) result = RecursivelyCalculateShapeTree(mode,shape,invert,dimensions);
SDL_UnlockSurface(shape); if(SDL_MUSTLOCK(shape))
return result; SDL_UnlockSurface(shape);
return result;
} }
void SDL_TraverseShapeTree(SDL_ShapeTree *tree,void(*function)(SDL_ShapeTree*,void*),void* closure) { void
if(tree->kind == QuadShape) { SDL_TraverseShapeTree(SDL_ShapeTree *tree,void(*function)(SDL_ShapeTree*,void*),void* closure) {
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;
} }
...@@ -24,48 +24,51 @@ ...@@ -24,48 +24,51 @@
#include "SDL_shape.h" #include "SDL_shape.h"
#include "SDL_cocoashape.h" #include "SDL_cocoashape.h"
SDL_WindowShaper* Cocoa_CreateShaper(SDL_Window* window) { SDL_WindowShaper*
SDL_WindowData* data = (SDL_WindowData*)window->driverdata; Cocoa_CreateShaper(SDL_Window* window) {
[data->nswindow setAlpha:1.0]; SDL_WindowData* data = (SDL_WindowData*)window->driverdata;
[data->nswindow setOpaque:YES]; [data->nswindow setAlpha:1.0];
[data->nswindow setStyleMask:NSBorderlessWindowMask]; [data->nswindow setOpaque:YES];
SDL_Shaper* result = result = malloc(sizeof(SDL_WindowShaper)); [data->nswindow setStyleMask:NSBorderlessWindowMask];
result->window = window; SDL_Shaper* result = result = malloc(sizeof(SDL_WindowShaper));
result->mode.mode = ShapeModeDefault; result->window = window;
result->mode.parameters.binarizationCutoff = 1; result->mode.mode = ShapeModeDefault;
result->usershownflag = 0; result->mode.parameters.binarizationCutoff = 1;
window->shaper = result; result->usershownflag = 0;
window->shaper = result;
SDL_ShapeData* data = malloc(sizeof(SDL_ShapeData));
result->driverdata = data; SDL_ShapeData* data = malloc(sizeof(SDL_ShapeData));
data->context = [data->nswindow graphicsContext]; result->driverdata = data;
data->saved = SDL_False; data->context = [data->nswindow graphicsContext];
data->rects = NULL; data->saved = SDL_False;
data->count = 0; data->rects = NULL;
data->count = 0;
int resized_properly = Cocoa_ResizeWindowShape(window);
assert(resized_properly == 0); int resized_properly = Cocoa_ResizeWindowShape(window);
return result; assert(resized_properly == 0);
return result;
} }
int Cocoa_SetWindowShape(SDL_WindowShaper *shaper,SDL_Surface *shape,SDL_WindowShapeMode *shapeMode) { int
SDL_WindowData* data = (SDL_WindowData*)shaper->window->driverdata; Cocoa_SetWindowShape(SDL_WindowShaper *shaper,SDL_Surface *shape,SDL_WindowShapeMode *shapeMode) {
if(data->saved == SDL_True) { SDL_WindowData* data = (SDL_WindowData*)shaper->window->driverdata;
[data->context restoreGraphicsState]; if(data->saved == SDL_True) {
data->saved = SDL_False; [data->context restoreGraphicsState];
} data->saved = SDL_False;
}
[data->context saveGraphicsState];
data->saved = SDL_True; [data->context saveGraphicsState];
data->saved = SDL_True;
[[NSColor clearColor] set];
NSRectFill([[data->nswindow contentView] frame]); [[NSColor clearColor] set];
/* TODO: It looks like Cocoa can set a clipping path based on a list of rectangles. That's what we get from the NSRectFill([[data->nswindow contentView] frame]);
/* TODO: It looks like Cocoa can set a clipping path based on a list of rectangles. That's what we get from the
Windoze shape-calculation code: a list of rectangles. This will work... I think. */ Windoze shape-calculation code: a list of rectangles. This will work... I think. */
} }
int Cocoa_ResizeWindowShape(SDL_Window *window) { int
SDL_ShapeData* data = window->shaper->driverdata; Cocoa_ResizeWindowShape(SDL_Window *window) {
assert(data != NULL); SDL_ShapeData* data = window->shaper->driverdata;
return 0; assert(data != NULL);
return 0;
} }
...@@ -23,79 +23,83 @@ ...@@ -23,79 +23,83 @@
#include "SDL_win32shape.h" #include "SDL_win32shape.h"
#include "SDL_win32video.h" #include "SDL_win32video.h"
SDL_WindowShaper* Win32_CreateShaper(SDL_Window * window) { SDL_WindowShaper*
int resized_properly; Win32_CreateShaper(SDL_Window * window) {
SDL_WindowShaper* result = (SDL_WindowShaper *)SDL_malloc(sizeof(SDL_WindowShaper)); int resized_properly;
result->window = window; SDL_WindowShaper* result = (SDL_WindowShaper *)SDL_malloc(sizeof(SDL_WindowShaper));
result->mode.mode = ShapeModeDefault; result->window = window;
result->mode.parameters.binarizationCutoff = 1; result->mode.mode = ShapeModeDefault;
result->usershownflag = 0; result->mode.parameters.binarizationCutoff = 1;
result->driverdata = (SDL_ShapeData*)SDL_malloc(sizeof(SDL_ShapeData)); result->usershownflag = 0;
((SDL_ShapeData*)result->driverdata)->mask_tree = NULL; result->driverdata = (SDL_ShapeData*)SDL_malloc(sizeof(SDL_ShapeData));
//Put some driver-data here. ((SDL_ShapeData*)result->driverdata)->mask_tree = NULL;
window->shaper = result; //Put some driver-data here.
resized_properly = Win32_ResizeWindowShape(window); window->shaper = result;
if (resized_properly != 0) resized_properly = Win32_ResizeWindowShape(window);
return NULL; if (resized_properly != 0)
return NULL;
return result; return result;
} }
void CombineRectRegions(SDL_ShapeTree* node, void* closure) { void
HRGN* mask_region = (HRGN *)closure; CombineRectRegions(SDL_ShapeTree* node, void* closure) {
if(node->kind == OpaqueShape) { HRGN* mask_region = (HRGN *)closure;
HRGN temp_region = CreateRectRgn(node->data.shape.x,node->data.shape.y,node->data.shape.w,node->data.shape.h); if(node->kind == OpaqueShape) {
CombineRgn(*mask_region,*mask_region,temp_region, RGN_OR); HRGN temp_region = CreateRectRgn(node->data.shape.x,node->data.shape.y,node->data.shape.w,node->data.shape.h);
DeleteObject(temp_region); CombineRgn(*mask_region,*mask_region,temp_region, RGN_OR);
} DeleteObject(temp_region);
}
} }
int Win32_SetWindowShape(SDL_WindowShaper *shaper,SDL_Surface *shape,SDL_WindowShapeMode *shapeMode) { int
SDL_ShapeData *data; Win32_SetWindowShape(SDL_WindowShaper *shaper,SDL_Surface *shape,SDL_WindowShapeMode *shapeMode) {
HRGN mask_region; SDL_ShapeData *data;
HRGN mask_region;
SDL_WindowData *windowdata; SDL_WindowData *windowdata;
HWND hwnd; HWND hwnd;
if (shaper == NULL || shape == NULL) if (shaper == NULL || shape == NULL)
return SDL_INVALID_SHAPE_ARGUMENT; return SDL_INVALID_SHAPE_ARGUMENT;
if(shape->format->Amask == 0 && shapeMode->mode != ShapeModeColorKey || shape->w != shaper->window->w || shape->h != shaper->window->h) if(shape->format->Amask == 0 && shapeMode->mode != ShapeModeColorKey || shape->w != shaper->window->w || shape->h != shaper->window->h)
return SDL_INVALID_SHAPE_ARGUMENT; return SDL_INVALID_SHAPE_ARGUMENT;
data = (SDL_ShapeData*)shaper->driverdata; data = (SDL_ShapeData*)shaper->driverdata;
if(data->mask_tree != NULL) if(data->mask_tree != NULL)
SDL_FreeShapeTree(&data->mask_tree); SDL_FreeShapeTree(&data->mask_tree);
data->mask_tree = SDL_CalculateShapeTree(*shapeMode,shape,SDL_FALSE); data->mask_tree = SDL_CalculateShapeTree(*shapeMode,shape,SDL_FALSE);
/* /*
* Start with empty region * Start with empty region
*/ */
mask_region = CreateRectRgn(0, 0, 0, 0); mask_region = CreateRectRgn(0, 0, 0, 0);
SDL_TraverseShapeTree(data->mask_tree,&CombineRectRegions,&mask_region); SDL_TraverseShapeTree(data->mask_tree,&CombineRectRegions,&mask_region);
/* /*
* Set the new region mask for the window * Set the new region mask for the window
*/ */
windowdata=(SDL_WindowData *)(shaper->window->driverdata); windowdata=(SDL_WindowData *)(shaper->window->driverdata);
hwnd = windowdata->hwnd; hwnd = windowdata->hwnd;
SetWindowRgn(hwnd, mask_region, TRUE); SetWindowRgn(hwnd, mask_region, TRUE);
return 0; return 0;
} }
int Win32_ResizeWindowShape(SDL_Window *window) { int
SDL_ShapeData* data; Win32_ResizeWindowShape(SDL_Window *window) {
SDL_ShapeData* data;
if (window == NULL) if (window == NULL)
return -1; return -1;
data = (SDL_ShapeData *)window->shaper->driverdata; data = (SDL_ShapeData *)window->shaper->driverdata;
if (data == NULL) if (data == NULL)
return -1; return -1;
if(data->mask_tree != NULL) if(data->mask_tree != NULL)
SDL_FreeShapeTree(&data->mask_tree); SDL_FreeShapeTree(&data->mask_tree);
window->shaper->usershownflag |= window->flags & SDL_WINDOW_SHOWN; window->shaper->usershownflag |= window->flags & SDL_WINDOW_SHOWN;
return 0; return 0;
} }
...@@ -25,80 +25,84 @@ ...@@ -25,80 +25,84 @@
#include "SDL_x11shape.h" #include "SDL_x11shape.h"
#include "SDL_x11window.h" #include "SDL_x11window.h"
SDL_Window* X11_CreateShapedWindow(const char *title,unsigned int x,unsigned int y,unsigned int w,unsigned int h,Uint32 flags) { SDL_Window*
return SDL_CreateWindow(title,x,y,w,h,flags); X11_CreateShapedWindow(const char *title,unsigned int x,unsigned int y,unsigned int w,unsigned int h,Uint32 flags) {
return SDL_CreateWindow(title,x,y,w,h,flags);
} }
SDL_WindowShaper* X11_CreateShaper(SDL_Window* window) { SDL_WindowShaper*
SDL_WindowShaper* result = NULL; X11_CreateShaper(SDL_Window* window) {
SDL_WindowShaper* result = NULL;
#if SDL_VIDEO_DRIVER_X11_XSHAPE #if SDL_VIDEO_DRIVER_X11_XSHAPE
if (SDL_X11_HAVE_XSHAPE) { /* Make sure X server supports it. */ if (SDL_X11_HAVE_XSHAPE) { /* Make sure X server supports it. */
result = malloc(sizeof(SDL_WindowShaper)); result = malloc(sizeof(SDL_WindowShaper));
result->window = window; result->window = window;
result->mode.mode = ShapeModeDefault; result->mode.mode = ShapeModeDefault;
result->mode.parameters.binarizationCutoff = 1; result->mode.parameters.binarizationCutoff = 1;
result->usershownflag = 0; result->usershownflag = 0;
SDL_ShapeData* data = malloc(sizeof(SDL_ShapeData)); SDL_ShapeData* data = malloc(sizeof(SDL_ShapeData));
result->driverdata = data; result->driverdata = data;
data->bitmapsize = 0; data->bitmapsize = 0;
data->bitmap = NULL; data->bitmap = NULL;
window->shaper = result; window->shaper = result;
int resized_properly = X11_ResizeWindowShape(window); int resized_properly = X11_ResizeWindowShape(window);
assert(resized_properly == 0); assert(resized_properly == 0);
} }
#endif #endif
return result; return result;
} }
int X11_ResizeWindowShape(SDL_Window* window) { int
SDL_ShapeData* data = window->shaper->driverdata; X11_ResizeWindowShape(SDL_Window* window) {
assert(data != NULL); SDL_ShapeData* data = window->shaper->driverdata;
assert(data != NULL);
unsigned int bitmapsize = window->w / 8;
if(window->w % 8 > 0) unsigned int bitmapsize = window->w / 8;
bitmapsize += 1; if(window->w % 8 > 0)
bitmapsize *= window->h; bitmapsize += 1;
if(data->bitmapsize != bitmapsize || data->bitmap == NULL) { bitmapsize *= window->h;
data->bitmapsize = bitmapsize; if(data->bitmapsize != bitmapsize || data->bitmap == NULL) {
if(data->bitmap != NULL) data->bitmapsize = bitmapsize;
free(data->bitmap); if(data->bitmap != NULL)
data->bitmap = malloc(data->bitmapsize); free(data->bitmap);
if(data->bitmap == NULL) { data->bitmap = malloc(data->bitmapsize);
SDL_SetError("Could not allocate memory for shaped-window bitmap."); if(data->bitmap == NULL) {
return -1; SDL_SetError("Could not allocate memory for shaped-window bitmap.");
} return -1;
} }
memset(data->bitmap,0,data->bitmapsize); }
memset(data->bitmap,0,data->bitmapsize);
window->shaper->usershownflag |= window->flags & SDL_WINDOW_SHOWN;
window->shaper->usershownflag |= window->flags & SDL_WINDOW_SHOWN;
return 0;
return 0;
} }
int X11_SetWindowShape(SDL_WindowShaper *shaper,SDL_Surface *shape,SDL_WindowShapeMode *shapeMode) { int
if(shaper == NULL || shape == NULL || shaper->driverdata == NULL) X11_SetWindowShape(SDL_WindowShaper *shaper,SDL_Surface *shape,SDL_WindowShapeMode *shapeMode) {
return -1; if(shaper == NULL || shape == NULL || shaper->driverdata == NULL)
return -1;
#if SDL_VIDEO_DRIVER_X11_XSHAPE #if SDL_VIDEO_DRIVER_X11_XSHAPE
if(!SDL_ISPIXELFORMAT_ALPHA(SDL_MasksToPixelFormatEnum(shape->format->BitsPerPixel,shape->format->Rmask,shape->format->Gmask,shape->format->Bmask,shape->format->Amask))) if(!SDL_ISPIXELFORMAT_ALPHA(SDL_MasksToPixelFormatEnum(shape->format->BitsPerPixel,shape->format->Rmask,shape->format->Gmask,shape->format->Bmask,shape->format->Amask)))
return -2; return -2;
if(shape->w != shaper->window->w || shape->h != shaper->window->h) if(shape->w != shaper->window->w || shape->h != shaper->window->h)
return -3; return -3;
SDL_ShapeData *data = shaper->driverdata; SDL_ShapeData *data = shaper->driverdata;
/* Assume that shaper->alphacutoff already has a value, because SDL_SetWindowShape() should have given it one. */ /* Assume that shaper->alphacutoff already has a value, because SDL_SetWindowShape() should have given it one. */
SDL_CalculateShapeBitmap(shaper->mode,shape,data->bitmap,8); SDL_CalculateShapeBitmap(shaper->mode,shape,data->bitmap,8);
SDL_WindowData *windowdata = (SDL_WindowData*)(shaper->window->driverdata); SDL_WindowData *windowdata = (SDL_WindowData*)(shaper->window->driverdata);
Pixmap shapemask = XCreateBitmapFromData(windowdata->videodata->display,windowdata->xwindow,data->bitmap,shaper->window->w,shaper->window->h); Pixmap shapemask = XCreateBitmapFromData(windowdata->videodata->display,windowdata->xwindow,data->bitmap,shaper->window->w,shaper->window->h);
XShapeCombineMask(windowdata->videodata->display,windowdata->xwindow, ShapeBounding, 0, 0,shapemask, ShapeSet); XShapeCombineMask(windowdata->videodata->display,windowdata->xwindow, ShapeBounding, 0, 0,shapemask, ShapeSet);
XSync(windowdata->videodata->display,False); XSync(windowdata->videodata->display,False);
XFreePixmap(windowdata->videodata->display,shapemask); XFreePixmap(windowdata->videodata->display,shapemask);
#endif #endif
return 0; return 0;
} }
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