SDL_shape.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2021 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_video.h"
  21. #include "SDL_sysvideo.h"
  22. #include "SDL_pixels.h"
  23. #include "SDL_surface.h"
  24. #include "SDL_shape.h"
  25. #include "SDL_shape_internals.h"
  26. SDL_Window*
  27. SDL_CreateShapedWindow(const char *title,unsigned int x,unsigned int y,unsigned int w,unsigned int h,Uint32 flags)
  28. {
  29. SDL_Window *result = NULL;
  30. result = SDL_CreateWindow(title,-1000,-1000,w,h,(flags | SDL_WINDOW_BORDERLESS) & (~SDL_WINDOW_FULLSCREEN) & (~SDL_WINDOW_RESIZABLE) /* & (~SDL_WINDOW_SHOWN) */);
  31. if(result != NULL) {
  32. if (SDL_GetVideoDevice()->shape_driver.CreateShaper == NULL) {
  33. SDL_DestroyWindow(result);
  34. return NULL;
  35. }
  36. result->shaper = SDL_GetVideoDevice()->shape_driver.CreateShaper(result);
  37. if(result->shaper != NULL) {
  38. result->shaper->userx = x;
  39. result->shaper->usery = y;
  40. result->shaper->mode.mode = ShapeModeDefault;
  41. result->shaper->mode.parameters.binarizationCutoff = 1;
  42. result->shaper->hasshape = SDL_FALSE;
  43. return result;
  44. }
  45. else {
  46. SDL_DestroyWindow(result);
  47. return NULL;
  48. }
  49. }
  50. else
  51. return NULL;
  52. }
  53. SDL_bool
  54. SDL_IsShapedWindow(const SDL_Window *window)
  55. {
  56. if(window == NULL)
  57. return SDL_FALSE;
  58. else
  59. return (SDL_bool)(window->shaper != NULL);
  60. }
  61. /* REQUIRES that bitmap point to a w-by-h bitmap with ppb pixels-per-byte. */
  62. void
  63. SDL_CalculateShapeBitmap(SDL_WindowShapeMode mode,SDL_Surface *shape,Uint8* bitmap,Uint8 ppb)
  64. {
  65. int x = 0;
  66. int y = 0;
  67. Uint8 r = 0,g = 0,b = 0,alpha = 0;
  68. Uint8* pixel = NULL;
  69. Uint32 pixel_value = 0,mask_value = 0;
  70. int bytes_per_scanline = (shape->w + (ppb - 1)) / ppb;
  71. Uint8 *bitmap_scanline;
  72. SDL_Color key;
  73. if(SDL_MUSTLOCK(shape))
  74. SDL_LockSurface(shape);
  75. for(y = 0;y<shape->h;y++) {
  76. bitmap_scanline = bitmap + y * bytes_per_scanline;
  77. for(x=0;x<shape->w;x++) {
  78. alpha = 0;
  79. pixel_value = 0;
  80. pixel = (Uint8 *)(shape->pixels) + (y*shape->pitch) + (x*shape->format->BytesPerPixel);
  81. switch(shape->format->BytesPerPixel) {
  82. case(1):
  83. pixel_value = *pixel;
  84. break;
  85. case(2):
  86. pixel_value = *(Uint16*)pixel;
  87. break;
  88. case(3):
  89. pixel_value = *(Uint32*)pixel & (~shape->format->Amask);
  90. break;
  91. case(4):
  92. pixel_value = *(Uint32*)pixel;
  93. break;
  94. }
  95. SDL_GetRGBA(pixel_value,shape->format,&r,&g,&b,&alpha);
  96. switch(mode.mode) {
  97. case(ShapeModeDefault):
  98. mask_value = (alpha >= 1 ? 1 : 0);
  99. break;
  100. case(ShapeModeBinarizeAlpha):
  101. mask_value = (alpha >= mode.parameters.binarizationCutoff ? 1 : 0);
  102. break;
  103. case(ShapeModeReverseBinarizeAlpha):
  104. mask_value = (alpha <= mode.parameters.binarizationCutoff ? 1 : 0);
  105. break;
  106. case(ShapeModeColorKey):
  107. key = mode.parameters.colorKey;
  108. mask_value = ((key.r != r || key.g != g || key.b != b) ? 1 : 0);
  109. break;
  110. }
  111. bitmap_scanline[x / ppb] |= mask_value << (x % ppb);
  112. }
  113. }
  114. if(SDL_MUSTLOCK(shape))
  115. SDL_UnlockSurface(shape);
  116. }
  117. static SDL_ShapeTree*
  118. RecursivelyCalculateShapeTree(SDL_WindowShapeMode mode,SDL_Surface* mask,SDL_Rect dimensions) {
  119. int x = 0,y = 0;
  120. Uint8* pixel = NULL;
  121. Uint32 pixel_value = 0;
  122. Uint8 r = 0,g = 0,b = 0,a = 0;
  123. SDL_bool pixel_opaque = SDL_FALSE;
  124. int last_opaque = -1;
  125. SDL_Color key;
  126. SDL_ShapeTree* result = (SDL_ShapeTree*)SDL_malloc(sizeof(SDL_ShapeTree));
  127. SDL_Rect next = {0,0,0,0};
  128. for(y=dimensions.y;y<dimensions.y + dimensions.h;y++) {
  129. for(x=dimensions.x;x<dimensions.x + dimensions.w;x++) {
  130. pixel_value = 0;
  131. pixel = (Uint8 *)(mask->pixels) + (y*mask->pitch) + (x*mask->format->BytesPerPixel);
  132. switch(mask->format->BytesPerPixel) {
  133. case(1):
  134. pixel_value = *pixel;
  135. break;
  136. case(2):
  137. pixel_value = *(Uint16*)pixel;
  138. break;
  139. case(3):
  140. pixel_value = *(Uint32*)pixel & (~mask->format->Amask);
  141. break;
  142. case(4):
  143. pixel_value = *(Uint32*)pixel;
  144. break;
  145. }
  146. SDL_GetRGBA(pixel_value,mask->format,&r,&g,&b,&a);
  147. switch(mode.mode) {
  148. case(ShapeModeDefault):
  149. pixel_opaque = (a >= 1 ? SDL_TRUE : SDL_FALSE);
  150. break;
  151. case(ShapeModeBinarizeAlpha):
  152. pixel_opaque = (a >= mode.parameters.binarizationCutoff ? SDL_TRUE : SDL_FALSE);
  153. break;
  154. case(ShapeModeReverseBinarizeAlpha):
  155. pixel_opaque = (a <= mode.parameters.binarizationCutoff ? SDL_TRUE : SDL_FALSE);
  156. break;
  157. case(ShapeModeColorKey):
  158. key = mode.parameters.colorKey;
  159. pixel_opaque = ((key.r != r || key.g != g || key.b != b) ? SDL_TRUE : SDL_FALSE);
  160. break;
  161. }
  162. if(last_opaque == -1)
  163. last_opaque = pixel_opaque;
  164. if(last_opaque != pixel_opaque) {
  165. const int halfwidth = dimensions.w / 2;
  166. const int halfheight = dimensions.h / 2;
  167. result->kind = QuadShape;
  168. next.x = dimensions.x;
  169. next.y = dimensions.y;
  170. next.w = halfwidth;
  171. next.h = halfheight;
  172. result->data.children.upleft = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next);
  173. next.x = dimensions.x + halfwidth;
  174. next.w = dimensions.w - halfwidth;
  175. result->data.children.upright = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next);
  176. next.x = dimensions.x;
  177. next.w = halfwidth;
  178. next.y = dimensions.y + halfheight;
  179. next.h = dimensions.h - halfheight;
  180. result->data.children.downleft = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next);
  181. next.x = dimensions.x + halfwidth;
  182. next.w = dimensions.w - halfwidth;
  183. result->data.children.downright = (struct SDL_ShapeTree *)RecursivelyCalculateShapeTree(mode,mask,next);
  184. return result;
  185. }
  186. }
  187. }
  188. /* If we never recursed, all the pixels in this quadrant have the same "value". */
  189. result->kind = (last_opaque == SDL_TRUE ? OpaqueShape : TransparentShape);
  190. result->data.shape = dimensions;
  191. return result;
  192. }
  193. SDL_ShapeTree*
  194. SDL_CalculateShapeTree(SDL_WindowShapeMode mode,SDL_Surface* shape)
  195. {
  196. SDL_Rect dimensions;
  197. SDL_ShapeTree* result = NULL;
  198. dimensions.x = 0;
  199. dimensions.y = 0;
  200. dimensions.w = shape->w;
  201. dimensions.h = shape->h;
  202. if(SDL_MUSTLOCK(shape))
  203. SDL_LockSurface(shape);
  204. result = RecursivelyCalculateShapeTree(mode,shape,dimensions);
  205. if(SDL_MUSTLOCK(shape))
  206. SDL_UnlockSurface(shape);
  207. return result;
  208. }
  209. void
  210. SDL_TraverseShapeTree(SDL_ShapeTree *tree,SDL_TraversalFunction function,void* closure)
  211. {
  212. SDL_assert(tree != NULL);
  213. if(tree->kind == QuadShape) {
  214. SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.upleft,function,closure);
  215. SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.upright,function,closure);
  216. SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.downleft,function,closure);
  217. SDL_TraverseShapeTree((SDL_ShapeTree *)tree->data.children.downright,function,closure);
  218. }
  219. else
  220. function(tree,closure);
  221. }
  222. void
  223. SDL_FreeShapeTree(SDL_ShapeTree** shape_tree)
  224. {
  225. if((*shape_tree)->kind == QuadShape) {
  226. SDL_FreeShapeTree((SDL_ShapeTree **)(char*)&(*shape_tree)->data.children.upleft);
  227. SDL_FreeShapeTree((SDL_ShapeTree **)(char*)&(*shape_tree)->data.children.upright);
  228. SDL_FreeShapeTree((SDL_ShapeTree **)(char*)&(*shape_tree)->data.children.downleft);
  229. SDL_FreeShapeTree((SDL_ShapeTree **)(char*)&(*shape_tree)->data.children.downright);
  230. }
  231. SDL_free(*shape_tree);
  232. *shape_tree = NULL;
  233. }
  234. int
  235. SDL_SetWindowShape(SDL_Window *window,SDL_Surface *shape,SDL_WindowShapeMode *shape_mode)
  236. {
  237. int result;
  238. if(window == NULL || !SDL_IsShapedWindow(window))
  239. /* The window given was not a shapeable window. */
  240. return SDL_NONSHAPEABLE_WINDOW;
  241. if(shape == NULL)
  242. /* Invalid shape argument. */
  243. return SDL_INVALID_SHAPE_ARGUMENT;
  244. if(shape_mode != NULL)
  245. window->shaper->mode = *shape_mode;
  246. result = SDL_GetVideoDevice()->shape_driver.SetWindowShape(window->shaper,shape,shape_mode);
  247. window->shaper->hasshape = SDL_TRUE;
  248. if(window->shaper->userx != 0 && window->shaper->usery != 0) {
  249. SDL_SetWindowPosition(window,window->shaper->userx,window->shaper->usery);
  250. window->shaper->userx = 0;
  251. window->shaper->usery = 0;
  252. }
  253. return result;
  254. }
  255. static SDL_bool
  256. SDL_WindowHasAShape(SDL_Window *window)
  257. {
  258. if (window == NULL || !SDL_IsShapedWindow(window))
  259. return SDL_FALSE;
  260. return window->shaper->hasshape;
  261. }
  262. int
  263. SDL_GetShapedWindowMode(SDL_Window *window,SDL_WindowShapeMode *shape_mode)
  264. {
  265. if(window != NULL && SDL_IsShapedWindow(window)) {
  266. if(shape_mode == NULL) {
  267. if(SDL_WindowHasAShape(window))
  268. /* The window given has a shape. */
  269. return 0;
  270. else
  271. /* The window given is shapeable but lacks a shape. */
  272. return SDL_WINDOW_LACKS_SHAPE;
  273. }
  274. else {
  275. *shape_mode = window->shaper->mode;
  276. return 0;
  277. }
  278. }
  279. else
  280. /* The window given is not a valid shapeable window. */
  281. return SDL_NONSHAPEABLE_WINDOW;
  282. }