SDL_shape.c 11 KB

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