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

Latest commit

 

History

History
122 lines (110 loc) · 3.98 KB

SDL_shape.c

File metadata and controls

122 lines (110 loc) · 3.98 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) {
Jun 30, 2010
Jun 30, 2010
32
33
34
35
36
37
SDL_Window *result = SDL_CreateWindow(title,x,y,w,h,SDL_WINDOW_BORDERLESS | flags & !SDL_WINDOW_FULLSCREEN & !SDL_WINDOW_SHOWN);
result->shaper = result->display->device->shape_driver.CreateShaper(result);
result->shaper->usershownflag = flags & SDL_WINDOW_SHOWN;
result->shaper->alphacutoff = 1;
result->shaper->hasshape = SDL_FALSE;
return result;
May 21, 2010
May 21, 2010
38
39
}
Jun 22, 2010
Jun 22, 2010
40
SDL_bool SDL_IsShapedWindow(const SDL_Window *window) {
Jun 30, 2010
Jun 30, 2010
41
42
43
44
45
46
47
if(window == NULL)
return SDL_FALSE;
else
return window->shaper != NULL;
}
/* REQUIRES that bitmap point to a w-by-h bitmap with 1bpp. */
Jul 7, 2010
Jul 7, 2010
48
void SDL_CalculateShapeBitmap(Uint8 alphacutoff,SDL_Surface *shape,Uint8* bitmap,Uint8 ppb,Uint8 value) {
Jun 30, 2010
Jun 30, 2010
49
50
51
52
53
54
55
56
57
if(SDL_MUSTLOCK(shape))
SDL_LockSurface(shape);
int x = 0,y = 0;
for(x = 0;x<shape->w;x++)
for(y = 0;y<shape->h;y++) {
void* pixel = shape->pixels + (y*shape->pitch) + (x*shape->format->BytesPerPixel);
Uint8 alpha = 0;
SDL_GetRGBA(*(Uint32*)pixel,shape->format,NULL,NULL,NULL,&alpha);
Uint32 bitmap_pixel = y*shape->w + x;
Jul 7, 2010
Jul 7, 2010
58
bitmap[bitmap_pixel / ppb] |= (alpha >= alphacutoff ? value : 0) << ((ppb - 1) - (bitmap_pixel % ppb));
Jun 30, 2010
Jun 30, 2010
59
60
61
}
if(SDL_MUSTLOCK(shape))
SDL_UnlockSurface(shape);
May 21, 2010
May 21, 2010
62
63
}
Jun 22, 2010
Jun 22, 2010
64
int SDL_SetWindowShape(SDL_Window *window,SDL_Surface *shape,SDL_WindowShapeMode *shapeMode) {
Jul 7, 2010
Jul 7, 2010
65
if(window == NULL || !SDL_IsShapedWindow(window))
Jun 30, 2010
Jun 30, 2010
66
//The window given was not a shapeable window.
Jun 10, 2010
Jun 10, 2010
67
return -2;
Jun 20, 2010
Jun 20, 2010
68
if(shape == NULL)
Jun 30, 2010
Jun 30, 2010
69
//Invalid shape argument.
Jun 20, 2010
Jun 20, 2010
70
return -1;
Jun 30, 2010
Jun 30, 2010
71
72
73
74
75
76
77
78
79
80
81
82
83
if(shapeMode != NULL) {
switch(shapeMode->mode) {
case ShapeModeDefault: {
window->shaper->alphacutoff = 1;
break;
}
case ShapeModeBinarizeAlpha: {
window->shaper->alphacutoff = shapeMode->parameters.binarizationCutoff;
break;
}
}
}
Jul 7, 2010
Jul 7, 2010
84
//TODO: Platform-specific implementations of SetWindowShape. X11 is finished. Win32 is in progress.
Jun 30, 2010
Jun 30, 2010
85
86
87
88
89
90
91
92
93
94
95
96
int result = window->display->device->shape_driver.SetWindowShape(window->shaper,shape,shapeMode);
window->shaper->hasshape = SDL_TRUE;
if(window->shaper->usershownflag & SDL_WINDOW_SHOWN == SDL_WINDOW_SHOWN) {
SDL_ShowWindow(window);
window->shaper->usershownflag &= !SDL_WINDOW_SHOWN;
}
return result;
}
SDL_bool SDL_WindowHasAShape(SDL_Window *window) {
assert(window != NULL && SDL_IsShapedWindow(window));
return window->shaper->hasshape;
Jun 10, 2010
Jun 10, 2010
97
98
}
Jun 22, 2010
Jun 22, 2010
99
int SDL_GetShapedWindowMode(SDL_Window *window,SDL_WindowShapeMode *shapeMode) {
Jun 30, 2010
Jun 30, 2010
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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.
return -2;
}
else {
if(window->shaper->alphacutoff != 1) {
shapeMode->mode = ShapeModeBinarizeAlpha;
shapeMode->parameters.binarizationCutoff = window->shaper->alphacutoff;
}
else
shapeMode->mode = ShapeModeDefault;
return 0;
}
}
else
//The window given is not a valid shapeable window.
Jun 10, 2010
Jun 10, 2010
121
122
return -1;
}