testmouse.c 8.0 KB

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