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. More...
 
SDL_bool SDL_IsShapedWindow (const SDL_Window *window)
 Return whether the given window is a shaped window. More...
 
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. More...
 
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. More...
 

Function Documentation

◆ RecursivelyCalculateShapeTree()

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

Definition at line 127 of file SDL_shape.c.

127 {
128 int x = 0,y = 0;
129 Uint8* pixel = NULL;
130 Uint32 pixel_value = 0;
131 Uint8 r = 0,g = 0,b = 0,a = 0;
132 SDL_bool pixel_opaque = SDL_FALSE;
133 int last_opaque = -1;
136 SDL_Rect next = {0,0,0,0};
137
138 for(y=dimensions.y;y<dimensions.y + dimensions.h;y++) {
139 for(x=dimensions.x;x<dimensions.x + dimensions.w;x++) {
140 pixel_value = 0;
141 pixel = (Uint8 *)(mask->pixels) + (y*mask->pitch) + (x*mask->format->BytesPerPixel);
142 switch(mask->format->BytesPerPixel) {
143 case(1):
144 pixel_value = *(Uint8*)pixel;
145 break;
146 case(2):
147 pixel_value = *(Uint16*)pixel;
148 break;
149 case(3):
150 pixel_value = *(Uint32*)pixel & (~mask->format->Amask);
151 break;
152 case(4):
153 pixel_value = *(Uint32*)pixel;
154 break;
155 }
156 SDL_GetRGBA(pixel_value,mask->format,&r,&g,&b,&a);
157 switch(mode.mode) {
158 case(ShapeModeDefault):
159 pixel_opaque = (a >= 1 ? SDL_TRUE : SDL_FALSE);
160 break;
162 pixel_opaque = (a >= mode.parameters.binarizationCutoff ? SDL_TRUE : SDL_FALSE);
163 break;
165 pixel_opaque = (a <= mode.parameters.binarizationCutoff ? SDL_TRUE : SDL_FALSE);
166 break;
167 case(ShapeModeColorKey):
168 key = mode.parameters.colorKey;
169 pixel_opaque = ((key.r != r || key.g != g || key.b != b) ? SDL_TRUE : SDL_FALSE);
170 break;
171 }
172 if(last_opaque == -1)
173 last_opaque = pixel_opaque;
174 if(last_opaque != pixel_opaque) {
175 const int halfwidth = dimensions.w / 2;
176 const int halfheight = dimensions.h / 2;
177
178 result->kind = QuadShape;
179
180 next.x = dimensions.x;
181 next.y = dimensions.y;
182 next.w = halfwidth;
183 next.h = halfheight;
184 result->data.children.upleft = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next);
185
186 next.x = dimensions.x + halfwidth;
187 next.w = dimensions.w - halfwidth;
188 result->data.children.upright = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next);
189
190 next.x = dimensions.x;
191 next.w = halfwidth;
192 next.y = dimensions.y + halfheight;
193 next.h = dimensions.h - halfheight;
194 result->data.children.downleft = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next);
195
196 next.x = dimensions.x + halfwidth;
197 next.w = dimensions.w - halfwidth;
198 result->data.children.downright = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next);
199
200 return result;
201 }
202 }
203 }
204
205
206 /* If we never recursed, all the pixels in this quadrant have the same "value". */
207 result->kind = (last_opaque == SDL_TRUE ? OpaqueShape : TransparentShape);
208 result->data.shape = dimensions;
209 return result;
210}
#define SDL_malloc
#define SDL_GetRGBA
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
GLdouble GLdouble GLdouble r
Definition: SDL_opengl.h:2079
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
GLboolean GLboolean GLboolean b
GLenum mode
GLuint64EXT * result
GLboolean GLboolean GLboolean GLboolean a
GLboolean GLboolean g
GLenum GLint GLuint mask
static SDL_ShapeTree * RecursivelyCalculateShapeTree(SDL_WindowShapeMode mode, SDL_Surface *mask, SDL_Rect dimensions)
Definition: SDL_shape.c:127
@ ShapeModeBinarizeAlpha
A binarized alpha cutoff with a given integer value.
Definition: SDL_shape.h:84
@ ShapeModeColorKey
A color key is applied.
Definition: SDL_shape.h:88
@ ShapeModeDefault
The default mode, a binarized alpha cutoff of 1.
Definition: SDL_shape.h:82
@ ShapeModeReverseBinarizeAlpha
A binarized alpha cutoff with a given integer value, but with the opposite comparison.
Definition: SDL_shape.h:86
@ QuadShape
@ TransparentShape
@ OpaqueShape
SDL_bool
Definition: SDL_stdinc.h:162
@ SDL_TRUE
Definition: SDL_stdinc.h:164
@ SDL_FALSE
Definition: SDL_stdinc.h:163
uint32_t Uint32
Definition: SDL_stdinc.h:203
uint16_t Uint16
Definition: SDL_stdinc.h:191
uint8_t Uint8
Definition: SDL_stdinc.h:179
#define NULL
Definition: begin_code.h:167
GLuint64 key
Definition: gl2ext.h:2192
A rectangle, with the origin at the upper left (integer).
Definition: SDL_rect.h:78
int h
Definition: SDL_rect.h:80
int w
Definition: SDL_rect.h:80
int y
Definition: SDL_rect.h:79
int x
Definition: SDL_rect.h:79

