testcustomcursor.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. Copyright (C) 1997-2019 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. 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 int current_cursor;
  127. static int show_cursor;
  128. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  129. static void
  130. quit(int rc)
  131. {
  132. SDLTest_CommonQuit(state);
  133. exit(rc);
  134. }
  135. void
  136. loop()
  137. {
  138. int i;
  139. SDL_Event event;
  140. /* Check for events */
  141. while (SDL_PollEvent(&event)) {
  142. SDLTest_CommonEvent(state, &event, &done);
  143. if (event.type == SDL_MOUSEBUTTONDOWN) {
  144. if (event.button.button == SDL_BUTTON_LEFT) {
  145. ++current_cursor;
  146. if (current_cursor == SDL_arraysize(cursors)) {
  147. current_cursor = 0;
  148. }
  149. SDL_SetCursor(cursors[current_cursor]);
  150. } else {
  151. show_cursor = !show_cursor;
  152. SDL_ShowCursor(show_cursor);
  153. }
  154. }
  155. }
  156. for (i = 0; i < state->num_windows; ++i) {
  157. SDL_Renderer *renderer = state->renderers[i];
  158. SDL_RenderClear(renderer);
  159. SDL_RenderPresent(renderer);
  160. }
  161. #ifdef __EMSCRIPTEN__
  162. if (done) {
  163. emscripten_cancel_main_loop();
  164. }
  165. #endif
  166. }
  167. int
  168. main(int argc, char *argv[])
  169. {
  170. int i;
  171. const char *color_cursor = NULL;
  172. /* Enable standard application logging */
  173. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  174. /* Initialize test framework */
  175. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  176. if (!state) {
  177. return 1;
  178. }
  179. for (i = 1; i < argc;) {
  180. int consumed;
  181. consumed = SDLTest_CommonArg(state, i);
  182. if (consumed == 0) {
  183. color_cursor = argv[i];
  184. break;
  185. }
  186. if (consumed < 0) {
  187. SDLTest_CommonLogUsage(state, argv[0], NULL);
  188. quit(1);
  189. }
  190. i += consumed;
  191. }
  192. if (!SDLTest_CommonInit(state)) {
  193. quit(2);
  194. }
  195. for (i = 0; i < state->num_windows; ++i) {
  196. SDL_Renderer *renderer = state->renderers[i];
  197. SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  198. SDL_RenderClear(renderer);
  199. }
  200. if (color_cursor) {
  201. cursors[0] = init_color_cursor(color_cursor);
  202. } else {
  203. cursors[0] = init_system_cursor(arrow);
  204. }
  205. if (!cursors[0]) {
  206. SDL_Log("Error, couldn't create cursor\n");
  207. quit(2);
  208. }
  209. for (i = 0; i < SDL_NUM_SYSTEM_CURSORS; ++i) {
  210. cursors[1+i] = SDL_CreateSystemCursor((SDL_SystemCursor)i);
  211. if (!cursors[1+i]) {
  212. SDL_Log("Error, couldn't create system cursor %d\n", i);
  213. quit(2);
  214. }
  215. }
  216. SDL_SetCursor(cursors[0]);
  217. /* Main render loop */
  218. done = 0;
  219. #ifdef __EMSCRIPTEN__
  220. emscripten_set_main_loop(loop, 0, 1);
  221. #else
  222. while (!done) {
  223. loop();
  224. }
  225. #endif
  226. for (i = 0; i < SDL_arraysize(cursors); ++i) {
  227. SDL_FreeCursor(cursors[i]);
  228. }
  229. quit(0);
  230. /* keep the compiler happy ... */
  231. return(0);
  232. }
  233. /* vi: set ts=4 sw=4 expandtab: */