SDL  2.0
SDL_shape.c File Reference
#include "../SDL_internal.h"
#include "SDL.h"
#include "SDL_assert.h"
#include "SDL_video.h"
#include "SDL_sysvideo.h"
#include "SDL_pixels.h"
#include "SDL_surface.h"
#include "SDL_shape.h"
#include "SDL_shape_internals.h"
+ Include dependency graph for SDL_shape.c:

Go to the source code of this file.

Functions

SDL_WindowSDL_CreateShapedWindow (const char *title, unsigned int x, unsigned int y, unsigned int w, unsigned int h, Uint32 flags)
 Create a window that can be shaped with the specified position, dimensions, and flags.
SDL_bool SDL_IsShapedWindow (const SDL_Window *window)
 Return whether the given window is a shaped window.
void SDL_CalculateShapeBitmap (SDL_WindowShapeMode mode, SDL_Surface *shape, Uint8 *bitmap, Uint8 ppb)
static SDL_ShapeTreeRecursivelyCalculateShapeTree (SDL_WindowShapeMode mode, SDL_Surface *mask, SDL_Rect dimensions)
SDL_ShapeTreeSDL_CalculateShapeTree (SDL_WindowShapeMode mode, SDL_Surface *shape)
void SDL_TraverseShapeTree (SDL_ShapeTree *tree, SDL_TraversalFunction function, void *closure)
void SDL_FreeShapeTree (SDL_ShapeTree **shape_tree)
int SDL_SetWindowShape (SDL_Window *window, SDL_Surface *shape, SDL_WindowShapeMode *shape_mode)
 Set the shape and parameters of a shaped window.
static SDL_bool SDL_WindowHasAShape (SDL_Window *window)
int SDL_GetShapedWindowMode (SDL_Window *window, SDL_WindowShapeMode *shape_mode)
 Get the shape parameters of a shaped window.

Function Documentation

static SDL_ShapeTree* RecursivelyCalculateShapeTree ( SDL_WindowShapeMode  mode,
SDL_Surface mask,
SDL_Rect  dimensions 
)
static

Definition at line 127 of file SDL_shape.c.

References SDL_PixelFormat::Amask, SDL_Color::b, SDL_WindowShapeParams::binarizationCutoff, SDL_PixelFormat::BytesPerPixel, SDL_ShapeUnion::children, SDL_WindowShapeParams::colorKey, SDL_ShapeTree::data, SDL_QuadTreeChildren::downleft, SDL_QuadTreeChildren::downright, SDL_Surface::format, SDL_Color::g, SDL_Rect::h, SDL_ShapeTree::kind, SDL_WindowShapeMode::mode, NULL, OpaqueShape, SDL_WindowShapeMode::parameters, SDL_Surface::pitch, SDL_Surface::pixels, QuadShape, SDL_Color::r, SDL_FALSE, SDL_GetRGBA, SDL_malloc, SDL_TRUE, SDL_ShapeUnion::shape, ShapeModeBinarizeAlpha, ShapeModeColorKey, ShapeModeDefault, ShapeModeReverseBinarizeAlpha, TransparentShape, SDL_QuadTreeChildren::upleft, SDL_QuadTreeChildren::upright, SDL_Rect::w, SDL_Rect::x, and SDL_Rect::y.

Referenced by SDL_CalculateShapeTree().

