touchTest/gestureTest.c
author Jim Grandpre <jim.tla@gmail.com>
Tue, 15 Jun 2010 02:12:16 -0400
changeset 4653 744b75ad18b8
parent 4651 86c171888eee
child 4654 7dbcd71216df
permissions -rw-r--r--
Started adding vectorization of touch data.
jim@4651
     1
#include <stdio.h>
jim@4651
     2
#include <SDL.h>
jim@4651
     3
#include <math.h>
jim@4651
     4
#include <SDL_touch.h>
jim@4651
     5
jim@4651
     6
#define PI 3.1415926535897
jim@4651
     7
#define WIDTH 640
jim@4651
     8
#define HEIGHT 480
jim@4651
     9
#define BPP 4
jim@4651
    10
#define DEPTH 32
jim@4651
    11
jim@4651
    12
#define MAXFINGERS 3
jim@4651
    13
jim@4651
    14
jim@4651
    15
jim@4651
    16
jim@4651
    17
//MUST BE A POWER OF 2!
jim@4651
    18
#define EVENT_BUF_SIZE 256
jim@4651
    19
jim@4651
    20
SDL_Event events[EVENT_BUF_SIZE];
jim@4651
    21
int eventWrite;
jim@4651
    22
jim@4651
    23
int mousx,mousy;
jim@4651
    24
int keystat[512];
jim@4651
    25
int bstatus;
jim@4651
    26
jim@4651
    27
int colors[7] = {0xFF,0xFF00,0xFF0000,0xFFFF00,0x00FFFF,0xFF00FF,0xFFFFFF};
jim@4651
    28
jim@4651
    29
int index2fingerid[MAXFINGERS];
jim@4651
    30
int fingersDown;
jim@4651
    31
jim@4651
    32
typedef struct {
jim@4651
    33
  float x,y;
jim@4651
    34
} Point;
jim@4651
    35
jim@4651
    36
typedef struct {
jim@4651
    37
  Point p;
jim@4651
    38
  float pressure;
jim@4651
    39
  int id;
jim@4651
    40
} Finger;
jim@4651
    41
jim@4653
    42
typedef struct { //dt + s
jim@4653
    43
  Point d,s; //direction, start
jim@4653
    44
  int points;
jim@4653
    45
} Line;
jim@4653
    46
jim@4651
    47
jim@4651
    48
Finger finger[MAXFINGERS];
jim@4651
    49
jim@4651
    50
Finger gestureLast[MAXFINGERS];
jim@4653
    51
Line gestureLine[MAXFINGERS];
jim@4651
    52
jim@4651
    53
void handler (int sig)
jim@4651
    54
{
jim@4651
    55
  printf ("\exiting...(%d)\n", sig);
jim@4651
    56
  exit (0);
jim@4651
    57
}
jim@4651
    58
jim@4651
    59
void perror_exit (char *error)
jim@4651
    60
{
jim@4651
    61
  perror (error);
jim@4651
    62
  handler (9);
jim@4651
    63
}
jim@4651
    64
jim@4651
    65
jim@4651
    66
void setpix(SDL_Surface *screen, int x, int y, unsigned int col)
jim@4651
    67
{
jim@4651
    68
  Uint32 *pixmem32;
jim@4651
    69
  Uint32 colour;
jim@4651
    70
  
jim@4651
    71
  if((unsigned)x > screen->w) return;
jim@4651
    72
  if((unsigned)y > screen->h) return;
jim@4651
    73
jim@4651
    74
  pixmem32 = (Uint32*) screen->pixels  + y*screen->pitch/BPP + x;
jim@4651
    75
  
jim@4651
    76
  Uint8 r,g,b;
jim@4651
    77
  float a;
jim@4651
    78
  
jim@4651
    79
  memcpy(&colour,pixmem32,screen->format->BytesPerPixel);
jim@4651
    80
jim@4651
    81
  SDL_GetRGB(colour,screen->format,&r,&g,&b); //Always returns 0xFFFFFF?
jim@4651
    82
  //r = 0;g = 0; b = 0;
jim@4651
    83
  a = (col>>24)&0xFF;
jim@4651
    84
  if(a == 0) a = 0xFF; //Hack, to make things easier.
jim@4651
    85
  a /= 0xFF;
jim@4651
    86
  r = r*(1-a) + ((col>>16)&0xFF)*(a);
jim@4651
    87
  g = g*(1-a) + ((col>> 8)&0xFF)*(a);
jim@4651
    88
  b = b*(1-a) + ((col>> 0)&0xFF)*(a);
jim@4651
    89
  colour = SDL_MapRGB( screen->format,r, g, b);
jim@4651
    90
  
jim@4651
    91
jim@4651
    92
  *pixmem32 = colour;
jim@4651
    93
}
jim@4651
    94
