Added gesture test c file. Beginning of gesture recognition.
authorJim Grandpre <jim.tla@gmail.com>
Mon, 14 Jun 2010 01:44:04 -0400
changeset 465186c171888eee
parent 4646 eea1bf53effa
child 4652 78f6e0b80d1e
Added gesture test c file. Beginning of gesture recognition.
Makefile.in
touchTest/TODO
touchTest/gestureTest.c
touchTest/makefile
     1.1 --- a/Makefile.in	Tue Jun 01 02:54:33 2010 -0400
     1.2 +++ b/Makefile.in	Mon Jun 14 01:44:04 2010 -0400
     1.3 @@ -44,7 +44,7 @@
     1.4  
     1.5  DIST = acinclude autogen.sh Borland.html Borland.zip BUGS build-scripts configure configure.in COPYING CREDITS include INSTALL Makefile.minimal Makefile.in README* sdl-config.in sdl.m4 sdl.pc.in SDL.spec SDL.spec.in src test TODO VisualC.html VisualC VisualCE Watcom-Win32.zip WhatsNew Xcode Xcode-iPhoneOS
     1.6  
     1.7 -HDRS = SDL.h SDL_assert.h SDL_atomic.h SDL_audio.h SDL_compat.h SDL_cpuinfo.h SDL_endian.h SDL_error.h SDL_events.h SDL_haptic.h SDL_joystick.h SDL_keyboard.h SDL_keysym.h SDL_loadso.h SDL_main.h SDL_mouse.h SDL_mutex.h SDL_name.h SDL_opengl.h SDL_opengles.h SDL_pixels.h SDL_platform.h SDL_power.h SDL_quit.h SDL_rect.h SDL_revision.h SDL_rwops.h SDL_scancode.h SDL_stdinc.h SDL_surface.h SDL_syswm.h SDL_thread.h SDL_timer.h SDL_types.h SDL_version.h SDL_video.h begin_code.h close_code.h
     1.8 +HDRS = SDL.h SDL_assert.h SDL_atomic.h SDL_audio.h SDL_compat.h SDL_cpuinfo.h SDL_endian.h SDL_error.h SDL_events.h SDL_haptic.h SDL_joystick.h SDL_keyboard.h SDL_keysym.h SDL_loadso.h SDL_main.h SDL_mouse.h SDL_mutex.h SDL_name.h SDL_opengl.h SDL_opengles.h SDL_pixels.h SDL_platform.h SDL_power.h SDL_quit.h SDL_rect.h SDL_revision.h SDL_rwops.h SDL_scancode.h SDL_stdinc.h SDL_surface.h SDL_syswm.h SDL_thread.h SDL_timer.h SDL_types.h SDL_version.h SDL_video.h begin_code.h close_code.h SDL_touch.h
     1.9  
    1.10  LT_AGE      = @LT_AGE@
    1.11  LT_CURRENT  = @LT_CURRENT@
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/touchTest/TODO	Mon Jun 14 01:44:04 2010 -0400
     2.3 @@ -0,0 +1,1 @@
     2.4 +Pressure is sometimes negative
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/touchTest/gestureTest.c	Mon Jun 14 01:44:04 2010 -0400
     3.3 @@ -0,0 +1,360 @@
     3.4 +#include <stdio.h>
     3.5 +#include <SDL.h>
     3.6 +#include <math.h>
     3.7 +#include <SDL_touch.h>
     3.8 +
     3.9 +#define PI 3.1415926535897
    3.10 +#define WIDTH 640
    3.11 +#define HEIGHT 480
    3.12 +#define BPP 4
    3.13 +#define DEPTH 32
    3.14 +
    3.15 +#define MAXFINGERS 3
    3.16 +
    3.17 +
    3.18 +
    3.19 +
    3.20 +//MUST BE A POWER OF 2!
    3.21 +#define EVENT_BUF_SIZE 256
    3.22 +
    3.23 +SDL_Event events[EVENT_BUF_SIZE];
    3.24 +int eventWrite;
    3.25 +
    3.26 +int mousx,mousy;
    3.27 +int keystat[512];
    3.28 +int bstatus;
    3.29 +
    3.30 +int colors[7] = {0xFF,0xFF00,0xFF0000,0xFFFF00,0x00FFFF,0xFF00FF,0xFFFFFF};
    3.31 +
    3.32 +int index2fingerid[MAXFINGERS];
    3.33 +int fingersDown;
    3.34 +
    3.35 +typedef struct {
    3.36 +  float x,y;
    3.37 +} Point;
    3.38 +
    3.39 +typedef struct {
    3.40 +  Point p;
    3.41 +  float pressure;
    3.42 +  int id;
    3.43 +} Finger;
    3.44 +
    3.45 +
    3.46 +Finger finger[MAXFINGERS];
    3.47 +
    3.48 +Finger gestureLast[MAXFINGERS];
    3.49 +
    3.50 +void handler (int sig)
    3.51 +{
    3.52 +  printf ("\exiting...(%d)\n", sig);
    3.53 +  exit (0);
    3.54 +}
    3.55 +
    3.56 +void perror_exit (char *error)
    3.57 +{
    3.58 +  perror (error);
    3.59 +  handler (9);
    3.60 +}
    3.61 +
    3.62 +
    3.63 +void setpix(SDL_Surface *screen, int x, int y, unsigned int col)
    3.64 +{
    3.65 +  Uint32 *pixmem32;
    3.66 +  Uint32 colour;
    3.67 +  
    3.68 +  if((unsigned)x > screen->w) return;
    3.69 +  if((unsigned)y > screen->h) return;
    3.70 +
    3.71 +  pixmem32 = (Uint32*) screen->pixels  + y*screen->pitch/BPP + x;
    3.72 +  
    3.73 +  Uint8 r,g,b;
    3.74 +  float a;
    3.75 +  
    3.76 +  memcpy(&colour,pixmem32,screen->format->BytesPerPixel);
    3.77 +
    3.78 +  SDL_GetRGB(colour,screen->format,&r,&g,&b); //Always returns 0xFFFFFF?
    3.79 +  //r = 0;g = 0; b = 0;
    3.80 +  a = (col>>24)&0xFF;
    3.81 +  if(a == 0) a = 0xFF; //Hack, to make things easier.
    3.82 +  a /= 0xFF;
    3.83 +  r = r*(1-a) + ((col>>16)&0xFF)*(a);
    3.84 +  g = g*(1-a) + ((col>> 8)&0xFF)*(a);
    3.85 +  b = b*(1-a) + ((col>> 0)&0xFF)*(a);
    3.86 +  colour = SDL_MapRGB( screen->format,r, g, b);
    3.87 +  
    3.88 +
    3.89 +  *pixmem32 = colour;
    3.90 +}
    3.91 +
    3.92 +void drawCircle(SDL_Surface* screen,int x,int y,int r,unsigned int c)
    3.93 +{
    3.94 +
    3.95 +  float a;
    3.96 +  int tx;
    3.97 +  /*
    3.98 +  for(a=0;a<PI/2;a+=1.f/(float)abs(r))
    3.99 +  {
   3.100 +    if(r > 0) { //r > 0 ==> filled circle
   3.101 +      for(tx=x-r*cos(a);tx<x+r*cos(a);tx++) {
   3.102 +	setpix(screen,tx,(int)(y+r*sin(a)),c);
   3.103 +	setpix(screen,tx,(int)(y-r*sin(a)),c);
   3.104 +      }
   3.105 +    }
   3.106 +    else {
   3.107 +      //Draw Outline
   3.108 +      setpix(screen,(int)(x+r*cos(a)),(int)(y+r*sin(a)),c);
   3.109 +      setpix(screen,(int)(x-r*cos(a)),(int)(y+r*sin(a)),c);
   3.110 +      setpix(screen,(int)(x+r*cos(a)),(int)(y-r*sin(a)),c);
   3.111 +      setpix(screen,(int)(x-r*cos(a)),(int)(y-r*sin(a)),c);
   3.112 +    }
   3.113 +    }*/
   3.114 +  int ty;
   3.115 +  float xr;
   3.116 +  for(ty = -abs(r);ty <= abs(r);ty++) {
   3.117 +    xr = sqrt(r*r - ty*ty);
   3.118 +    if(r > 0) { //r > 0 ==> filled circle
   3.119 +      for(tx=-xr+.5;tx<=xr-.5;tx++) {
   3.120 +	setpix(screen,x+tx,y+ty,c);
   3.121 +      }
   3.122 +    }
   3.123 +    else {
   3.124 +      setpix(screen,x-xr+.5,y+ty,c);
   3.125 +      setpix(screen,x+xr-.5,y+ty,c);
   3.126 +    }
   3.127 +  }
   3.128 +}
   3.129 +
   3.130 +void DrawScreen(SDL_Surface* screen, int h)
   3.131 +{
   3.132 +  int x, y, xm,ym,c;
   3.133 +  if(SDL_MUSTLOCK(screen))
   3.134 +    {                                              
   3.135 +      if(SDL_LockSurface(screen) < 0) return;
   3.136 +    }
   3.137 +  for(y = 0; y < screen->h; y++ )
   3.138 +    {
   3.139 +      for( x = 0; x < screen->w; x++ )
   3.140 +        {
   3.141 +	  //setpixel(screen, x, y, (x*x)/256+3*y+h, (y*y)/256+x+h, h);
   3.142 +	  //xm = (x+h)%screen->w;
   3.143 +	  //ym = (y+h)%screen->w;
   3.144 +	  //c = sin(h/256*2*PI)*x*y/screen->w/screen->h;
   3.145 +	  //setpix(screen,x,y,255*sin(xm/screen->w*2*PI),sin(h/255*2*PI)*255*y/screen->h,c);
   3.146 +	  setpix(screen,x,y,((x%255)<<16) + ((y%255)<<8) + (x+y)%255);
   3.147 +	  //setpix(screen,x,y,0); //Inefficient, but that's okay...
   3.148 +        }
   3.149 +    }
   3.150 +  drawCircle(screen,mousx,mousy,-30,0xFFFFFF);
   3.151 +  
   3.152 +  int i;
   3.153 +//draw Touch History
   3.154 +  for(i = 0;i < MAXFINGERS;i++) gestureLast[i].id = -1;
   3.155 +  for(i = SDL_max(0,eventWrite - EVENT_BUF_SIZE);i != eventWrite;i++) {
   3.156 +    SDL_Event event = events[i&(EVENT_BUF_SIZE-1)];
   3.157 +    int age = eventWrite - i - 1;
   3.158 +    if(event.type == SDL_FINGERMOTION || 
   3.159 +       event.type == SDL_FINGERDOWN ||
   3.160 +       event.type == SDL_FINGERUP) {
   3.161 +      SDL_Touch* inTouch = SDL_GetTouch(event.tfinger.touchId);
   3.162 +      //SDL_Finger* inFinger = SDL_GetFinger(inTouch,event.tfinger.fingerId);
   3.163 +	    
   3.164 +      float x = ((float)event.tfinger.x)/inTouch->xres;
   3.165 +      float y = ((float)event.tfinger.y)/inTouch->yres;
   3.166 +      int j,empty = -1;
   3.167 +      for(j = 0;j<MAXFINGERS;j++) {
   3.168 +	if(gestureLast[j].id == event.tfinger.fingerId) {
   3.169 +	  if(event.type == SDL_FINGERUP) {
   3.170 +	    gestureLast[j].id = -1;
   3.171 +	    break;
   3.172 +	  }
   3.173 +	  else {
   3.174 +	    gestureLast[j].p.x = x;
   3.175 +	    gestureLast[j].p.y = y;
   3.176 +	    break;
   3.177 +	    //pressure?
   3.178 +	  }	  
   3.179 +	}
   3.180 +	else if(gestureLast[j].id == -1 && empty == -1) {
   3.181 +	  empty = j;
   3.182 +	}
   3.183 +      }
   3.184 +
   3.185 +      if(j >= MAXFINGERS && empty >= 0) {
   3.186 +	printf("Finger Down!!!\n");
   3.187 +	j = empty; //important that j is the index of the added finger
   3.188 +	gestureLast[j].id = event.tfinger.fingerId;
   3.189 +	gestureLast[j].p.x  = x;
   3.190 +	gestureLast[j].p.y  = y;
   3.191 +      }
   3.192 +
   3.193 +      //draw the touch && each centroid:
   3.194 +
   3.195 +      if(gestureLast[j].id < 0) continue; //Finger up. Or some error...
   3.196 +      int k;
   3.197 +      for(k = 0; k < MAXFINGERS;k++) {
   3.198 +
   3.199 +	if(gestureLast[k].id < 0) continue;
   3.200 +	//colors have no alpha, so shouldn't overflow
   3.201 +	unsigned int c = (colors[gestureLast[j].id%7] + 
   3.202 +			  colors[gestureLast[k].id%7])/2; 
   3.203 +	unsigned int col = 
   3.204 +	  ((unsigned int)(c*(.1+.85))) |
   3.205 +	  ((unsigned int)((0xFF*(1-((float)age)/EVENT_BUF_SIZE))) & 0xFF)<<24;
   3.206 +	x = (gestureLast[j].p.x + gestureLast[k].p.x)/2;
   3.207 +	y = (gestureLast[j].p.y + gestureLast[k].p.y)/2;
   3.208 +	if(event.type == SDL_FINGERMOTION)
   3.209 +	  drawCircle(screen,x*screen->w,y*screen->h,5,col);
   3.210 +	else if(event.type == SDL_FINGERDOWN)
   3.211 +	  drawCircle(screen,x*screen->w,y*screen->h,-10,col);
   3.212 +      }      
   3.213 +    }
   3.214 +  }
   3.215 +  
   3.216 +
   3.217 +  for(i=0;i<MAXFINGERS;i++)
   3.218 +    if(finger[i].p.x >= 0 && finger[i].p.y >= 0)
   3.219 +      if(finger[i].pressure > 0)
   3.220 +	drawCircle(screen,finger[i].p.x*screen->w,finger[i].p.y*screen->h
   3.221 +		   ,20,0xFF*finger[i].pressure);
   3.222 +      else
   3.223 +	drawCircle(screen,finger[i].p.x*screen->w,finger[i].p.y*screen->h
   3.224 +		   ,20,0xFF);
   3.225 +
   3.226 +  
   3.227 +  
   3.228 +
   3.229 +  if(SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen);
   3.230 +  
   3.231 +  SDL_Flip(screen);
   3.232 +}
   3.233 +
   3.234 +SDL_Surface* initScreen(int width,int height)
   3.235 +{
   3.236 +  return SDL_SetVideoMode(width, height, DEPTH,
   3.237 +			  SDL_HWSURFACE | SDL_RESIZABLE);
   3.238 +}
   3.239 +
   3.240 +int main(int argc, char* argv[])
   3.241 +{  
   3.242 +  SDL_Surface *screen;
   3.243 +  SDL_Event event;
   3.244 +  
   3.245 +  int keypress = 0;
   3.246 +  int h=0,s=1,i,j;
   3.247 +
   3.248 +  memset(keystat,0,512*sizeof(keystat[0]));
   3.249 +  if (SDL_Init(SDL_INIT_VIDEO) < 0 ) return 1;
   3.250 +  
   3.251 +  if (!(screen = initScreen(WIDTH,HEIGHT)))
   3.252 +    {
   3.253 +      SDL_Quit();
   3.254 +      return 1;
   3.255 +    }
   3.256 +
   3.257 +  while(!keystat[27]) {
   3.258 +    //Poll SDL
   3.259 +    while(SDL_PollEvent(&event)) 
   3.260 +      {
   3.261 +	//Record _all_ events
   3.262 +	events[eventWrite & (EVENT_BUF_SIZE-1)] = event;
   3.263 +	eventWrite++;
   3.264 +	
   3.265 +	switch (event.type) 
   3.266 +	  {
   3.267 +	  case SDL_QUIT:
   3.268 +	    keystat[27] = 1;
   3.269 +	    break;
   3.270 +	  case SDL_KEYDOWN:
   3.271 +	    //printf("%i\n",event.key.keysym.sym);
   3.272 +	    keystat[event.key.keysym.sym] = 1;
   3.273 +	    //keypress = 1;
   3.274 +	    break;
   3.275 +	  case SDL_KEYUP:
   3.276 +	      //printf("%i\n",event.key.keysym.sym);
   3.277 +	    keystat[event.key.keysym.sym] = 0;
   3.278 +	    //keypress = 1;
   3.279 +	    break;
   3.280 +	  case SDL_VIDEORESIZE:
   3.281 +	    if (!(screen = initScreen(event.resize.w,
   3.282 +				      event.resize.h)))
   3.283 +	      {
   3.284 +		SDL_Quit();
   3.285 +		return 1;
   3.286 +	      }
   3.287 +	    break;
   3.288 +	  case SDL_MOUSEMOTION:
   3.289 +	    mousx = event.motion.x;
   3.290 +	    mousy = event.motion.y; 
   3.291 +	    break;
   3.292 +	  case SDL_MOUSEBUTTONDOWN:
   3.293 +	    bstatus |=  (1<<(event.button.button-1));
   3.294 +	    break;
   3.295 +	  case SDL_MOUSEBUTTONUP:
   3.296 +	    bstatus &= ~(1<<(event.button.button-1));
   3.297 +	    break;
   3.298 +	  case SDL_FINGERMOTION:    
   3.299 +	    ;
   3.300 +	    //printf("Finger: %i,x: %i, y: %i\n",event.tfinger.fingerId,
   3.301 +	    //	   event.tfinger.x,event.tfinger.y);
   3.302 +	    SDL_Touch* inTouch = SDL_GetTouch(event.tfinger.touchId);
   3.303 +	    SDL_Finger* inFinger = SDL_GetFinger(inTouch,event.tfinger.fingerId);
   3.304 +
   3.305 +	    for(i = 0;i<MAXFINGERS;i++) 
   3.306 +	      if(index2fingerid[i] == event.tfinger.fingerId) 	      
   3.307 +		break;
   3.308 +	    if(i == MAXFINGERS) break;  
   3.309 +	    if(inTouch > 0) {
   3.310 +	      finger[i].p.x = ((float)event.tfinger.x)/
   3.311 +		inTouch->xres;
   3.312 +	      finger[i].p.y = ((float)event.tfinger.y)/
   3.313 +		inTouch->yres;
   3.314 +	      
   3.315 +	      finger[i].pressure = 
   3.316 +		((float)event.tfinger.pressure)/inTouch->pressureres;
   3.317 +	      
   3.318 +	      printf("Finger: %i, Pressure: %f Pressureres: %i\n",
   3.319 +		     event.tfinger.fingerId,
   3.320 +		     finger[i].pressure,
   3.321 +		     inTouch->pressureres);
   3.322 +	      //printf("Finger: %i, pressure: %f\n",event.tfinger.fingerId,
   3.323 +	      //   finger[event.tfinger.fingerId].pressure);
   3.324 +	    }
   3.325 +	    
   3.326 +	    break;	    
   3.327 +	  case SDL_FINGERDOWN:
   3.328 +	    printf("Finger: %i down - x: %i, y: %i\n",event.tfinger.fingerId,
   3.329 +		   event.tfinger.x,event.tfinger.y);
   3.330 +
   3.331 +	    for(i = 0;i<MAXFINGERS;i++) 
   3.332 +	      if(index2fingerid[i] == -1) {
   3.333 +		index2fingerid[i] = event.tfinger.fingerId;
   3.334 +		break;
   3.335 +	      }
   3.336 +	    finger[i].p.x = event.tfinger.x;
   3.337 +	    finger[i].p.y = event.tfinger.y;
   3.338 +	    break;
   3.339 +	  case SDL_FINGERUP:
   3.340 +	    printf("Figner: %i up - x: %i, y: %i\n",event.tfinger.fingerId,
   3.341 +	           event.tfinger.x,event.tfinger.y);
   3.342 +	    for(i = 0;i<MAXFINGERS;i++) 
   3.343 +	      if(index2fingerid[i] == event.tfinger.fingerId) {
   3.344 +		index2fingerid[i] = -1;
   3.345 +		break;
   3.346 +	      }
   3.347 +	    finger[i].p.x = -1;
   3.348 +	    finger[i].p.y = -1;
   3.349 +	    break;
   3.350 +	  }
   3.351 +      }
   3.352 +    //And draw
   3.353 +    DrawScreen(screen,h);
   3.354 +    /*
   3.355 +      for(i=0;i<512;i++) 
   3.356 +      if(keystat[i]) printf("%i\n",i);
   3.357 +      printf("Buttons:%i\n",bstatus);
   3.358 +    */
   3.359 +  }  
   3.360 +  SDL_Quit();
   3.361 +      
   3.362 +  return 0;
   3.363 +}
     4.1 --- a/touchTest/makefile	Tue Jun 01 02:54:33 2010 -0400
     4.2 +++ b/touchTest/makefile	Mon Jun 14 01:44:04 2010 -0400
     4.3 @@ -1,4 +1,5 @@
     4.4  SDLTest : touchSimp.c touchPong.c
     4.5 +	gcc gestureTest.c -o gestureTest `sdl-config --cflags --libs` -g
     4.6  	gcc touchTest.c -o touchTest `sdl-config --cflags --libs` -g		
     4.7  	gcc touchSimp.c -o touchSimp `sdl-config --cflags --libs` -g
     4.8  	gcc touchPong.c -o touchPong `sdl-config --cflags --libs` -g