{
int x = 0,y = 0;
Uint8* pixel = NULL;
Uint32 pixel_value = 0;
Uint8 r = 0,g = 0,b = 0,a = 0;
SDL_bool pixel_opaque = SDL_FALSE;
int last_opaque = -1;
SDL_Rect next = {0,0,0,0};
for(y=dimensions.y;y<dimensions.y + dimensions.h;y++) {
for(x=dimensions.x;x<dimensions.x + dimensions.w;x++) {
pixel_value = 0;
pixel = (Uint8 *)(mask->pixels) + (y*mask->pitch) + (x*mask->format->BytesPerPixel);
switch(mask->format->BytesPerPixel) {
case(1):
pixel_value = *(Uint8*)pixel;
break;
case(2):
pixel_value = *(Uint16*)pixel;
break;
case(3):
pixel_value = *(Uint32*)pixel & (~mask->format->Amask);
break;
case(4):
pixel_value = *(Uint32*)pixel;
break;
}
SDL_GetRGBA(pixel_value,mask->format,&r,&g,&b,&a);
switch(mode.mode) {
pixel_opaque = (a >= 1 ? SDL_TRUE : SDL_FALSE);
break;
pixel_opaque = (a >= mode.parameters.binarizationCutoff ? SDL_TRUE : SDL_FALSE);
break;
pixel_opaque = (a <= mode.parameters.binarizationCutoff ? SDL_TRUE : SDL_FALSE);
break;
key = mode.parameters.colorKey;
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 != pixel_opaque) {
const int halfwidth = dimensions.w / 2;
const int halfheight = dimensions.h / 2;
result->kind = QuadShape;
next.x = dimensions.x;
next.y = dimensions.y;
next.w = halfwidth;
next.h = halfheight;
result->data.children.upleft = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next);
next.x = dimensions.x + halfwidth;
next.w = dimensions.w - halfwidth;
result->data.children.upright = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next);
next.x = dimensions.x;
next.w = halfwidth;
next.y = dimensions.y + halfheight;
next.h = dimensions.h - halfheight;
result->data.children.downleft = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next);
next.x = dimensions.x + halfwidth;
next.w = dimensions.w - halfwidth;
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);
result->data.shape = dimensions;
return result;
}
void SDL_CalculateShapeBitmap ( SDL_WindowShapeMode  mode,
SDL_Surface shape,
Uint8 bitmap,
Uint8  ppb 
)

Definition at line 71 of file SDL_shape.c.

References SDL_PixelFormat::Amask, SDL_Color::b, SDL_WindowShapeParams::binarizationCutoff, SDL_PixelFormat::BytesPerPixel, SDL_WindowShapeParams::colorKey, SDL_Surface::format, SDL_Color::g, SDL_Surface::h, SDL_WindowShapeMode::mode, NULL, SDL_WindowShapeMode::parameters, SDL_Surface::pitch, SDL_Surface::pixels, SDL_Color::r, SDL_GetRGBA, SDL_LockSurface, SDL_MUSTLOCK, SDL_UnlockSurface, ShapeModeBinarizeAlpha, ShapeModeColorKey, ShapeModeDefault, ShapeModeReverseBinarizeAlpha, and SDL_Surface::w.

{
int x = 0;
int y = 0;
Uint8 r = 0,g = 0,b = 0,alpha = 0;
Uint8* pixel = NULL;
Uint32 pixel_value = 0,mask_value = 0;
int bytes_per_scanline = (shape->w + (ppb - 1)) / ppb;
Uint8 *bitmap_scanline;
if(SDL_MUSTLOCK(shape))
for(y = 0;y<shape->h;y++) {
bitmap_scanline = bitmap + y * bytes_per_scanline;
for(x=0;x<shape->w;x++) {
alpha = 0;
pixel_value = 0;
pixel = (Uint8 *)(shape->pixels) + (y*shape->pitch) + (x*shape->format->BytesPerPixel);
switch(shape->format->BytesPerPixel) {
case(1):
pixel_value = *(Uint8*)pixel;
break;
case(2):
pixel_value = *(Uint16*)pixel;
break;
case(3):
pixel_value = *(Uint32*)pixel & (~shape->format->Amask);
break;
case(4):
pixel_value = *(Uint32*)pixel;
break;
}
SDL_GetRGBA(pixel_value,shape->format,&r,&g,&b,&alpha);
switch(mode.mode) {
mask_value = (alpha >= 1 ? 1 : 0);
break;
mask_value = (alpha >= mode.parameters.binarizationCutoff ? 1 : 0);
break;
mask_value = (alpha <= mode.parameters.binarizationCutoff ? 1 : 0);
break;
key = mode.parameters.colorKey;
mask_value = ((key.r != r || key.g != g || key.b != b) ? 1 : 0);
break;
}
bitmap_scanline[x / ppb] |= mask_value << (x % ppb);
}
}
if(SDL_MUSTLOCK(shape))
}
SDL_ShapeTree* SDL_CalculateShapeTree ( SDL_WindowShapeMode  mode,
SDL_Surface shape 
)

