zenilib: zenilib/jni/external/sdl/src/video/SDL_shape.c Source File

zenilib 0.5.3.0

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SDL_shape.c
Go to the documentation of this file.
1 /*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20 */
21 #include "SDL_config.h"
22
23 #include "SDL.h"
24 #include "SDL_assert.h"
25 #include "SDL_video.h"
26 #include "SDL_sysvideo.h"
27 #include "SDL_pixels.h"
28 #include "SDL_surface.h"
29 #include "SDL_shape.h"
30 #include "SDL_shape_internals.h"
31
32 SDL_Window*
33 SDL_CreateShapedWindow(const char *title,unsigned int x,unsigned int y,unsigned int w,unsigned int h,Uint32 flags)
34 {
35 SDL_Window *result = NULL;
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 result->shaper = SDL_GetVideoDevice()->shape_driver.CreateShaper(result);
39 if(result->shaper != NULL) {
40 result->shaper->userx = x;
41 result->shaper->usery = y;
42 result->shaper->mode.mode = ShapeModeDefault;
43 result->shaper->mode.parameters.binarizationCutoff = 1;
44 result->shaper->hasshape = SDL_FALSE;
45 return result;
46 }
47 else {
48 SDL_DestroyWindow(result);
49 return NULL;
50 }
51 }
52 else
53 return NULL;
54 }
55
56 SDL_bool
57 SDL_IsShapedWindow(const SDL_Window *window)
58 {
59 if(window == NULL)
60 return SDL_FALSE;
61 else
62 return (SDL_bool)(window->shaper != NULL);
63 }
64
65 /* REQUIRES that bitmap point to a w-by-h bitmap with ppb pixels-per-byte. */
66 void
67 SDL_CalculateShapeBitmap(SDL_WindowShapeMode mode,SDL_Surface *shape,Uint8* bitmap,Uint8 ppb)
68 {
69 int x = 0;
70 int y = 0;
71 Uint8 r = 0,g = 0,b = 0,alpha = 0;
72 Uint8* pixel = NULL;
73 Uint32 bitmap_pixel,pixel_value = 0,mask_value = 0;
74 SDL_Color key;
75 if(SDL_MUSTLOCK(shape))
76 SDL_LockSurface(shape);
77 for(y = 0;y<shape->h;y++) {
78 for(x=0;x<shape->w;x++) {
79 alpha = 0;
80 pixel_value = 0;
81 pixel = (Uint8 *)(shape->pixels) + (y*shape->pitch) + (x*shape->format->BytesPerPixel);
82 switch(shape->format->BytesPerPixel) {
83 case(1):
84 pixel_value = *(Uint8*)pixel;
85 break;
86 case(2):
87 pixel_value = *(Uint16*)pixel;
88 break;
89 case(3):
90 pixel_value = *(Uint32*)pixel & (~shape->format->Amask);
91 break;
92 case(4):
93 pixel_value = *(Uint32*)pixel;
94 break;
95 }
96 SDL_GetRGBA(pixel_value,shape->format,&r,&g,&b,&alpha);
97 bitmap_pixel = y*shape->w + x;
98 switch(mode.mode) {
99 case(ShapeModeDefault):
100 mask_value = (alpha >= 1 ? 1 : 0);
101 break;
102 case(ShapeModeBinarizeAlpha):
103 mask_value = (alpha >= mode.parameters.binarizationCutoff ? 1 : 0);
104 break;
105 case(ShapeModeReverseBinarizeAlpha):
106 mask_value = (alpha <= mode.parameters.binarizationCutoff ? 1 : 0);
107 break;
108 case(ShapeModeColorKey):
109 key = mode.parameters.colorKey;
110 mask_value = ((key.r != r || key.g != g || key.b != b) ? 1 : 0);
111 break;
112 }
113 bitmap[bitmap_pixel / ppb] |= mask_value << (7 - ((ppb - 1) - (bitmap_pixel % ppb)));
114 }
115 }
116 if(SDL_MUSTLOCK(shape))
117 SDL_UnlockSurface(shape);
118 }
119
120 static SDL_ShapeTree*
121 RecursivelyCalculateShapeTree(SDL_WindowShapeMode mode,SDL_Surface* mask,SDL_Rect dimensions) {
122 int x = 0,y = 0;
123 Uint8* pixel = NULL;
124 Uint32 pixel_value = 0;
125 Uint8 r = 0,g = 0,b = 0,a = 0;
126 SDL_bool pixel_opaque = SDL_FALSE;
127 int last_opaque = -1;
128 SDL_Color key;
129 SDL_ShapeTree* result = (SDL_ShapeTree*)SDL_malloc(sizeof(SDL_ShapeTree));
130 SDL_Rect next = {0,0,0,0};
131 for(y=dimensions.y;y<dimensions.y + dimensions.h;y++) {
132 for(x=dimensions.x;x<dimensions.x + dimensions.w;x++) {
133 pixel_value = 0;
134 pixel = (Uint8 *)(mask->pixels) + (y*mask->pitch) + (x*mask->format->BytesPerPixel);
135 switch(mask->format->BytesPerPixel) {
136 case(1):
137 pixel_value = *(Uint8*)pixel;
138 break;
139 case(2):
140 pixel_value = *(Uint16*)pixel;
141 break;
142 case(3):
143 pixel_value = *(Uint32*)pixel & (~mask->format->Amask);
144 break;
145 case(4):
146 pixel_value = *(Uint32*)pixel;
147 break;
148 }
149 SDL_GetRGBA(pixel_value,mask->format,&r,&g,&b,&a);
150 switch(mode.mode) {
151 case(ShapeModeDefault):
152 pixel_opaque = (a >= 1 ? SDL_TRUE : SDL_FALSE);
153 break;
154 case(ShapeModeBinarizeAlpha):
155 pixel_opaque = (a >= mode.parameters.binarizationCutoff ? SDL_TRUE : SDL_FALSE);
156 break;
157 case(ShapeModeReverseBinarizeAlpha):
158 pixel_opaque = (a <= mode.parameters.binarizationCutoff ? SDL_TRUE : SDL_FALSE);
159 break;
160 case(ShapeModeColorKey):
161 key = mode.parameters.colorKey;
162 pixel_opaque = ((key.r != r || key.g != g || key.b != b) ? SDL_TRUE : SDL_FALSE);
163 break;
164 }
165 if(last_opaque == -1)
166 last_opaque = pixel_opaque;
167 if(last_opaque != pixel_opaque) {
168 result->kind = QuadShape;
169 /* These will stay the same. */
170 next.w = dimensions.w / 2;
171 next.h = dimensions.h / 2;
172 /* These will change from recursion to recursion. */
173 next.x = dimensions.x;
174 next.y = dimensions.y;
175 result->data.children.upleft = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next);
176 next.x += next.w;
177 /* Unneeded: next.y = dimensions.y; */
178 result->data.children.upright = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next);
179 next.x = dimensions.x;
180 next.y += next.h;
181 result->data.children.downleft = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next);
182 next.x += next.w;
183 /* Unneeded: next.y = dimensions.y + dimensions.h /2; */
184 result->data.children.downright = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next);
185 return result;
186 }
187 }
188 }
189 /* If we never recursed, all the pixels in this quadrant have the same "value". */
190 result->kind = (last_opaque == SDL_TRUE ? OpaqueShape : TransparentShape);
191 result->data.shape = dimensions;
192 return result;
193 }
194
195 SDL_ShapeTree*
196 SDL_CalculateShapeTree(SDL_WindowShapeMode mode,SDL_Surface* shape)
197 {
198 SDL_Rect dimensions = {0,0,shape->w,shape->h};
199 SDL_ShapeTree* result = NULL;
200 if(SDL_MUSTLOCK(shape))
201 SDL_LockSurface(shape);
202 result = RecursivelyCalculateShapeTree(mode,shape,dimensions);
203 if(SDL_MUSTLOCK(shape))
204 SDL_UnlockSurface(shape);
205 return result;
206 }
207
208 void
209 SDL_TraverseShapeTree(SDL_ShapeTree *tree,SDL_TraversalFunction function,void* closure)
210 {
211 SDL_assert(tree != NULL);
212 if(tree->kind == QuadShape) {
213 SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.upleft,function,closure);
214 SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.upright,function,closure);
215 SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.downleft,function,closure);
216 SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.downright,function,closure);
217 }
218 else
219 function(tree,closure);
220 }
221
222 void
223 SDL_FreeShapeTree(SDL_ShapeTree** shape_tree)
224 {
225 if((*shape_tree)->kind == QuadShape) {
226 SDL_FreeShapeTree((SDL_ShapeTree **)&(*shape_tree)->data.children.upleft);
227 SDL_FreeShapeTree((SDL_ShapeTree **)&(*shape_tree)->data.children.upright);
228 SDL_FreeShapeTree((SDL_ShapeTree **)&(*shape_tree)->data.children.downleft);
229 SDL_FreeShapeTree((SDL_ShapeTree **)&(*shape_tree)->data.children.downright);
230 }
231 SDL_free(*shape_tree);
232 *shape_tree = NULL;
233 }
234
235 int
236 SDL_SetWindowShape(SDL_Window *window,SDL_Surface *shape,SDL_WindowShapeMode *shape_mode)
237 {
238 int result;
239 if(window == NULL || !SDL_IsShapedWindow(window))
240 /* The window given was not a shapeable window. */
241 return SDL_NONSHAPEABLE_WINDOW;
242 if(shape == NULL)
243 /* Invalid shape argument. */
244 return SDL_INVALID_SHAPE_ARGUMENT;
245
246 if(shape_mode != NULL)
247 window->shaper->mode = *shape_mode;
248 result = SDL_GetVideoDevice()->shape_driver.SetWindowShape(window->shaper,shape,shape_mode);
249 window->shaper->hasshape = SDL_TRUE;
250 if(window->shaper->userx != 0 && window->shaper->usery != 0) {
251 SDL_SetWindowPosition(window,window->shaper->userx,window->shaper->usery);
252 window->shaper->userx = 0;
253 window->shaper->usery = 0;
254 }
255 return result;
256 }
257
258 static SDL_bool
259 SDL_WindowHasAShape(SDL_Window *window)
260 {
261 if (window == NULL || !SDL_IsShapedWindow(window))
262 return SDL_FALSE;
263 return window->shaper->hasshape;
264 }
265
266 int
267 SDL_GetShapedWindowMode(SDL_Window *window,SDL_WindowShapeMode *shape_mode)
268 {
269 if(window != NULL && SDL_IsShapedWindow(window)) {
270 if(shape_mode == NULL) {
271 if(SDL_WindowHasAShape(window))
272 /* The window given has a shape. */
273 return 0;
274 else
275 /* The window given is shapeable but lacks a shape. */
276 return SDL_WINDOW_LACKS_SHAPE;
277 }
278 else {
279 *shape_mode = window->shaper->mode;
280 return 0;
281 }
282 }
283 else
284 /* The window given is not a valid shapeable window. */
285 return SDL_NONSHAPEABLE_WINDOW;
286 }
SDL_FreeShapeTree
void SDL_FreeShapeTree(SDL_ShapeTree **shape_tree)
Definition: SDL_shape.c:223
SDL_Color
Definition: SDL_pixels.h:252
h
GLfloat GLfloat GLfloat GLfloat h
Definition: glew.h:7294
SDL_CalculateShapeTree
SDL_ShapeTree * SDL_CalculateShapeTree(SDL_WindowShapeMode mode, SDL_Surface *shape)
Definition: SDL_shape.c:196
SDL_Color::g
Uint8 g
Definition: SDL_pixels.h:255
g
GLboolean GLboolean g
Definition: glew.h:8736
NULL
#define NULL
Definition: ftobjs.h:61
SDL_Surface::h
int h
Definition: SDL_surface.h:73
SDL_PixelFormat::BytesPerPixel
Uint8 BytesPerPixel
Definition: SDL_pixels.h:277
SDL_bool
SDL_bool
Definition: SDL_stdinc.h:116
SDL_QuadTreeChildren::upright
struct SDL_ShapeTree * upright
Definition: SDL_shape_internals.h:39
SDL_WindowShaper::hasshape
SDL_bool hasshape
Definition: SDL_sysvideo.h:50
SDL_TRUE
Definition: SDL_stdinc.h:119
x
EGLSurface EGLint x
Definition: eglext.h:293
SDL_Surface
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
ShapeModeColorKey
A color key is applied.
Definition: SDL_shape.h:87
SDL_free
DECLSPEC void SDLCALL SDL_free(void *mem)
OpaqueShape
Definition: SDL_shape_internals.h:47
SDL_GetShapedWindowMode
DECLSPEC int SDLCALL SDL_GetShapedWindowMode(SDL_Window *window, SDL_WindowShapeMode *shape_mode)
Get the shape parameters of a shaped window.
Definition: SDL_shape.c:267
a
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:8736
SDL_Color::b
Uint8 b
Definition: SDL_pixels.h:256
SDL_ShapeTree::data
SDL_ShapeUnion data
Definition: SDL_shape_internals.h:51
SDL_WindowHasAShape
static SDL_bool SDL_WindowHasAShape(SDL_Window *window)
Definition: SDL_shape.c:259
SDL_Surface::w
int w
Definition: SDL_surface.h:73
SDL_FALSE
Definition: SDL_stdinc.h:118
SDL_config.h
Uint32
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:145
SDL_INVALID_SHAPE_ARGUMENT
#define SDL_INVALID_SHAPE_ARGUMENT
Definition: SDL_shape.h:43
SDL_UnlockSurface
DECLSPEC void SDLCALL SDL_UnlockSurface(SDL_Surface *surface)
Definition: SDL_surface.c:786
SDL_surface.h
SDL_shape.h
SDL_IsShapedWindow
DECLSPEC SDL_bool SDLCALL SDL_IsShapedWindow(const SDL_Window *window)
Return whether the given window is a shaped window.
Definition: SDL_shape.c:57
SDL_video.h
SDL_WindowShaper::mode
SDL_WindowShapeMode mode
Definition: SDL_sysvideo.h:47
result
GLuint64EXT * result
Definition: glew.h:12708
SDL_Color::r
Uint8 r
Definition: SDL_pixels.h:254
SDL_ShapeDriver::SetWindowShape
int(* SetWindowShape)(SDL_WindowShaper *shaper, SDL_Surface *shape, SDL_WindowShapeMode *shape_mode)
Definition: SDL_sysvideo.h:59
SDL_Surface::pixels
void * pixels
Definition: SDL_surface.h:75
SDL_Window::shaper
SDL_WindowShaper * shaper
Definition: SDL_sysvideo.h:95
QuadShape
Definition: SDL_shape_internals.h:47
SDL_NONSHAPEABLE_WINDOW
#define SDL_NONSHAPEABLE_WINDOW
Definition: SDL_shape.h:42
SDL_WINDOW_LACKS_SHAPE
#define SDL_WINDOW_LACKS_SHAPE
Definition: SDL_shape.h:44
ShapeModeDefault
The default mode, a binarized alpha cutoff of 1.
Definition: SDL_shape.h:81
SDL_malloc
DECLSPEC void *SDLCALL SDL_malloc(size_t size)
SDL_WindowShapeMode::parameters
SDL_WindowShapeParams parameters
Window-shape parameters.
Definition: SDL_shape.h:104
TransparentShape
Definition: SDL_shape_internals.h:47
SDL_Rect::x
int x
Definition: SDL_rect.h:65
SDL_TraversalFunction
void(* SDL_TraversalFunction)(SDL_ShapeTree *, void *)
Definition: SDL_shape_internals.h:54
alpha
GLclampf GLclampf GLclampf alpha
Definition: glew.h:1506
SDL_WindowShaper::userx
Uint32 userx
Definition: SDL_sysvideo.h:44
SDL_sysvideo.h
SDL_WindowShaper::usery
Uint32 usery
Definition: SDL_sysvideo.h:44
SDL_Rect::w
int w
Definition: SDL_rect.h:66
SDL_ShapeDriver::CreateShaper
SDL_WindowShaper *(* CreateShaper)(SDL_Window *window)
Definition: SDL_sysvideo.h:58
SDL_assert
#define SDL_assert(condition)
Definition: SDL_assert.h:159
SDL_pixels.h
y
EGLSurface EGLint EGLint y
Definition: eglext.h:293
ShapeModeBinarizeAlpha
A binarized alpha cutoff with a given integer value.
Definition: SDL_shape.h:83
SDL_WINDOW_RESIZABLE
Definition: SDL_video.h:103
SDL_Surface::format
SDL_PixelFormat * format
Definition: SDL_surface.h:72
SDL_CreateWindow
DECLSPEC SDL_Window *SDLCALL SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)
Create a window with the specified position, dimensions, and flags.
Definition: SDL_video.c:1190
SDL_WINDOW_BORDERLESS
Definition: SDL_video.h:102
SDL_ShapeUnion::shape
SDL_Rect shape
Definition: SDL_shape_internals.h:44
SDL_GetRGBA
DECLSPEC void SDLCALL SDL_GetRGBA(Uint32 pixel, const SDL_PixelFormat *format, Uint8 *r, Uint8 *g, Uint8 *b, Uint8 *a)
Get the RGBA components from a pixel of the specified format.
Definition: SDL_pixels.c:849
SDL_SetWindowPosition
DECLSPEC void SDLCALL SDL_SetWindowPosition(SDL_Window *window, int x, int y)
Set the position of a window.
Definition: SDL_video.c:1535
flags
GLenum GLsizei GLsizei GLsizei GLsizei GLbitfield flags
Definition: glew.h:2767
SDL_MUSTLOCK
#define SDL_MUSTLOCK(S)
Definition: SDL_surface.h:61
SDL_Rect::h
int h
Definition: SDL_rect.h:66
SDL_WindowShapeParams::colorKey
SDL_Color colorKey
Definition: SDL_shape.h:96
Uint8
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:129
ShapeModeReverseBinarizeAlpha
A binarized alpha cutoff with a given integer value, but with the opposite comparison.
Definition: SDL_shape.h:85
SDL_Window
Definition: SDL_sysvideo.h:71
mask
GLint GLint GLint GLint GLint GLint GLint GLbitfield mask
Definition: gl2ext.h:961
SDL_SetWindowShape
DECLSPEC int SDLCALL SDL_SetWindowShape(SDL_Window *window, SDL_Surface *shape, SDL_WindowShapeMode *shape_mode)
Set the shape and parameters of a shaped window.
Definition: SDL_shape.c:236
SDL_WINDOW_FULLSCREEN
Definition: SDL_video.h:98
SDL_TraverseShapeTree
void SDL_TraverseShapeTree(SDL_ShapeTree *tree, SDL_TraversalFunction function, void *closure)
Definition: SDL_shape.c:209
SDL_ShapeTree
Definition: SDL_shape_internals.h:49
SDL_QuadTreeChildren::upleft
struct SDL_ShapeTree * upleft
Definition: SDL_shape_internals.h:39
SDL_Surface::pitch
int pitch
Definition: SDL_surface.h:74
SDL_DestroyWindow
DECLSPEC void SDLCALL SDL_DestroyWindow(SDL_Window *window)
Destroy a window.
Definition: SDL_video.c:2154
r
GLdouble GLdouble GLdouble r
Definition: glew.h:1392
b
GLdouble GLdouble GLdouble b
Definition: glew.h:8383
SDL_GetVideoDevice
SDL_VideoDevice * SDL_GetVideoDevice(void)
Definition: SDL_video.c:548
SDL_VideoDevice::shape_driver
SDL_ShapeDriver shape_driver
Definition: SDL_sysvideo.h:212
SDL_WindowShapeParams::binarizationCutoff
Uint8 binarizationCutoff
a cutoff alpha value for binarization of the window shape&#39;s alpha channel.
Definition: SDL_shape.h:95
w
GLint GLint GLint GLint GLint w
Definition: gl2ext.h:1215
Uint16
uint16_t Uint16
An unsigned 16-bit integer type.
Definition: SDL_stdinc.h:137
SDL_shape_internals.h
SDL_WindowShapeMode
A struct that tags the SDL_WindowShapeParams union with an enum describing the type of its contents...
Definition: SDL_shape.h:100
bitmap
GLsizei GLfixed GLfixed GLfixed GLfixed const GLubyte * bitmap
Definition: glext.h:4510
SDL_PixelFormat::Amask
Uint32 Amask
Definition: SDL_pixels.h:282
RecursivelyCalculateShapeTree
static SDL_ShapeTree * RecursivelyCalculateShapeTree(SDL_WindowShapeMode mode, SDL_Surface *mask, SDL_Rect dimensions)
Definition: SDL_shape.c:121
SDL_ShapeTree::kind
SDL_ShapeKind kind
Definition: SDL_shape_internals.h:50
SDL_WindowShapeMode::mode
WindowShapeMode mode
The mode of these window-shape parameters.
Definition: SDL_shape.h:102
SDL_QuadTreeChildren::downright
struct SDL_ShapeTree * downright
Definition: SDL_shape_internals.h:39
SDL_CreateShapedWindow
DECLSPEC SDL_Window *SDLCALL 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.
Definition: SDL_shape.c:33
SDL_LockSurface
DECLSPEC int SDLCALL SDL_LockSurface(SDL_Surface *surface)
Sets up a surface for directly accessing the pixels.
Definition: SDL_surface.c:765
SDL_Rect::y
int y
Definition: SDL_rect.h:65
mode
GLenum mode
Definition: glew.h:2394
SDL_ShapeUnion::children
SDL_QuadTreeChildren children
Definition: SDL_shape_internals.h:43
SDL_Rect
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:63
SDL_QuadTreeChildren::downleft
struct SDL_ShapeTree * downleft
Definition: SDL_shape_internals.h:39
SDL_CalculateShapeBitmap
void SDL_CalculateShapeBitmap(SDL_WindowShapeMode mode, SDL_Surface *shape, Uint8 *bitmap, Uint8 ppb)
Definition: SDL_shape.c:67
SDL_assert.h