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

Latest commit

 

History

History
153 lines (141 loc) · 4.97 KB

SDL_shape.c

File metadata and controls

153 lines (141 loc) · 4.97 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
25
26
#include "SDL.h"
#include "SDL_video.h"
#include "SDL_sysvideo.h"
Jun 30, 2010
Jun 30, 2010
27
28
#include "SDL_pixels.h"
#include "SDL_surface.h"
29
#include "SDL_shape.h"
May 21, 2010
May 21, 2010
30
May 27, 2010
May 27, 2010
31
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
32
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
33
34
35
36
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
37
38
result->shaper->mode.mode = ShapeModeDefault;
result->shaper->mode.parameters.binarizationCutoff = 1;
Jul 15, 2010
Jul 15, 2010
39
40
41
42
43
44
45
46
47
48
result->shaper->hasshape = SDL_FALSE;
return result;
}
else {
SDL_DestroyWindow(result);
return NULL;
}
}
else
return NULL;
May 21, 2010
May 21, 2010
49
50
}
Jun 22, 2010
Jun 22, 2010
51
SDL_bool SDL_IsShapedWindow(const SDL_Window *window) {
Jun 30, 2010
Jun 30, 2010
52
53
54
if(window == NULL)
return SDL_FALSE;
else
Jul 11, 2010
Jul 11, 2010
55
return (SDL_bool)(window->shaper != NULL);
Jun 30, 2010
Jun 30, 2010
56
57
58
}
/* REQUIRES that bitmap point to a w-by-h bitmap with 1bpp. */
Jul 23, 2010
Jul 23, 2010
59
void SDL_CalculateShapeBitmap(SDL_WindowShapeMode mode,SDL_Surface *shape,Uint8* bitmap,Uint8 ppb,Uint8 value) {
Jul 9, 2010
Jul 9, 2010
60
61
int x = 0;
int y = 0;
Jul 14, 2010
Jul 14, 2010
62
Uint8 r = 0,g = 0,b = 0,alpha = 0;
Jul 19, 2010
Jul 19, 2010
63
64
Uint8* pixel = NULL;
Uint32 bitmap_pixel,pixel_value = 0;
Jul 23, 2010
Jul 23, 2010
65
SDL_Color key;
Jun 30, 2010
Jun 30, 2010
66
67
if(SDL_MUSTLOCK(shape))
SDL_LockSurface(shape);
Jul 19, 2010
Jul 19, 2010
68
69
70
pixel = (Uint8*)shape->pixels;
for(y = 0;y<shape->h;y++) {
for(x=0;x<shape->w;x++) {
Jul 9, 2010
Jul 9, 2010
71
alpha = 0;
Jul 19, 2010
Jul 19, 2010
72
pixel_value = 0;
Jul 20, 2010
Jul 20, 2010
73
pixel = (Uint8 *)(shape->pixels) + (y*shape->pitch) + (x*shape->format->BytesPerPixel);
Jul 19, 2010
Jul 19, 2010
74
75
76
77
78
79
80
81
82
83
84
85
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);
Jul 15, 2010
Jul 15, 2010
86
bitmap_pixel = y*shape->w + x;
Jul 23, 2010
Jul 23, 2010
87
88
89
90
91
92
93
94
95
96
97
98
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;
Jul 23, 2010
Jul 23, 2010
99
bitmap[bitmap_pixel / ppb] |= ((key.r == r && key.g == g && key.b == b) ? value : 0) << ((ppb - 1) - (bitmap_pixel % ppb));
Jul 23, 2010
Jul 23, 2010
100
101
break;
}
Jun 30, 2010
Jun 30, 2010
102
}
Jul 19, 2010
Jul 19, 2010
103
}
Jun 30, 2010
Jun 30, 2010
104
105
if(SDL_MUSTLOCK(shape))
SDL_UnlockSurface(shape);
May 21, 2010
May 21, 2010
106
107
}
Jun 22, 2010
Jun 22, 2010
108
int SDL_SetWindowShape(SDL_Window *window,SDL_Surface *shape,SDL_WindowShapeMode *shapeMode) {
Jul 9, 2010
Jul 9, 2010
109
int result;
Jul 7, 2010
Jul 7, 2010
110
if(window == NULL || !SDL_IsShapedWindow(window))
Jun 30, 2010
Jun 30, 2010
111
//The window given was not a shapeable window.
Jul 19, 2010
Jul 19, 2010
112
return SDL_NONSHAPEABLE_WINDOW;
Jun 20, 2010
Jun 20, 2010
113
if(shape == NULL)
Jun 30, 2010
Jun 30, 2010
114
//Invalid shape argument.
Jul 19, 2010
Jul 19, 2010
115
return SDL_INVALID_SHAPE_ARGUMENT;
Jun 30, 2010
Jun 30, 2010
116
Jul 23, 2010
Jul 23, 2010
117
118
if(shapeMode != NULL)
window->shaper->mode = *shapeMode;
Jul 19, 2010
Jul 19, 2010
119
//TODO: Platform-specific implementations of SetWindowShape. X11 is finished. Win32 is finished. Debugging is in progress on both.
Jul 9, 2010
Jul 9, 2010
120
result = window->display->device->shape_driver.SetWindowShape(window->shaper,shape,shapeMode);
Jun 30, 2010
Jun 30, 2010
121
window->shaper->hasshape = SDL_TRUE;
Jul 9, 2010
Jul 9, 2010
122
if((window->shaper->usershownflag & SDL_WINDOW_SHOWN) == SDL_WINDOW_SHOWN) {
Jun 30, 2010
Jun 30, 2010
123
124
125
126
127
128
129
SDL_ShowWindow(window);
window->shaper->usershownflag &= !SDL_WINDOW_SHOWN;
}
return result;
}
SDL_bool SDL_WindowHasAShape(SDL_Window *window) {
Jul 9, 2010
Jul 9, 2010
130
131
if (window == NULL && !SDL_IsShapedWindow(window))
return SDL_FALSE;
Jun 30, 2010
Jun 30, 2010
132
return window->shaper->hasshape;
Jun 10, 2010
Jun 10, 2010
133
134
}
Jun 22, 2010
Jun 22, 2010
135
int SDL_GetShapedWindowMode(SDL_Window *window,SDL_WindowShapeMode *shapeMode) {
Jun 30, 2010
Jun 30, 2010
136
137
138
139
140
141
142
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
143
return SDL_WINDOW_LACKS_SHAPE;
Jun 30, 2010
Jun 30, 2010
144
145
}
else {
Jul 23, 2010
Jul 23, 2010
146
*shapeMode = window->shaper->mode;
Jun 30, 2010
Jun 30, 2010
147
148
149
150
151
return 0;
}
}
else
//The window given is not a valid shapeable window.
Jul 19, 2010
Jul 19, 2010
152
return SDL_NONSHAPEABLE_WINDOW;
Jun 10, 2010
Jun 10, 2010
153
}