References SDL_Rect::h, NULL, OpaqueShape, QuadShape, RecursivelyCalculateShapeTree(), SDL_FALSE, SDL_GetRGBA, SDL_malloc, SDL_TRUE, ShapeModeBinarizeAlpha, ShapeModeColorKey, ShapeModeDefault, ShapeModeReverseBinarizeAlpha, TransparentShape, SDL_Rect::w, SDL_Rect::x, and SDL_Rect::y.

Referenced by RecursivelyCalculateShapeTree(), and SDL_CalculateShapeTree().

◆ SDL_CalculateShapeBitmap()

void SDL_CalculateShapeBitmap ( SDL_WindowShapeMode  mode,
SDL_Surface shape,
Uint8 bitmap,
Uint8  ppb 
)

Definition at line 71 of file SDL_shape.c.

72{
73 int x = 0;
74 int y = 0;
75 Uint8 r = 0,g = 0,b = 0,alpha = 0;
76 Uint8* pixel = NULL;
77 Uint32 pixel_value = 0,mask_value = 0;
78 int bytes_per_scanline = (shape->w + (ppb - 1)) / ppb;
79 Uint8 *bitmap_scanline;
81 if(SDL_MUSTLOCK(shape))
82 SDL_LockSurface(shape);
83 for(y = 0;y<shape->h;y++) {
84 bitmap_scanline = bitmap + y * bytes_per_scanline;
85 for(x=0;x<shape->w;x++) {
86 alpha = 0;
87 pixel_value = 0;
88 pixel = (Uint8 *)(shape->pixels) + (y*shape->pitch) + (x*shape->format->BytesPerPixel);
89 switch(shape->format->BytesPerPixel) {
90 case(1):
91 pixel_value = *(Uint8*)pixel;
92 break;
93 case(2):
94 pixel_value = *(Uint16*)pixel;
95 break;
96 case(3):
97 pixel_value = *(Uint32*)pixel & (~shape->format->Amask);
98 break;
99 case(4):
100 pixel_value = *(Uint32*)pixel;
101 break;
102 }
103 SDL_GetRGBA(pixel_value,shape->format,&r,&g,&b,&alpha);
104 switch(mode.mode) {
105 case(ShapeModeDefault):
106 mask_value = (alpha >= 1 ? 1 : 0);
107 break;
109 mask_value = (alpha >= mode.parameters.binarizationCutoff ? 1 : 0);
110 break;
112 mask_value = (alpha <= mode.parameters.binarizationCutoff ? 1 : 0);
113 break;
114 case(ShapeModeColorKey):
115 key = mode.parameters.colorKey;
116 mask_value = ((key.r != r || key.g != g || key.b != b) ? 1 : 0);
117 break;
118 }
119 bitmap_scanline[x / ppb] |= mask_value << (x % ppb);
120 }
121 }
122 if(SDL_MUSTLOCK(shape))
123 SDL_UnlockSurface(shape);
124}
#define SDL_UnlockSurface
#define SDL_LockSurface
GLsizei GLfixed GLfixed GLfixed GLfixed const GLubyte * bitmap
GLfloat GLfloat GLfloat alpha
#define SDL_MUSTLOCK(S)
Definition: SDL_surface.h:62
Uint8 BytesPerPixel
Definition: SDL_pixels.h:320
SDL_PixelFormat * format
Definition: SDL_surface.h:73
void * pixels
Definition: SDL_surface.h:76

