testrelative.c 6.0 KB

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