Commit abb4ad73 authored by Eli Gottlieb's avatar Eli Gottlieb

Added color-key mode and redid the code to work with it.

parent f9048e72
...@@ -83,12 +83,17 @@ typedef enum { ...@@ -83,12 +83,17 @@ typedef enum {
/** \brief The default mode, a binarized alpha cutoff of 1. */ /** \brief The default mode, a binarized alpha cutoff of 1. */
ShapeModeDefault, ShapeModeDefault,
/** \brief A binarized alpha cutoff with a given integer value. */ /** \brief A binarized alpha cutoff with a given integer value. */
ShapeModeBinarizeAlpha ShapeModeBinarizeAlpha,
/** \brief A binarized alpha cutoff with a given integer value, but with the opposite comparison. */
ShapeModeReverseBinarizeAlpha,
/** \brief A color key is applied. */
ShapeModeColorKey
} WindowShapeMode; } WindowShapeMode;
/** \brief A union containing parameters for shaped windows. */ /** \brief A union containing parameters for shaped windows. */
typedef union { typedef union {
/** \brief a cutoff alpha value for binarization of the window shape's alpha channel. */ /** \brief a cutoff alpha value for binarization of the window shape's alpha channel. */
Uint8 binarizationCutoff; Uint8 binarizationCutoff;
SDL_Color colorKey;
} SDL_WindowShapeParams; } SDL_WindowShapeParams;
/** \brief A struct that tags the SDL_WindowShapeParams union with an enum describing the type of its contents. */ /** \brief A struct that tags the SDL_WindowShapeParams union with an enum describing the type of its contents. */
......
...@@ -34,7 +34,8 @@ SDL_Window* SDL_CreateShapedWindow(const char *title,unsigned int x,unsigned int ...@@ -34,7 +34,8 @@ SDL_Window* SDL_CreateShapedWindow(const char *title,unsigned int x,unsigned int
result->shaper = result->display->device->shape_driver.CreateShaper(result); result->shaper = result->display->device->shape_driver.CreateShaper(result);
if(result->shaper != NULL) { if(result->shaper != NULL) {
result->shaper->usershownflag = flags & SDL_WINDOW_SHOWN; result->shaper->usershownflag = flags & SDL_WINDOW_SHOWN;
result->shaper->alphacutoff = 1; result->shaper->mode.mode = ShapeModeDefault;
result->shaper->mode.parameters.binarizationCutoff = 1;
result->shaper->hasshape = SDL_FALSE; result->shaper->hasshape = SDL_FALSE;
return result; return result;
} }
...@@ -55,12 +56,13 @@ SDL_bool SDL_IsShapedWindow(const SDL_Window *window) { ...@@ -55,12 +56,13 @@ SDL_bool SDL_IsShapedWindow(const SDL_Window *window) {
} }
/* REQUIRES that bitmap point to a w-by-h bitmap with 1bpp. */ /* REQUIRES that bitmap point to a w-by-h bitmap with 1bpp. */
void SDL_CalculateShapeBitmap(Uint8 alphacutoff,SDL_Surface *shape,Uint8* bitmap,Uint8 ppb,Uint8 value) { void SDL_CalculateShapeBitmap(SDL_WindowShapeMode mode,SDL_Surface *shape,Uint8* bitmap,Uint8 ppb,Uint8 value) {
int x = 0; int x = 0;
int y = 0; int y = 0;
Uint8 r = 0,g = 0,b = 0,alpha = 0; Uint8 r = 0,g = 0,b = 0,alpha = 0;
Uint8* pixel = NULL; Uint8* pixel = NULL;
Uint32 bitmap_pixel,pixel_value = 0; Uint32 bitmap_pixel,pixel_value = 0;
SDL_Color key;
if(SDL_MUSTLOCK(shape)) if(SDL_MUSTLOCK(shape))
SDL_LockSurface(shape); SDL_LockSurface(shape);
pixel = (Uint8*)shape->pixels; pixel = (Uint8*)shape->pixels;
...@@ -82,7 +84,21 @@ void SDL_CalculateShapeBitmap(Uint8 alphacutoff,SDL_Surface *shape,Uint8* bitmap ...@@ -82,7 +84,21 @@ void SDL_CalculateShapeBitmap(Uint8 alphacutoff,SDL_Surface *shape,Uint8* bitmap
} }
SDL_GetRGBA(pixel_value,shape->format,&r,&g,&b,&alpha); SDL_GetRGBA(pixel_value,shape->format,&r,&g,&b,&alpha);
bitmap_pixel = y*shape->w + x; bitmap_pixel = y*shape->w + x;
bitmap[bitmap_pixel / ppb] |= (alpha >= alphacutoff ? value : 0) << ((ppb - 1) - (bitmap_pixel % ppb)); switch(mode.mode) {
case(ShapeModeDefault):
bitmap[bitmap_pixel / ppb] |= (alpha >= 1 ? value : 0) << ((ppb - 1) - (bitmap_pixel % ppb));
break;
case(ShapeModeBinarizeAlpha):
bitmap[bitmap_pixel / ppb] |= (alpha >= mode.parameters.binarizationCutoff ? value : 0) << ((ppb - 1) - (bitmap_pixel % ppb));
break;
case(ShapeModeReverseBinarizeAlpha):
bitmap[bitmap_pixel / ppb] |= (alpha <= mode.parameters.binarizationCutoff ? value : 0) << ((ppb - 1) - (bitmap_pixel % ppb));
break;
case(ShapeModeColorKey):
key = mode.parameters.colorKey;
bitmap[bitmap_pixel / ppb] |= (key.r == r && key.g == g && key.b == b ? value : 0) << ((ppb - 1) - (bitmap_pixel % ppb));
break;
}
} }
} }
if(SDL_MUSTLOCK(shape)) if(SDL_MUSTLOCK(shape))
...@@ -98,18 +114,8 @@ int SDL_SetWindowShape(SDL_Window *window,SDL_Surface *shape,SDL_WindowShapeMode ...@@ -98,18 +114,8 @@ int SDL_SetWindowShape(SDL_Window *window,SDL_Surface *shape,SDL_WindowShapeMode
//Invalid shape argument. //Invalid shape argument.
return SDL_INVALID_SHAPE_ARGUMENT; return SDL_INVALID_SHAPE_ARGUMENT;
if(shapeMode != NULL) { if(shapeMode != NULL)
switch(shapeMode->mode) { window->shaper->mode = *shapeMode;
case ShapeModeDefault: {
window->shaper->alphacutoff = 1;
break;
}
case ShapeModeBinarizeAlpha: {
window->shaper->alphacutoff = shapeMode->parameters.binarizationCutoff;
break;
}
}
}
//TODO: Platform-specific implementations of SetWindowShape. X11 is finished. Win32 is finished. Debugging is in progress on both. //TODO: Platform-specific implementations of SetWindowShape. X11 is finished. Win32 is finished. Debugging is in progress on both.
result = window->display->device->shape_driver.SetWindowShape(window->shaper,shape,shapeMode); result = window->display->device->shape_driver.SetWindowShape(window->shaper,shape,shapeMode);
window->shaper->hasshape = SDL_TRUE; window->shaper->hasshape = SDL_TRUE;
...@@ -137,12 +143,7 @@ int SDL_GetShapedWindowMode(SDL_Window *window,SDL_WindowShapeMode *shapeMode) { ...@@ -137,12 +143,7 @@ int SDL_GetShapedWindowMode(SDL_Window *window,SDL_WindowShapeMode *shapeMode) {
return SDL_WINDOW_LACKS_SHAPE; return SDL_WINDOW_LACKS_SHAPE;
} }
else { else {
if(window->shaper->alphacutoff != 1) { *shapeMode = window->shaper->mode;
shapeMode->mode = ShapeModeBinarizeAlpha;
shapeMode->parameters.binarizationCutoff = window->shaper->alphacutoff;
}
else
shapeMode->mode = ShapeModeDefault;
return 0; return 0;
} }
} }
......
...@@ -144,9 +144,8 @@ struct SDL_WindowShaper ...@@ -144,9 +144,8 @@ struct SDL_WindowShaper
/* The user's specified SDL_WINDOW_SHOWN flag, for use once the user gives the window a shape. */ /* The user's specified SDL_WINDOW_SHOWN flag, for use once the user gives the window a shape. */
Uint32 usershownflag; Uint32 usershownflag;
/* The cutoff value for alpha-channel binarization. When alpha is greater-than-or-equal-to this value in the shape /* The parameters for shape calculation. */
image, the corresponding pixel of the actual window will be considered part of the window's shape. */ SDL_WindowShapeMode mode;
Uint8 alphacutoff;
/* Has this window been assigned a shape? */ /* Has this window been assigned a shape? */
SDL_bool hasshape; SDL_bool hasshape;
......
...@@ -26,7 +26,8 @@ ...@@ -26,7 +26,8 @@
SDL_WindowShaper* Win32_CreateShaper(SDL_Window * window) { SDL_WindowShaper* Win32_CreateShaper(SDL_Window * window) {
SDL_WindowShaper* result = malloc(sizeof(SDL_WindowShaper)); SDL_WindowShaper* result = malloc(sizeof(SDL_WindowShaper));
result->window = window; result->window = window;
result->alphacutoff = 0; result->mode.mode = ShapeModeDefault;
result->mode.parameters.binarizationCutoff = 1;
result->usershownflag = 0; result->usershownflag = 0;
//Put some driver-data here. //Put some driver-data here.
window->shaper = result; window->shaper = result;
...@@ -42,7 +43,6 @@ int Win32_SetWindowShape(SDL_WindowShaper *shaper,SDL_Surface *shape,SDL_WindowS ...@@ -42,7 +43,6 @@ int Win32_SetWindowShape(SDL_WindowShaper *shaper,SDL_Surface *shape,SDL_WindowS
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;
/* Assume that shaper->alphacutoff already has a value, because SDL_SetWindowShape() should have given it one. */
/* /*
* Start with empty region * Start with empty region
*/ */
...@@ -57,7 +57,7 @@ int Win32_SetWindowShape(SDL_WindowShaper *shaper,SDL_Surface *shape,SDL_WindowS ...@@ -57,7 +57,7 @@ int Win32_SetWindowShape(SDL_WindowShaper *shaper,SDL_Surface *shape,SDL_WindowS
/* /*
* Transfer binarized mask image into workbuffer * Transfer binarized mask image into workbuffer
*/ */
SDL_CalculateShapeBitmap(shaper->alphacutoff,shape,data->shapebuffer,1,0xff); SDL_CalculateShapeBitmap(shaper->mode,shape,data->shapebuffer,1,0xff);
//Move code over to here from AW_windowShape.c //Move code over to here from AW_windowShape.c
Uint8 *pos1 = data->shapebuffer + width - 1; Uint8 *pos1 = data->shapebuffer + width - 1;
...@@ -338,6 +338,8 @@ int Win32_ResizeWindowShape(SDL_Window *window) { ...@@ -338,6 +338,8 @@ int Win32_ResizeWindowShape(SDL_Window *window) {
return -1; return -1;
} }
} }
else
memset(data->shapebuffer,0,data->buffersize);
window->shaper->usershownflag |= window->flags & SDL_WINDOW_SHOWN; window->shaper->usershownflag |= window->flags & SDL_WINDOW_SHOWN;
......
...@@ -32,7 +32,8 @@ SDL_WindowShaper* X11_CreateShaper(SDL_Window* window) { ...@@ -32,7 +32,8 @@ SDL_WindowShaper* X11_CreateShaper(SDL_Window* window) {
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->alphacutoff = 0; result->mode.mode = ShapeModeDefault;
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;
...@@ -65,6 +66,8 @@ int X11_ResizeWindowShape(SDL_Window* window) { ...@@ -65,6 +66,8 @@ int X11_ResizeWindowShape(SDL_Window* window) {
return -1; return -1;
} }
} }
else
memset(data->bitmap,0,data->bitmapsize);
window->shaper->usershownflag |= window->flags & SDL_WINDOW_SHOWN; window->shaper->usershownflag |= window->flags & SDL_WINDOW_SHOWN;
...@@ -83,7 +86,7 @@ int X11_SetWindowShape(SDL_WindowShaper *shaper,SDL_Surface *shape,SDL_WindowSha ...@@ -83,7 +86,7 @@ int X11_SetWindowShape(SDL_WindowShaper *shaper,SDL_Surface *shape,SDL_WindowSha
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->alphacutoff,shape,data->bitmap,8,1); SDL_CalculateShapeBitmap(shaper->mode,shape,data->bitmap,8,1);
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);
......
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