test/testgesture.c
changeset 4689 f9ab8df6d45a
child 4918 f5f70fed2c4c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testgesture.c	Sun Aug 15 00:36:28 2010 -0400
     1.3 @@ -0,0 +1,297 @@
     1.4 +/*  Usage:
     1.5 + *  Spacebar to begin recording a gesture on all touches.
     1.6 + *  s to save all touches into "./gestureSave"
     1.7 + *  l to load all touches from "./gestureSave"
     1.8 + */
     1.9 +
    1.10 +#include <stdio.h>
    1.11 +#include <SDL.h>
    1.12 +#include <math.h>
    1.13 +#include <SDL_touch.h>
    1.14 +#include <SDL_gesture.h>
    1.15 +
    1.16 +
    1.17 +/* Make sure we have good macros for printing 32 and 64 bit values */
    1.18 +#ifndef PRIs32
    1.19 +#define PRIs32 "d"
    1.20 +#endif
    1.21 +#ifndef PRIu32
    1.22 +#define PRIu32 "u"
    1.23 +#endif
    1.24 +#ifndef PRIs64
    1.25 +#ifdef __WIN32__
    1.26 +#define PRIs64 "I64"
    1.27 +#else
    1.28 +#define PRIs64 "lld"
    1.29 +#endif
    1.30 +#endif
    1.31 +#ifndef PRIu64
    1.32 +#ifdef __WIN32__
    1.33 +#define PRIu64 "I64u"
    1.34 +#else
    1.35 +#define PRIu64 "llu"
    1.36 +#endif
    1.37 +#endif
    1.38 +
    1.39 +#define WIDTH 640
    1.40 +#define HEIGHT 480
    1.41 +#define BPP 4
    1.42 +#define DEPTH 32
    1.43 +
    1.44 +//MUST BE A POWER OF 2!
    1.45 +#define EVENT_BUF_SIZE 256
    1.46 +
    1.47 +
    1.48 +#define VERBOSE SDL_FALSE
    1.49 +
    1.50 +SDL_Event events[EVENT_BUF_SIZE];
    1.51 +int eventWrite;
    1.52 +
    1.53 +int colors[7] = {0xFF,0xFF00,0xFF0000,0xFFFF00,0x00FFFF,0xFF00FF,0xFFFFFF};
    1.54 +
    1.55 +typedef struct {
    1.56 +  float x,y;
    1.57 +} Point;
    1.58 +
    1.59 +typedef struct {
    1.60 +  float ang,r;
    1.61 +  Point p;
    1.62 +} Knob;
    1.63 +
    1.64 +Knob knob;
    1.65 +
    1.66 +void handler (int sig)
    1.67 +{
    1.68 +  printf ("\exiting...(%d)\n", sig);
    1.69 +  exit (0);
    1.70 +}
    1.71 +
    1.72 +void perror_exit (char *error)
    1.73 +{
    1.74 +  perror (error);
    1.75 +  handler (9);
    1.76 +}
    1.77 +
    1.78 +void setpix(SDL_Surface *screen, int x, int y, unsigned int col)
    1.79 +{
    1.80 +  Uint32 *pixmem32;
    1.81 +  Uint32 colour;
    1.82 +  
    1.83 +  if((unsigned)x > screen->w) return;
    1.84 +  if((unsigned)y > screen->h) return;
    1.85 +
    1.86 +  pixmem32 = (Uint32*) screen->pixels  + y*screen->pitch/BPP + x;
    1.87 +  
    1.88 +  Uint8 r,g,b;
    1.89 +  float a;
    1.90 +  
    1.91 +  memcpy(&colour,pixmem32,screen->format->BytesPerPixel);
    1.92 +
    1.93 +  SDL_GetRGB(colour,screen->format,&r,&g,&b);
    1.94 +  //r = 0;g = 0; b = 0;
    1.95 +  a = (col>>24)&0xFF;
    1.96 +  if(a == 0) a = 0xFF; //Hack, to make things easier.
    1.97 +  a /= 0xFF;
    1.98 +  r = r*(1-a) + ((col>>16)&0xFF)*(a);
    1.99 +  g = g*(1-a) + ((col>> 8)&0xFF)*(a);
   1.100 +  b = b*(1-a) + ((col>> 0)&0xFF)*(a);
   1.101 +  colour = SDL_MapRGB( screen->format,r, g, b);
   1.102 +  
   1.103 +
   1.104 +  *pixmem32 = colour;
   1.105 +}
   1.106 +
   1.107 +void drawLine(SDL_Surface *screen,int x0,int y0,int x1,int y1,unsigned int col) {
   1.108 +  float t;
   1.109 +  for(t=0;t<1;t+=1.f/SDL_max(abs(x0-x1),abs(y0-y1)))
   1.110 +    setpix(screen,x1+t*(x0-x1),y1+t*(y0-y1),col);
   1.111 +}
   1.112 +
   1.113 +void drawCircle(SDL_Surface* screen,int x,int y,int r,unsigned int c)
   1.114 +{
   1.115 +  int tx,ty;
   1.116 +  float xr;
   1.117 +  for(ty = -abs(r);ty <= abs(r);ty++) {
   1.118 +    xr = sqrt(r*r - ty*ty);
   1.119 +    if(r > 0) { //r > 0 ==> filled circle
   1.120 +      for(tx=-xr+.5;tx<=xr-.5;tx++) {
   1.121 +	setpix(screen,x+tx,y+ty,c);
   1.122 +      }
   1.123 +    }
   1.124 +    else {
   1.125 +      setpix(screen,x-xr+.5,y+ty,c);
   1.126 +      setpix(screen,x+xr-.5,y+ty,c);
   1.127 +    }
   1.128 +  }
   1.129 +}
   1.130 +
   1.131 +void drawKnob(SDL_Surface* screen,Knob k) {
   1.132 +  drawCircle(screen,k.p.x*screen->w,k.p.y*screen->h,k.r*screen->w,0xFFFFFF);  
   1.133 +  drawCircle(screen,(k.p.x+k.r/2*cos(k.ang))*screen->w,
   1.134 +  	            (k.p.y+k.r/2*sin(k.ang))*screen->h,k.r/4*screen->w,0);
   1.135 +}
   1.136 +
   1.137 +void DrawScreen(SDL_Surface* screen)
   1.138 +{
   1.139 +  int x, y;
   1.140 +  if(SDL_MUSTLOCK(screen))
   1.141 +    {                                              
   1.142 +      if(SDL_LockSurface(screen) < 0) return;
   1.143 +    }
   1.144 +  for(y = 0;y < screen->h;y++)
   1.145 +    for(x = 0;x < screen->w;x++)
   1.146 +	setpix(screen,x,y,((x%255)<<16) + ((y%255)<<8) + (x+y)%255);
   1.147 +
   1.148 +  int i;
   1.149 +  //draw Touch History
   1.150 +  for(i = SDL_max(0,eventWrite - EVENT_BUF_SIZE);i < eventWrite;i++) {
   1.151 +    SDL_Event event = events[i&(EVENT_BUF_SIZE-1)];
   1.152 +    int age = eventWrite - i - 1;
   1.153 +    if(event.type == SDL_FINGERMOTION || 
   1.154 +       event.type == SDL_FINGERDOWN ||
   1.155 +       event.type == SDL_FINGERUP) {
   1.156 +      SDL_Touch* inTouch = SDL_GetTouch(event.tfinger.touchId);
   1.157 +      if(inTouch == NULL) continue;
   1.158 +
   1.159 +      float x = ((float)event.tfinger.x)/inTouch->xres;
   1.160 +      float y = ((float)event.tfinger.y)/inTouch->yres;      
   1.161 +      
   1.162 +      //draw the touch:      
   1.163 +      unsigned int c = colors[event.tfinger.touchId%7]; 
   1.164 +      unsigned int col = 
   1.165 +	((unsigned int)(c*(.1+.85))) |
   1.166 +	((unsigned int)((0xFF*(1-((float)age)/EVENT_BUF_SIZE))) & 0xFF)<<24;
   1.167 +
   1.168 +      if(event.type == SDL_FINGERMOTION)
   1.169 +	drawCircle(screen,x*screen->w,y*screen->h,5,col);
   1.170 +      else if(event.type == SDL_FINGERDOWN)
   1.171 +	drawCircle(screen,x*screen->w,y*screen->h,-10,col);     
   1.172 +    }
   1.173 +  }
   1.174 +  
   1.175 +  if(knob.p.x > 0)
   1.176 +    drawKnob(screen,knob);
   1.177 +  
   1.178 +  if(SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen);
   1.179 +  SDL_Flip(screen);
   1.180 +}
   1.181 +
   1.182 +SDL_Surface* initScreen(int width,int height)
   1.183 +{
   1.184 +  return SDL_SetVideoMode(width, height, DEPTH,
   1.185 +			  SDL_HWSURFACE | SDL_RESIZABLE);
   1.186 +}
   1.187 +
   1.188 +int main(int argc, char* argv[])
   1.189 +{  
   1.190 +  SDL_Surface *screen;
   1.191 +  SDL_Event event;
   1.192 +
   1.193 +  //gesture variables
   1.194 +  knob.r = .1;
   1.195 +  knob.ang = 0;
   1.196 +
   1.197 +  
   1.198 +  SDL_bool quitting = SDL_FALSE;
   1.199 +  SDL_RWops *src;
   1.200 +
   1.201 +  if (SDL_Init(SDL_INIT_VIDEO) < 0 ) return 1;
   1.202 +  
   1.203 +  if (!(screen = initScreen(WIDTH,HEIGHT)))
   1.204 +    {
   1.205 +      SDL_Quit();
   1.206 +      return 1;
   1.207 +    }
   1.208 +
   1.209 +  while(!quitting) {
   1.210 +    while(SDL_PollEvent(&event)) 
   1.211 +      {
   1.212 +	//Record _all_ events
   1.213 +	events[eventWrite & (EVENT_BUF_SIZE-1)] = event;
   1.214 +	eventWrite++;
   1.215 +	
   1.216 +	switch (event.type) 
   1.217 +	  {
   1.218 +	  case SDL_QUIT:
   1.219 +	    quitting = SDL_TRUE;
   1.220 +	    break;
   1.221 +	  case SDL_KEYDOWN:
   1.222 +	    switch (event.key.keysym.sym)
   1.223 +	      {
   1.224 +	      case SDLK_SPACE:
   1.225 +		SDL_RecordGesture(-1);
   1.226 +		break;
   1.227 +	      case SDLK_s:
   1.228 +		src = SDL_RWFromFile("gestureSave","w");
   1.229 +		printf("Wrote %i templates\n",SDL_SaveAllDollarTemplates(src));
   1.230 +		SDL_RWclose(src);
   1.231 +		break;
   1.232 +	      case SDLK_l:
   1.233 +		src = SDL_RWFromFile("gestureSave","r");
   1.234 +		printf("Loaded: %i\n",SDL_LoadDollarTemplates(-1,src));
   1.235 +		SDL_RWclose(src);
   1.236 +		break;
   1.237 +	      case SDLK_ESCAPE:
   1.238 +		quitting = SDL_TRUE;
   1.239 +		break;
   1.240 +	    }
   1.241 +	    break;
   1.242 +	  case SDL_VIDEORESIZE:
   1.243 +	    if (!(screen = initScreen(event.resize.w,
   1.244 +				      event.resize.h)))
   1.245 +	      {
   1.246 +		SDL_Quit();
   1.247 +		return 1;
   1.248 +	      }
   1.249 +	    break;
   1.250 +	  case SDL_FINGERMOTION:    
   1.251 +	    ;
   1.252 +#if VERBOSE
   1.253 +	    printf("Finger: %i,x: %i, y: %i\n",event.tfinger.fingerId,
   1.254 +	    	   event.tfinger.x,event.tfinger.y);
   1.255 +#endif
   1.256 +	    SDL_Touch* inTouch = SDL_GetTouch(event.tfinger.touchId);
   1.257 +	    SDL_Finger* inFinger = SDL_GetFinger(inTouch,event.tfinger.fingerId);
   1.258 +	    break;	    
   1.259 +	  case SDL_FINGERDOWN:
   1.260 +#if VERBOSE
   1.261 +	    printf("Finger: %"PRIs64" down - x: %i, y: %i\n",
   1.262 +		   event.tfinger.fingerId,event.tfinger.x,event.tfinger.y);
   1.263 +#endif
   1.264 +	    break;
   1.265 +	  case SDL_FINGERUP:
   1.266 +#if VERBOSE
   1.267 +	    printf("Finger: %"PRIs64" up - x: %i, y: %i\n",
   1.268 +	    	   event.tfinger.fingerId,event.tfinger.x,event.tfinger.y);
   1.269 +#endif
   1.270 +	    break;
   1.271 +	  case SDL_MULTIGESTURE:
   1.272 +#if VERBOSE	    
   1.273 +	    printf("Multi Gesture: x = %f, y = %f, dAng = %f, dR = %f\n",
   1.274 +		   event.mgesture.x,
   1.275 +		   event.mgesture.y,
   1.276 +		   event.mgesture.dTheta,
   1.277 +		   event.mgesture.dDist);
   1.278 +	    printf("MG: numDownTouch = %i\n",event.mgesture.numFingers);
   1.279 +#endif
   1.280 +	    knob.p.x = event.mgesture.x;
   1.281 +	    knob.p.y = event.mgesture.y;
   1.282 +	    knob.ang += event.mgesture.dTheta;
   1.283 +	    knob.r += event.mgesture.dDist;
   1.284 +	    break;
   1.285 +	  case SDL_DOLLARGESTURE:
   1.286 +	    printf("Gesture %"PRIs64" performed, error: %f\n",
   1.287 +		   event.dgesture.gestureId,
   1.288 +		   event.dgesture.error);
   1.289 +	    break;
   1.290 +	  case SDL_DOLLARRECORD:
   1.291 +	    printf("Recorded gesture: %"PRIs64"\n",event.dgesture.gestureId);
   1.292 +	    break;
   1.293 +	  }
   1.294 +      }
   1.295 +    DrawScreen(screen);
   1.296 +  }  
   1.297 +  SDL_Quit();  
   1.298 +  return 0;
   1.299 +}
   1.300 +