testcustomcursor.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. Copyright (C) 1997-2017 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. cursor = SDL_CreateColorCursor(surface, 0, 0);
  68. SDL_FreeSurface(surface);
  69. }
  70. return cursor;
  71. }
  72. static SDL_Cursor*
  73. init_system_cursor(const char *image[])
  74. {
  75. int i, row, col;
  76. Uint8 data[4*32];
  77. Uint8 mask[4*32];
  78. int hot_x, hot_y;
  79. i = -1;
  80. for (row=0; row<32; ++row) {
  81. for (col=0; col<32; ++col) {
  82. if (col % 8) {
  83. data[i] <<= 1;
  84. mask[i] <<= 1;
  85. } else {
  86. ++i;
  87. data[i] = mask[i] = 0;
  88. }
  89. switch (image[4+row][col]) {
  90. case 'X':
  91. data[i] |= 0x01;
  92. mask[i] |= 0x01;
  93. break;
  94. case '.':
  95. mask[i] |= 0x01;
  96. break;
  97. case ' ':
  98. break;
  99. }
  100. }
  101. }
  102. sscanf(image[4+row], "%d,%d", &hot_x, &hot_y);
  103. return SDL_CreateCursor(data, mask, 32, 32, hot_x, hot_y);
  104. }
  105. static SDLTest_CommonState *state;
  106. int done;
  107. SDL_Cursor *cursor = NULL;
  108. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  109. static void
  110. quit(int rc)
  111. {
  112. SDLTest_CommonQuit(state);
  113. exit(rc);
  114. }
  115. void
  116. loop()
  117. {
  118. int i;
  119. SDL_Event event;
  120. /* Check for events */
  121. while (SDL_PollEvent(&event)) {
  122. SDLTest_CommonEvent(state, &event, &done);
  123. }
  124. for (i = 0; i < state->num_windows; ++i) {
  125. SDL_Renderer *renderer = state->renderers[i];
  126. SDL_RenderClear(renderer);
  127. SDL_RenderPresent(renderer);
  128. }
  129. #ifdef __EMSCRIPTEN__
  130. if (done) {
  131. emscripten_cancel_main_loop();
  132. }
  133. #endif
  134. }
  135. int
  136. main(int argc, char *argv[])
  137. {
  138. int i;
  139. const char *color_cursor = NULL;
  140. /* Enable standard application logging */
  141. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  142. /* Initialize test framework */
  143. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  144. if (!state) {
  145. return 1;
  146. }
  147. for (i = 1; i < argc;) {
  148. int consumed;
  149. consumed = SDLTest_CommonArg(state, i);
  150. if (consumed == 0) {
  151. color_cursor = argv[i];
  152. break;
  153. }
  154. if (consumed < 0) {
  155. SDL_Log("Usage: %s %s\n", argv[0], SDLTest_CommonUsage(state));
  156. quit(1);
  157. }
  158. i += consumed;
  159. }
  160. if (!SDLTest_CommonInit(state)) {
  161. quit(2);
  162. }
  163. for (i = 0; i < state->num_windows; ++i) {
  164. SDL_Renderer *renderer = state->renderers[i];
  165. SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  166. SDL_RenderClear(renderer);
  167. }
  168. if (color_cursor) {
  169. cursor = init_color_cursor(color_cursor);
  170. } else {
  171. cursor = init_system_cursor(arrow);
  172. }
  173. if (!cursor) {
  174. SDL_Log("Error, couldn't create cursor\n");
  175. quit(2);
  176. }
  177. SDL_SetCursor(cursor);
  178. /* Main render loop */
  179. done = 0;
  180. #ifdef __EMSCRIPTEN__
  181. emscripten_set_main_loop(loop, 0, 1);
  182. #else
  183. while (!done) {
  184. loop();
  185. }
  186. #endif
  187. SDL_FreeCursor(cursor);
  188. quit(0);
  189. /* keep the compiler happy ... */
  190. return(0);
  191. }