testgesture.c 7.9 KB

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