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

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