testmouse.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. Copyright (C) 1997-2022 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. #include "SDL.h"
  11. #ifdef __EMSCRIPTEN__
  12. #include <emscripten/emscripten.h>
  13. #endif
  14. #include <stdlib.h> /* exit() */
  15. #ifdef __IPHONEOS__
  16. #define SCREEN_WIDTH 320
  17. #define SCREEN_HEIGHT 480
  18. #else
  19. #define SCREEN_WIDTH 640
  20. #define SCREEN_HEIGHT 480
  21. #endif
  22. static SDL_Window *window;
  23. typedef struct _Line {
  24. struct _Line *next;
  25. int x1, y1, x2, y2;
  26. Uint8 r, g, b;
  27. } Line;
  28. static Line *active = NULL;
  29. static Line *lines = NULL;
  30. static int buttons = 0;
  31. static SDL_bool done = SDL_FALSE;
  32. void
  33. DrawLine(SDL_Renderer * renderer, Line * line)
  34. {
  35. SDL_SetRenderDrawColor(renderer, line->r, line->g, line->b, 255);
  36. SDL_RenderDrawLine(renderer, line->x1, line->y1, line->x2, line->y2);
  37. }
  38. void
  39. DrawLines(SDL_Renderer * renderer)
  40. {
  41. Line *next = lines;
  42. while (next != NULL) {
  43. DrawLine(renderer, next);
  44. next = next->next;
  45. }
  46. }
  47. void
  48. AppendLine(Line *line)
  49. {
  50. if (lines) {
  51. Line *next = lines;
  52. while (next->next != NULL) {
  53. next = next->next;
  54. }
  55. next->next = line;
  56. } else {
  57. lines = line;
  58. }
  59. }
  60. void
  61. loop(void *arg)
  62. {
  63. SDL_Renderer *renderer = (SDL_Renderer *)arg;
  64. SDL_Event event;
  65. /* Check for events */
  66. while (SDL_PollEvent(&event)) {
  67. switch (event.type) {
  68. case SDL_MOUSEMOTION:
  69. if (!active)
  70. break;
  71. active->x2 = event.motion.x;
  72. active->y2 = event.motion.y;
  73. break;
  74. case SDL_MOUSEBUTTONDOWN:
  75. if (!active) {
  76. active = SDL_calloc(1, sizeof(*active));
  77. active->x1 = active->x2 = event.button.x;
  78. active->y1 = active->y2 = event.button.y;
  79. }
  80. switch (event.button.button) {
  81. case SDL_BUTTON_LEFT: active->r = 255; buttons |= SDL_BUTTON_LMASK; break;
  82. case SDL_BUTTON_MIDDLE: active->g = 255; buttons |= SDL_BUTTON_MMASK; break;
  83. case SDL_BUTTON_RIGHT: active->b = 255; buttons |= SDL_BUTTON_RMASK; break;
  84. case SDL_BUTTON_X1: active->r = 255; active->b = 255; buttons |= SDL_BUTTON_X1MASK; break;
  85. case SDL_BUTTON_X2: active->g = 255; active->b = 255; buttons |= SDL_BUTTON_X2MASK; break;
  86. }
  87. break;
  88. case SDL_MOUSEBUTTONUP:
  89. if (!active)
  90. break;
  91. switch (event.button.button) {
  92. case SDL_BUTTON_LEFT: buttons &= ~SDL_BUTTON_LMASK; break;
  93. case SDL_BUTTON_MIDDLE: buttons &= ~SDL_BUTTON_MMASK; break;
  94. case SDL_BUTTON_RIGHT: buttons &= ~SDL_BUTTON_RMASK; break;
  95. case SDL_BUTTON_X1: buttons &= ~SDL_BUTTON_X1MASK; break;
  96. case SDL_BUTTON_X2: buttons &= ~SDL_BUTTON_X2MASK; break;
  97. }
  98. if (buttons == 0) {
  99. AppendLine(active);
  100. active = NULL;
  101. }
  102. break;
  103. case SDL_QUIT:
  104. done = SDL_TRUE;
  105. break;
  106. default:
  107. break;
  108. }
  109. }
  110. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
  111. SDL_RenderClear(renderer);
  112. DrawLines(renderer);
  113. if (active)
  114. DrawLine(renderer, active);
  115. SDL_RenderPresent(renderer);
  116. #ifdef __EMSCRIPTEN__
  117. if (done) {
  118. emscripten_cancel_main_loop();
  119. }
  120. #endif
  121. }
  122. int
  123. main(int argc, char *argv[])
  124. {
  125. SDL_Renderer *renderer;
  126. /* Enable standard application logging */
  127. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  128. /* Initialize SDL (Note: video is required to start event loop) */
  129. if (SDL_Init(SDL_INIT_VIDEO) < 0) {
  130. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  131. exit(1);
  132. }
  133. /* Create a window to display joystick axis position */
  134. window = SDL_CreateWindow("Mouse Test", SDL_WINDOWPOS_CENTERED,
  135. SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
  136. SCREEN_HEIGHT, 0);
  137. if (window == NULL) {
  138. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
  139. return SDL_FALSE;
  140. }
  141. renderer = SDL_CreateRenderer(window, -1, 0);
  142. if (renderer == NULL) {
  143. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
  144. SDL_DestroyWindow(window);
  145. return SDL_FALSE;
  146. }
  147. /* Main render loop */
  148. #ifdef __EMSCRIPTEN__
  149. emscripten_set_main_loop_arg(loop, renderer, 0, 1);
  150. #else
  151. while (!done) {
  152. loop(renderer);
  153. }
  154. #endif
  155. SDL_DestroyRenderer(renderer);
  156. SDL_DestroyWindow(window);
  157. SDL_Quit();
  158. return 0;
  159. }
  160. /* vi: set ts=4 sw=4 expandtab: */