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

300 lines
8.1 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. /* Usage:
  11. * Spacebar to begin recording a gesture on all touches.
  12. * s to save all touches into "./gestureSave"
  13. * l to load all touches from "./gestureSave"
  14. */
  15. #include "SDL.h"
  16. #include <stdlib.h> /* for exit() */
  17. #ifdef __EMSCRIPTEN__
  18. #include <emscripten/emscripten.h>
  19. #endif
  20. #include "SDL_test.h"
  21. #include "SDL_test_common.h"
  22. #define WIDTH 640
  23. #define HEIGHT 480
  24. #define BPP 4
  25. /* MUST BE A POWER OF 2! */
  26. #define EVENT_BUF_SIZE 256
  27. #define VERBOSE 0
  28. static SDLTest_CommonState *state;
  29. static SDL_Event events[EVENT_BUF_SIZE];
  30. static int eventWrite;
  31. static int colors[7] = { 0xFF, 0xFF00, 0xFF0000, 0xFFFF00, 0x00FFFF, 0xFF00FF, 0xFFFFFF };
  32. static int quitting = 0;
  33. typedef struct
  34. {
  35. float x, y;
  36. } Point;
  37. typedef struct
  38. {
  39. float ang, r;
  40. Point p;
  41. } Knob;
  42. static Knob knob = { 0.0f, 0.1f, { 0.0f, 0.0f } };
  43. static void
  44. setpix(SDL_Surface *screen, float _x, float _y, unsigned int col)
  45. {
  46. Uint32 *pixmem32;
  47. Uint32 colour;
  48. Uint8 r, g, b;
  49. const int x = (int)_x;
  50. const int y = (int)_y;
  51. float a;
  52. if ((x < 0) || (x >= screen->w) || (y < 0) || (y >= screen->h)) {
  53. return;
  54. }
  55. pixmem32 = (Uint32 *)screen->pixels + y * screen->pitch / BPP + x;
  56. SDL_memcpy(&colour, pixmem32, screen->format->BytesPerPixel);
  57. SDL_GetRGB(colour, screen->format, &r, &g, &b);
  58. /* r = 0;g = 0; b = 0; */
  59. a = (float)((col >> 24) & 0xFF);
  60. if (a == 0) {
  61. a = 0xFF; /* Hack, to make things easier. */
  62. }
  63. a = (a == 0.0f) ? 1 : (a / 255.0f);
  64. r = (Uint8)(r * (1 - a) + ((col >> 16) & 0xFF) * a);
  65. g = (Uint8)(g * (1 - a) + ((col >> 8) & 0xFF) * a);
  66. b = (Uint8)(b * (1 - a) + ((col >> 0) & 0xFF) * a);
  67. colour = SDL_MapRGB(screen->format, r, g, b);
  68. *pixmem32 = colour;
  69. }
  70. #if 0 /* unused */
  71. static void
  72. drawLine(SDL_Surface *screen, float x0, float y0, float x1, float y1, unsigned int col)
  73. {
  74. float t;
  75. for (t = 0; t < 1; t += (float) (1.0f / SDL_max(SDL_fabs(x0 - x1), SDL_fabs(y0 - y1)))) {
  76. setpix(screen, x1 + t * (x0 - x1), y1 + t * (y0 - y1), col);
  77. }
  78. }
  79. #endif
  80. static void
  81. drawCircle(SDL_Surface *screen, float x, float y, float r, unsigned int c)
  82. {
  83. float tx, ty, xr;
  84. for (ty = (float)-SDL_fabs(r); ty <= (float)SDL_fabs((int)r); ty++) {
  85. xr = (float)SDL_sqrt(r * r - ty * ty);
  86. if (r > 0) { /* r > 0 ==> filled circle */
  87. for (tx = -xr + 0.5f; tx <= xr - 0.5f; tx++) {
  88. setpix(screen, x + tx, y + ty, c);
  89. }
  90. } else {
  91. setpix(screen, x - xr + 0.5f, y + ty, c);
  92. setpix(screen, x + xr - 0.5f, y + ty, c);
  93. }
  94. }
  95. }
  96. static void
  97. drawKnob(SDL_Surface *screen, const Knob *k)
  98. {
  99. drawCircle(screen, k->p.x * screen->w, k->p.y * screen->h, k->r * screen->w, 0xFFFFFF);
  100. drawCircle(screen, (k->p.x + k->r / 2 * SDL_cosf(k->ang)) * screen->w,
  101. (k->p.y + k->r / 2 * SDL_sinf(k->ang)) * screen->h, k->r / 4 * screen->w, 0);
  102. }
  103. static void
  104. DrawScreen(SDL_Window *window)
  105. {
  106. SDL_Surface *screen = SDL_GetWindowSurface(window);
  107. int i;
  108. if (screen == NULL) {
  109. return;
  110. }
  111. SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 75, 75, 75));
  112. /* draw Touch History */
  113. for (i = eventWrite; i < eventWrite + EVENT_BUF_SIZE; ++i) {
  114. const SDL_Event *event = &events[i & (EVENT_BUF_SIZE - 1)];
  115. const float age = (float)(i - eventWrite) / EVENT_BUF_SIZE;
  116. float x, y;
  117. unsigned int c, col;
  118. if ((event->type == SDL_FINGERMOTION) ||
  119. (event->type == SDL_FINGERDOWN) ||
  120. (event->type == SDL_FINGERUP)) {
  121. x = event->tfinger.x;
  122. y = event->tfinger.y;
  123. /* draw the touch: */
  124. c = colors[event->tfinger.fingerId % 7];
  125. col = ((unsigned int)(c * (0.1f + 0.85f))) | (unsigned int)(0xFF * age) << 24;
  126. if (event->type == SDL_FINGERMOTION) {
  127. drawCircle(screen, x * screen->w, y * screen->h, 5, col);
  128. } else if (event->type == SDL_FINGERDOWN) {
  129. drawCircle(screen, x * screen->w, y * screen->h, -10, col);
  130. }
  131. }
  132. }
  133. if (knob.p.x > 0) {
  134. drawKnob(screen, &knob);
  135. }
  136. SDL_UpdateWindowSurface(window);
  137. }
  138. static void
  139. loop(void)
  140. {
  141. SDL_Event event;
  142. SDL_RWops *stream;
  143. int i;
  144. while (SDL_PollEvent(&event)) {
  145. SDLTest_CommonEvent(state, &event, &quitting);
  146. /* Record _all_ events */
  147. events[eventWrite & (EVENT_BUF_SIZE - 1)] = event;
  148. eventWrite++;
  149. switch (event.type) {
  150. case SDL_KEYDOWN:
  151. switch (event.key.keysym.sym) {
  152. case SDLK_i:
  153. {
  154. for (i = 0; i < SDL_GetNumTouchDevices(); ++i) {
  155. const SDL_TouchID id = SDL_GetTouchDevice(i);
  156. const char *name = SDL_GetTouchName(i);
  157. SDL_Log("Fingers Down on device %" SDL_PRIs64 " (%s): %d", id, name, SDL_GetNumTouchFingers(id));
  158. }
  159. break;
  160. }
  161. case SDLK_SPACE:
  162. SDL_RecordGesture(-1);
  163. break;
  164. case SDLK_s:
  165. stream = SDL_RWFromFile("gestureSave", "w");
  166. SDL_Log("Wrote %i templates", SDL_SaveAllDollarTemplates(stream));
  167. SDL_RWclose(stream);
  168. break;
  169. case SDLK_l:
  170. stream = SDL_RWFromFile("gestureSave", "r");
  171. SDL_Log("Loaded: %i", SDL_LoadDollarTemplates(-1, stream));
  172. SDL_RWclose(stream);
  173. break;
  174. }
  175. break;
  176. #if VERBOSE
  177. case SDL_FINGERMOTION:
  178. SDL_Log("Finger: %" SDL_PRIs64 ", x: %f, y: %f", event.tfinger.fingerId,
  179. event.tfinger.x, event.tfinger.y);
  180. break;
  181. case SDL_FINGERDOWN:
  182. SDL_Log("Finger: %" SDL_PRIs64 " down - x: %f, y: %f",
  183. event.tfinger.fingerId, event.tfinger.x, event.tfinger.y);
  184. break;
  185. case SDL_FINGERUP:
  186. SDL_Log("Finger: %" SDL_PRIs64 " up - x: %f, y: %f",
  187. event.tfinger.fingerId, event.tfinger.x, event.tfinger.y);
  188. break;
  189. #endif
  190. case SDL_MULTIGESTURE:
  191. #if VERBOSE
  192. SDL_Log("Multi Gesture: x = %f, y = %f, dAng = %f, dR = %f",
  193. event.mgesture.x, event.mgesture.y,
  194. event.mgesture.dTheta, event.mgesture.dDist);
  195. SDL_Log("MG: numDownTouch = %i", event.mgesture.numFingers);
  196. #endif
  197. knob.p.x = event.mgesture.x;
  198. knob.p.y = event.mgesture.y;
  199. knob.ang += event.mgesture.dTheta;
  200. knob.r += event.mgesture.dDist;
  201. break;
  202. case SDL_DOLLARGESTURE:
  203. SDL_Log("Gesture %" SDL_PRIs64 " performed, error: %f",
  204. event.dgesture.gestureId, event.dgesture.error);
  205. break;
  206. case SDL_DOLLARRECORD:
  207. SDL_Log("Recorded gesture: %" SDL_PRIs64 "", event.dgesture.gestureId);
  208. break;
  209. }
  210. }
  211. for (i = 0; i < state->num_windows; ++i) {
  212. if (state->windows[i]) {
  213. DrawScreen(state->windows[i]);
  214. }
  215. }
  216. #ifdef __EMSCRIPTEN__
  217. if (quitting) {
  218. emscripten_cancel_main_loop();
  219. }
  220. #endif
  221. }
  222. int main(int argc, char *argv[])
  223. {
  224. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  225. if (state == NULL) {
  226. return 1;
  227. }
  228. state->window_title = "Gesture Test";
  229. state->window_w = WIDTH;
  230. state->window_h = HEIGHT;
  231. state->skip_renderer = SDL_TRUE;
  232. if (!SDLTest_CommonDefaultArgs(state, argc, argv) || !SDLTest_CommonInit(state)) {
  233. SDLTest_CommonQuit(state);
  234. return 1;
  235. }
  236. #ifdef __EMSCRIPTEN__
  237. emscripten_set_main_loop(loop, 0, 1);
  238. #else
  239. while (!quitting) {
  240. loop();
  241. }
  242. #endif
  243. SDLTest_CommonQuit(state);
  244. return 0;
  245. }