🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
3.3 KiB

  1. /*
  2. Copyright (C) 1997-2023 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 <stdlib.h>
  12. #include <stdio.h>
  13. #include <time.h>
  14. #include "SDL_test_common.h"
  15. #ifdef __EMSCRIPTEN__
  16. #include <emscripten/emscripten.h>
  17. #endif
  18. static SDLTest_CommonState *state;
  19. int i, done;
  20. SDL_Rect rect;
  21. SDL_Event event;
  22. static void
  23. DrawRects(SDL_Renderer *renderer)
  24. {
  25. SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
  26. SDL_RenderFillRect(renderer, &rect);
  27. }
  28. static void
  29. loop()
  30. {
  31. /* Check for events */
  32. while (SDL_PollEvent(&event)) {
  33. SDLTest_CommonEvent(state, &event, &done);
  34. switch (event.type) {
  35. case SDL_MOUSEMOTION:
  36. {
  37. rect.x += event.motion.xrel;
  38. rect.y += event.motion.yrel;
  39. } break;
  40. }
  41. }
  42. for (i = 0; i < state->num_windows; ++i) {
  43. SDL_Rect viewport;
  44. SDL_Renderer *renderer = state->renderers[i];
  45. if (state->windows[i] == NULL) {
  46. continue;
  47. }
  48. SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
  49. SDL_RenderClear(renderer);
  50. /* Wrap the cursor rectangle at the screen edges to keep it visible */
  51. SDL_RenderGetViewport(renderer, &viewport);
  52. if (rect.x < viewport.x) {
  53. rect.x += viewport.w;
  54. }
  55. if (rect.y < viewport.y) {
  56. rect.y += viewport.h;
  57. }
  58. if (rect.x > viewport.x + viewport.w) {
  59. rect.x -= viewport.w;
  60. }
  61. if (rect.y > viewport.y + viewport.h) {
  62. rect.y -= viewport.h;
  63. }
  64. DrawRects(renderer);
  65. SDL_RenderPresent(renderer);
  66. }
  67. #ifdef __EMSCRIPTEN__
  68. if (done) {
  69. emscripten_cancel_main_loop();
  70. }
  71. #endif
  72. }
  73. int main(int argc, char *argv[])
  74. {
  75. /* Enable standard application logging */
  76. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  77. /* Initialize test framework */
  78. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  79. if (state == NULL) {
  80. return 1;
  81. }
  82. for (i = 1; i < argc; ++i) {
  83. SDLTest_CommonArg(state, i);
  84. }
  85. if (!SDLTest_CommonInit(state)) {
  86. return 2;
  87. }
  88. /* Create the windows and initialize the renderers */
  89. for (i = 0; i < state->num_windows; ++i) {
  90. SDL_Renderer *renderer = state->renderers[i];
  91. SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
  92. SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  93. SDL_RenderClear(renderer);
  94. }
  95. srand((unsigned int)time(NULL));
  96. if (SDL_SetRelativeMouseMode(SDL_TRUE) < 0) {
  97. return 3;
  98. }
  99. rect.x = DEFAULT_WINDOW_WIDTH / 2;
  100. rect.y = DEFAULT_WINDOW_HEIGHT / 2;
  101. rect.w = 10;
  102. rect.h = 10;
  103. /* Main render loop */
  104. done = 0;
  105. #ifdef __EMSCRIPTEN__
  106. emscripten_set_main_loop(loop, 0, 1);
  107. #else
  108. while (!done) {
  109. loop();
  110. }
  111. #endif
  112. SDLTest_CommonQuit(state);
  113. return 0;
  114. }
  115. /* vi: set ts=4 sw=4 expandtab: */