testmouse.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. Copyright (C) 1997-2021 Sam Lantinga <[email protected]>
  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. static SDL_Renderer *renderer;
  24. typedef struct _Line {
  25. struct _Line *next;
  26. int x1, y1, x2, y2;
  27. Uint8 r, g, b;
  28. } Line;
  29. static Line *active = NULL;
  30. static Line *lines = NULL;
  31. static int buttons = 0;
  32. static SDL_bool done = SDL_FALSE;
  33. void
  34. DrawLine(SDL_Renderer * renderer, Line * line)
  35. {
  36. SDL_SetRenderDrawColor(renderer, line->r, line->g, line->b, 255);
  37. SDL_RenderDrawLine(renderer, line->x1, line->y1, line->x2, line->y2);
  38. }
  39. void
  40. DrawLines(SDL_Renderer * renderer)
  41. {
  42. Line *next = lines;
  43. while (next != NULL) {
  44. DrawLine(renderer, next);
  45. next = next->next;
  46. }
  47. }
  48. void
  49. AppendLine(Line *line)
  50. {
  51. if (lines) {
  52. Line *next = lines;
  53. while (next->next != NULL) {
  54. next = next->next;
  55. }
  56. next->next = line;
  57. } else {
  58. lines = line;
  59. }
  60. }
  61. void
  62. loop(void)
  63. {
  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. }
  85. break;
  86. case SDL_MOUSEBUTTONUP:
  87. if (!active)
  88. break;
  89. switch (event.button.button) {
  90. case SDL_BUTTON_LEFT: buttons &= ~SDL_BUTTON_LMASK; break;
  91. case SDL_BUTTON_MIDDLE: buttons &= ~SDL_BUTTON_MMASK; break;
  92. case SDL_BUTTON_RIGHT: buttons &= ~SDL_BUTTON_RMASK; break;
  93. }
  94. if (buttons == 0) {
  95. AppendLine(active);
  96. active = NULL;
  97. }
  98. break;
  99. case SDL_QUIT:
  100. done = SDL_TRUE;
  101. break;
  102. default:
  103. break;
  104. }
  105. }
  106. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
  107. SDL_RenderClear(renderer);
  108. DrawLines(renderer);
  109. if (active)
  110. DrawLine(renderer, active);
  111. SDL_RenderPresent(renderer);
  112. #ifdef __EMSCRIPTEN__
  113. if (done) {
  114. emscripten_cancel_main_loop();
  115. }
  116. #endif
  117. }
  118. int
  119. main(int argc, char *argv[])
  120. {
  121. /* Enable standard application logging */
  122. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  123. /* Initialize SDL (Note: video is required to start event loop) */
  124. if (SDL_Init(SDL_INIT_VIDEO) < 0) {
  125. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  126. exit(1);
  127. }
  128. /* Create a window to display joystick axis position */
  129. window = SDL_CreateWindow("Mouse Test", SDL_WINDOWPOS_CENTERED,
  130. SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
  131. SCREEN_HEIGHT, 0);
  132. if (window == NULL) {
  133. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
  134. return SDL_FALSE;
  135. }
  136. renderer = SDL_CreateRenderer(window, -1, 0);
  137. if (renderer == NULL) {
  138. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
  139. SDL_DestroyWindow(window);
  140. return SDL_FALSE;
  141. }
  142. /* Main render loop */
  143. #ifdef __EMSCRIPTEN__
  144. emscripten_set_main_loop(loop, 0, 1);
  145. #else
  146. while (!done) {
  147. loop();
  148. }
  149. #endif
  150. SDL_DestroyRenderer(renderer);
  151. SDL_DestroyWindow(window);
  152. SDL_Quit();
  153. return 0;
  154. }
  155. /* vi: set ts=4 sw=4 expandtab: */