testcustomcursor.c 4.6 KB

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