🛠️🐜 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.

126 lines
3.2 KiB

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