Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
268 lines (252 loc) · 9.12 KB

SDL_shape.c

File metadata and controls

268 lines (252 loc) · 9.12 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
SDL - Simple DirectMedia Layer
Copyright (C) 2010 Eli Gottlieb
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Eli Gottlieb
eligottlieb@gmail.com
*/
May 27, 2010
May 27, 2010
22
#include "SDL_config.h"
Jun 5, 2010
Jun 5, 2010
23
May 27, 2010
May 27, 2010
24
#include "SDL.h"
Aug 5, 2010
Aug 5, 2010
25
#include "SDL_assert.h"
May 27, 2010
May 27, 2010
26
27
#include "SDL_video.h"
#include "SDL_sysvideo.h"
Jun 30, 2010
Jun 30, 2010
28
29
#include "SDL_pixels.h"
#include "SDL_surface.h"
30
#include "SDL_shape.h"
Aug 3, 2010
Aug 3, 2010
31
#include "SDL_shape_internals.h"
May 21, 2010
May 21, 2010
32
May 27, 2010
May 27, 2010
33
SDL_Window* SDL_CreateShapedWindow(const char *title,unsigned int x,unsigned int y,unsigned int w,unsigned int h,Uint32 flags) {
Jul 27, 2010
Jul 27, 2010
34
SDL_Window *result = SDL_CreateWindow(title,x,y,w,h,SDL_WINDOW_BORDERLESS | flags & !SDL_WINDOW_FULLSCREEN & !SDL_WINDOW_SHOWN);
Jul 15, 2010
Jul 15, 2010
35
36
37
38
if(result != NULL) {
result->shaper = result->display->device->shape_driver.CreateShaper(result);
if(result->shaper != NULL) {
result->shaper->usershownflag = flags & SDL_WINDOW_SHOWN;
Jul 23, 2010
Jul 23, 2010
39
40
result->shaper->mode.mode = ShapeModeDefault;
result->shaper->mode.parameters.binarizationCutoff = 1;
Jul 15, 2010
Jul 15, 2010
41
42
43
44
45
46
47
48
49
50
result->shaper->hasshape = SDL_FALSE;
return result;
}
else {
SDL_DestroyWindow(result);
return NULL;
}
}
else
return NULL;
May 21, 2010
May 21, 2010
51
52
}
Jun 22, 2010
Jun 22, 2010
53
SDL_bool SDL_IsShapedWindow(const SDL_Window *window) {
Jun 30, 2010
Jun 30, 2010
54
55
56
if(window == NULL)
return SDL_FALSE;
else
Jul 11, 2010
Jul 11, 2010
57
return (SDL_bool)(window->shaper != NULL);
Jun 30, 2010
Jun 30, 2010
58
59
}
Jul 29, 2010
Jul 29, 2010
60
/* REQUIRES that bitmap point to a w-by-h bitmap with ppb pixels-per-byte. */
Aug 2, 2010
Aug 2, 2010
61
void SDL_CalculateShapeBitmap(SDL_WindowShapeMode mode,SDL_Surface *shape,Uint8* bitmap,Uint8 ppb) {
Jul 9, 2010
Jul 9, 2010
62
63
int x = 0;
int y = 0;
Jul 14, 2010
Jul 14, 2010
64
Uint8 r = 0,g = 0,b = 0,alpha = 0;
Jul 19, 2010
Jul 19, 2010
65
Uint8* pixel = NULL;
Aug 2, 2010
Aug 2, 2010
66
Uint32 bitmap_pixel,pixel_value = 0,mask_value = 0;
Jul 23, 2010
Jul 23, 2010
67
SDL_Color key;
Jun 30, 2010
Jun 30, 2010
68
69
if(SDL_MUSTLOCK(shape))
SDL_LockSurface(shape);
Jul 19, 2010
Jul 19, 2010
70
71
72
pixel = (Uint8*)shape->pixels;
for(y = 0;y<shape->h;y++) {
for(x=0;x<shape->w;x++) {
Jul 9, 2010
Jul 9, 2010
73
alpha = 0;
Jul 19, 2010
Jul 19, 2010
74
pixel_value = 0;
Jul 20, 2010
Jul 20, 2010
75
pixel = (Uint8 *)(shape->pixels) + (y*shape->pitch) + (x*shape->format->BytesPerPixel);
Jul 19, 2010
Jul 19, 2010
76
77
78
79
80
81
82
switch(shape->format->BytesPerPixel) {
case(1):
pixel_value = *(Uint8*)pixel;
break;
case(2):
pixel_value = *(Uint16*)pixel;
break;
Aug 2, 2010
Aug 2, 2010
83
84
85
case(3):
pixel_value = *(Uint32*)pixel & (~shape->format->Amask);
break;
Jul 19, 2010
Jul 19, 2010
86
87
88
89
90
case(4):
pixel_value = *(Uint32*)pixel;
break;
}
SDL_GetRGBA(pixel_value,shape->format,&r,&g,&b,&alpha);
Jul 15, 2010
Jul 15, 2010
91
bitmap_pixel = y*shape->w + x;
Jul 23, 2010
Jul 23, 2010
92
93
switch(mode.mode) {
case(ShapeModeDefault):
Aug 2, 2010
Aug 2, 2010
94
mask_value = (alpha >= 1 ? 1 : 0);
Jul 23, 2010
Jul 23, 2010
95
96
break;
case(ShapeModeBinarizeAlpha):
Aug 2, 2010
Aug 2, 2010
97
mask_value = (alpha >= mode.parameters.binarizationCutoff ? 1 : 0);
Jul 23, 2010
Jul 23, 2010
98
99
break;
case(ShapeModeReverseBinarizeAlpha):
Aug 2, 2010
Aug 2, 2010
100
mask_value = (alpha <= mode.parameters.binarizationCutoff ? 1 : 0);
Jul 23, 2010
Jul 23, 2010
101
102
103
break;
case(ShapeModeColorKey):
key = mode.parameters.colorKey;
Aug 2, 2010
Aug 2, 2010
104
mask_value = ((key.r != r && key.g != g && key.b != b) ? 1 : 0);
Jul 23, 2010
Jul 23, 2010
105
106
break;
}
Aug 2, 2010
Aug 2, 2010
107
bitmap[bitmap_pixel / ppb] |= mask_value << (7 - ((ppb - 1) - (bitmap_pixel % ppb)));
Jun 30, 2010
Jun 30, 2010
108
}
Jul 19, 2010
Jul 19, 2010
109
}
Jun 30, 2010
Jun 30, 2010
110
111
if(SDL_MUSTLOCK(shape))
SDL_UnlockSurface(shape);
May 21, 2010
May 21, 2010
112
113
}
Jul 29, 2010
Jul 29, 2010
114
115
116
117
118
119
120
121
SDL_ShapeTree* RecursivelyCalculateShapeTree(SDL_WindowShapeMode mode,SDL_Surface* mask,SDL_bool invert,SDL_Rect dimensions) {
int x = 0,y = 0;
Uint8* pixel = NULL;
Uint32 pixel_value = 0;
Uint8 r = 0,g = 0,b = 0,a = 0;
SDL_bool pixel_transparent = SDL_FALSE;
int last_transparent = -1;
SDL_Color key;
Jul 30, 2010
Jul 30, 2010
122
SDL_ShapeTree* result = (SDL_ShapeTree*)SDL_malloc(sizeof(SDL_ShapeTree));
Jul 29, 2010
Jul 29, 2010
123
124
125
126
127
128
129
130
131
132
133
134
SDL_Rect next = {0,0,0,0};
for(y=dimensions.y;y<dimensions.h;y++)
for(x=dimensions.x;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;
Aug 2, 2010
Aug 2, 2010
135
136
137
case(3):
pixel_value = *(Uint32*)pixel & (~mask->format->Amask);
break;
Jul 29, 2010
Jul 29, 2010
138
139
140
141
142
143
144
case(4):
pixel_value = *(Uint32*)pixel;
break;
}
SDL_GetRGBA(pixel_value,mask->format,&r,&g,&b,&a);
switch(mode.mode) {
case(ShapeModeDefault):
Jul 30, 2010
Jul 30, 2010
145
pixel_transparent = (SDL_bool)(a >= 1 ? !invert : invert);
Jul 29, 2010
Jul 29, 2010
146
147
break;
case(ShapeModeBinarizeAlpha):
Jul 30, 2010
Jul 30, 2010
148
pixel_transparent = (SDL_bool)(a >= mode.parameters.binarizationCutoff ? !invert : invert);
Jul 29, 2010
Jul 29, 2010
149
150
break;
case(ShapeModeReverseBinarizeAlpha):
Jul 30, 2010
Jul 30, 2010
151
pixel_transparent = (SDL_bool)(a <= mode.parameters.binarizationCutoff ? !invert : invert);
Jul 29, 2010
Jul 29, 2010
152
153
154
break;
case(ShapeModeColorKey):
key = mode.parameters.colorKey;
Jul 30, 2010
Jul 30, 2010
155
pixel_transparent = (SDL_bool)((key.r == r && key.g == g && key.b == b) ? !invert : invert);
Jul 29, 2010
Jul 29, 2010
156
157
158
159
160
161
162
163
164
165
166
167
168
169
break;
}
if(last_transparent == -1) {
last_transparent = pixel_transparent;
break;
}
if(last_transparent != pixel_transparent) {
result->kind = QuadShape;
//These will stay the same.
next.w = dimensions.w / 2;
next.h = dimensions.h / 2;
//These will change from recursion to recursion.
next.x = dimensions.x;
next.y = dimensions.y;
Jul 30, 2010
Jul 30, 2010
170
result->data.children.upleft = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,invert,next);
Jul 29, 2010
Jul 29, 2010
171
172
next.x = dimensions.w / 2 + 1;
//Unneeded: next.y = dimensions.y;
Jul 30, 2010
Jul 30, 2010
173
result->data.children.upright = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,invert,next);
Jul 29, 2010
Jul 29, 2010
174
175
next.x = dimensions.x;
next.y = dimensions.h / 2 + 1;
Jul 30, 2010
Jul 30, 2010
176
result->data.children.downleft = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,invert,next);
Jul 29, 2010
Jul 29, 2010
177
178
next.x = dimensions.w / 2 + 1;
//Unneeded: next.y = dimensions.h / 2 + 1;
Jul 30, 2010
Jul 30, 2010
179
result->data.children.downright = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,invert,next);
Jul 29, 2010
Jul 29, 2010
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
return result;
}
}
//If we never recursed, all the pixels in this quadrant have the same "value".
result->kind = (last_transparent == SDL_FALSE ? OpaqueShape : TransparentShape);
result->data.shape = dimensions;
return result;
}
SDL_ShapeTree* SDL_CalculateShapeTree(SDL_WindowShapeMode mode,SDL_Surface* shape,SDL_bool invert) {
SDL_Rect dimensions = {0,0,shape->w,shape->h};
SDL_ShapeTree* result = NULL;
if(SDL_MUSTLOCK(shape))
SDL_LockSurface(shape);
result = RecursivelyCalculateShapeTree(mode,shape,invert,dimensions);
if(SDL_MUSTLOCK(shape))
SDL_UnlockSurface(shape);
return result;
}
Aug 4, 2010
Aug 4, 2010
200
void SDL_TraverseShapeTree(SDL_ShapeTree *tree,SDL_TraversalFunction function,void* closure) {
Aug 5, 2010
Aug 5, 2010
201
SDL_assert(tree != NULL);
Jul 29, 2010
Jul 29, 2010
202
if(tree->kind == QuadShape) {
Jul 30, 2010
Jul 30, 2010
203
204
205
206
SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.upleft,function,closure);
SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.upright,function,closure);
SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.downleft,function,closure);
SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.downright,function,closure);
Jul 29, 2010
Jul 29, 2010
207
208
209
210
211
212
213
}
else
function(tree,closure);
}
void SDL_FreeShapeTree(SDL_ShapeTree** shapeTree) {
if((*shapeTree)->kind == QuadShape) {
Jul 30, 2010
Jul 30, 2010
214
215
216
217
SDL_FreeShapeTree((SDL_ShapeTree **)&(*shapeTree)->data.children.upleft);
SDL_FreeShapeTree((SDL_ShapeTree **)&(*shapeTree)->data.children.upright);
SDL_FreeShapeTree((SDL_ShapeTree **)&(*shapeTree)->data.children.downleft);
SDL_FreeShapeTree((SDL_ShapeTree **)&(*shapeTree)->data.children.downright);
Jul 29, 2010
Jul 29, 2010
218
}
Jul 30, 2010
Jul 30, 2010
219
SDL_free(*shapeTree);
Jul 29, 2010
Jul 29, 2010
220
221
222
*shapeTree = NULL;
}
Jun 22, 2010
Jun 22, 2010
223
int SDL_SetWindowShape(SDL_Window *window,SDL_Surface *shape,SDL_WindowShapeMode *shapeMode) {
Jul 9, 2010
Jul 9, 2010
224
int result;
Jul 7, 2010
Jul 7, 2010
225
if(window == NULL || !SDL_IsShapedWindow(window))
Jun 30, 2010
Jun 30, 2010
226
//The window given was not a shapeable window.
Jul 19, 2010
Jul 19, 2010
227
return SDL_NONSHAPEABLE_WINDOW;
Jun 20, 2010
Jun 20, 2010
228
if(shape == NULL)
Jun 30, 2010
Jun 30, 2010
229
//Invalid shape argument.
Jul 19, 2010
Jul 19, 2010
230
return SDL_INVALID_SHAPE_ARGUMENT;
Jun 30, 2010
Jun 30, 2010
231
Jul 23, 2010
Jul 23, 2010
232
233
if(shapeMode != NULL)
window->shaper->mode = *shapeMode;
Jul 19, 2010
Jul 19, 2010
234
//TODO: Platform-specific implementations of SetWindowShape. X11 is finished. Win32 is finished. Debugging is in progress on both.
Jul 9, 2010
Jul 9, 2010
235
result = window->display->device->shape_driver.SetWindowShape(window->shaper,shape,shapeMode);
Jun 30, 2010
Jun 30, 2010
236
window->shaper->hasshape = SDL_TRUE;
Jul 9, 2010
Jul 9, 2010
237
if((window->shaper->usershownflag & SDL_WINDOW_SHOWN) == SDL_WINDOW_SHOWN) {
Jun 30, 2010
Jun 30, 2010
238
239
240
241
242
243
244
SDL_ShowWindow(window);
window->shaper->usershownflag &= !SDL_WINDOW_SHOWN;
}
return result;
}
SDL_bool SDL_WindowHasAShape(SDL_Window *window) {
Aug 5, 2010
Aug 5, 2010
245
if (window == NULL || !SDL_IsShapedWindow(window))
Jul 9, 2010
Jul 9, 2010
246
return SDL_FALSE;
Jun 30, 2010
Jun 30, 2010
247
return window->shaper->hasshape;
Jun 10, 2010
Jun 10, 2010
248
249
}
Jun 22, 2010
Jun 22, 2010
250
int SDL_GetShapedWindowMode(SDL_Window *window,SDL_WindowShapeMode *shapeMode) {
Jun 30, 2010
Jun 30, 2010
251
252
253
254
255
256
257
if(window != NULL && SDL_IsShapedWindow(window)) {
if(shapeMode == NULL) {
if(SDL_WindowHasAShape(window))
//The window given has a shape.
return 0;
else
//The window given is shapeable but lacks a shape.
Jul 19, 2010
Jul 19, 2010
258
return SDL_WINDOW_LACKS_SHAPE;
Jun 30, 2010
Jun 30, 2010
259
260
}
else {
Jul 23, 2010
Jul 23, 2010
261
*shapeMode = window->shaper->mode;
Jun 30, 2010
Jun 30, 2010
262
263
264
265
266
return 0;
}
}
else
//The window given is not a valid shapeable window.
Jul 19, 2010
Jul 19, 2010
267
return SDL_NONSHAPEABLE_WINDOW;
Jun 10, 2010
Jun 10, 2010
268
}