test/testgesture.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 20 Jan 2011 18:04:05 -0800
changeset 5062 e8916fe9cfc8
parent 4921 40a43c6d9220
child 5086 c2539ff054c8
permissions -rw-r--r--
Fixed bug #925

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