References SDL_PixelFormat::Amask, SDL_PixelFormat::BytesPerPixel, SDL_Surface::format, SDL_Surface::h, NULL, SDL_Surface::pitch, SDL_Surface::pixels, SDL_GetRGBA, SDL_LockSurface, SDL_MUSTLOCK, SDL_UnlockSurface, ShapeModeBinarizeAlpha, ShapeModeColorKey, ShapeModeDefault, ShapeModeReverseBinarizeAlpha, and SDL_Surface::w.

◆ SDL_CalculateShapeTree()

SDL_ShapeTree * SDL_CalculateShapeTree ( SDL_WindowShapeMode  mode,
SDL_Surface shape 
)

Definition at line 213 of file SDL_shape.c.

214{
215 SDL_Rect dimensions;
217
218 dimensions.x = 0;
219 dimensions.y = 0;
220 dimensions.w = shape->w;
221 dimensions.h = shape->h;
222
223 if(SDL_MUSTLOCK(shape))
224 SDL_LockSurface(shape);
225 result = RecursivelyCalculateShapeTree(mode,shape,dimensions);
226 if(SDL_MUSTLOCK(shape))
227 SDL_UnlockSurface(shape);
228 return result;
229}

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_CreateShapedWindow()

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.

34{
36 result = SDL_CreateWindow(title,-1000,-1000,w,h,(flags | SDL_WINDOW_BORDERLESS) & (~SDL_WINDOW_FULLSCREEN) & (~SDL_WINDOW_RESIZABLE) /* & (~SDL_WINDOW_SHOWN) */);
37 if(result != NULL) {
38 if (SDL_GetVideoDevice()->shape_driver.CreateShaper == NULL) {
40 return NULL;
41 }
43 if(result->shaper != NULL) {
44 result->shaper->userx = x;
45 result->shaper->usery = y;
46 result->shaper->mode.mode = ShapeModeDefault;
47 result->shaper->mode.parameters.binarizationCutoff = 1;
48 result->shaper->hasshape = SDL_FALSE;
49 return result;
50 }
51 else {
53 return NULL;
54 }
55 }
56 else
57 return NULL;
58}
#define SDL_DestroyWindow
#define SDL_CreateWindow
GLbitfield flags
GLfloat GLfloat GLfloat GLfloat h
GLubyte GLubyte GLubyte GLubyte w
SDL_VideoDevice * SDL_GetVideoDevice(void)
Definition: SDL_video.c:583
@ SDL_WINDOW_RESIZABLE
Definition: SDL_video.h:105
@ SDL_WINDOW_FULLSCREEN
Definition: SDL_video.h:100
@ SDL_WINDOW_BORDERLESS
Definition: SDL_video.h:104
SDL_WindowShaper *(* CreateShaper)(SDL_Window *window)
Definition: SDL_sysvideo.h:60
SDL_ShapeDriver shape_driver
Definition: SDL_sysvideo.h:245
The type used to identify a window.
Definition: SDL_sysvideo.h:74

References SDL_ShapeDriver::CreateShaper, NULL, SDL_CreateWindow, SDL_DestroyWindow, SDL_FALSE, SDL_GetVideoDevice(), SDL_WINDOW_BORDERLESS, SDL_WINDOW_FULLSCREEN, SDL_WINDOW_RESIZABLE, SDL_VideoDevice::shape_driver, ShapeModeDefault, and SDL_WindowShaper::userx.

◆ SDL_FreeShapeTree()

void SDL_FreeShapeTree ( SDL_ShapeTree **  shape_tree)

Definition at line 246 of file SDL_shape.c.

247{
248 if((*shape_tree)->kind == QuadShape) {
249 SDL_FreeShapeTree((SDL_ShapeTree **)(char*)&(*shape_tree)->data.children.upleft);
250 SDL_FreeShapeTree((SDL_ShapeTree **)(char*)&(*shape_tree)->data.children.upright);
251 SDL_FreeShapeTree((SDL_ShapeTree **)(char*)&(*shape_tree)->data.children.downleft);
252 SDL_FreeShapeTree((SDL_ShapeTree **)(char*)&(*shape_tree)->data.children.downright);
253 }
254 SDL_free(*shape_tree);
255 *shape_tree = NULL;
256}
#define SDL_free
void SDL_FreeShapeTree(SDL_ShapeTree **shape_tree)
Definition: SDL_shape.c:246

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

Referenced by SDL_FreeShapeTree().

◆ SDL_GetShapedWindowMode()

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.