Definition at line 213 of file SDL_shape.c.

References SDL_Rect::h, SDL_Surface::h, NULL, RecursivelyCalculateShapeTree(), SDL_LockSurface, SDL_MUSTLOCK, SDL_UnlockSurface, SDL_Rect::w, SDL_Surface::w, SDL_Rect::x, and SDL_Rect::y.

{
SDL_Rect dimensions;
dimensions.x = 0;
dimensions.y = 0;
dimensions.w = shape->w;
dimensions.h = shape->h;
if(SDL_MUSTLOCK(shape))
result = RecursivelyCalculateShapeTree(mode,shape,dimensions);
if(SDL_MUSTLOCK(shape))
return result;
}
SDL_Window* SDL_CreateShapedWindow ( const char *  title,
unsigned int  x,
unsigned int  y,
unsigned int  w,
unsigned int  h,
Uint32  flags 
)

Create a window that can be shaped with the specified position, dimensions, and flags.

Parameters
titleThe title of the window, in UTF-8 encoding.
xThe x position of the window, SDL_WINDOWPOS_CENTERED, or SDL_WINDOWPOS_UNDEFINED.
yThe y position of the window, SDL_WINDOWPOS_CENTERED, or SDL_WINDOWPOS_UNDEFINED.
wThe width of the window.
hThe height of the window.
flagsThe flags for the window, a mask of SDL_WINDOW_BORDERLESS with any of the following: SDL_WINDOW_OPENGL, SDL_WINDOW_INPUT_GRABBED, SDL_WINDOW_HIDDEN, SDL_WINDOW_RESIZABLE, SDL_WINDOW_MAXIMIZED, SDL_WINDOW_MINIMIZED, SDL_WINDOW_BORDERLESS is always set, and SDL_WINDOW_FULLSCREEN is always unset.
Returns
The window created, or NULL if window creation failed.
See Also
SDL_DestroyWindow()

Definition at line 33 of file SDL_shape.c.

References SDL_WindowShapeParams::binarizationCutoff, SDL_ShapeDriver::CreateShaper, SDL_WindowShaper::hasshape, SDL_WindowShaper::mode, SDL_WindowShapeMode::mode, NULL, SDL_WindowShapeMode::parameters, SDL_CreateWindow, SDL_DestroyWindow, SDL_FALSE, SDL_GetVideoDevice(), SDL_WINDOW_BORDERLESS, SDL_WINDOW_FULLSCREEN, SDL_WINDOW_RESIZABLE, SDL_VideoDevice::shape_driver, ShapeModeDefault, SDL_Window::shaper, SDL_WindowShaper::userx, and SDL_WindowShaper::usery.

{
result = SDL_CreateWindow(title,-1000,-1000,w,h,(flags | SDL_WINDOW_BORDERLESS) & (~SDL_WINDOW_FULLSCREEN) & (~SDL_WINDOW_RESIZABLE) /* & (~SDL_WINDOW_SHOWN) */);
if(result != NULL) {
if (SDL_GetVideoDevice()->shape_driver.CreateShaper == NULL) {
return NULL;
}
if(result->shaper != NULL) {
result->shaper->userx = x;
result->shaper->usery = y;
return result;
}
else {
return NULL;
}
}
else
return NULL;
}
void SDL_FreeShapeTree ( SDL_ShapeTree **  shape_tree)

Definition at line 246 of file SDL_shape.c.

References NULL, QuadShape, SDL_free, and SDL_FreeShapeTree().

Referenced by SDL_FreeShapeTree().

{
if((*shape_tree)->kind == QuadShape) {
SDL_FreeShapeTree((SDL_ShapeTree **)(char*)&(*shape_tree)->data.children.upleft);
SDL_FreeShapeTree((SDL_ShapeTree **)(char*)&(*shape_tree)->data.children.upright);
SDL_FreeShapeTree((SDL_ShapeTree **)(char*)&(*shape_tree)->data.children.downleft);
SDL_FreeShapeTree((SDL_ShapeTree **)(char*)&(*shape_tree)->data.children.downright);
}
SDL_free(*shape_tree);
*shape_tree = NULL;
}
int SDL_GetShapedWindowMode ( SDL_Window window,
SDL_WindowShapeMode shape_mode 
)

