testrelative.c 5.8 KB

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