SDL_shape.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../SDL_internal.h"
  19. #include "SDL.h"
  20. #include "SDL_assert.h"
  21. #include "SDL_video.h"
  22. #include "SDL_sysvideo.h"
  23. #include "SDL_pixels.h"
  24. #include "SDL_surface.h"
  25. #include "SDL_shape.h"
  26. #include "SDL_shape_internals.h"
  27. SDL_Window*
  28. SDL_CreateShapedWindow(const char *title,unsigned int x,unsigned int y,unsigned int w,unsigned int h,Uint32 flags)
  29. {
  30. SDL_Window *result = NULL;
  31. result = SDL_CreateWindow(title,-1000,-1000,w,h,(flags | SDL_WINDOW_BORDERLESS) & (~SDL_WINDOW_FULLSCREEN) & (~SDL_WINDOW_RESIZABLE) /* & (~SDL_WINDOW_SHOWN) */);
  32. if(result != NULL) {
  33. result->shaper = SDL_GetVideoDevice()->shape_driver.CreateShaper(result);
  34. if(result->shaper != NULL) {
  35. result->shaper->userx = x;
  36. result->shaper->usery = y;
  37. result->shaper->mode.mode = ShapeModeDefault;
  38. result->shaper->mode.parameters.binarizationCutoff = 1;
  39. result->shaper->hasshape = SDL_FALSE;
  40. return result;
  41. }
  42. else {
  43. SDL_DestroyWindow(result);
  44. return NULL;
  45. }
  46. }
  47. else
  48. return NULL;
  49. }
  50. SDL_bool
  51. SDL_IsShapedWindow(const SDL_Window *window)
  52. {
  53. if(window == NULL)
  54. return SDL_FALSE;
  55. else
  56. return (SDL_bool)(window->shaper != NULL);
  57. }
  58. /* REQUIRES that bitmap point to a w-by-h bitmap with ppb pixels-per-byte. */
  59. void
  60. SDL_CalculateShapeBitmap(SDL_WindowShapeMode mode,SDL_Surface *shape,Uint8* bitmap,Uint8 ppb)
  61. {
  62. int x = 0;
  63. int y = 0;
  64. Uint8 r = 0,g = 0,b = 0,alpha = 0;
  65. Uint8* pixel = NULL;
  66. Uint32 bitmap_pixel,pixel_value = 0,mask_value = 0;
  67. SDL_Color key;
  68. if(SDL_MUSTLOCK(shape))
  69. SDL_LockSurface(shape);
  70. for(y = 0;y<shape->h;y++) {
  71. 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. switch(shape->format->BytesPerPixel) {
  76. case(1):
  77. pixel_value = *(Uint8*)pixel;
  78. break;
  79. case(2):
  80. pixel_value = *(Uint16*)pixel;
  81. break;
  82. case(3):
  83. pixel_value = *(Uint32*)pixel & (~shape->format->Amask);
  84. break;
  85. case(4):
  86. pixel_value = *(Uint32*)pixel;
  87. break;
  88. }
  89. SDL_GetRGBA(pixel_value,shape->format,&r,&g,&b,&alpha);
  90. bitmap_pixel = y*shape->w + x;
  91. switch(mode.mode) {
  92. case(ShapeModeDefault):
  93. mask_value = (alpha >= 1 ? 1 : 0);
  94. break;
  95. case(ShapeModeBinarizeAlpha):
  96. mask_value = (alpha >= mode.parameters.binarizationCutoff ? 1 : 0);
  97. break;
  98. case(ShapeModeReverseBinarizeAlpha):
  99. mask_value = (alpha <= mode.parameters.binarizationCutoff ? 1 : 0);
  100. break;
  101. case(ShapeModeColorKey):
  102. key = mode.parameters.colorKey;
  103. mask_value = ((key.r != r || key.g != g || key.b != b) ? 1 : 0);
  104. break;
  105. }
  106. bitmap[bitmap_pixel / ppb] |= mask_value << (7 - ((ppb - 1) - (bitmap_pixel % ppb)));
  107. }
  108. }
  109. if(SDL_MUSTLOCK(shape))
  110. SDL_UnlockSurface(shape);
  111. }
  112. static SDL_ShapeTree*
  113. RecursivelyCalculateShapeTree(SDL_WindowShapeMode mode,SDL_Surface* mask,SDL_Rect dimensions) {
  114. int x = 0,y = 0;
  115. Uint8* pixel = NULL;
  116. Uint32 pixel_value = 0;
  117. Uint8 r = 0,g = 0,b = 0,a = 0;
  118. SDL_bool pixel_opaque = SDL_FALSE;
  119. int last_opaque = -1;
  120. SDL_Color key;
  121. SDL_ShapeTree* result = (SDL_ShapeTree*)SDL_malloc(sizeof(SDL_ShapeTree));
  122. SDL_Rect next = {0,0,0,0};
  123. for(y=dimensions.y;y<dimensions.y + dimensions.h;y++) {
  124. for(x=dimensions.x;x<dimensions.x + dimensions.w;x++) {
  125. pixel_value = 0;
  126. pixel = (Uint8 *)(mask->pixels) + (y*mask->pitch) + (x*mask->format->BytesPerPixel);
  127. switch(mask->format->BytesPerPixel) {
  128. case(1):
  129. pixel_value = *(Uint8*)pixel;
  130. break;
  131. case(2):
  132. pixel_value = *(Uint16*)pixel;
  133. break;
  134. case(3):
  135. pixel_value = *(Uint32*)pixel & (~mask->format->Amask);
  136. break;
  137. case(4):
  138. pixel_value = *(Uint32*)pixel;
  139. break;
  140. }
  141. SDL_GetRGBA(pixel_value,mask->format,&r,&g,&b,&a);
  142. switch(mode.mode) {
  143. case(ShapeModeDefault):
  144. pixel_opaque = (a >= 1 ? SDL_TRUE : SDL_FALSE);
  145. break;
  146. case(ShapeModeBinarizeAlpha):
  147. pixel_opaque = (a >= mode.parameters.binarizationCutoff ? SDL_TRUE : SDL_FALSE);
  148. break;
  149. case(ShapeModeReverseBinarizeAlpha):
  150. pixel_opaque = (a <= mode.parameters.binarizationCutoff ? SDL_TRUE : SDL_FALSE);
  151. break;
  152. case(ShapeModeColorKey):
  153. key = mode.parameters.colorKey;
  154. pixel_opaque = ((key.r != r || key.g != g || key.b != b) ? SDL_TRUE : SDL_FALSE);
  155. break;
  156. }
  157. if(last_opaque == -1)
  158. last_opaque = pixel_opaque;
  159. if(last_opaque != pixel_opaque) {
  160. result->kind = QuadShape;
  161. /* These will stay the same. */
  162. next.w = dimensions.w / 2;
  163. next.h = dimensions.h / 2;
  164. /* These will change from recursion to recursion. */
  165. next.x = dimensions.x;
  166. next.y = dimensions.y;
  167. result->data.children.upleft = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next);
  168. next.x += next.w;
  169. /* Unneeded: next.y = dimensions.y; */
  170. result->data.children.upright = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next);
  171. next.x = dimensions.x;
  172. next.y += next.h;
  173. result->data.children.downleft = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next);
  174. next.x += next.w;
  175. /* Unneeded: next.y = dimensions.y + dimensions.h /2; */
  176. result->data.children.downright = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next);
  177. return result;
  178. }
  179. }
  180. }
  181. /* If we never recursed, all the pixels in this quadrant have the same "value". */
  182. result->kind = (last_opaque == SDL_TRUE ? OpaqueShape : TransparentShape);
  183. result->data.shape = dimensions;
  184. return result;
  185. }
  186. SDL_ShapeTree*
  187. SDL_CalculateShapeTree(SDL_WindowShapeMode mode,SDL_Surface* shape)
  188. {
  189. SDL_Rect dimensions = {0,0,shape->w,shape->h};
  190. SDL_ShapeTree* result = NULL;
  191. if(SDL_MUSTLOCK(shape))
  192. SDL_LockSurface(shape);
  193. result = RecursivelyCalculateShapeTree(mode,shape,dimensions);
  194. if(SDL_MUSTLOCK(shape))
  195. SDL_UnlockSurface(shape);
  196. return result;
  197. }
  198. void
  199. SDL_TraverseShapeTree(SDL_ShapeTree *tree,SDL_TraversalFunction function,void* closure)
  200. {
  201. SDL_assert(tree != NULL);
  202. if(tree->kind == QuadShape) {
  203. SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.upleft,function,closure);
  204. SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.upright,function,closure);
  205. SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.downleft,function,closure);
  206. SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.downright,function,closure);
  207. }
  208. else
  209. function(tree,closure);
  210. }
  211. void
  212. SDL_FreeShapeTree(SDL_ShapeTree** shape_tree)
  213. {
  214. if((*shape_tree)->kind == QuadShape) {
  215. SDL_FreeShapeTree((SDL_ShapeTree **)&(*shape_tree)->data.children.upleft);
  216. SDL_FreeShapeTree((SDL_ShapeTree **)&(*shape_tree)->data.children.upright);
  217. SDL_FreeShapeTree((SDL_ShapeTree **)&(*shape_tree)->data.children.downleft);
  218. SDL_FreeShapeTree((SDL_ShapeTree **)&(*shape_tree)->data.children.downright);
  219. }
  220. SDL_free(*shape_tree);
  221. *shape_tree = NULL;
  222. }
  223. int
  224. SDL_SetWindowShape(SDL_Window *window,SDL_Surface *shape,SDL_WindowShapeMode *shape_mode)
  225. {
  226. int result;
  227. if(window == NULL || !SDL_IsShapedWindow(window))
  228. /* The window given was not a shapeable window. */
  229. return SDL_NONSHAPEABLE_WINDOW;
  230. if(shape == NULL)
  231. /* Invalid shape argument. */
  232. return SDL_INVALID_SHAPE_ARGUMENT;
  233. if(shape_mode != NULL)
  234. window->shaper->mode = *shape_mode;
  235. result = SDL_GetVideoDevice()->shape_driver.SetWindowShape(window->shaper,shape,shape_mode);
  236. window->shaper->hasshape = SDL_TRUE;
  237. if(window->shaper->userx != 0 && window->shaper->usery != 0) {
  238. SDL_SetWindowPosition(window,window->shaper->userx,window->shaper->usery);
  239. window->shaper->userx = 0;
  240. window->shaper->usery = 0;
  241. }
  242. return result;
  243. }
  244. static SDL_bool
  245. SDL_WindowHasAShape(SDL_Window *window)
  246. {
  247. if (window == NULL || !SDL_IsShapedWindow(window))
  248. return SDL_FALSE;
  249. return window->shaper->hasshape;
  250. }
  251. int
  252. SDL_GetShapedWindowMode(SDL_Window *window,SDL_WindowShapeMode *shape_mode)
  253. {
  254. if(window != NULL && SDL_IsShapedWindow(window)) {
  255. if(shape_mode == NULL) {
  256. if(SDL_WindowHasAShape(window))
  257. /* The window given has a shape. */
  258. return 0;
  259. else
  260. /* The window given is shapeable but lacks a shape. */
  261. return SDL_WINDOW_LACKS_SHAPE;
  262. }
  263. else {
  264. *shape_mode = window->shaper->mode;
  265. return 0;
  266. }
  267. }
  268. else
  269. /* The window given is not a valid shapeable window. */
  270. return SDL_NONSHAPEABLE_WINDOW;
  271. }