Get the shape parameters of a shaped window.

Parameters
windowThe shaped window whose parameters should be retrieved.
shape_modeAn empty shape-mode structure to fill, or NULL to check whether the window has a shape.
Returns
0 if the window has a shape and, provided shape_mode was not NULL, shape_mode has been filled with the mode data, SDL_NONSHAPEABLE_WINDOW if the SDL_Window given is not a shaped window, or SDL_WINDOW_LACKS_SHAPE if the SDL_Window given is a shapeable window currently lacking a shape.
See Also
SDL_WindowShapeMode
SDL_SetWindowShape

Definition at line 290 of file SDL_shape.c.

References SDL_WindowShaper::mode, NULL, SDL_IsShapedWindow, SDL_NONSHAPEABLE_WINDOW, SDL_WINDOW_LACKS_SHAPE, SDL_WindowHasAShape(), and SDL_Window::shaper.

{
if(window != NULL && SDL_IsShapedWindow(window)) {
if(shape_mode == NULL) {
if(SDL_WindowHasAShape(window))
/* The window given has a shape. */
return 0;
else
/* The window given is shapeable but lacks a shape. */
}
else {
*shape_mode = window->shaper->mode;
return 0;
}
}
else
/* The window given is not a valid shapeable window. */
}
SDL_bool SDL_IsShapedWindow ( const SDL_Window window)

Return whether the given window is a shaped window.

Parameters
windowThe window to query for being shaped.
Returns
SDL_TRUE if the window is a window that can be shaped, SDL_FALSE if the window is unshaped or NULL.
See Also
SDL_CreateShapedWindow

Definition at line 61 of file SDL_shape.c.

References NULL, SDL_FALSE, and SDL_Window::shaper.

{
if(window == NULL)
return SDL_FALSE;
else
return (SDL_bool)(window->shaper != NULL);
}
int SDL_SetWindowShape ( SDL_Window window,
SDL_Surface shape,
SDL_WindowShapeMode shape_mode 
)

Set the shape and parameters of a shaped window.

Parameters
windowThe shaped window whose parameters should be set.
shapeA surface encoding the desired shape for the window.
shape_modeThe parameters to set for the shaped window.
Returns
0 on success, SDL_INVALID_SHAPE_ARGUMENT on an invalid shape argument, or SDL_NONSHAPEABLE_WINDOW if the SDL_Window given does not reference a valid shaped window.
See Also
SDL_WindowShapeMode
SDL_GetShapedWindowMode.

Definition at line 259 of file SDL_shape.c.

References SDL_WindowShaper::hasshape, SDL_WindowShaper::mode, NULL, SDL_GetVideoDevice(), SDL_INVALID_SHAPE_ARGUMENT, SDL_IsShapedWindow, SDL_NONSHAPEABLE_WINDOW, SDL_SetWindowPosition, SDL_TRUE, SDL_ShapeDriver::SetWindowShape, SDL_VideoDevice::shape_driver, SDL_Window::shaper, SDL_WindowShaper::userx, and SDL_WindowShaper::usery.

{
int result;
if(window == NULL || !SDL_IsShapedWindow(window))
/* The window given was not a shapeable window. */
if(shape == NULL)
/* Invalid shape argument. */
if(shape_mode != NULL)
window->shaper->mode = *shape_mode;
result = SDL_GetVideoDevice()->shape_driver.SetWindowShape(window->shaper,shape,shape_mode);
window->shaper->hasshape = SDL_TRUE;
if(window->shaper->userx != 0 && window->shaper->usery != 0) {
SDL_SetWindowPosition(window,window->shaper->userx,window->shaper->usery);
window->shaper->userx = 0;
window->shaper->usery = 0;
}
return result;
}
static SDL_bool SDL_WindowHasAShape ( SDL_Window window)
static

Definition at line 282 of file SDL_shape.c.

References SDL_WindowShaper::hasshape, NULL, SDL_FALSE, SDL_IsShapedWindow, and SDL_Window::shaper.

Referenced by SDL_GetShapedWindowMode().

{
if (window == NULL || !SDL_IsShapedWindow(window))
return SDL_FALSE;
return window->shaper->hasshape;
}