testmouse.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 <SDL3/SDL.h>
  11. #include <SDL3/SDL_main.h>
  12. #include <SDL3/SDL_test.h>
  13. #ifdef SDL_PLATFORM_EMSCRIPTEN
  14. #include <emscripten/emscripten.h>
  15. #endif
  16. #include <stdlib.h> /* exit() */
  17. #ifdef SDL_PLATFORM_3DS
  18. /* For mouse-based tests, we want to have the window on the touch screen */
  19. #define SCREEN_X 40
  20. #define SCREEN_Y 240
  21. #define SCREEN_WIDTH 320
  22. #define SCREEN_HEIGHT 240
  23. #elif defined(SDL_PLATFORM_IOS)
  24. #define SCREEN_WIDTH 320
  25. #define SCREEN_HEIGHT 480
  26. #else
  27. #define SCREEN_WIDTH 640
  28. #define SCREEN_HEIGHT 480
  29. #endif
  30. static SDL_Window *window;
  31. typedef struct _Object
  32. {
  33. struct _Object *next;
  34. float x1, y1, x2, y2;
  35. Uint8 r, g, b;
  36. bool isRect;
  37. } Object;
  38. static Object *active = NULL;
  39. static Object *objects = NULL;
  40. static int buttons = 0;
  41. static bool isRect = false;
  42. static bool wheel_x_active = false;
  43. static bool wheel_y_active = false;
  44. static float wheel_x = SCREEN_WIDTH * 0.5f;
  45. static float wheel_y = SCREEN_HEIGHT * 0.5f;
  46. struct mouse_loop_data {
  47. bool done;
  48. SDL_Renderer *renderer;
  49. };
  50. static void DrawObject(SDL_Renderer *renderer, Object *object)
  51. {
  52. SDL_SetRenderDrawColor(renderer, object->r, object->g, object->b, 255);
  53. if (object->isRect) {
  54. SDL_FRect rect;
  55. if (object->x1 > object->x2) {
  56. rect.x = object->x2;
  57. rect.w = object->x1 - object->x2;
  58. } else {
  59. rect.x = object->x1;
  60. rect.w = object->x2 - object->x1;
  61. }
  62. if (object->y1 > object->y2) {
  63. rect.y = object->y2;
  64. rect.h = object->y1 - object->y2;
  65. } else {
  66. rect.y = object->y1;
  67. rect.h = object->y2 - object->y1;
  68. }
  69. SDL_RenderFillRect(renderer, &rect);
  70. } else {
  71. SDL_RenderLine(renderer, object->x1, object->y1, object->x2, object->y2);
  72. }
  73. }
  74. static void DrawObjects(SDL_Renderer *renderer)
  75. {
  76. Object *next = objects;
  77. while (next) {
  78. DrawObject(renderer, next);
  79. next = next->next;
  80. }
  81. }
  82. static void AppendObject(Object *object)
  83. {
  84. if (objects) {
  85. Object *next = objects;
  86. while (next->next) {
  87. next = next->next;
  88. }
  89. next->next = object;
  90. } else {
  91. objects = object;
  92. }
  93. }
  94. static void loop(void *arg)
  95. {
  96. struct mouse_loop_data *loop_data = (struct mouse_loop_data *)arg;
  97. SDL_Event event;
  98. SDL_Renderer *renderer = loop_data->renderer;
  99. /* Check for events */
  100. while (SDL_PollEvent(&event)) {
  101. switch (event.type) {
  102. case SDL_EVENT_MOUSE_WHEEL:
  103. if (event.wheel.direction == SDL_MOUSEWHEEL_FLIPPED) {
  104. event.wheel.x *= -1;
  105. event.wheel.y *= -1;
  106. }
  107. if (event.wheel.x != 0.0f) {
  108. wheel_x_active = true;
  109. /* "positive to the right and negative to the left" */
  110. wheel_x += event.wheel.x * 10.0f;
  111. }
  112. if (event.wheel.y != 0.0f) {
  113. wheel_y_active = true;
  114. /* "positive away from the user and negative towards the user" */
  115. wheel_y -= event.wheel.y * 10.0f;
  116. }
  117. break;
  118. case SDL_EVENT_MOUSE_MOTION:
  119. if (!active) {
  120. break;
  121. }
  122. active->x2 = event.motion.x;
  123. active->y2 = event.motion.y;
  124. break;
  125. case SDL_EVENT_MOUSE_BUTTON_DOWN:
  126. if (!active) {
  127. active = SDL_calloc(1, sizeof(*active));
  128. active->x1 = active->x2 = event.button.x;
  129. active->y1 = active->y2 = event.button.y;
  130. active->isRect = isRect;
  131. }
  132. switch (event.button.button) {
  133. case SDL_BUTTON_LEFT:
  134. active->r = 255;
  135. buttons |= SDL_BUTTON_LMASK;
  136. break;
  137. case SDL_BUTTON_MIDDLE:
  138. active->g = 255;
  139. buttons |= SDL_BUTTON_MMASK;
  140. break;
  141. case SDL_BUTTON_RIGHT:
  142. active->b = 255;
  143. buttons |= SDL_BUTTON_RMASK;
  144. break;
  145. case SDL_BUTTON_X1:
  146. active->r = 255;
  147. active->b = 255;
  148. buttons |= SDL_BUTTON_X1MASK;
  149. break;
  150. case SDL_BUTTON_X2:
  151. active->g = 255;
  152. active->b = 255;
  153. buttons |= SDL_BUTTON_X2MASK;
  154. break;
  155. }
  156. break;
  157. case SDL_EVENT_MOUSE_BUTTON_UP:
  158. if (!active) {
  159. break;
  160. }
  161. switch (event.button.button) {
  162. case SDL_BUTTON_LEFT:
  163. buttons &= ~SDL_BUTTON_LMASK;
  164. break;
  165. case SDL_BUTTON_MIDDLE:
  166. buttons &= ~SDL_BUTTON_MMASK;
  167. break;
  168. case SDL_BUTTON_RIGHT:
  169. buttons &= ~SDL_BUTTON_RMASK;
  170. break;
  171. case SDL_BUTTON_X1:
  172. buttons &= ~SDL_BUTTON_X1MASK;
  173. break;
  174. case SDL_BUTTON_X2:
  175. buttons &= ~SDL_BUTTON_X2MASK;
  176. break;
  177. }
  178. if (buttons == 0) {
  179. AppendObject(active);
  180. active = NULL;
  181. }
  182. break;
  183. case SDL_EVENT_KEY_DOWN:
  184. if (event.key.key == SDLK_C) {
  185. int x, y, w, h;
  186. SDL_GetWindowPosition(window, &x, &y);
  187. SDL_GetWindowSize(window, &w, &h);
  188. w /= 2;
  189. h /= 2;
  190. if (event.key.mod & SDL_KMOD_ALT) {
  191. SDL_WarpMouseGlobal((float)(x + w), (float)(y + h));
  192. } else {
  193. SDL_WarpMouseInWindow(window, (float)w, (float)h);
  194. }
  195. }
  196. SDL_FALLTHROUGH;
  197. case SDL_EVENT_KEY_UP:
  198. switch (event.key.key) {
  199. case SDLK_LSHIFT:
  200. isRect = event.key.down;
  201. if (active) {
  202. active->isRect = isRect;
  203. }
  204. break;
  205. default:
  206. break;
  207. }
  208. break;
  209. case SDL_EVENT_QUIT:
  210. loop_data->done = true;
  211. break;
  212. default:
  213. break;
  214. }
  215. }
  216. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
  217. SDL_RenderClear(renderer);
  218. /* Mouse wheel */
  219. SDL_SetRenderDrawColor(renderer, 0, 255, 128, 255);
  220. if (wheel_x_active) {
  221. SDL_RenderLine(renderer, wheel_x, 0.0f, wheel_x, (float)SCREEN_HEIGHT);
  222. }
  223. if (wheel_y_active) {
  224. SDL_RenderLine(renderer, 0.0f, wheel_y, (float)SCREEN_WIDTH, wheel_y);
  225. }
  226. /* Objects from mouse clicks */
  227. DrawObjects(renderer);
  228. if (active) {
  229. DrawObject(renderer, active);
  230. }
  231. SDL_RenderPresent(renderer);
  232. #ifdef SDL_PLATFORM_EMSCRIPTEN
  233. if (loop_data->done) {
  234. emscripten_cancel_main_loop();
  235. }
  236. #endif
  237. }
  238. int main(int argc, char *argv[])
  239. {
  240. struct mouse_loop_data loop_data;
  241. SDLTest_CommonState *state;
  242. #ifdef SDL_PLATFORM_3DS
  243. SDL_PropertiesID props;
  244. #endif
  245. /* Initialize test framework */
  246. state = SDLTest_CommonCreateState(argv, 0);
  247. if (!state) {
  248. return 1;
  249. }
  250. /* Parse commandline */
  251. if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
  252. return 1;
  253. }
  254. /* Initialize SDL (Note: video is required to start event loop) */
  255. if (!SDL_Init(SDL_INIT_VIDEO)) {
  256. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  257. exit(1);
  258. }
  259. /* Create a window to display joystick axis position */
  260. #ifdef SDL_PLATFORM_3DS
  261. props = SDL_CreateProperties();
  262. SDL_SetStringProperty(props, SDL_PROP_WINDOW_CREATE_TITLE_STRING, "Mouse Test");
  263. SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_X_NUMBER, SCREEN_X);
  264. SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_Y_NUMBER, SCREEN_Y);
  265. SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, SCREEN_WIDTH);
  266. SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, SCREEN_HEIGHT);
  267. SDL_SetNumberProperty(props, "flags", 0);
  268. window = SDL_CreateWindowWithProperties(props);
  269. #else
  270. window = SDL_CreateWindow("Mouse Test", SCREEN_WIDTH, SCREEN_HEIGHT, 0);
  271. #endif
  272. if (!window) {
  273. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
  274. return 0;
  275. }
  276. loop_data.done = false;
  277. loop_data.renderer = SDL_CreateRenderer(window, NULL);
  278. if (!loop_data.renderer) {
  279. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
  280. SDL_DestroyWindow(window);
  281. return 0;
  282. }
  283. /* Main render loop */
  284. #ifdef SDL_PLATFORM_EMSCRIPTEN
  285. emscripten_set_main_loop_arg(loop, &loop_data, 0, 1);
  286. #else
  287. while (loop_data.done == false) {
  288. loop(&loop_data);
  289. }
  290. #endif
  291. SDL_DestroyRenderer(loop_data.renderer);
  292. SDL_DestroyWindow(window);
  293. SDL_Quit();
  294. SDLTest_CommonDestroyState(state);
  295. return 0;
  296. }