jim@4653
    95
void drawLine(SDL_Surface *screen,int x0,int y0,int x1,int y1,unsigned int col) {
jim@4653
    96
  float t;
jim@4653
    97
  for(t=0;t<1;t+=1.f/SDL_max(abs(x0-x1),abs(y0-y1)))
jim@4653
    98
    setpix(screen,x1+t*(x0-x1),y1+t*(y0-y1),col);
jim@4653
    99
}
jim@4651
   100
void drawCircle(SDL_Surface* screen,int x,int y,int r,unsigned int c)
jim@4651
   101
{
jim@4651
   102
jim@4651
   103
  float a;
jim@4651
   104
  int tx;
jim@4651
   105
  /*
jim@4651
   106
  for(a=0;a<PI/2;a+=1.f/(float)abs(r))
jim@4651
   107
  {
jim@4651
   108
    if(r > 0) { //r > 0 ==> filled circle
jim@4651
   109
      for(tx=x-r*cos(a);tx<x+r*cos(a);tx++) {
jim@4651
   110
	setpix(screen,tx,(int)(y+r*sin(a)),c);
jim@4651
   111
	setpix(screen,tx,(int)(y-r*sin(a)),c);
jim@4651
   112
      }
jim@4651
   113
    }
jim@4651
   114
    else {
jim@4651
   115
      //Draw Outline
jim@4651
   116
      setpix(screen,(int)(x+r*cos(a)),(int)(y+r*sin(a)),c);
jim@4651
   117
      setpix(screen,(int)(x-r*cos(a)),(int)(y+r*sin(a)),c);
jim@4651
   118
      setpix(screen,(int)(x+r*cos(a)),(int)(y-r*sin(a)),c);
jim@4651
   119
      setpix(screen,(int)(x-r*cos(a)),(int)(y-r*sin(a)),c);
jim@4651
   120
    }
jim@4651
   121
    }*/
jim@4651
   122
  int ty;
jim@4651
   123
  float xr;
jim@4651
   124
  for(ty = -abs(r);ty <= abs(r);ty++) {
jim@4651
   125
    xr = sqrt(r*r - ty*ty);
jim@4651
   126
    if(r > 0) { //r > 0 ==> filled circle
jim@4651
   127
      for(tx=-xr+.5;tx<=xr-.5;tx++) {
jim@4651
   128
	setpix(screen,x+tx,y+ty,c);
jim@4651
   129
      }
jim@4651
   130
    }
jim@4651
   131
    else {
jim@4651
   132
      setpix(screen,x-xr+.5,y+ty,c);
jim@4651
   133
      setpix(screen,x+xr-.5,y+ty,c);
jim@4651
   134
    }
jim@4651
   135
  }
jim@4651
   136
}
jim@4651
   137
jim@4651
   138
