comparison src/video/SDL_shape.c @ 4782:b6930aefd008

Finished X11 shaped-window functionality and removed ellipse+polygon rendering.
author Eli Gottlieb <eligottlieb@gmail.com>
date Wed, 30 Jun 2010 16:19:44 -0400
parents fc4c775b468a
children ef8b32ef9793
comparison
equal deleted inserted replaced
4781:fc4c775b468a 4782:b6930aefd008
22 #include "SDL_config.h" 22 #include "SDL_config.h"
23 23
24 #include "SDL.h" 24 #include "SDL.h"
25 #include "SDL_video.h" 25 #include "SDL_video.h"
26 #include "SDL_sysvideo.h" 26 #include "SDL_sysvideo.h"
27 #include "SDL_pixels.h"
28 #include "SDL_surface.h"
27 #include "SDL_shape.h" 29 #include "SDL_shape.h"
28 30
29 SDL_Window* SDL_CreateShapedWindow(const char *title,unsigned int x,unsigned int y,unsigned int w,unsigned int h,Uint32 flags) { 31 SDL_Window* SDL_CreateShapedWindow(const char *title,unsigned int x,unsigned int y,unsigned int w,unsigned int h,Uint32 flags) {
30 return NULL; 32 SDL_Window *result = SDL_CreateWindow(title,x,y,w,h,SDL_WINDOW_BORDERLESS | flags & !SDL_WINDOW_FULLSCREEN & !SDL_WINDOW_SHOWN);
33 result->shaper = result->display->device->shape_driver.CreateShaper(result);
34 result->shaper->usershownflag = flags & SDL_WINDOW_SHOWN;
35 result->shaper->alphacutoff = 1;
36 result->shaper->hasshape = SDL_FALSE;
37 return result;
31 } 38 }
32 39
33 SDL_bool SDL_IsShapedWindow(const SDL_Window *window) { 40 SDL_bool SDL_IsShapedWindow(const SDL_Window *window) {
34 return SDL_FALSE; 41 if(window == NULL)
42 return SDL_FALSE;
43 else
44 return window->shaper != NULL;
45 }
46
47 /* REQUIRES that bitmap point to a w-by-h bitmap with 1bpp. */
48 void SDL_CalculateShapeBitmap(Uint8 alphacutoff,SDL_Surface *shape,Uint8* bitmap) {
49 if(SDL_MUSTLOCK(shape))
50 SDL_LockSurface(shape);
51 int x = 0,y = 0;
52 for(x = 0;x<shape->w;x++)
53 for(y = 0;y<shape->h;y++) {
54 void* pixel = shape->pixels + (y*shape->pitch) + (x*shape->format->BytesPerPixel);
55 Uint8 alpha = 0;
56 SDL_GetRGBA(*(Uint32*)pixel,shape->format,NULL,NULL,NULL,&alpha);
57 Uint32 bitmap_pixel = y*shape->w + x;
58 bitmap[bitmap_pixel / 8] |= (alpha >= alphacutoff ? 1 : 0) << (8 - (bitmap_pixel % 8));
59 }
60 if(SDL_MUSTLOCK(shape))
61 SDL_UnlockSurface(shape);
35 } 62 }
36 63
37 int SDL_SetWindowShape(SDL_Window *window,SDL_Surface *shape,SDL_WindowShapeMode *shapeMode) { 64 int SDL_SetWindowShape(SDL_Window *window,SDL_Surface *shape,SDL_WindowShapeMode *shapeMode) {
38 if(window == NULL || !SDL_WindowIsShaped(window)) 65 if(window == NULL || !SDL_WindowIsShaped(window))
66 //The window given was not a shapeable window.
39 return -2; 67 return -2;
40 if(shape == NULL) 68 if(shape == NULL)
69 //Invalid shape argument.
41 return -1; 70 return -1;
42 return -3; 71
72 if(shapeMode != NULL) {
73 switch(shapeMode->mode) {
74 case ShapeModeDefault: {
75 window->shaper->alphacutoff = 1;
76 break;
77 }
78 case ShapeModeBinarizeAlpha: {
79 window->shaper->alphacutoff = shapeMode->parameters.binarizationCutoff;
80 break;
81 }
82 }
83 }
84 //TODO: Platform-specific implementations of SetWindowShape. X11 is in-progress.
85 int result = window->display->device->shape_driver.SetWindowShape(window->shaper,shape,shapeMode);
86 window->shaper->hasshape = SDL_TRUE;
87 if(window->shaper->usershownflag & SDL_WINDOW_SHOWN == SDL_WINDOW_SHOWN) {
88 SDL_ShowWindow(window);
89 window->shaper->usershownflag &= !SDL_WINDOW_SHOWN;
90 }
91 return result;
92 }
93
94 SDL_bool SDL_WindowHasAShape(SDL_Window *window) {
95 assert(window != NULL && SDL_IsShapedWindow(window));
96 return window->shaper->hasshape;
43 } 97 }
44 98
45 int SDL_GetShapedWindowMode(SDL_Window *window,SDL_WindowShapeMode *shapeMode) { 99 int SDL_GetShapedWindowMode(SDL_Window *window,SDL_WindowShapeMode *shapeMode) {
46 if(shapeMode == NULL) 100 if(window != NULL && SDL_IsShapedWindow(window)) {
101 if(shapeMode == NULL) {
102 if(SDL_WindowHasAShape(window))
103 //The window given has a shape.
104 return 0;
105 else
106 //The window given is shapeable but lacks a shape.
107 return -2;
108 }
109 else {
110 if(window->shaper->alphacutoff != 1) {
111 shapeMode->mode = ShapeModeBinarizeAlpha;
112 shapeMode->parameters.binarizationCutoff = window->shaper->alphacutoff;
113 }
114 else
115 shapeMode->mode = ShapeModeDefault;
116 return 0;
117 }
118 }
119 else
120 //The window given is not a valid shapeable window.
47 return -1; 121 return -1;
48 if(window == NULL || !SDL_WindowIsShaped(window))
49 return -2;
50 return -3;
51 } 122 }