291{
293 if(shape_mode == NULL) {
295 /* The window given has a shape. */
296 return 0;
297 else
298 /* The window given is shapeable but lacks a shape. */
300 }
301 else {
302 *shape_mode = window->shaper->mode;
303 return 0;
304 }
305 }
306 else
307 /* The window given is not a valid shapeable window. */
309}
static SDL_bool SDL_WindowHasAShape(SDL_Window *window)
Definition: SDL_shape.c:282
SDL_bool SDL_IsShapedWindow(const SDL_Window *window)
Return whether the given window is a shaped window.
Definition: SDL_shape.c:61
#define SDL_NONSHAPEABLE_WINDOW
Definition: SDL_shape.h:42
#define SDL_WINDOW_LACKS_SHAPE
Definition: SDL_shape.h:44
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
WindowShapeMode mode
The mode of these window-shape parameters.
Definition: SDL_shape.h:103

References SDL_WindowShapeMode::mode, NULL, SDL_IsShapedWindow(), SDL_NONSHAPEABLE_WINDOW, SDL_WINDOW_LACKS_SHAPE, and SDL_WindowHasAShape().

◆ SDL_IsShapedWindow()

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.

62{
63 if(window == NULL)
64 return SDL_FALSE;
65 else
66 return (SDL_bool)(window->shaper != NULL);
67}

References NULL, and SDL_FALSE.

Referenced by SDL_GetShapedWindowMode(), SDL_SetWindowShape(), and SDL_WindowHasAShape().

◆ SDL_SetWindowShape()

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.

260{
261 int result;
263 /* The window given was not a shapeable window. */
265 if(shape == NULL)
266 /* Invalid shape argument. */
268
269 if(shape_mode != NULL)
270 window->shaper->mode = *shape_mode;
271 result = SDL_GetVideoDevice()->shape_driver.SetWindowShape(window->shaper,shape,shape_mode);
272 window->shaper->hasshape = SDL_TRUE;
273 if(window->shaper->userx != 0 && window->shaper->usery != 0) {
274 SDL_SetWindowPosition(window,window->shaper->userx,window->shaper->usery);
275 window->shaper->userx = 0;
276 window->shaper->usery = 0;
277 }
278 return result;
279}
#define SDL_SetWindowPosition
#define SDL_INVALID_SHAPE_ARGUMENT
Definition: SDL_shape.h:43
int(* SetWindowShape)(SDL_WindowShaper *shaper, SDL_Surface *shape, SDL_WindowShapeMode *shape_mode)
Definition: SDL_sysvideo.h:61

References NULL, SDL_GetVideoDevice(), SDL_INVALID_SHAPE_ARGUMENT, SDL_IsShapedWindow(), SDL_NONSHAPEABLE_WINDOW, SDL_SetWindowPosition, SDL_TRUE, SDL_ShapeDriver::SetWindowShape, and SDL_VideoDevice::shape_driver.

◆ SDL_TraverseShapeTree()

void SDL_TraverseShapeTree ( SDL_ShapeTree tree,
SDL_TraversalFunction  function,
void closure 
)

Definition at line 232 of file SDL_shape.c.

233{
234 SDL_assert(tree != NULL);
235 if(tree->kind == QuadShape) {
236 SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.upleft,function,closure);
237 SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.upright,function,closure);
238 SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.downleft,function,closure);
239 SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.downright,function,closure);
240 }
241 else
242 function(tree,closure);
243}
#define SDL_assert(condition)
Definition: SDL_assert.h:169
void SDL_TraverseShapeTree(SDL_ShapeTree *tree, SDL_TraversalFunction function, void *closure)
Definition: SDL_shape.c:232
struct SDL_ShapeTree * downright
struct SDL_ShapeTree * upleft
struct SDL_ShapeTree * downleft
struct SDL_ShapeTree * upright
SDL_ShapeKind kind
SDL_ShapeUnion data
SDL_QuadTreeChildren children

References SDL_ShapeUnion::children, SDL_ShapeTree::data, SDL_QuadTreeChildren::downleft, SDL_QuadTreeChildren::downright, SDL_ShapeTree::kind, NULL, QuadShape, SDL_assert, SDL_TraverseShapeTree(), SDL_QuadTreeChildren::upleft, and SDL_QuadTreeChildren::upright.

Referenced by SDL_TraverseShapeTree().

◆ SDL_WindowHasAShape()

static SDL_bool SDL_WindowHasAShape ( SDL_Window window)
static

Definition at line 282 of file SDL_shape.c.

283{
285 return SDL_FALSE;
286 return window->shaper->hasshape;
287}

References NULL, SDL_FALSE, and SDL_IsShapedWindow().

Referenced by SDL_GetShapedWindowMode().