test/testgesture.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 09 Mar 2011 15:08:41 -0800
changeset 5458 a96fcc853bef
parent 5457 e4ee8761f5cd
child 5460 43582fccdc0d
permissions -rw-r--r--
Use SDL_Log() so the messages show up on iPhone and Android
     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)\n", 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 = SDL_max(0,eventWrite - EVENT_BUF_SIZE);i < eventWrite;i++) {
   151     SDL_Event event = events[i&(EVENT_BUF_SIZE-1)];
   152     int age = eventWrite - i - 1;
   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.touchId%7]; 
   167       col = 
   168 	((unsigned int)(c*(.1+.85))) |
   169 	((unsigned int)((0xFF*(1-((float)age)/EVENT_BUF_SIZE))) & 0xFF)<<24;
   170 
   171       if(event.type == SDL_FINGERMOTION)
   172 	drawCircle(screen,x*screen->w,y*screen->h,5,col);
   173       else if(event.type == SDL_FINGERDOWN)
   174 	drawCircle(screen,x*screen->w,y*screen->h,-10,col);     
   175     }
   176   }
   177   
   178   if(knob.p.x > 0)
   179     drawKnob(screen,knob);
   180   
   181   SDL_UpdateWindowSurface(window);
   182 }
   183 
   184 SDL_Surface* initScreen(int width,int height)
   185 {
   186   if (!window) {
   187     window = SDL_CreateWindow("Gesture Test",
   188                               SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
   189                               WIDTH, HEIGHT, SDL_WINDOW_RESIZABLE);
   190   }
   191   if (!window) {
   192     return NULL;
   193   }
   194   return SDL_GetWindowSurface(window);
   195 }
   196 
   197 int main(int argc, char* argv[])
   198 {  
   199   SDL_Surface *screen;
   200   SDL_Event event;
   201   SDL_bool quitting = SDL_FALSE;
   202   SDL_RWops *src;
   203 
   204   //gesture variables
   205   knob.r = .1f;
   206   knob.ang = 0;
   207 
   208   if (SDL_Init(SDL_INIT_VIDEO) < 0 ) return 1;
   209   
   210   if (!(screen = initScreen(WIDTH,HEIGHT)))
   211     {
   212       SDL_Quit();
   213       return 1;
   214     }
   215 
   216   while(!quitting) {
   217     while(SDL_PollEvent(&event)) 
   218       {
   219 	//Record _all_ events
   220 	events[eventWrite & (EVENT_BUF_SIZE-1)] = event;
   221 	eventWrite++;
   222 	
   223 	switch (event.type) 
   224 	  {
   225 	  case SDL_QUIT:
   226 	    quitting = SDL_TRUE;
   227 	    break;
   228 	  case SDL_KEYDOWN:
   229 	    switch (event.key.keysym.sym)
   230 	      {
   231 	      case SDLK_SPACE:
   232 		SDL_RecordGesture(-1);
   233 		break;
   234 	      case SDLK_s:
   235 		src = SDL_RWFromFile("gestureSave","w");
   236 		SDL_Log("Wrote %i templates\n",SDL_SaveAllDollarTemplates(src));
   237 		SDL_RWclose(src);
   238 		break;
   239 	      case SDLK_l:
   240 		src = SDL_RWFromFile("gestureSave","r");
   241 		SDL_Log("Loaded: %i\n",SDL_LoadDollarTemplates(-1,src));
   242 		SDL_RWclose(src);
   243 		break;
   244 	      case SDLK_ESCAPE:
   245 		quitting = SDL_TRUE;
   246 		break;
   247 	    }
   248 	    break;
   249 	  case SDL_WINDOWEVENT:
   250             if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
   251 	      if (!(screen = initScreen(0, 0)))
   252 	      {
   253 		SDL_Quit();
   254 		return 1;
   255 	      }
   256             }
   257 	    break;
   258 	  case SDL_FINGERMOTION:
   259 #if VERBOSE
   260 	    SDL_Log("Finger: %i,x: %i, y: %i\n",event.tfinger.fingerId,
   261 	    	   event.tfinger.x,event.tfinger.y);
   262 #endif
   263 		{
   264 			SDL_Touch* inTouch = SDL_GetTouch(event.tfinger.touchId);
   265 			SDL_Finger* inFinger = SDL_GetFinger(inTouch,event.tfinger.fingerId);
   266 		}
   267 	    break;	    
   268 	  case SDL_FINGERDOWN:
   269 #if VERBOSE
   270 	    SDL_Log("Finger: %"PRIs64" down - x: %i, y: %i\n",
   271 		   event.tfinger.fingerId,event.tfinger.x,event.tfinger.y);
   272 #endif
   273 	    break;
   274 	  case SDL_FINGERUP:
   275 #if VERBOSE
   276 	    SDL_Log("Finger: %"PRIs64" up - x: %i, y: %i\n",
   277 	    	   event.tfinger.fingerId,event.tfinger.x,event.tfinger.y);
   278 #endif
   279 	    break;
   280 	  case SDL_MULTIGESTURE:
   281 #if VERBOSE	    
   282 	    SDL_Log("Multi Gesture: x = %f, y = %f, dAng = %f, dR = %f\n",
   283 		   event.mgesture.x,
   284 		   event.mgesture.y,
   285 		   event.mgesture.dTheta,
   286 		   event.mgesture.dDist);
   287 	    SDL_Log("MG: numDownTouch = %i\n",event.mgesture.numFingers);
   288 #endif
   289 	    knob.p.x = event.mgesture.x;
   290 	    knob.p.y = event.mgesture.y;
   291 	    knob.ang += event.mgesture.dTheta;
   292 	    knob.r += event.mgesture.dDist;
   293 	    break;
   294 	  case SDL_DOLLARGESTURE:
   295 	    SDL_Log("Gesture %"PRIs64" performed, error: %f\n",
   296 		   event.dgesture.gestureId,
   297 		   event.dgesture.error);
   298 	    break;
   299 	  case SDL_DOLLARRECORD:
   300 	    SDL_Log("Recorded gesture: %"PRIs64"\n",event.dgesture.gestureId);
   301 	    break;
   302 	  }
   303       }
   304     DrawScreen(screen);
   305   }  
   306   SDL_Quit();  
   307   return 0;
   308 }
   309