testgesture.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. Copyright (C) 1997-2019 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. static void
  71. drawLine(SDL_Surface *screen, float x0, float y0, float x1, float y1, unsigned int col)
  72. {
  73. float t;
  74. for (t = 0; t < 1; t += (float) (1.0f / SDL_max(SDL_fabs(x0 - x1), SDL_fabs(y0 - y1)))) {
  75. setpix(screen, x1 + t * (x0 - x1), y1 + t * (y0 - y1), col);
  76. }
  77. }
  78. static void
  79. drawCircle(SDL_Surface *screen, float x, float y, float r, unsigned int c)
  80. {
  81. float tx,ty, xr;
  82. for (ty = (float) -SDL_fabs(r); ty <= (float) SDL_fabs((int) r); ty++) {
  83. xr = (float) SDL_sqrt(r * r - ty * ty);
  84. if (r > 0) { /* r > 0 ==> filled circle */
  85. for(tx = -xr + 0.5f; tx <= xr - 0.5f; tx++) {
  86. setpix(screen, x + tx, y + ty, c);
  87. }
  88. } else {
  89. setpix(screen, x - xr + 0.5f, y + ty, c);
  90. setpix(screen, x + xr - 0.5f, y + ty, c);
  91. }
  92. }
  93. }
  94. static void
  95. drawKnob(SDL_Surface *screen, const Knob *k)
  96. {
  97. drawCircle(screen, k->p.x * screen->w, k->p.y * screen->h, k->r * screen->w, 0xFFFFFF);
  98. drawCircle(screen, (k->p.x + k->r / 2 * SDL_cosf(k->ang)) * screen->w,
  99. (k->p.y + k->r / 2 * SDL_sinf(k->ang)) * screen->h, k->r / 4 * screen->w, 0);
  100. }
  101. static void
  102. DrawScreen(SDL_Window *window)
  103. {
  104. SDL_Surface *screen = SDL_GetWindowSurface(window);
  105. int i;
  106. if (!screen) {
  107. return;
  108. }
  109. SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 75, 75, 75));
  110. /* draw Touch History */
  111. for (i = eventWrite; i < eventWrite + EVENT_BUF_SIZE; ++i) {
  112. const SDL_Event *event = &events[i & (EVENT_BUF_SIZE - 1)];
  113. const float age = (float)(i - eventWrite) / EVENT_BUF_SIZE;
  114. float x, y;
  115. unsigned int c, col;
  116. if ( (event->type == SDL_FINGERMOTION) ||
  117. (event->type == SDL_FINGERDOWN) ||
  118. (event->type == SDL_FINGERUP) ) {
  119. x = event->tfinger.x;
  120. y = event->tfinger.y;
  121. /* draw the touch: */
  122. c = colors[event->tfinger.fingerId % 7];
  123. col = ((unsigned int) (c * (0.1f + 0.85f))) | (unsigned int) (0xFF * age) << 24;
  124. if (event->type == SDL_FINGERMOTION) {
  125. drawCircle(screen, x * screen->w, y * screen->h, 5, col);
  126. } else if (event->type == SDL_FINGERDOWN) {
  127. drawCircle(screen, x * screen->w, y * screen->h, -10, col);
  128. }
  129. }
  130. }
  131. if (knob.p.x > 0) {
  132. drawKnob(screen, &knob);
  133. }
  134. SDL_UpdateWindowSurface(window);
  135. }
  136. static void
  137. loop(void)
  138. {
  139. SDL_Event event;
  140. SDL_RWops *stream;
  141. int i;
  142. while (SDL_PollEvent(&event)) {
  143. SDLTest_CommonEvent(state, &event, &quitting);
  144. /* Record _all_ events */
  145. events[eventWrite & (EVENT_BUF_SIZE-1)] = event;
  146. eventWrite++;
  147. switch (event.type) {
  148. case SDL_KEYDOWN:
  149. switch (event.key.keysym.sym) {
  150. case SDLK_i: {
  151. for (i = 0; i < SDL_GetNumTouchDevices(); ++i) {
  152. const SDL_TouchID id = SDL_GetTouchDevice(i);
  153. SDL_Log("Fingers Down on device %"SDL_PRIs64": %d", id, SDL_GetNumTouchFingers(id));
  154. }
  155. break;
  156. }
  157. case SDLK_SPACE:
  158. SDL_RecordGesture(-1);
  159. break;
  160. case SDLK_s:
  161. stream = SDL_RWFromFile("gestureSave", "w");
  162. SDL_Log("Wrote %i templates", SDL_SaveAllDollarTemplates(stream));
  163. SDL_RWclose(stream);
  164. break;
  165. case SDLK_l:
  166. stream = SDL_RWFromFile("gestureSave", "r");
  167. SDL_Log("Loaded: %i", SDL_LoadDollarTemplates(-1, stream));
  168. SDL_RWclose(stream);
  169. break;
  170. }
  171. break;
  172. #if VERBOSE
  173. case SDL_FINGERMOTION:
  174. SDL_Log("Finger: %"SDL_PRIs64",x: %f, y: %f",event.tfinger.fingerId,
  175. event.tfinger.x,event.tfinger.y);
  176. break;
  177. case SDL_FINGERDOWN:
  178. SDL_Log("Finger: %"SDL_PRIs64" down - x: %f, y: %f",
  179. event.tfinger.fingerId,event.tfinger.x,event.tfinger.y);
  180. break;
  181. case SDL_FINGERUP:
  182. SDL_Log("Finger: %"SDL_PRIs64" up - x: %f, y: %f",
  183. event.tfinger.fingerId,event.tfinger.x,event.tfinger.y);
  184. break;
  185. #endif
  186. case SDL_MULTIGESTURE:
  187. #if VERBOSE
  188. SDL_Log("Multi Gesture: x = %f, y = %f, dAng = %f, dR = %f",
  189. event.mgesture.x, event.mgesture.y,
  190. event.mgesture.dTheta, event.mgesture.dDist);
  191. SDL_Log("MG: numDownTouch = %i",event.mgesture.numFingers);
  192. #endif
  193. knob.p.x = event.mgesture.x;
  194. knob.p.y = event.mgesture.y;
  195. knob.ang += event.mgesture.dTheta;
  196. knob.r += event.mgesture.dDist;
  197. break;
  198. case SDL_DOLLARGESTURE:
  199. SDL_Log("Gesture %"SDL_PRIs64" performed, error: %f",
  200. event.dgesture.gestureId, event.dgesture.error);
  201. break;
  202. case SDL_DOLLARRECORD:
  203. SDL_Log("Recorded gesture: %"SDL_PRIs64"",event.dgesture.gestureId);
  204. break;
  205. }
  206. }
  207. for (i = 0; i < state->num_windows; ++i) {
  208. if (state->windows[i]) {
  209. DrawScreen(state->windows[i]);
  210. }
  211. }
  212. #ifdef __EMSCRIPTEN__
  213. if (quitting) {
  214. emscripten_cancel_main_loop();
  215. }
  216. #endif
  217. }
  218. int main(int argc, char* argv[])
  219. {
  220. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  221. if (!state) {
  222. return 1;
  223. }
  224. state->window_title = "Gesture Test";
  225. state->window_w = WIDTH;
  226. state->window_h = HEIGHT;
  227. state->skip_renderer = SDL_TRUE;
  228. if (!SDLTest_CommonDefaultArgs(state, argc, argv) || !SDLTest_CommonInit(state)) {
  229. SDLTest_CommonQuit(state);
  230. return 1;
  231. }
  232. #ifdef __EMSCRIPTEN__
  233. emscripten_set_main_loop(loop, 0, 1);
  234. #else
  235. while (!quitting) {
  236. loop();
  237. }
  238. #endif
  239. SDLTest_CommonQuit(state);
  240. return 0;
  241. }