testgesture.c 7.6 KB

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