🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

299 lines
8.6 KiB

  1. /*
  2. Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely.
  9. */
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #ifdef __EMSCRIPTEN__
  13. #include <emscripten/emscripten.h>
  14. #endif
  15. #include "SDL_test_common.h"
  16. /* Stolen from the mailing list */
  17. /* Creates a new mouse cursor from an XPM */
  18. /* XPM */
  19. static const char *arrow[] = {
  20. /* width height num_colors chars_per_pixel */
  21. " 32 32 3 1",
  22. /* colors */
  23. "X c #000000",
  24. ". c #ffffff",
  25. " c None",
  26. /* pixels */
  27. "X ",
  28. "XX ",
  29. "X.X ",
  30. "X..X ",
  31. "X...X ",
  32. "X....X ",
  33. "X.....X ",
  34. "X......X ",
  35. "X.......X ",
  36. "X........X ",
  37. "X.....XXXXX ",
  38. "X..X..X ",
  39. "X.X X..X ",
  40. "XX X..X ",
  41. "X X..X ",
  42. " X..X ",
  43. " X..X ",
  44. " X..X ",
  45. " XX ",
  46. " ",
  47. " ",
  48. " ",
  49. " ",
  50. " ",
  51. " ",
  52. " ",
  53. " ",
  54. " ",
  55. " ",
  56. " ",
  57. " ",
  58. " ",
  59. "0,0"
  60. };
  61. static SDL_Cursor*
  62. init_color_cursor(const char *file)
  63. {
  64. SDL_Cursor *cursor = NULL;
  65. SDL_Surface *surface = SDL_LoadBMP(file);
  66. if (surface) {
  67. if (surface->format->palette) {
  68. SDL_SetColorKey(surface, 1, *(Uint8 *) surface->pixels);
  69. } else {
  70. switch (surface->format->BitsPerPixel) {
  71. case 15:
  72. SDL_SetColorKey(surface, 1, (*(Uint16 *)surface->pixels) & 0x00007FFF);
  73. break;
  74. case 16:
  75. SDL_SetColorKey(surface, 1, *(Uint16 *)surface->pixels);
  76. break;
  77. case 24:
  78. SDL_SetColorKey(surface, 1, (*(Uint32 *)surface->pixels) & 0x00FFFFFF);
  79. break;
  80. case 32:
  81. SDL_SetColorKey(surface, 1, *(Uint32 *)surface->pixels);
  82. break;
  83. }
  84. }
  85. cursor = SDL_CreateColorCursor(surface, 0, 0);
  86. SDL_FreeSurface(surface);
  87. }
  88. return cursor;
  89. }
  90. static SDL_Cursor*
  91. init_system_cursor(const char *image[])
  92. {
  93. int i, row, col;
  94. Uint8 data[4*32];
  95. Uint8 mask[4*32];
  96. int hot_x, hot_y;
  97. i = -1;
  98. for (row=0; row<32; ++row) {
  99. for (col=0; col<32; ++col) {
  100. if (col % 8) {
  101. data[i] <<= 1;
  102. mask[i] <<= 1;
  103. } else {
  104. ++i;
  105. data[i] = mask[i] = 0;
  106. }
  107. switch (image[4+row][col]) {
  108. case 'X':
  109. data[i] |= 0x01;
  110. mask[i] |= 0x01;
  111. break;
  112. case '.':
  113. mask[i] |= 0x01;
  114. break;
  115. case ' ':
  116. break;
  117. }
  118. }
  119. }
  120. SDL_sscanf(image[4+row], "%d,%d", &hot_x, &hot_y);
  121. return SDL_CreateCursor(data, mask, 32, 32, hot_x, hot_y);
  122. }
  123. static SDLTest_CommonState *state;
  124. int done;
  125. static SDL_Cursor *cursors[1+SDL_NUM_SYSTEM_CURSORS];
  126. static SDL_SystemCursor cursor_types[1+SDL_NUM_SYSTEM_CURSORS];
  127. static int num_cursors;
  128. static int current_cursor;
  129. static int show_cursor;
  130. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  131. static void
  132. quit(int rc)
  133. {
  134. SDLTest_CommonQuit(state);
  135. exit(rc);
  136. }
  137. void
  138. loop()
  139. {
  140. int i;
  141. SDL_Event event;
  142. /* Check for events */
  143. while (SDL_PollEvent(&event)) {
  144. SDLTest_CommonEvent(state, &event, &done);
  145. if (event.type == SDL_MOUSEBUTTONDOWN) {
  146. if (event.button.button == SDL_BUTTON_LEFT) {
  147. if (num_cursors == 0) {
  148. continue;
  149. }
  150. ++current_cursor;
  151. if (current_cursor == num_cursors) {
  152. current_cursor = 0;
  153. }
  154. SDL_SetCursor(cursors[current_cursor]);
  155. switch ((int)cursor_types[current_cursor]) {
  156. case (SDL_SystemCursor)-1: SDL_Log("Custom cursor"); break;
  157. case SDL_SYSTEM_CURSOR_ARROW: SDL_Log("Arrow"); break;
  158. case SDL_SYSTEM_CURSOR_IBEAM: SDL_Log("I-beam"); break;
  159. case SDL_SYSTEM_CURSOR_WAIT: SDL_Log("Wait"); break;
  160. case SDL_SYSTEM_CURSOR_CROSSHAIR: SDL_Log("Crosshair"); break;
  161. case SDL_SYSTEM_CURSOR_WAITARROW: SDL_Log("Small wait cursor (or Wait if not available)"); break;
  162. case SDL_SYSTEM_CURSOR_SIZENWSE: SDL_Log("Double arrow pointing northwest and southeast"); break;
  163. case SDL_SYSTEM_CURSOR_SIZENESW: SDL_Log("Double arrow pointing northeast and southwest"); break;
  164. case SDL_SYSTEM_CURSOR_SIZEWE: SDL_Log("Double arrow pointing west and east"); break;
  165. case SDL_SYSTEM_CURSOR_SIZENS: SDL_Log("Double arrow pointing north and south"); break;
  166. case SDL_SYSTEM_CURSOR_SIZEALL: SDL_Log("Four pointed arrow pointing north, south, east, and west"); break;
  167. case SDL_SYSTEM_CURSOR_NO: SDL_Log("Slashed circle or crossbones"); break;
  168. case SDL_SYSTEM_CURSOR_HAND: SDL_Log("Hand"); break;
  169. default: SDL_Log("UNKNOWN CURSOR TYPE, FIX THIS PROGRAM."); break;
  170. }
  171. } else {
  172. show_cursor = !show_cursor;
  173. SDL_ShowCursor(show_cursor);
  174. }
  175. }
  176. }
  177. for (i = 0; i < state->num_windows; ++i) {
  178. SDL_Renderer *renderer = state->renderers[i];
  179. SDL_RenderClear(renderer);
  180. SDL_RenderPresent(renderer);
  181. }
  182. #ifdef __EMSCRIPTEN__
  183. if (done) {
  184. emscripten_cancel_main_loop();
  185. }
  186. #endif
  187. }
  188. int
  189. main(int argc, char *argv[])
  190. {
  191. int i;
  192. const char *color_cursor = NULL;
  193. /* Enable standard application logging */
  194. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  195. /* Initialize test framework */
  196. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  197. if (!state) {
  198. return 1;
  199. }
  200. for (i = 1; i < argc;) {
  201. int consumed;
  202. consumed = SDLTest_CommonArg(state, i);
  203. if (consumed == 0) {
  204. color_cursor = argv[i];
  205. break;
  206. }
  207. if (consumed < 0) {
  208. SDLTest_CommonLogUsage(state, argv[0], NULL);
  209. quit(1);
  210. }
  211. i += consumed;
  212. }
  213. if (!SDLTest_CommonInit(state)) {
  214. quit(2);
  215. }
  216. for (i = 0; i < state->num_windows; ++i) {
  217. SDL_Renderer *renderer = state->renderers[i];
  218. SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  219. SDL_RenderClear(renderer);
  220. }
  221. num_cursors = 0;
  222. if (color_cursor) {
  223. SDL_Cursor *cursor = init_color_cursor(color_cursor);
  224. if (cursor) {
  225. cursors[num_cursors] = cursor;
  226. cursor_types[num_cursors] = (SDL_SystemCursor)-1;
  227. num_cursors++;
  228. }
  229. } else {
  230. SDL_Cursor *cursor = init_system_cursor(arrow);
  231. if (cursor) {
  232. cursors[num_cursors] = cursor;
  233. cursor_types[num_cursors] = (SDL_SystemCursor)-1;
  234. num_cursors++;
  235. }
  236. }
  237. for (i = 0; i < SDL_NUM_SYSTEM_CURSORS; ++i) {
  238. SDL_Cursor *cursor = SDL_CreateSystemCursor((SDL_SystemCursor)i);
  239. if (cursor) {
  240. cursors[num_cursors] = cursor;
  241. cursor_types[num_cursors] = i;
  242. num_cursors++;
  243. }
  244. }
  245. if (num_cursors > 0) {
  246. SDL_SetCursor(cursors[0]);
  247. }
  248. show_cursor = SDL_ShowCursor(SDL_QUERY);
  249. /* Main render loop */
  250. done = 0;
  251. #ifdef __EMSCRIPTEN__
  252. emscripten_set_main_loop(loop, 0, 1);
  253. #else
  254. while (!done) {
  255. loop();
  256. }
  257. #endif
  258. for (i = 0; i < num_cursors; ++i) {
  259. SDL_FreeCursor(cursors[i]);
  260. }
  261. quit(0);
  262. /* keep the compiler happy ... */
  263. return(0);
  264. }
  265. /* vi: set ts=4 sw=4 expandtab: */