testmouse.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. Copyright (C) 1997-2021 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. }
  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. SDL_Renderer *renderer;
  122. /* Enable standard application logging */
  123. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  124. /* Initialize SDL (Note: video is required to start event loop) */
  125. if (SDL_Init(SDL_INIT_VIDEO) < 0) {
  126. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  127. exit(1);
  128. }
  129. /* Create a window to display joystick axis position */
  130. window = SDL_CreateWindow("Mouse Test", SDL_WINDOWPOS_CENTERED,
  131. SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
  132. SCREEN_HEIGHT, 0);
  133. if (window == NULL) {
  134. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
  135. return SDL_FALSE;
  136. }
  137. renderer = SDL_CreateRenderer(window, -1, 0);
  138. if (renderer == NULL) {
  139. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
  140. SDL_DestroyWindow(window);
  141. return SDL_FALSE;
  142. }
  143. /* Main render loop */
  144. #ifdef __EMSCRIPTEN__
  145. emscripten_set_main_loop_arg(loop, renderer, 0, 1);
  146. #else
  147. while (!done) {
  148. loop(renderer);
  149. }
  150. #endif
  151. SDL_DestroyRenderer(renderer);
  152. SDL_DestroyWindow(window);
  153. SDL_Quit();
  154. return 0;
  155. }
  156. /* vi: set ts=4 sw=4 expandtab: */