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

298 lines
7.8 KiB

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