comparison src/video/SDL_shape.c @ 4838:1f9915666afd

Reformatting code to match the rest of SDL. Variable names seem OK; replaced tabs with 4 spaces each for indentation.
author Eli Gottlieb <eligottlieb@gmail.com>
date Mon, 09 Aug 2010 15:37:20 -0400
parents 55f32099a4b5
children 05d172e92b52
comparison
equal deleted inserted replaced
4826:d532a5a114cd 4838:1f9915666afd
27 #include "SDL_pixels.h" 27 #include "SDL_pixels.h"
28 #include "SDL_surface.h" 28 #include "SDL_surface.h"
29 #include "SDL_shape.h" 29 #include "SDL_shape.h"
30 #include "../src/video/SDL_shape_internals.h" 30 #include "../src/video/SDL_shape_internals.h"
31 31
32 SDL_Window* SDL_CreateShapedWindow(const char *title,unsigned int x,unsigned int y,unsigned int w,unsigned int h,Uint32 flags) { 32 SDL_Window*
33 SDL_Window *result = SDL_CreateWindow(title,x,y,w,h,SDL_WINDOW_BORDERLESS | flags & !SDL_WINDOW_FULLSCREEN & !SDL_WINDOW_SHOWN); 33 SDL_CreateShapedWindow(const char *title,unsigned int x,unsigned int y,unsigned int w,unsigned int h,Uint32 flags) {
34 if(result != NULL) { 34 SDL_Window *result = SDL_CreateWindow(title,x,y,w,h,SDL_WINDOW_BORDERLESS | flags & !SDL_WINDOW_FULLSCREEN & !SDL_WINDOW_SHOWN);
35 result->shaper = result->display->device->shape_driver.CreateShaper(result); 35 if(result != NULL) {
36 if(result->shaper != NULL) { 36 result->shaper = result->display->device->shape_driver.CreateShaper(result);
37 result->shaper->usershownflag = flags & SDL_WINDOW_SHOWN; 37 if(result->shaper != NULL) {
38 result->shaper->mode.mode = ShapeModeDefault; 38 result->shaper->usershownflag = flags & SDL_WINDOW_SHOWN;
39 result->shaper->mode.parameters.binarizationCutoff = 1; 39 result->shaper->mode.mode = ShapeModeDefault;
40 result->shaper->hasshape = SDL_FALSE; 40 result->shaper->mode.parameters.binarizationCutoff = 1;
41 return result; 41 result->shaper->hasshape = SDL_FALSE;
42 } 42 return result;
43 else { 43 }
44 SDL_DestroyWindow(result); 44 else {
45 return NULL; 45 SDL_DestroyWindow(result);
46 } 46 return NULL;
47 } 47 }
48 else 48 }
49 return NULL; 49 else
50 } 50 return NULL;
51 51 }
52 SDL_bool SDL_IsShapedWindow(const SDL_Window *window) { 52
53 if(window == NULL) 53 SDL_bool
54 return SDL_FALSE; 54 SDL_IsShapedWindow(const SDL_Window *window) {
55 else 55 if(window == NULL)
56 return (SDL_bool)(window->shaper != NULL); 56 return SDL_FALSE;
57 else
58 return (SDL_bool)(window->shaper != NULL);
57 } 59 }
58 60
59 /* REQUIRES that bitmap point to a w-by-h bitmap with ppb pixels-per-byte. */ 61 /* REQUIRES that bitmap point to a w-by-h bitmap with ppb pixels-per-byte. */
60 void SDL_CalculateShapeBitmap(SDL_WindowShapeMode mode,SDL_Surface *shape,Uint8* bitmap,Uint8 ppb) { 62 void
61 int x = 0; 63 SDL_CalculateShapeBitmap(SDL_WindowShapeMode mode,SDL_Surface *shape,Uint8* bitmap,Uint8 ppb) {
62 int y = 0; 64 int x = 0;
63 Uint8 r = 0,g = 0,b = 0,alpha = 0; 65 int y = 0;
64 Uint8* pixel = NULL; 66 Uint8 r = 0,g = 0,b = 0,alpha = 0;
65 Uint32 bitmap_pixel,pixel_value = 0,mask_value = 0; 67 Uint8* pixel = NULL;
66 SDL_Color key; 68 Uint32 bitmap_pixel,pixel_value = 0,mask_value = 0;
67 if(SDL_MUSTLOCK(shape)) 69 SDL_Color key;
68 SDL_LockSurface(shape); 70 if(SDL_MUSTLOCK(shape))
69 pixel = (Uint8*)shape->pixels; 71 SDL_LockSurface(shape);
70 for(y = 0;y<shape->h;y++) { 72 pixel = (Uint8*)shape->pixels;
71 for(x=0;x<shape->w;x++) { 73 for(y = 0;y<shape->h;y++) {
72 alpha = 0; 74 for(x=0;x<shape->w;x++) {
73 pixel_value = 0; 75 alpha = 0;
74 pixel = (Uint8 *)(shape->pixels) + (y*shape->pitch) + (x*shape->format->BytesPerPixel); 76 pixel_value = 0;
75 switch(shape->format->BytesPerPixel) { 77 pixel = (Uint8 *)(shape->pixels) + (y*shape->pitch) + (x*shape->format->BytesPerPixel);
76 case(1): 78 switch(shape->format->BytesPerPixel) {
77 pixel_value = *(Uint8*)pixel; 79 case(1):
78 break; 80 pixel_value = *(Uint8*)pixel;
79 case(2): 81 break;
80 pixel_value = *(Uint16*)pixel; 82 case(2):
81 break; 83 pixel_value = *(Uint16*)pixel;
82 case(3): 84 break;
83 pixel_value = *(Uint32*)pixel & (~shape->format->Amask); 85 case(3):
84 break; 86 pixel_value = *(Uint32*)pixel & (~shape->format->Amask);
85 case(4): 87 break;
86 pixel_value = *(Uint32*)pixel; 88 case(4):
87 break; 89 pixel_value = *(Uint32*)pixel;
88 } 90 break;
89 SDL_GetRGBA(pixel_value,shape->format,&r,&g,&b,&alpha); 91 }
90 bitmap_pixel = y*shape->w + x; 92 SDL_GetRGBA(pixel_value,shape->format,&r,&g,&b,&alpha);
91 switch(mode.mode) { 93 bitmap_pixel = y*shape->w + x;
92 case(ShapeModeDefault): 94 switch(mode.mode) {
93 mask_value = (alpha >= 1 ? 1 : 0); 95 case(ShapeModeDefault):
94 break; 96 mask_value = (alpha >= 1 ? 1 : 0);
95 case(ShapeModeBinarizeAlpha): 97 break;
96 mask_value = (alpha >= mode.parameters.binarizationCutoff ? 1 : 0); 98 case(ShapeModeBinarizeAlpha):
97 break; 99 mask_value = (alpha >= mode.parameters.binarizationCutoff ? 1 : 0);
98 case(ShapeModeReverseBinarizeAlpha): 100 break;
99 mask_value = (alpha <= mode.parameters.binarizationCutoff ? 1 : 0); 101 case(ShapeModeReverseBinarizeAlpha):
100 break; 102 mask_value = (alpha <= mode.parameters.binarizationCutoff ? 1 : 0);
101 case(ShapeModeColorKey): 103 break;
102 key = mode.parameters.colorKey; 104 case(ShapeModeColorKey):
103 mask_value = ((key.r != r && key.g != g && key.b != b) ? 1 : 0); 105 key = mode.parameters.colorKey;
104 break; 106 mask_value = ((key.r != r && key.g != g && key.b != b) ? 1 : 0);
105 } 107 break;
106 bitmap[bitmap_pixel / ppb] |= mask_value << (7 - ((ppb - 1) - (bitmap_pixel % ppb))); 108 }
107 } 109 bitmap[bitmap_pixel / ppb] |= mask_value << (7 - ((ppb - 1) - (bitmap_pixel % ppb)));
108 } 110 }
109 if(SDL_MUSTLOCK(shape)) 111 }
110 SDL_UnlockSurface(shape); 112 if(SDL_MUSTLOCK(shape))
111 } 113 SDL_UnlockSurface(shape);
112 114 }
113 SDL_ShapeTree* RecursivelyCalculateShapeTree(SDL_WindowShapeMode mode,SDL_Surface* mask,SDL_bool invert,SDL_Rect dimensions) { 115
114 int x = 0,y = 0; 116 SDL_ShapeTree*
115 Uint8* pixel = NULL; 117 RecursivelyCalculateShapeTree(SDL_WindowShapeMode mode,SDL_Surface* mask,SDL_bool invert,SDL_Rect dimensions) {
116 Uint32 pixel_value = 0; 118 int x = 0,y = 0;
117 Uint8 r = 0,g = 0,b = 0,a = 0; 119 Uint8* pixel = NULL;
118 SDL_bool pixel_transparent = SDL_FALSE; 120 Uint32 pixel_value = 0;
119 int last_transparent = -1; 121 Uint8 r = 0,g = 0,b = 0,a = 0;
120 SDL_Color key; 122 SDL_bool pixel_transparent = SDL_FALSE;
121 SDL_ShapeTree* result = (SDL_ShapeTree*)SDL_malloc(sizeof(SDL_ShapeTree)); 123 int last_transparent = -1;
122 SDL_Rect next = {0,0,0,0}; 124 SDL_Color key;
123 for(y=dimensions.y;y<dimensions.h;y++) 125 SDL_ShapeTree* result = (SDL_ShapeTree*)SDL_malloc(sizeof(SDL_ShapeTree));
124 for(x=dimensions.x;x<dimensions.w;x++) { 126 SDL_Rect next = {0,0,0,0};
125 pixel_value = 0; 127 for(y=dimensions.y;y<dimensions.h;y++)
126 pixel = (Uint8 *)(mask->pixels) + (y*mask->pitch) + (x*mask->format->BytesPerPixel); 128 for(x=dimensions.x;x<dimensions.w;x++) {
127 switch(mask->format->BytesPerPixel) { 129 pixel_value = 0;
128 case(1): 130 pixel = (Uint8 *)(mask->pixels) + (y*mask->pitch) + (x*mask->format->BytesPerPixel);
129 pixel_value = *(Uint8*)pixel; 131 switch(mask->format->BytesPerPixel) {
130 break; 132 case(1):
131 case(2): 133 pixel_value = *(Uint8*)pixel;
132 pixel_value = *(Uint16*)pixel; 134 break;
133 break; 135 case(2):
134 case(3): 136 pixel_value = *(Uint16*)pixel;
135 pixel_value = *(Uint32*)pixel & (~mask->format->Amask); 137 break;
136 break; 138 case(3):
137 case(4): 139 pixel_value = *(Uint32*)pixel & (~mask->format->Amask);
138 pixel_value = *(Uint32*)pixel; 140 break;
139 break; 141 case(4):
140 } 142 pixel_value = *(Uint32*)pixel;
141 SDL_GetRGBA(pixel_value,mask->format,&r,&g,&b,&a); 143 break;
142 switch(mode.mode) { 144 }
143 case(ShapeModeDefault): 145 SDL_GetRGBA(pixel_value,mask->format,&r,&g,&b,&a);
144 pixel_transparent = (SDL_bool)(a >= 1 ? !invert : invert); 146 switch(mode.mode) {
145 break; 147 case(ShapeModeDefault):
146 case(ShapeModeBinarizeAlpha): 148 pixel_transparent = (SDL_bool)(a >= 1 ? !invert : invert);
147 pixel_transparent = (SDL_bool)(a >= mode.parameters.binarizationCutoff ? !invert : invert); 149 break;
148 break; 150 case(ShapeModeBinarizeAlpha):
149 case(ShapeModeReverseBinarizeAlpha): 151 pixel_transparent = (SDL_bool)(a >= mode.parameters.binarizationCutoff ? !invert : invert);
150 pixel_transparent = (SDL_bool)(a <= mode.parameters.binarizationCutoff ? !invert : invert); 152 break;
151 break; 153 case(ShapeModeReverseBinarizeAlpha):
152 case(ShapeModeColorKey): 154 pixel_transparent = (SDL_bool)(a <= mode.parameters.binarizationCutoff ? !invert : invert);
153 key = mode.parameters.colorKey; 155 break;
154 pixel_transparent = (SDL_bool)((key.r == r && key.g == g && key.b == b) ? !invert : invert); 156 case(ShapeModeColorKey):
155 break; 157 key = mode.parameters.colorKey;
156 } 158 pixel_transparent = (SDL_bool)((key.r == r && key.g == g && key.b == b) ? !invert : invert);
157 if(last_transparent == -1) { 159 break;
158 last_transparent = pixel_transparent; 160 }
159 break; 161 if(last_transparent == -1) {
160 } 162 last_transparent = pixel_transparent;
161 if(last_transparent != pixel_transparent) { 163 break;
162 result->kind = QuadShape; 164 }
163 //These will stay the same. 165 if(last_transparent != pixel_transparent) {
164 next.w = dimensions.w / 2; 166 result->kind = QuadShape;
165 next.h = dimensions.h / 2; 167 //These will stay the same.
166 //These will change from recursion to recursion. 168 next.w = dimensions.w / 2;
167 next.x = dimensions.x; 169 next.h = dimensions.h / 2;
168 next.y = dimensions.y; 170 //These will change from recursion to recursion.
169 result->data.children.upleft = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,invert,next); 171 next.x = dimensions.x;
170 next.x = dimensions.w / 2 + 1; 172 next.y = dimensions.y;
171 //Unneeded: next.y = dimensions.y; 173 result->data.children.upleft = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,invert,next);
172 result->data.children.upright = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,invert,next); 174 next.x = dimensions.w / 2 + 1;
173 next.x = dimensions.x; 175 //Unneeded: next.y = dimensions.y;
174 next.y = dimensions.h / 2 + 1; 176 result->data.children.upright = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,invert,next);
175 result->data.children.downleft = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,invert,next); 177 next.x = dimensions.x;
176 next.x = dimensions.w / 2 + 1; 178 next.y = dimensions.h / 2 + 1;
177 //Unneeded: next.y = dimensions.h / 2 + 1; 179 result->data.children.downleft = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,invert,next);
178 result->data.children.downright = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,invert,next); 180 next.x = dimensions.w / 2 + 1;
179 return result; 181 //Unneeded: next.y = dimensions.h / 2 + 1;
180 } 182 result->data.children.downright = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,invert,next);
181 } 183 return result;
182 //If we never recursed, all the pixels in this quadrant have the same "value". 184 }
183 result->kind = (last_transparent == SDL_FALSE ? OpaqueShape : TransparentShape); 185 }
184 result->data.shape = dimensions; 186 //If we never recursed, all the pixels in this quadrant have the same "value".
185 return result; 187 result->kind = (last_transparent == SDL_FALSE ? OpaqueShape : TransparentShape);
186 } 188 result->data.shape = dimensions;
187 189 return result;
188 SDL_ShapeTree* SDL_CalculateShapeTree(SDL_WindowShapeMode mode,SDL_Surface* shape,SDL_bool invert) { 190 }
189 SDL_Rect dimensions = {0,0,shape->w,shape->h}; 191
190 SDL_ShapeTree* result = NULL; 192 SDL_ShapeTree*
191 if(SDL_MUSTLOCK(shape)) 193 SDL_CalculateShapeTree(SDL_WindowShapeMode mode,SDL_Surface* shape,SDL_bool invert) {
192 SDL_LockSurface(shape); 194 SDL_Rect dimensions = {0,0,shape->w,shape->h};
193 result = RecursivelyCalculateShapeTree(mode,shape,invert,dimensions); 195 SDL_ShapeTree* result = NULL;
194 if(SDL_MUSTLOCK(shape)) 196 if(SDL_MUSTLOCK(shape))
195 SDL_UnlockSurface(shape); 197 SDL_LockSurface(shape);
196 return result; 198 result = RecursivelyCalculateShapeTree(mode,shape,invert,dimensions);
197 } 199 if(SDL_MUSTLOCK(shape))
198 200 SDL_UnlockSurface(shape);
199 void SDL_TraverseShapeTree(SDL_ShapeTree *tree,void(*function)(SDL_ShapeTree*,void*),void* closure) { 201 return result;
200 if(tree->kind == QuadShape) { 202 }
201 SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.upleft,function,closure); 203
202 SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.upright,function,closure); 204 void
203 SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.downleft,function,closure); 205 SDL_TraverseShapeTree(SDL_ShapeTree *tree,void(*function)(SDL_ShapeTree*,void*),void* closure) {
204 SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.downright,function,closure); 206 if(tree->kind == QuadShape) {
205 } 207 SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.upleft,function,closure);
206 else 208 SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.upright,function,closure);
207 function(tree,closure); 209 SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.downleft,function,closure);
208 } 210 SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.downright,function,closure);
209 211 }
210 void SDL_FreeShapeTree(SDL_ShapeTree** shapeTree) { 212 else
211 if((*shapeTree)->kind == QuadShape) { 213 function(tree,closure);
212 SDL_FreeShapeTree((SDL_ShapeTree **)&(*shapeTree)->data.children.upleft); 214 }
213 SDL_FreeShapeTree((SDL_ShapeTree **)&(*shapeTree)->data.children.upright); 215
214 SDL_FreeShapeTree((SDL_ShapeTree **)&(*shapeTree)->data.children.downleft); 216 void
215 SDL_FreeShapeTree((SDL_ShapeTree **)&(*shapeTree)->data.children.downright); 217 SDL_FreeShapeTree(SDL_ShapeTree** shapeTree) {
216 } 218 if((*shapeTree)->kind == QuadShape) {
217 SDL_free(*shapeTree); 219 SDL_FreeShapeTree((SDL_ShapeTree **)&(*shapeTree)->data.children.upleft);
218 *shapeTree = NULL; 220 SDL_FreeShapeTree((SDL_ShapeTree **)&(*shapeTree)->data.children.upright);
219 } 221 SDL_FreeShapeTree((SDL_ShapeTree **)&(*shapeTree)->data.children.downleft);
220 222 SDL_FreeShapeTree((SDL_ShapeTree **)&(*shapeTree)->data.children.downright);
221 int SDL_SetWindowShape(SDL_Window *window,SDL_Surface *shape,SDL_WindowShapeMode *shapeMode) { 223 }
222 int result; 224 SDL_free(*shapeTree);
223 if(window == NULL || !SDL_IsShapedWindow(window)) 225 *shapeTree = NULL;
224 //The window given was not a shapeable window. 226 }
225 return SDL_NONSHAPEABLE_WINDOW; 227
226 if(shape == NULL) 228 int
227 //Invalid shape argument. 229 SDL_SetWindowShape(SDL_Window *window,SDL_Surface *shape,SDL_WindowShapeMode *shapeMode) {
228 return SDL_INVALID_SHAPE_ARGUMENT; 230 int result;
229 231 if(window == NULL || !SDL_IsShapedWindow(window))
230 if(shapeMode != NULL) 232 //The window given was not a shapeable window.
231 window->shaper->mode = *shapeMode; 233 return SDL_NONSHAPEABLE_WINDOW;
232 //TODO: Platform-specific implementations of SetWindowShape. X11 is finished. Win32 is finished. Debugging is in progress on both. 234 if(shape == NULL)
233 result = window->display->device->shape_driver.SetWindowShape(window->shaper,shape,shapeMode); 235 //Invalid shape argument.
234 window->shaper->hasshape = SDL_TRUE; 236 return SDL_INVALID_SHAPE_ARGUMENT;
235 if((window->shaper->usershownflag & SDL_WINDOW_SHOWN) == SDL_WINDOW_SHOWN) { 237
236 SDL_ShowWindow(window); 238 if(shapeMode != NULL)
237 window->shaper->usershownflag &= !SDL_WINDOW_SHOWN; 239 window->shaper->mode = *shapeMode;
238 } 240 //TODO: Platform-specific implementations of SetWindowShape. X11 is finished. Win32 is finished. Debugging is in progress on both.
239 return result; 241 result = window->display->device->shape_driver.SetWindowShape(window->shaper,shape,shapeMode);
240 } 242 window->shaper->hasshape = SDL_TRUE;
241 243 if((window->shaper->usershownflag & SDL_WINDOW_SHOWN) == SDL_WINDOW_SHOWN) {
242 SDL_bool SDL_WindowHasAShape(SDL_Window *window) { 244 SDL_ShowWindow(window);
243 if (window == NULL && !SDL_IsShapedWindow(window)) 245 window->shaper->usershownflag &= !SDL_WINDOW_SHOWN;
244 return SDL_FALSE; 246 }
245 return window->shaper->hasshape; 247 return result;
246 } 248 }
247 249
248 int SDL_GetShapedWindowMode(SDL_Window *window,SDL_WindowShapeMode *shapeMode) { 250 SDL_bool
249 if(window != NULL && SDL_IsShapedWindow(window)) { 251 SDL_WindowHasAShape(SDL_Window *window) {
250 if(shapeMode == NULL) { 252 if (window == NULL && !SDL_IsShapedWindow(window))
251 if(SDL_WindowHasAShape(window)) 253 return SDL_FALSE;
252 //The window given has a shape. 254 return window->shaper->hasshape;
253 return 0; 255 }
254 else 256
255 //The window given is shapeable but lacks a shape. 257 int
256 return SDL_WINDOW_LACKS_SHAPE; 258 SDL_GetShapedWindowMode(SDL_Window *window,SDL_WindowShapeMode *shapeMode) {
257 } 259 if(window != NULL && SDL_IsShapedWindow(window)) {
258 else { 260 if(shapeMode == NULL) {
259 *shapeMode = window->shaper->mode; 261 if(SDL_WindowHasAShape(window))
260 return 0; 262 //The window given has a shape.
261 } 263 return 0;
262 } 264 else
263 else 265 //The window given is shapeable but lacks a shape.
264 //The window given is not a valid shapeable window. 266 return SDL_WINDOW_LACKS_SHAPE;
265 return SDL_NONSHAPEABLE_WINDOW; 267 }
266 } 268 else {
269 *shapeMode = window->shaper->mode;
270 return 0;
271 }
272 }
273 else
274 //The window given is not a valid shapeable window.
275 return SDL_NONSHAPEABLE_WINDOW;
276 }