void DrawScreen(SDL_Surface* screen, int h)
jim@4651
   139
{
jim@4651
   140
  int x, y, xm,ym,c;
jim@4651
   141
  if(SDL_MUSTLOCK(screen))
jim@4651
   142
    {                                              
jim@4651
   143
      if(SDL_LockSurface(screen) < 0) return;
jim@4651
   144
    }
jim@4651
   145
  for(y = 0; y < screen->h; y++ )
jim@4651
   146
    {
jim@4651
   147
      for( x = 0; x < screen->w; x++ )
jim@4651
   148
        {
jim@4651
   149
	  //setpixel(screen, x, y, (x*x)/256+3*y+h, (y*y)/256+x+h, h);
jim@4651
   150
	  //xm = (x+h)%screen->w;
jim@4651
   151
	  //ym = (y+h)%screen->w;
jim@4651
   152
	  //c = sin(h/256*2*PI)*x*y/screen->w/screen->h;
jim@4651
   153
	  //setpix(screen,x,y,255*sin(xm/screen->w*2*PI),sin(h/255*2*PI)*255*y/screen->h,c);
jim@4651
   154
	  setpix(screen,x,y,((x%255)<<16) + ((y%255)<<8) + (x+y)%255);
jim@4651
   155
	  //setpix(screen,x,y,0); //Inefficient, but that's okay...
jim@4651
   156
        }
jim@4651
   157
    }
jim@4651
   158
  drawCircle(screen,mousx,mousy,-30,0xFFFFFF);
jim@4653
   159
  drawLine(screen,0,0,screen->w,screen->h,0xFFFFFF);
jim@4651
   160
  int i;
jim@4651
   161
//draw Touch History
jim@4653
   162
  for(i = 0;i < MAXFINGERS;i++) {
jim@4653
   163
    gestureLast[i].id = -1;
jim@4653
   164
    gestureLine[i].points = 0;
jim@4653
   165
  }
jim@4651
   166
  for(i = SDL_max(0,eventWrite - EVENT_BUF_SIZE);i != eventWrite;i++) {
jim@4651
   167
    SDL_Event event = events[i&(EVENT_BUF_SIZE-1)];
jim@4651
   168
    int age = eventWrite - i - 1;
jim@4651
   169
    if(event.type == SDL_FINGERMOTION || 
jim@4651
   170
       event.type == SDL_FINGERDOWN ||
jim@4651
   171
       event.type == SDL_FINGERUP) {
jim@4651
   172
      SDL_Touch* inTouch = SDL_GetTouch(event.tfinger.touchId);
jim@4651
   173
      //SDL_Finger* inFinger = SDL_GetFinger(inTouch,event.tfinger.fingerId);
jim@4651
   174
	    
jim@4651
   175
      float x = ((float)event.tfinger.x)/inTouch->xres;
jim@4651
   176
      float y = ((float)event.tfinger.y)/inTouch->yres;
jim@4651
   177
      int j,empty = -1;
jim@4651
   178
      for(j = 0;j<MAXFINGERS;j++) {
jim@4651
   179
	if(gestureLast[j].id == event.tfinger.fingerId) {
jim@4651
   180
	  if(event.type == SDL_FINGERUP) {
jim@4653
   181
	    if(gestureLine[j].points > 0)
jim@4653
   182
	    drawLine(screen,
jim@4653
   183
		     gestureLine[j].s.x*screen->w,
jim@4653
   184
		     gestureLine[j].s.y*screen->h,
jim@4653
   185
		     (gestureLine[j].s.x +50*gestureLine[j].d.x)*screen->w,
jim@4653
   186
		     (gestureLine[j].s.y +50*gestureLine[j].d.y)*screen->h,
jim@4653
   187
		     0xFF00);
jim@4653
   188
jim@4653
   189
	    gestureLine[j].points = 0;
jim@4651
   190
	    gestureLast[j].id = -1;
jim@4651
   191
	    break;
jim@4651
   192
	  }
jim@4651
   193
	  else {
jim@4653
   194
	    if(gestureLine[j].points == 1) {
jim@4653
   195
	      gestureLine[j].d.x = x - gestureLine[j].s.x;
jim@4653
   196
	      gestureLine[j].d.y = y - gestureLine[j].s.y;
jim@4653
   197
	    }
jim@4653
   198
jim@4653
   199
	    gestureLine[j].s.x = gestureLine[j].s.x*gestureLine[j].points+x;
jim@4653
   200
	    gestureLine[j].s.y = gestureLine[j].s.y*gestureLine[j].points+y;
jim@4653
   201
jim@4653
   202
	    gestureLine[j].d.x = gestureLine[j].d.x*gestureLine[j].points+
jim@4653
   203
	      x - gestureLast[j].p.x;
jim@4653
   204
	    gestureLine[j].d.y = gestureLine[j].d.y*gestureLine[j].points+
jim@4653
   205
	      y - gestureLast[j].p.y;;
jim@4653
   206
jim@4653
   207
jim@4653
   208
	    gestureLine[j].points++;
jim@4653
   209
	    
jim@4653
   210
	    gestureLine[j].s.x /= gestureLine[j].points;
jim@4653
   211
	    gestureLine[j].s.y /= gestureLine[j].points;
jim@4653
   212
jim@4653
   213
	    gestureLine[j].d.x /= gestureLine[j].points;
jim@4653
   214
	    gestureLine[j].d.y /= gestureLine[j].points;
jim@4653
   215
jim@4653
   216
	    
jim@4653
   217
	    
jim@4653
   218
jim@4651
   219
	    gestureLast[j].p.x = x;
jim@4651
   220
	    gestureLast[j].p.y = y;
jim@4651
   221
	    break;
jim@4651
   222
	    //pressure?
jim@4651
   223
	  }	  
jim@4651
   224
	}
jim@4651
   225
	else if(gestureLast[j].id == -1 && empty == -1) {
jim@4651
   226
	  empty = j;
jim@4651
   227
	}
jim@4651
   228
      }
jim@4651
   229
jim@4651
   230
      if(j >= MAXFINGERS && empty >= 0) {
jim@4651
   231
	printf("Finger Down!!!\n");
jim@4651
   232
	j = empty; //important that j is the index of the added finger
jim@4651
   233
	gestureLast[j].id = event.tfinger.fingerId;
jim@4651
   234
	gestureLast[j].p.x  = x;
jim@4651
   235
	gestureLast[j].p.y  = y;
jim@4653
   236
jim@4653
   237
	gestureLine[j].s.x = x;
jim@4653
   238
	gestureLine[j].s.y = y;
jim@4653
   239
	gestureLine[j].points = 1;
jim@4651
   240
      }
jim@4651
   241
jim@4651
   242
      //draw the touch && each centroid:
jim@4651
   243
jim@4651
   244
      if(gestureLast[j].id < 0) continue; //Finger up. Or some error...
jim@4651
   245
      int k;
jim@4651
   246
      for(k = 0; k < MAXFINGERS;k++) {
jim@4651
   247
jim@4651
   248
	if(gestureLast[k].id < 0) continue;
jim@4651
   249
	//colors have no alpha, so shouldn't overflow
jim@4651
   250
	unsigned int c = (colors[gestureLast[j].id%7] + 
jim@4651
   251
			  colors[gestureLast[k].id%7])/2; 
jim@4651
   252
	unsigned int col = 
jim@4651
   253
	  ((unsigned int)(c*(.1+.85))) |
jim@4651
   254
	  ((unsigned int)((0xFF*(1-((float)age)/EVENT_BUF_SIZE))) & 0xFF)<<24;
jim@4651
   255
	x = (gestureLast[j].p.x + gestureLast[k].p.x)/2;
jim@4651
   256
	y = (gestureLast[j].p.y + gestureLast[k].p.y)/2;
jim@4651
   257
	if(event.type == SDL_FINGERMOTION)
jim@4651
   258
	  drawCircle(screen,x*screen->w,y*screen->h,5,col);
jim@4651
   259
	else if(event.type == SDL_FINGERDOWN)
jim@4651
   260
	  drawCircle(screen,x*screen->w,y*screen->h,-10,col);
jim@4651
   261
      }      
jim@4651
   262
    }
jim@4651
   263
  }
jim@4651
   264
  
jim@4651
   265
jim@4651
   266
  for(i=0;i<MAXFINGERS;i++)
jim@4651
   267
    if(finger[i].p.x >= 0 && finger[i].p.y >= 0)
jim@4651
   268
      if(finger[i].pressure > 0)
jim@4651
   269
	drawCircle(screen,finger[i].p.x*screen->w,finger[i].p.y*screen->h
jim@4651
   270
		   ,20,0xFF*finger[i].pressure);
jim@4651
   271
      else
jim@4651
   272
	drawCircle(screen,finger[i].p.x*screen->w,finger[i].p.y*screen->h
jim@4651
   273
		   ,20,0xFF);
jim@4651
   274
jim@4651
   275
  
jim@4651
   276
  
jim@4651
   277
jim@4651
   278
  if(SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen);
jim@4651
   279
  
jim@4651
   280
  SDL_Flip(screen);
jim@4651
   281
}
jim@4651
   282
