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

231 lines
6.4 KiB

  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 wheel_x_active = SDL_FALSE;
  32. static SDL_bool wheel_y_active = SDL_FALSE;
  33. static float wheel_x = SCREEN_WIDTH * 0.5f;
  34. static float wheel_y = SCREEN_HEIGHT * 0.5f;
  35. static SDL_bool done = SDL_FALSE;
  36. void
  37. DrawLine(SDL_Renderer * renderer, Line * line)
  38. {
  39. SDL_SetRenderDrawColor(renderer, line->r, line->g, line->b, 255);
  40. SDL_RenderDrawLine(renderer, line->x1, line->y1, line->x2, line->y2);
  41. }
  42. void
  43. DrawLines(SDL_Renderer * renderer)
  44. {
  45. Line *next = lines;
  46. while (next != NULL) {
  47. DrawLine(renderer, next);
  48. next = next->next;
  49. }
  50. }
  51. void
  52. AppendLine(Line *line)
  53. {
  54. if (lines) {
  55. Line *next = lines;
  56. while (next->next != NULL) {
  57. next = next->next;
  58. }
  59. next->next = line;
  60. } else {
  61. lines = line;
  62. }
  63. }
  64. void
  65. loop(void *arg)
  66. {
  67. SDL_Renderer *renderer = (SDL_Renderer *)arg;
  68. SDL_Event event;
  69. /* Check for events */
  70. while (SDL_PollEvent(&event)) {
  71. switch (event.type) {
  72. case SDL_MOUSEWHEEL:
  73. if (event.wheel.direction == SDL_MOUSEWHEEL_FLIPPED) {
  74. event.wheel.preciseX *= -1.0f;
  75. event.wheel.preciseY *= -1.0f;
  76. event.wheel.x *= -1;
  77. event.wheel.y *= -1;
  78. }
  79. if (event.wheel.preciseX != 0.0f) {
  80. wheel_x_active = SDL_TRUE;
  81. /* "positive to the right and negative to the left" */
  82. wheel_x += event.wheel.preciseX * 10.0f;
  83. }
  84. if (event.wheel.preciseY != 0.0f) {
  85. wheel_y_active = SDL_TRUE;
  86. /* "positive away from the user and negative towards the user" */
  87. wheel_y -= event.wheel.preciseY * 10.0f;
  88. }
  89. break;
  90. case SDL_MOUSEMOTION:
  91. if (!active)
  92. break;
  93. active->x2 = event.motion.x;
  94. active->y2 = event.motion.y;
  95. break;
  96. case SDL_MOUSEBUTTONDOWN:
  97. if (!active) {
  98. active = SDL_calloc(1, sizeof(*active));
  99. active->x1 = active->x2 = event.button.x;
  100. active->y1 = active->y2 = event.button.y;
  101. }
  102. switch (event.button.button) {
  103. case SDL_BUTTON_LEFT: active->r = 255; buttons |= SDL_BUTTON_LMASK; break;
  104. case SDL_BUTTON_MIDDLE: active->g = 255; buttons |= SDL_BUTTON_MMASK; break;
  105. case SDL_BUTTON_RIGHT: active->b = 255; buttons |= SDL_BUTTON_RMASK; break;
  106. case SDL_BUTTON_X1: active->r = 255; active->b = 255; buttons |= SDL_BUTTON_X1MASK; break;
  107. case SDL_BUTTON_X2: active->g = 255; active->b = 255; buttons |= SDL_BUTTON_X2MASK; break;
  108. }
  109. break;
  110. case SDL_MOUSEBUTTONUP:
  111. if (!active)
  112. break;
  113. switch (event.button.button) {
  114. case SDL_BUTTON_LEFT: buttons &= ~SDL_BUTTON_LMASK; break;
  115. case SDL_BUTTON_MIDDLE: buttons &= ~SDL_BUTTON_MMASK; break;
  116. case SDL_BUTTON_RIGHT: buttons &= ~SDL_BUTTON_RMASK; break;
  117. case SDL_BUTTON_X1: buttons &= ~SDL_BUTTON_X1MASK; break;
  118. case SDL_BUTTON_X2: buttons &= ~SDL_BUTTON_X2MASK; break;
  119. }
  120. if (buttons == 0) {
  121. AppendLine(active);
  122. active = NULL;
  123. }
  124. break;
  125. case SDL_QUIT:
  126. done = SDL_TRUE;
  127. break;
  128. default:
  129. break;
  130. }
  131. }
  132. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
  133. SDL_RenderClear(renderer);
  134. /* Mouse wheel */
  135. SDL_SetRenderDrawColor(renderer, 0, 255, 128, 255);
  136. if (wheel_x_active) {
  137. SDL_RenderDrawLine(renderer, wheel_x, 0, wheel_x, SCREEN_HEIGHT);
  138. }
  139. if (wheel_y_active) {
  140. SDL_RenderDrawLine(renderer, 0, wheel_y, SCREEN_WIDTH, wheel_y);
  141. }
  142. /* Lines from mouse clicks */
  143. DrawLines(renderer);
  144. if (active)
  145. DrawLine(renderer, active);
  146. SDL_RenderPresent(renderer);
  147. #ifdef __EMSCRIPTEN__
  148. if (done) {
  149. emscripten_cancel_main_loop();
  150. }
  151. #endif
  152. }
  153. int
  154. main(int argc, char *argv[])
  155. {
  156. SDL_Renderer *renderer;
  157. /* Enable standard application logging */
  158. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  159. /* Initialize SDL (Note: video is required to start event loop) */
  160. if (SDL_Init(SDL_INIT_VIDEO) < 0) {
  161. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  162. exit(1);
  163. }
  164. /* Create a window to display joystick axis position */
  165. window = SDL_CreateWindow("Mouse Test", SDL_WINDOWPOS_CENTERED,
  166. SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
  167. SCREEN_HEIGHT, 0);
  168. if (window == NULL) {
  169. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
  170. return SDL_FALSE;
  171. }
  172. renderer = SDL_CreateRenderer(window, -1, 0);
  173. if (renderer == NULL) {
  174. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
  175. SDL_DestroyWindow(window);
  176. return SDL_FALSE;
  177. }
  178. /* Main render loop */
  179. #ifdef __EMSCRIPTEN__
  180. emscripten_set_main_loop_arg(loop, renderer, 0, 1);
  181. #else
  182. while (!done) {
  183. loop(renderer);
  184. }
  185. #endif
  186. SDL_DestroyRenderer(renderer);
  187. SDL_DestroyWindow(window);
  188. SDL_Quit();
  189. return 0;
  190. }
  191. /* vi: set ts=4 sw=4 expandtab: */