test/testgesture.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 09 Mar 2011 14:46:09 -0800
changeset 5456 63258b963fc1
parent 5086 c2539ff054c8
child 5457 e4ee8761f5cd
permissions -rw-r--r--
Fixed testgesture to work on the iPhone
     1 /*  Usage:
     2  *  Spacebar to begin recording a gesture on all touches.
     3  *  s to save all touches into "./gestureSave"
     4  *  l to load all touches from "./gestureSave"
     5  */
     6 
     7 #include <stdio.h>
     8 #include <math.h>
     9 
    10 #include "SDL.h"
    11 #include "SDL_touch.h"
    12 #include "SDL_gesture.h"
    13 
    14 
    15 /* Make sure we have good macros for printing 32 and 64 bit values */
    16 #ifndef PRIs32
    17 #define PRIs32 "d"
    18 #endif
    19 #ifndef PRIu32
    20 #define PRIu32 "u"
    21 #endif
    22 #ifndef PRIs64
    23 #ifdef __WIN32__
    24 #define PRIs64 "I64"
    25 #else
    26 #define PRIs64 "lld"
    27 #endif
    28 #endif
    29 #ifndef PRIu64
    30 #ifdef __WIN32__
    31 #define PRIu64 "I64u"
    32 #else
    33 #define PRIu64 "llu"
    34 #endif
    35 #endif
    36 
    37 #define WIDTH 640
    38 #define HEIGHT 480
    39 #define BPP 4
    40 #define DEPTH 32
    41 
    42 //MUST BE A POWER OF 2!
    43 #define EVENT_BUF_SIZE 256
    44 
    45 
    46 #define VERBOSE SDL_FALSE
    47 
    48 SDL_Window *window;
    49 SDL_Event events[EVENT_BUF_SIZE];
    50 int eventWrite;
    51 
    52 
    53 int colors[7] = {0xFF,0xFF00,0xFF0000,0xFFFF00,0x00FFFF,0xFF00FF,0xFFFFFF};
    54 
    55 typedef struct {
    56   float x,y;
    57 } Point;
    58 
    59 typedef struct {
    60   float ang,r;
    61   Point p;
    62 } Knob;
    63 
    64 Knob knob;
    65 
    66 void handler (int sig)
    67 {
    68   printf ("exiting...(%d)\n", sig);
    69   exit (0);
    70 }
    71 
    72 void perror_exit (char *error)
    73 {
    74   perror (error);
    75   handler (9);
    76 }
    77 
    78 void setpix(SDL_Surface *screen, float _x, float _y, unsigned int col)
    79 {
    80   Uint32 *pixmem32;
    81   Uint32 colour;
    82   Uint8 r,g,b;
    83   int x = (int)_x;
    84   int y = (int)_y;
    85   float a;
    86   
    87   if(x < 0 || x > screen->w) return;
    88   if(y < 0 || y > screen->h) return;
    89 
    90   pixmem32 = (Uint32*) screen->pixels  + y*screen->pitch/BPP + x;
    91   
    92   SDL_memcpy(&colour,pixmem32,screen->format->BytesPerPixel);
    93 
    94   SDL_GetRGB(colour,screen->format,&r,&g,&b);
    95   //r = 0;g = 0; b = 0;
    96   a = (float)((col>>24)&0xFF);
    97   if(a == 0) a = 0xFF; //Hack, to make things easier.
    98   a /= 0xFF;
    99   r = (Uint8)(r*(1-a) + ((col>>16)&0xFF)*(a));
   100   g = (Uint8)(g*(1-a) + ((col>> 8)&0xFF)*(a));
   101   b = (Uint8)(b*(1-a) + ((col>> 0)&0xFF)*(a));
   102   colour = SDL_MapRGB( screen->format,r, g, b);
   103   
   104 
   105   *pixmem32 = colour;
   106 }
   107 
   108 void drawLine(SDL_Surface *screen,float x0,float y0,float x1,float y1,unsigned int col) {
   109   float t;
   110   for(t=0;t<1;t+=(float)(1.f/SDL_max(SDL_fabs(x0-x1),SDL_fabs(y0-y1))))
   111     setpix(screen,x1+t*(x0-x1),y1+t*(y0-y1),col);
   112 }
   113 
   114 void drawCircle(SDL_Surface* screen,float x,float y,float r,unsigned int c)
   115 {
   116   float tx,ty;
   117   float xr;
   118   for(ty = (float)-SDL_fabs(r);ty <= (float)SDL_fabs((int)r);ty++) {
   119     xr = (float)sqrt(r*r - ty*ty);
   120     if(r > 0) { //r > 0 ==> filled circle
   121       for(tx=-xr+.5f;tx<=xr-.5;tx++) {
   122 	setpix(screen,x+tx,y+ty,c);
   123       }
   124     }
   125     else {
   126       setpix(screen,x-xr+.5f,y+ty,c);
   127       setpix(screen,x+xr-.5f,y+ty,c);
   128     }
   129   }
   130 }
   131 
   132 void drawKnob(SDL_Surface* screen,Knob k) {
   133   drawCircle(screen,k.p.x*screen->w,k.p.y*screen->h,k.r*screen->w,0xFFFFFF);  
   134   drawCircle(screen,(k.p.x+k.r/2*cosf(k.ang))*screen->w,
   135   	            (k.p.y+k.r/2*sinf(k.ang))*screen->h,k.r/4*screen->w,0);
   136 }
   137 
   138 void DrawScreen(SDL_Surface* screen)
   139 {
   140   int i;
   141 #if 1
   142   SDL_FillRect(screen, NULL, 0);
   143 #else
   144   int x, y;
   145   for(y = 0;y < screen->h;y++)
   146     for(x = 0;x < screen->w;x++)
   147 	setpix(screen,(float)x,(float)y,((x%255)<<16) + ((y%255)<<8) + (x+y)%255);
   148 #endif
   149 
   150   //draw Touch History
   151   for(i = SDL_max(0,eventWrite - EVENT_BUF_SIZE);i < eventWrite;i++) {
   152     SDL_Event event = events[i&(EVENT_BUF_SIZE-1)];
   153     int age = eventWrite - i - 1;
   154 	float x, y;
   155 	unsigned int c, col;
   156 
   157     if(event.type == SDL_FINGERMOTION || 
   158        event.type == SDL_FINGERDOWN ||
   159        event.type == SDL_FINGERUP) {
   160       SDL_Touch* inTouch = SDL_GetTouch(event.tfinger.touchId);
   161       if(inTouch == NULL) continue;
   162 
   163       x = ((float)event.tfinger.x)/inTouch->xres;
   164       y = ((float)event.tfinger.y)/inTouch->yres;      
   165       
   166       //draw the touch:      
   167       c = colors[event.tfinger.touchId%7]; 
   168       col = 
   169 	((unsigned int)(c*(.1+.85))) |
   170 	((unsigned int)((0xFF*(1-((float)age)/EVENT_BUF_SIZE))) & 0xFF)<<24;
   171 
   172       if(event.type == SDL_FINGERMOTION)
   173 	drawCircle(screen,x*screen->w,y*screen->h,5,col);
   174       else if(event.type == SDL_FINGERDOWN)
   175 	drawCircle(screen,x*screen->w,y*screen->h,-10,col);     
   176     }
   177   }
   178   
   179   if(knob.p.x > 0)
   180     drawKnob(screen,knob);
   181   
   182   SDL_UpdateWindowSurface(window);
   183 }
   184 
   185 SDL_Surface* initScreen(int width,int height)
   186 {
   187   if (!window) {
   188     window = SDL_CreateWindow("Gesture Test",
   189                               SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
   190                               WIDTH, HEIGHT, SDL_WINDOW_RESIZABLE);
   191   }
   192   if (!window) {
   193     return NULL;
   194   }
   195   return SDL_GetWindowSurface(window);
   196 }
   197 
   198 int main(int argc, char* argv[])
   199 {  
   200   SDL_Surface *screen;
   201   SDL_Event event;
   202   SDL_bool quitting = SDL_FALSE;
   203   SDL_RWops *src;
   204 
   205   //gesture variables
   206   knob.r = .1f;
   207   knob.ang = 0;
   208 
   209   if (SDL_Init(SDL_INIT_VIDEO) < 0 ) return 1;
   210   
   211   if (!(screen = initScreen(WIDTH,HEIGHT)))
   212     {
   213       SDL_Quit();
   214       return 1;
   215     }
   216 
   217   while(!quitting) {
   218     while(SDL_PollEvent(&event)) 
   219       {
   220 	//Record _all_ events
   221 	events[eventWrite & (EVENT_BUF_SIZE-1)] = event;
   222 	eventWrite++;
   223 	
   224 	switch (event.type) 
   225 	  {
   226 	  case SDL_QUIT:
   227 	    quitting = SDL_TRUE;
   228 	    break;
   229 	  case SDL_KEYDOWN:
   230 	    switch (event.key.keysym.sym)
   231 	      {
   232 	      case SDLK_SPACE:
   233 		SDL_RecordGesture(-1);
   234 		break;
   235 	      case SDLK_s:
   236 		src = SDL_RWFromFile("gestureSave","w");
   237 		printf("Wrote %i templates\n",SDL_SaveAllDollarTemplates(src));
   238 		SDL_RWclose(src);
   239 		break;
   240 	      case SDLK_l:
   241 		src = SDL_RWFromFile("gestureSave","r");
   242 		printf("Loaded: %i\n",SDL_LoadDollarTemplates(-1,src));
   243 		SDL_RWclose(src);
   244 		break;
   245 	      case SDLK_ESCAPE:
   246 		quitting = SDL_TRUE;
   247 		break;
   248 	    }
   249 	    break;
   250 	  case SDL_WINDOWEVENT:
   251             if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
   252 	      if (!(screen = initScreen(0, 0)))
   253 	      {
   254 		SDL_Quit();
   255 		return 1;
   256 	      }
   257             }
   258 	    break;
   259 	  case SDL_FINGERMOTION:
   260 #if VERBOSE
   261 	    printf("Finger: %i,x: %i, y: %i\n",event.tfinger.fingerId,
   262 	    	   event.tfinger.x,event.tfinger.y);
   263 #endif
   264 		{
   265 			SDL_Touch* inTouch = SDL_GetTouch(event.tfinger.touchId);
   266 			SDL_Finger* inFinger = SDL_GetFinger(inTouch,event.tfinger.fingerId);
   267 		}
   268 	    break;	    
   269 	  case SDL_FINGERDOWN:
   270 #if VERBOSE
   271 	    printf("Finger: %"PRIs64" down - x: %i, y: %i\n",
   272 		   event.tfinger.fingerId,event.tfinger.x,event.tfinger.y);
   273 #endif
   274 	    break;
   275 	  case SDL_FINGERUP:
   276 #if VERBOSE
   277 	    printf("Finger: %"PRIs64" up - x: %i, y: %i\n",
   278 	    	   event.tfinger.fingerId,event.tfinger.x,event.tfinger.y);
   279 #endif
   280 	    break;
   281 	  case SDL_MULTIGESTURE:
   282 #if VERBOSE	    
   283 	    printf("Multi Gesture: x = %f, y = %f, dAng = %f, dR = %f\n",
   284 		   event.mgesture.x,
   285 		   event.mgesture.y,
   286 		   event.mgesture.dTheta,
   287 		   event.mgesture.dDist);
   288 	    printf("MG: numDownTouch = %i\n",event.mgesture.numFingers);
   289 #endif
   290 	    knob.p.x = event.mgesture.x;
   291 	    knob.p.y = event.mgesture.y;
   292 	    knob.ang += event.mgesture.dTheta;
   293 	    knob.r += event.mgesture.dDist;
   294 	    break;
   295 	  case SDL_DOLLARGESTURE:
   296 	    printf("Gesture %"PRIs64" performed, error: %f\n",
   297 		   event.dgesture.gestureId,
   298 		   event.dgesture.error);
   299 	    break;
   300 	  case SDL_DOLLARRECORD:
   301 	    printf("Recorded gesture: %"PRIs64"\n",event.dgesture.gestureId);
   302 	    break;
   303 	  }
   304       }
   305     DrawScreen(screen);
   306   }  
   307   SDL_Quit();  
   308   return 0;
   309 }
   310