1
0

testgesture.c 7.6 KB

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