This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_shape.c
259 lines (243 loc) · 9.05 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
*/
22
#include "SDL_config.h"
23
24
25
26
#include "SDL.h"
#include "SDL_video.h"
#include "SDL_sysvideo.h"
27
28
#include "SDL_pixels.h"
#include "SDL_surface.h"
29
#include "SDL_shape.h"
30
#include "SDL_shape_internals.h"
31
32
SDL_Window* SDL_CreateShapedWindow(const char *title,unsigned int x,unsigned int y,unsigned int w,unsigned int h,Uint32 flags) {
33
SDL_Window *result = SDL_CreateWindow(title,x,y,w,h,SDL_WINDOW_BORDERLESS | flags & !SDL_WINDOW_FULLSCREEN & !SDL_WINDOW_SHOWN);
34
35
36
37
if(result != NULL) {
result->shaper = result->display->device->shape_driver.CreateShaper(result);
if(result->shaper != NULL) {
result->shaper->usershownflag = flags & SDL_WINDOW_SHOWN;
38
39
result->shaper->mode.mode = ShapeModeDefault;
result->shaper->mode.parameters.binarizationCutoff = 1;
40
41
42
43
44
45
46
47
48
49
result->shaper->hasshape = SDL_FALSE;
return result;
}
else {
SDL_DestroyWindow(result);
return NULL;
}
}
else
return NULL;
50
51
}
52
SDL_bool SDL_IsShapedWindow(const SDL_Window *window) {
53
54
55
if(window == NULL)
return SDL_FALSE;
else
56
return (SDL_bool)(window->shaper != NULL);
57
58
}
59
/* 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,Uint8 value) {
61
62
int x = 0;
int y = 0;
63
Uint8 r = 0,g = 0,b = 0,alpha = 0;
64
65
Uint8* pixel = NULL;
Uint32 bitmap_pixel,pixel_value = 0;
66
SDL_Color key;
67
68
if(SDL_MUSTLOCK(shape))
SDL_LockSurface(shape);
69
70
71
pixel = (Uint8*)shape->pixels;
for(y = 0;y<shape->h;y++) {
for(x=0;x<shape->w;x++) {
72
alpha = 0;
73
pixel_value = 0;
74
pixel = (Uint8 *)(shape->pixels) + (y*shape->pitch) + (x*shape->format->BytesPerPixel);
75
76
77
78
79
80
81
82
83
84
85
86
switch(shape->format->BytesPerPixel) {
case(1):
pixel_value = *(Uint8*)pixel;
break;
case(2):
pixel_value = *(Uint16*)pixel;
break;
case(4):
pixel_value = *(Uint32*)pixel;
break;
}
SDL_GetRGBA(pixel_value,shape->format,&r,&g,&b,&alpha);
87
bitmap_pixel = y*shape->w + x;
88
89
90
91
92
93
94
95
96
97
98
99
switch(mode.mode) {
case(ShapeModeDefault):
bitmap[bitmap_pixel / ppb] |= (alpha >= 1 ? value : 0) << ((ppb - 1) - (bitmap_pixel % ppb));
break;
case(ShapeModeBinarizeAlpha):
bitmap[bitmap_pixel / ppb] |= (alpha >= mode.parameters.binarizationCutoff ? value : 0) << ((ppb - 1) - (bitmap_pixel % ppb));
break;
case(ShapeModeReverseBinarizeAlpha):
bitmap[bitmap_pixel / ppb] |= (alpha <= mode.parameters.binarizationCutoff ? value : 0) << ((ppb - 1) - (bitmap_pixel % ppb));
break;
case(ShapeModeColorKey):
key = mode.parameters.colorKey;
100
bitmap[bitmap_pixel / ppb] |= ((key.r == r && key.g == g && key.b == b) ? value : 0) << ((ppb - 1) - (bitmap_pixel % ppb));
101
102
break;
}
103
}
104
}
105
106
if(SDL_MUSTLOCK(shape))
SDL_UnlockSurface(shape);
107
108
}
109
110
111
112
113
114
115
116
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;
117
SDL_ShapeTree* result = (SDL_ShapeTree*)SDL_malloc(sizeof(SDL_ShapeTree));
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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;
case(4):
pixel_value = *(Uint32*)pixel;
break;
}
SDL_GetRGBA(pixel_value,mask->format,&r,&g,&b,&a);
switch(mode.mode) {
case(ShapeModeDefault):
137
pixel_transparent = (SDL_bool)(a >= 1 ? !invert : invert);
138
139
break;
case(ShapeModeBinarizeAlpha):
140
pixel_transparent = (SDL_bool)(a >= mode.parameters.binarizationCutoff ? !invert : invert);
141
142
break;
case(ShapeModeReverseBinarizeAlpha):
143
pixel_transparent = (SDL_bool)(a <= mode.parameters.binarizationCutoff ? !invert : invert);
144
145
146
break;
case(ShapeModeColorKey):
key = mode.parameters.colorKey;
147
pixel_transparent = (SDL_bool)((key.r == r && key.g == g && key.b == b) ? !invert : invert);
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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;
162
result->data.children.upleft = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,invert,next);
163
164
next.x = dimensions.w / 2 + 1;
//Unneeded: next.y = dimensions.y;
165
result->data.children.upright = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,invert,next);
166
167
next.x = dimensions.x;
next.y = dimensions.h / 2 + 1;
168
result->data.children.downleft = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,invert,next);
169
170
next.x = dimensions.w / 2 + 1;
//Unneeded: next.y = dimensions.h / 2 + 1;
171
result->data.children.downright = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,invert,next);
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
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;
}
void SDL_TraverseShapeTree(SDL_ShapeTree *tree,void(*function)(SDL_ShapeTree*,void*),void* closure) {
if(tree->kind == QuadShape) {
194
195
196
197
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);
198
199
200
201
202
203
204
}
else
function(tree,closure);
}
void SDL_FreeShapeTree(SDL_ShapeTree** shapeTree) {
if((*shapeTree)->kind == QuadShape) {
205
206
207
208
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);
209
}
210
SDL_free(*shapeTree);
211
212
213
*shapeTree = NULL;
}
214
int SDL_SetWindowShape(SDL_Window *window,SDL_Surface *shape,SDL_WindowShapeMode *shapeMode) {
215
int result;
216
if(window == NULL || !SDL_IsShapedWindow(window))
217
//The window given was not a shapeable window.
218
return SDL_NONSHAPEABLE_WINDOW;
219
if(shape == NULL)
220
//Invalid shape argument.
221
return SDL_INVALID_SHAPE_ARGUMENT;
222
223
224
if(shapeMode != NULL)
window->shaper->mode = *shapeMode;
225
//TODO: Platform-specific implementations of SetWindowShape. X11 is finished. Win32 is finished. Debugging is in progress on both.
226
result = window->display->device->shape_driver.SetWindowShape(window->shaper,shape,shapeMode);
227
window->shaper->hasshape = SDL_TRUE;
228
if((window->shaper->usershownflag & SDL_WINDOW_SHOWN) == SDL_WINDOW_SHOWN) {
229
230
231
232
233
234
235
SDL_ShowWindow(window);
window->shaper->usershownflag &= !SDL_WINDOW_SHOWN;
}
return result;
}
SDL_bool SDL_WindowHasAShape(SDL_Window *window) {
236
237
if (window == NULL && !SDL_IsShapedWindow(window))
return SDL_FALSE;
238
return window->shaper->hasshape;
239
240
}
241
int SDL_GetShapedWindowMode(SDL_Window *window,SDL_WindowShapeMode *shapeMode) {
242
243
244
245
246
247
248
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.
249
return SDL_WINDOW_LACKS_SHAPE;
250
251
}
else {
252
*shapeMode = window->shaper->mode;
253
254
255
256
257
return 0;
}
}
else
//The window given is not a valid shapeable window.
258
return SDL_NONSHAPEABLE_WINDOW;
259
}