comparison src/video/SDL_shape.c @ 4807:c9eb95f29770

Added color-key mode and redid the code to work with it.
author Eli Gottlieb <eligottlieb@gmail.com>
date Thu, 22 Jul 2010 23:11:01 -0400
parents 007567dbb8c1
children 2ae79ed78a5a
comparison
equal deleted inserted replaced
4806:007567dbb8c1 4807:c9eb95f29770
32 SDL_Window *result = SDL_CreateWindow(title,x,y,w,h,SDL_WINDOW_BORDERLESS | flags & !SDL_WINDOW_FULLSCREEN & !SDL_WINDOW_SHOWN); 32 SDL_Window *result = SDL_CreateWindow(title,x,y,w,h,SDL_WINDOW_BORDERLESS | flags & !SDL_WINDOW_FULLSCREEN & !SDL_WINDOW_SHOWN);
33 if(result != NULL) { 33 if(result != NULL) {
34 result->shaper = result->display->device->shape_driver.CreateShaper(result); 34 result->shaper = result->display->device->shape_driver.CreateShaper(result);
35 if(result->shaper != NULL) { 35 if(result->shaper != NULL) {
36 result->shaper->usershownflag = flags & SDL_WINDOW_SHOWN; 36 result->shaper->usershownflag = flags & SDL_WINDOW_SHOWN;
37 result->shaper->alphacutoff = 1; 37 result->shaper->mode.mode = ShapeModeDefault;
38 result->shaper->mode.parameters.binarizationCutoff = 1;
38 result->shaper->hasshape = SDL_FALSE; 39 result->shaper->hasshape = SDL_FALSE;
39 return result; 40 return result;
40 } 41 }
41 else { 42 else {
42 SDL_DestroyWindow(result); 43 SDL_DestroyWindow(result);
53 else 54 else
54 return (SDL_bool)(window->shaper != NULL); 55 return (SDL_bool)(window->shaper != NULL);
55 } 56 }
56 57
57 /* REQUIRES that bitmap point to a w-by-h bitmap with 1bpp. */ 58 /* REQUIRES that bitmap point to a w-by-h bitmap with 1bpp. */
58 void SDL_CalculateShapeBitmap(Uint8 alphacutoff,SDL_Surface *shape,Uint8* bitmap,Uint8 ppb,Uint8 value) { 59 void SDL_CalculateShapeBitmap(SDL_WindowShapeMode mode,SDL_Surface *shape,Uint8* bitmap,Uint8 ppb,Uint8 value) {
59 int x = 0; 60 int x = 0;
60 int y = 0; 61 int y = 0;
61 Uint8 r = 0,g = 0,b = 0,alpha = 0; 62 Uint8 r = 0,g = 0,b = 0,alpha = 0;
62 Uint8* pixel = NULL; 63 Uint8* pixel = NULL;
63 Uint32 bitmap_pixel,pixel_value = 0; 64 Uint32 bitmap_pixel,pixel_value = 0;
65 SDL_Color key;
64 if(SDL_MUSTLOCK(shape)) 66 if(SDL_MUSTLOCK(shape))
65 SDL_LockSurface(shape); 67 SDL_LockSurface(shape);
66 pixel = (Uint8*)shape->pixels; 68 pixel = (Uint8*)shape->pixels;
67 for(y = 0;y<shape->h;y++) { 69 for(y = 0;y<shape->h;y++) {
68 for(x=0;x<shape->w;x++) { 70 for(x=0;x<shape->w;x++) {
80 pixel_value = *(Uint32*)pixel; 82 pixel_value = *(Uint32*)pixel;
81 break; 83 break;
82 } 84 }
83 SDL_GetRGBA(pixel_value,shape->format,&r,&g,&b,&alpha); 85 SDL_GetRGBA(pixel_value,shape->format,&r,&g,&b,&alpha);
84 bitmap_pixel = y*shape->w + x; 86 bitmap_pixel = y*shape->w + x;
85 bitmap[bitmap_pixel / ppb] |= (alpha >= alphacutoff ? value : 0) << ((ppb - 1) - (bitmap_pixel % ppb)); 87 switch(mode.mode) {
88 case(ShapeModeDefault):
89 bitmap[bitmap_pixel / ppb] |= (alpha >= 1 ? value : 0) << ((ppb - 1) - (bitmap_pixel % ppb));
90 break;
91 case(ShapeModeBinarizeAlpha):
92 bitmap[bitmap_pixel / ppb] |= (alpha >= mode.parameters.binarizationCutoff ? value : 0) << ((ppb - 1) - (bitmap_pixel % ppb));
93 break;
94 case(ShapeModeReverseBinarizeAlpha):
95 bitmap[bitmap_pixel / ppb] |= (alpha <= mode.parameters.binarizationCutoff ? value : 0) << ((ppb - 1) - (bitmap_pixel % ppb));
96 break;
97 case(ShapeModeColorKey):
98 key = mode.parameters.colorKey;
99 bitmap[bitmap_pixel / ppb] |= (key.r == r && key.g == g && key.b == b ? value : 0) << ((ppb - 1) - (bitmap_pixel % ppb));
100 break;
101 }
86 } 102 }
87 } 103 }
88 if(SDL_MUSTLOCK(shape)) 104 if(SDL_MUSTLOCK(shape))
89 SDL_UnlockSurface(shape); 105 SDL_UnlockSurface(shape);
90 } 106 }
96 return SDL_NONSHAPEABLE_WINDOW; 112 return SDL_NONSHAPEABLE_WINDOW;
97 if(shape == NULL) 113 if(shape == NULL)
98 //Invalid shape argument. 114 //Invalid shape argument.
99 return SDL_INVALID_SHAPE_ARGUMENT; 115 return SDL_INVALID_SHAPE_ARGUMENT;
100 116
101 if(shapeMode != NULL) { 117 if(shapeMode != NULL)
102 switch(shapeMode->mode) { 118 window->shaper->mode = *shapeMode;
103 case ShapeModeDefault: {
104 window->shaper->alphacutoff = 1;
105 break;
106 }
107 case ShapeModeBinarizeAlpha: {
108 window->shaper->alphacutoff = shapeMode->parameters.binarizationCutoff;
109 break;
110 }
111 }
112 }
113 //TODO: Platform-specific implementations of SetWindowShape. X11 is finished. Win32 is finished. Debugging is in progress on both. 119 //TODO: Platform-specific implementations of SetWindowShape. X11 is finished. Win32 is finished. Debugging is in progress on both.
114 result = window->display->device->shape_driver.SetWindowShape(window->shaper,shape,shapeMode); 120 result = window->display->device->shape_driver.SetWindowShape(window->shaper,shape,shapeMode);
115 window->shaper->hasshape = SDL_TRUE; 121 window->shaper->hasshape = SDL_TRUE;
116 if((window->shaper->usershownflag & SDL_WINDOW_SHOWN) == SDL_WINDOW_SHOWN) { 122 if((window->shaper->usershownflag & SDL_WINDOW_SHOWN) == SDL_WINDOW_SHOWN) {
117 SDL_ShowWindow(window); 123 SDL_ShowWindow(window);
135 else 141 else
136 //The window given is shapeable but lacks a shape. 142 //The window given is shapeable but lacks a shape.
137 return SDL_WINDOW_LACKS_SHAPE; 143 return SDL_WINDOW_LACKS_SHAPE;
138 } 144 }
139 else { 145 else {
140 if(window->shaper->alphacutoff != 1) { 146 *shapeMode = window->shaper->mode;
141 shapeMode->mode = ShapeModeBinarizeAlpha;
142 shapeMode->parameters.binarizationCutoff = window->shaper->alphacutoff;
143 }
144 else
145 shapeMode->mode = ShapeModeDefault;
146 return 0; 147 return 0;
147 } 148 }
148 } 149 }
149 else 150 else
150 //The window given is not a valid shapeable window. 151 //The window given is not a valid shapeable window.