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