🛠️🐜 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.4 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. /* 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) {
  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. for (i = 0; i < SDL_GetNumTouchDevices(); ++i) {
  154. const SDL_TouchID id = SDL_GetTouchDevice(i);
  155. const char *name = SDL_GetTouchName(i);
  156. SDL_Log("Fingers Down on device %"SDL_PRIs64" (%s): %d", id, name, SDL_GetNumTouchFingers(id));
  157. }
  158. break;
  159. }
  160. case SDLK_SPACE:
  161. SDL_RecordGesture(-1);
  162. break;
  163. case SDLK_s:
  164. stream = SDL_RWFromFile("gestureSave", "w");
  165. SDL_Log("Wrote %i templates", SDL_SaveAllDollarTemplates(stream));
  166. SDL_RWclose(stream);
  167. break;
  168. case SDLK_l:
  169. stream = SDL_RWFromFile("gestureSave", "r");
  170. SDL_Log("Loaded: %i", SDL_LoadDollarTemplates(-1, stream));
  171. SDL_RWclose(stream);
  172. break;
  173. }
  174. break;
  175. #if VERBOSE
  176. case SDL_FINGERMOTION:
  177. SDL_Log("Finger: %"SDL_PRIs64", x: %f, y: %f",event.tfinger.fingerId,
  178. event.tfinger.x,event.tfinger.y);
  179. break;
  180. case SDL_FINGERDOWN:
  181. SDL_Log("Finger: %"SDL_PRIs64" down - x: %f, y: %f",
  182. event.tfinger.fingerId,event.tfinger.x,event.tfinger.y);
  183. break;
  184. case SDL_FINGERUP:
  185. SDL_Log("Finger: %"SDL_PRIs64" up - x: %f, y: %f",
  186. event.tfinger.fingerId,event.tfinger.x,event.tfinger.y);
  187. break;
  188. #endif
  189. case SDL_MULTIGESTURE:
  190. #if VERBOSE
  191. SDL_Log("Multi Gesture: x = %f, y = %f, dAng = %f, dR = %f",
  192. event.mgesture.x, event.mgesture.y,
  193. event.mgesture.dTheta, event.mgesture.dDist);
  194. SDL_Log("MG: numDownTouch = %i",event.mgesture.numFingers);
  195. #endif
  196. knob.p.x = event.mgesture.x;
  197. knob.p.y = event.mgesture.y;
  198. knob.ang += event.mgesture.dTheta;
  199. knob.r += event.mgesture.dDist;
  200. break;
  201. case SDL_DOLLARGESTURE:
  202. SDL_Log("Gesture %"SDL_PRIs64" performed, error: %f",
  203. event.dgesture.gestureId, event.dgesture.error);
  204. break;
  205. case SDL_DOLLARRECORD:
  206. SDL_Log("Recorded gesture: %"SDL_PRIs64"",event.dgesture.gestureId);
  207. break;
  208. }
  209. }
  210. for (i = 0; i < state->num_windows; ++i) {
  211. if (state->windows[i]) {
  212. DrawScreen(state->windows[i]);
  213. }
  214. }
  215. #ifdef __EMSCRIPTEN__
  216. if (quitting) {
  217. emscripten_cancel_main_loop();
  218. }
  219. #endif
  220. }
  221. int main(int argc, char* argv[])
  222. {
  223. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  224. if (!state) {
  225. return 1;
  226. }
  227. state->window_title = "Gesture Test";
  228. state->window_w = WIDTH;
  229. state->window_h = HEIGHT;
  230. state->skip_renderer = SDL_TRUE;
  231. if (!SDLTest_CommonDefaultArgs(state, argc, argv) || !SDLTest_CommonInit(state)) {
  232. SDLTest_CommonQuit(state);
  233. return 1;
  234. }
  235. #ifdef __EMSCRIPTEN__
  236. emscripten_set_main_loop(loop, 0, 1);
  237. #else
  238. while (!quitting) {
  239. loop();
  240. }
  241. #endif
  242. SDLTest_CommonQuit(state);
  243. return 0;
  244. }