testrelative.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. Copyright (C) 1997-2024 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. /* Simple program: Test relative mouse motion */
  11. #include <SDL3/SDL_test.h>
  12. #include <SDL3/SDL_test_common.h>
  13. #include <SDL3/SDL_main.h>
  14. #ifdef SDL_PLATFORM_EMSCRIPTEN
  15. #include <emscripten/emscripten.h>
  16. #endif
  17. static SDLTest_CommonState *state;
  18. static int i, done;
  19. static SDL_FRect rect;
  20. static SDL_Event event;
  21. static bool warp;
  22. static bool raw;
  23. static void DrawRects(SDL_Renderer *renderer)
  24. {
  25. SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
  26. SDL_RenderFillRect(renderer, &rect);
  27. SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  28. SDLTest_DrawString(renderer, 0.f, 0.f, "Relative Mode: Enabled");
  29. }
  30. static void CenterMouse(void)
  31. {
  32. /* Warp the mouse back to the center of the window with input focus to use the
  33. * center point for calculating future motion deltas.
  34. *
  35. * NOTE: DO NOT DO THIS IN REAL APPS/GAMES!
  36. *
  37. * This is an outdated method of handling relative pointer motion, and
  38. * may not work properly, if at all, on some platforms. It is here *only*
  39. * for testing the warp emulation code path internal to SDL.
  40. *
  41. * Relative mouse mode should be used instead!
  42. */
  43. SDL_Window *window = SDL_GetKeyboardFocus();
  44. if (window) {
  45. int w, h;
  46. float cx, cy;
  47. SDL_GetWindowSize(window, &w, &h);
  48. cx = (float)w / 2.f;
  49. cy = (float)h / 2.f;
  50. SDL_WarpMouseInWindow(window, cx, cy);
  51. }
  52. }
  53. static void loop(void)
  54. {
  55. /* Check for events */
  56. while (SDL_PollEvent(&event)) {
  57. SDLTest_CommonEvent(state, &event, &done);
  58. switch (event.type) {
  59. case SDL_EVENT_WINDOW_FOCUS_GAINED:
  60. if (warp) {
  61. /* This should activate relative mode for warp emulation, unless disabled via a hint. */
  62. CenterMouse();
  63. }
  64. break;
  65. case SDL_EVENT_KEY_DOWN:
  66. if (event.key.key == SDLK_C) {
  67. /* If warp emulation is active, showing the cursor should turn
  68. * relative mode off, and it should re-activate after a warp
  69. * when hidden again.
  70. */
  71. if (SDL_CursorVisible()) {
  72. SDL_HideCursor();
  73. } else {
  74. SDL_ShowCursor();
  75. }
  76. }
  77. break;
  78. case SDL_EVENT_MOUSE_MOTION:
  79. {
  80. if (!raw) {
  81. rect.x += event.motion.xrel;
  82. rect.y += event.motion.yrel;
  83. if (warp) {
  84. CenterMouse();
  85. }
  86. }
  87. } break;
  88. case SDL_EVENT_MOUSE_RAW_MOTION:
  89. {
  90. rect.x += event.maxis.dx / event.maxis.ux;
  91. rect.y += event.maxis.dy / event.maxis.uy;
  92. } break;
  93. default:
  94. break;
  95. }
  96. }
  97. for (i = 0; i < state->num_windows; ++i) {
  98. SDL_Rect viewport;
  99. SDL_Renderer *renderer = state->renderers[i];
  100. if (state->windows[i] == NULL) {
  101. continue;
  102. }
  103. SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
  104. SDL_RenderClear(renderer);
  105. /* Wrap the cursor rectangle at the screen edges to keep it visible */
  106. SDL_GetRenderViewport(renderer, &viewport);
  107. if (rect.x < viewport.x) {
  108. rect.x += viewport.w;
  109. }
  110. if (rect.y < viewport.y) {
  111. rect.y += viewport.h;
  112. }
  113. if (rect.x > viewport.x + viewport.w) {
  114. rect.x -= viewport.w;
  115. }
  116. if (rect.y > viewport.y + viewport.h) {
  117. rect.y -= viewport.h;
  118. }
  119. DrawRects(renderer);
  120. SDL_RenderPresent(renderer);
  121. }
  122. #ifdef SDL_PLATFORM_EMSCRIPTEN
  123. if (done) {
  124. emscripten_cancel_main_loop();
  125. }
  126. #endif
  127. }
  128. int main(int argc, char *argv[])
  129. {
  130. /* Initialize test framework */
  131. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  132. if (!state) {
  133. return 1;
  134. }
  135. /* Parse commandline */
  136. for (i = 1; i < argc;) {
  137. int consumed;
  138. consumed = SDLTest_CommonArg(state, i);
  139. if (consumed == 0) {
  140. consumed = -1;
  141. if (SDL_strcasecmp(argv[i], "--warp") == 0) {
  142. warp = true;
  143. consumed = 1;
  144. } else if (SDL_strcasecmp(argv[i], "--raw") == 0) {
  145. raw = true;
  146. consumed = 1;
  147. }
  148. }
  149. if (consumed < 0) {
  150. static const char *options[] = {
  151. "[--warp]",
  152. "[--raw]",
  153. NULL
  154. };
  155. SDLTest_CommonLogUsage(state, argv[0], options);
  156. return 1;
  157. }
  158. i += consumed;
  159. }
  160. if (!SDLTest_CommonInit(state)) {
  161. return 2;
  162. }
  163. /* Create the windows and initialize the renderers */
  164. for (i = 0; i < state->num_windows; ++i) {
  165. SDL_Renderer *renderer = state->renderers[i];
  166. SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
  167. SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  168. SDL_RenderClear(renderer);
  169. }
  170. /* If warp mode is activated, the cursor will be repeatedly warped back to
  171. * the center of the window to simulate the behavior of older games. The cursor
  172. * is initially hidden in this case to trigger the warp emulation unless it has
  173. * been explicitly disabled via a hint.
  174. *
  175. * Otherwise, try to activate relative mode.
  176. */
  177. if (warp) {
  178. SDL_HideCursor();
  179. } else {
  180. for (i = 0; i < state->num_windows; ++i) {
  181. SDL_SetWindowRelativeMouseMode(state->windows[i], true);
  182. }
  183. }
  184. if (raw) {
  185. SDL_SetEventEnabled(SDL_EVENT_MOUSE_RAW_MOTION, true);
  186. }
  187. rect.x = DEFAULT_WINDOW_WIDTH / 2;
  188. rect.y = DEFAULT_WINDOW_HEIGHT / 2;
  189. rect.w = 10;
  190. rect.h = 10;
  191. /* Main render loop */
  192. done = 0;
  193. #ifdef SDL_PLATFORM_EMSCRIPTEN
  194. emscripten_set_main_loop(loop, 0, 1);
  195. #else
  196. while (!done) {
  197. loop();
  198. }
  199. #endif
  200. SDLTest_CommonQuit(state);
  201. return 0;
  202. }