jim@4651
   283
SDL_Surface* initScreen(int width,int height)
jim@4651
   284
{
jim@4651
   285
  return SDL_SetVideoMode(width, height, DEPTH,
jim@4651
   286
			  SDL_HWSURFACE | SDL_RESIZABLE);
jim@4651
   287
}
jim@4651
   288
jim@4651
   289
int main(int argc, char* argv[])
jim@4651
   290
{  
jim@4651
   291
  SDL_Surface *screen;
jim@4651
   292
  SDL_Event event;
jim@4651
   293
  
jim@4651
   294
  int keypress = 0;
jim@4651
   295
  int h=0,s=1,i,j;
jim@4651
   296
jim@4651
   297
  memset(keystat,0,512*sizeof(keystat[0]));
jim@4651
   298
  if (SDL_Init(SDL_INIT_VIDEO) < 0 ) return 1;
jim@4651
   299
  
jim@4651
   300
  if (!(screen = initScreen(WIDTH,HEIGHT)))
jim@4651
   301
    {
jim@4651
   302
      SDL_Quit();
jim@4651
   303
      return 1;
jim@4651
   304
    }
jim@4651
   305
jim@4651
   306
  while(!keystat[27]) {
jim@4651
   307
    //Poll SDL
jim@4651
   308
    while(SDL_PollEvent(&event)) 
jim@4651
   309
      {
jim@4651
   310
	//Record _all_ events
jim@4651
   311
	events[eventWrite & (EVENT_BUF_SIZE-1)] = event;
jim@4651
   312
	eventWrite++;
jim@4651
   313
	
jim@4651
   314
	switch (event.type) 
jim@4651
   315
	  {
jim@4651
   316
	  case SDL_QUIT:
jim@4651
   317
	    keystat[27] = 1;
jim@4651
   318
	    break;
jim@4651
   319
	  case SDL_KEYDOWN:
jim@4651
   320
	    //printf("%i\n",event.key.keysym.sym);
jim@4651
   321
	    keystat[event.key.keysym.sym] = 1;
jim@4651
   322
	    //keypress = 1;
jim@4651
   323
	    break;
jim@4651
   324
	  case SDL_KEYUP:
jim@4651
   325
	      //printf("%i\n",event.key.keysym.sym);
jim@4651
   326
	    keystat[event.key.keysym.sym] = 0;
jim@4651
   327
	    //keypress = 1;
jim@4651
   328
	    break;
jim@4651
   329
	  case SDL_VIDEORESIZE:
jim@4651
   330
	    if (!(screen = initScreen(event.resize.w,
jim@4651
   331
				      event.resize.h)))
jim@4651
   332
	      {
jim@4651
   333
		SDL_Quit();
jim@4651
   334
		return 1;
jim@4651
   335
	      }
jim@4651
   336
	    break;
jim@4651
   337
	  case SDL_MOUSEMOTION:
jim@4651
   338
	    mousx = event.motion.x;
jim@4651
   339
	    mousy = event.motion.y; 
jim@4651
   340
	    break;
jim@4651
   341
	  case SDL_MOUSEBUTTONDOWN:
jim@4651
   342
	    bstatus |=  (1<<(event.button.button-1));
jim@4651
   343
	    break;
jim@4651
   344
	  case SDL_MOUSEBUTTONUP:
jim@4651
   345
	    bstatus &= ~(1<<(event.button.button-1));
jim@4651
   346
	    break;
jim@4651
   347
	  case SDL_FINGERMOTION:    
jim@4651
   348
	    ;
jim@4651
   349
	    //printf("Finger: %i,x: %i, y: %i\n",event.tfinger.fingerId,
jim@4651
   350
	    //	   event.tfinger.x,event.tfinger.y);
jim@4651
   351
	    SDL_Touch* inTouch = SDL_GetTouch(event.tfinger.touchId);
jim@4651
   352
	    SDL_Finger* inFinger = SDL_GetFinger(inTouch,event.tfinger.fingerId);
jim@4651
   353
jim@4651
   354
	    for(i = 0;i<MAXFINGERS;i++) 
jim@4651
   355
	      if(index2fingerid[i] == event.tfinger.fingerId) 	      
jim@4651
   356
		break;
jim@4651
   357
	    if(i == MAXFINGERS) break;  
jim@4651
   358
	    if(inTouch > 0) {
jim@4651
   359
	      finger[i].p.x = ((float)event.tfinger.x)/
jim@4651
   360
		inTouch->xres;
jim@4651
   361
	      finger[i].p.y = ((float)event.tfinger.y)/
jim@4651
   362
		inTouch->yres;
jim@4651
   363
	      
jim@4651
   364
	      finger[i].pressure = 
jim@4651
   365
		((float)event.tfinger.pressure)/inTouch->pressureres;
jim@4651
   366
	      
jim@4651
   367
	      printf("Finger: %i, Pressure: %f Pressureres: %i\n",
jim@4651
   368
		     event.tfinger.fingerId,
jim@4651
   369
		     finger[i].pressure,
jim@4651
   370
		     inTouch->pressureres);
jim@4651
   371
	      //printf("Finger: %i, pressure: %f\n",event.tfinger.fingerId,
jim@4651
   372
	      //   finger[event.tfinger.fingerId].pressure);
jim@4651
   373
	    }
jim@4651
   374
	    
jim@4651
   375
	    break;	    
jim@4651
   376
	  case SDL_FINGERDOWN:
jim@4651
   377
	    printf("Finger: %i down - x: %i, y: %i\n",event.tfinger.fingerId,
jim@4651
   378
		   event.tfinger.x,event.tfinger.y);
jim@4651
   379
jim@4651
   380
	    for(i = 0;i<MAXFINGERS;i++) 
jim@4651
   381
	      if(index2fingerid[i] == -1) {
jim@4651
   382
		index2fingerid[i] = event.tfinger.fingerId;
jim@4651
   383
		break;
jim@4651
   384
	      }
jim@4651
   385
	    finger[i].p.x = event.tfinger.x;
jim@4651
   386
	    finger[i].p.y = event.tfinger.y;
jim@4651
   387
	    break;
jim@4651
   388
	  case SDL_FINGERUP:
jim@4651
   389
	    printf("Figner: %i up - x: %i, y: %i\n",event.tfinger.fingerId,
jim@4651
   390
	           event.tfinger.x,event.tfinger.y);
jim@4651
   391
	    for(i = 0;i<MAXFINGERS;i++) 
jim@4651
   392
	      if(index2fingerid[i] == event.tfinger.fingerId) {
jim@4651
   393
		index2fingerid[i] = -1;
jim@4651
   394
		break;
jim@4651
   395
	      }
jim@4651
   396
	    finger[i].p.x = -1;
jim@4651
   397
	    finger[i].p.y = -1;
jim@4651
   398
	    break;
jim@4651
   399
	  }
jim@4651
   400
      }
jim@4651
   401
    //And draw
jim@4651
   402
    DrawScreen(screen,h);
jim@4651
   403
    /*
jim@4651
   404
      for(i=0;i<512;i++) 
jim@4651
   405
      if(keystat[i]) printf("%i\n",i);
jim@4651
   406
      printf("Buttons:%i\n",bstatus);
jim@4651
   407
    */
jim@4651
   408
  }  
jim@4651
   409
  SDL_Quit();
jim@4651
   410
      
jim@4651
   411
  return 0;
jim@4651
   412
}