test/testgesture.c
author Jim Grandpre <jim.tla@gmail.com>
Sun, 15 Aug 2010 00:36:28 -0400
changeset 4689 f9ab8df6d45a
child 4918 f5f70fed2c4c
permissions -rw-r--r--
Added README.touch and README.gesture. Moved touchtest/gestureSDLTest to test/testgesture
jim@4689
     1
/*  Usage:
jim@4689
     2
 *  Spacebar to begin recording a gesture on all touches.
jim@4689
     3
 *  s to save all touches into "./gestureSave"
jim@4689
     4
 *  l to load all touches from "./gestureSave"
jim@4689
     5
 */
jim@4689
     6
jim@4689
     7
#include <stdio.h>
jim@4689
     8
#include <SDL.h>
jim@4689
     9
#include <math.h>
jim@4689
    10
#include <SDL_touch.h>
jim@4689
    11
#include <SDL_gesture.h>
jim@4689
    12
jim@4689
    13
jim@4689
    14
/* Make sure we have good macros for printing 32 and 64 bit values */
jim@4689
    15
#ifndef PRIs32
jim@4689
    16
#define PRIs32 "d"
jim@4689
    17
#endif
jim@4689
    18
#ifndef PRIu32
jim@4689
    19
#define PRIu32 "u"
jim@4689
    20
#endif
jim@4689
    21
#ifndef PRIs64
jim@4689
    22
#ifdef __WIN32__
jim@4689
    23
#define PRIs64 "I64"
jim@4689
    24
#else
jim@4689
    25
#define PRIs64 "lld"
jim@4689
    26
#endif
jim@4689
    27
#endif
jim@4689
    28
#ifndef PRIu64
jim@4689
    29
#ifdef __WIN32__
jim@4689
    30
#define PRIu64 "I64u"
jim@4689
    31
#else
jim@4689
    32
#define PRIu64 "llu"
jim@4689
    33
#endif
jim@4689
    34
#endif
jim@4689
    35
jim@4689
    36
#define WIDTH 640
jim@4689
    37
#define HEIGHT 480
jim@4689
    38
#define BPP 4
jim@4689
    39
#define DEPTH 32
jim@4689
    40
jim@4689
    41
//MUST BE A POWER OF 2!
jim@4689
    42
#define EVENT_BUF_SIZE 256
jim@4689
    43
jim@4689
    44
jim@4689
    45
#define VERBOSE SDL_FALSE
jim@4689
    46
jim@4689
    47
SDL_Event events[EVENT_BUF_SIZE];
jim@4689
    48
int eventWrite;
jim@4689
    49
jim@4689
    50
int colors[7] = {0xFF,0xFF00,0xFF0000,0xFFFF00,0x00FFFF,0xFF00FF,0xFFFFFF};
jim@4689
    51
jim@4689
    52
typedef struct {
jim@4689
    53
  float x,y;
jim@4689
    54
} Point;
jim@4689
    55
jim@4689
    56
typedef struct {
jim@4689
    57
  float ang,r;
jim@4689
    58
  Point p;
jim@4689
    59
} Knob;
jim@4689
    60
jim@4689
    61
Knob knob;
jim@4689
    62
jim@4689
    63
void handler (int sig)
jim@4689
    64
{
jim@4689
    65
  printf ("\exiting...(%d)\n", sig);
jim@4689
    66
  exit (0);
jim@4689
    67
}
jim@4689
    68
jim@4689
    69
void perror_exit (char *error)
jim@4689
    70
{
jim@4689
    71
  perror (error);
jim@4689
    72
  handler (9);
jim@4689
    73
}
jim@4689
    74
jim@4689
    75
void setpix(SDL_Surface *screen, int x, int y, unsigned int col)
jim@4689
    76
{
jim@4689
    77
  Uint32 *pixmem32;
jim@4689
    78
  Uint32 colour;
jim@4689
    79
  
jim@4689
    80
  if((unsigned)x > screen->w) return;
jim@4689
    81
  if((unsigned)y > screen->h) return;
jim@4689
    82
jim@4689
    83
  pixmem32 = (Uint32*) screen->pixels  + y*screen->pitch/BPP + x;
jim@4689
    84
  
jim@4689
    85
  Uint8 r,g,b;
jim@4689
    86
  float a;
jim@4689
    87
  
jim@4689
    88
  memcpy(&colour,pixmem32,screen->format->BytesPerPixel);
jim@4689
    89
jim@4689
    90
  SDL_GetRGB(colour,screen->format,&r,&g,&b);
jim@4689
    91
  //r = 0;g = 0; b = 0;
jim@4689
    92
  a = (col>>24)&0xFF;
jim@4689
    93
  if(a == 0) a = 0xFF; //Hack, to make things easier.
jim@4689
    94
  a /= 0xFF;
jim@4689
    95
  r = r*(1-a) + ((col>>16)&0xFF)*(a);
jim@4689
    96
  g = g*(1-a) + ((col>> 8)&0xFF)*(a);
jim@4689
    97
  b = b*(1-a) + ((col>> 0)&0xFF)*(a);
jim@4689
    98
  colour = SDL_MapRGB( screen->format,r, g, b);
jim@4689
    99
  
jim@4689
   100
jim@4689
   101
  *pixmem32 = colour;
jim@4689
   102
}
jim@4689
   103
jim@4689
   104
void drawLine(SDL_Surface *screen,int x0,int y0,int x1,int y1,unsigned int col) {
jim@4689
   105
  float t;
jim@4689
   106
  for(t=0;t<1;t+=1.f/SDL_max(abs(x0-x1),abs(y0-y1)))
jim@4689
   107
    setpix(screen,x1+t*(x0-x1),y1+t*(y0-y1),col);
jim@4689
   108
}
jim@4689
   109
jim@4689
   110
void drawCircle(SDL_Surface* screen,int x,int y,int r,unsigned int c)
jim@4689
   111
{
jim@4689
   112
  int tx,ty;
jim@4689
   113
  float xr;
jim@4689
   114
  for(ty = -abs(r);ty <= abs(r);ty++) {
jim@4689
   115
    xr = sqrt(r*r - ty*ty);
jim@4689
   116
    if(r > 0) { //r > 0 ==> filled circle
jim@4689
   117
      for(tx=-xr+.5;tx<=xr-.5;tx++) {
jim@4689
   118
	setpix(screen,x+tx,y+ty,c);
jim@4689
   119
      }
jim@4689
   120
    }
jim@4689
   121
    else {
jim@4689
   122
      setpix(screen,x-xr+.5,y+ty,c);
jim@4689
   123
      setpix(screen,x+xr-.5,y+ty,c);
jim@4689
   124
    }
jim@4689
   125
  }
jim@4689
   126
}
jim@4689
   127
jim@4689
   128
void drawKnob(SDL_Surface* screen,Knob k) {
jim@4689
   129
  drawCircle(screen,k.p.x*screen->w,k.p.y*screen->h,k.r*screen->w,0xFFFFFF);  
jim@4689
   130
  drawCircle(screen,(k.p.x+k.r/2*cos(k.ang))*screen->w,
jim@4689
   131
  	            (k.p.y+k.r/2*sin(k.ang))*screen->h,k.r/4*screen->w,0);
jim@4689
   132
}
jim@4689
   133
jim@4689
   134
void DrawScreen(SDL_Surface* screen)
jim@4689
   135
{
jim@4689
   136
  int x, y;
jim@4689
   137
  if(SDL_MUSTLOCK(screen))
jim@4689
   138
    {                                              
jim@4689
   139
      if(SDL_LockSurface(screen) < 0) return;
jim@4689
   140
    }
jim@4689
   141
  for(y = 0;y < screen->h;y++)
jim@4689
   142
    for(x = 0;x < screen->w;x++)
jim@4689
   143
	setpix(screen,x,y,((x%255)<<16) + ((y%255)<<8) + (x+y)%255);
jim@4689
   144
jim@4689
   145
  int i;
jim@4689
   146
  //draw Touch History
jim@4689
   147
  for(i = SDL_max(0,eventWrite - EVENT_BUF_SIZE);i < eventWrite;i++) {
jim@4689
   148
    SDL_Event event = events[i&(EVENT_BUF_SIZE-1)];
jim@4689
   149
    int age = eventWrite - i - 1;
jim@4689
   150
    if(event.type == SDL_FINGERMOTION || 
jim@4689
   151
       event.type == SDL_FINGERDOWN ||
jim@4689
   152
       event.type == SDL_FINGERUP) {
jim@4689
   153
      SDL_Touch* inTouch = SDL_GetTouch(event.tfinger.touchId);
jim@4689
   154
      if(inTouch == NULL) continue;
jim@4689
   155
jim@4689
   156
      float x = ((float)event.tfinger.x)/inTouch->xres;
jim@4689
   157
      float y = ((float)event.tfinger.y)/inTouch->yres;      
jim@4689
   158
      
jim@4689
   159
      //draw the touch:      
jim@4689
   160
      unsigned int c = colors[event.tfinger.touchId%7]; 
jim@4689
   161
      unsigned int col = 
jim@4689
   162
	((unsigned int)(c*(.1+.85))) |
jim@4689
   163
	((unsigned int)((0xFF*(1-((float)age)/EVENT_BUF_SIZE))) & 0xFF)<<24;
jim@4689
   164
jim@4689
   165
      if(event.type == SDL_FINGERMOTION)
jim@4689
   166
	drawCircle(screen,x*screen->w,y*screen->h,5,col);
jim@4689
   167
      else if(event.type == SDL_FINGERDOWN)
jim@4689
   168
	drawCircle(screen,x*screen->w,y*screen->h,-10,col);     
jim@4689
   169
    }
jim@4689
   170
  }
jim@4689
   171
  
jim@4689
   172
  if(knob.p.x > 0)
jim@4689
   173
    drawKnob(screen,knob);
jim@4689
   174
  
jim@4689
   175
  if(SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen);
jim@4689
   176
  SDL_Flip(screen);
jim@4689
   177
}
jim@4689
   178
jim@4689
   179
SDL_Surface* initScreen(int width,int height)
jim@4689
   180
{
jim@4689
   181
  return SDL_SetVideoMode(width, height, DEPTH,
jim@4689
   182
			  SDL_HWSURFACE | SDL_RESIZABLE);
jim@4689
   183
}
jim@4689
   184
jim@4689
   185
int main(int argc, char* argv[])
jim@4689
   186
{  
jim@4689
   187
  SDL_Surface *screen;
jim@4689
   188
  SDL_Event event;
jim@4689
   189
jim@4689
   190
  //gesture variables
jim@4689
   191
  knob.r = .1;
jim@4689
   192
  knob.ang = 0;
jim@4689
   193
jim@4689
   194
  
jim@4689
   195
  SDL_bool quitting = SDL_FALSE;
jim@4689
   196
  SDL_RWops *src;
jim@4689
   197
jim@4689
   198
  if (SDL_Init(SDL_INIT_VIDEO) < 0 ) return 1;
jim@4689
   199
  
jim@4689
   200
  if (!(screen = initScreen(WIDTH,HEIGHT)))
jim@4689
   201
    {
jim@4689
   202
      SDL_Quit();
jim@4689
   203
      return 1;
jim@4689
   204
    }
jim@4689
   205
jim@4689
   206
  while(!quitting) {
jim@4689
   207
    while(SDL_PollEvent(&event)) 
jim@4689
   208
      {
jim@4689
   209
	//Record _all_ events
jim@4689
   210
	events[eventWrite & (EVENT_BUF_SIZE-1)] = event;
jim@4689
   211
	eventWrite++;
jim@4689
   212
	
jim@4689
   213
	switch (event.type) 
jim@4689
   214
	  {
jim@4689
   215
	  case SDL_QUIT:
jim@4689
   216
	    quitting = SDL_TRUE;
jim@4689
   217
	    break;
jim@4689
   218
	  case SDL_KEYDOWN:
jim@4689
   219
	    switch (event.key.keysym.sym)
jim@4689
   220
	      {
jim@4689
   221
	      case SDLK_SPACE:
jim@4689
   222
		SDL_RecordGesture(-1);
jim@4689
   223
		break;
jim@4689
   224
	      case SDLK_s:
jim@4689
   225
		src = SDL_RWFromFile("gestureSave","w");
jim@4689
   226
		printf("Wrote %i templates\n",SDL_SaveAllDollarTemplates(src));
jim@4689
   227
		SDL_RWclose(src);
jim@4689
   228
		break;
jim@4689
   229
	      case SDLK_l:
jim@4689
   230
		src = SDL_RWFromFile("gestureSave","r");
jim@4689
   231
		printf("Loaded: %i\n",SDL_LoadDollarTemplates(-1,src));
jim@4689
   232
		SDL_RWclose(src);
jim@4689
   233
		break;
jim@4689
   234
	      case SDLK_ESCAPE:
jim@4689
   235
		quitting = SDL_TRUE;
jim@4689
   236
		break;
jim@4689
   237
	    }
jim@4689
   238
	    break;
jim@4689
   239
	  case SDL_VIDEORESIZE:
jim@4689
   240
	    if (!(screen = initScreen(event.resize.w,
jim@4689
   241
				      event.resize.h)))
jim@4689
   242
	      {
jim@4689
   243
		SDL_Quit();
jim@4689
   244
		return 1;
jim@4689
   245
	      }
jim@4689
   246
	    break;
jim@4689
   247
	  case SDL_FINGERMOTION:    
jim@4689
   248
	    ;
jim@4689
   249
#if VERBOSE
jim@4689
   250
	    printf("Finger: %i,x: %i, y: %i\n",event.tfinger.fingerId,
jim@4689
   251
	    	   event.tfinger.x,event.tfinger.y);
jim@4689
   252
#endif
jim@4689
   253
	    SDL_Touch* inTouch = SDL_GetTouch(event.tfinger.touchId);
jim@4689
   254
	    SDL_Finger* inFinger = SDL_GetFinger(inTouch,event.tfinger.fingerId);
jim@4689
   255
	    break;	    
jim@4689
   256
	  case SDL_FINGERDOWN:
jim@4689
   257
#if VERBOSE
jim@4689
   258
	    printf("Finger: %"PRIs64" down - x: %i, y: %i\n",
jim@4689
   259
		   event.tfinger.fingerId,event.tfinger.x,event.tfinger.y);
jim@4689
   260
#endif
jim@4689
   261
	    break;
jim@4689
   262
	  case SDL_FINGERUP:
jim@4689
   263
#if VERBOSE
jim@4689
   264
	    printf("Finger: %"PRIs64" up - x: %i, y: %i\n",
jim@4689
   265
	    	   event.tfinger.fingerId,event.tfinger.x,event.tfinger.y);
jim@4689
   266
#endif
jim@4689
   267
	    break;
jim@4689
   268
	  case SDL_MULTIGESTURE:
jim@4689
   269
#if VERBOSE	    
jim@4689
   270
	    printf("Multi Gesture: x = %f, y = %f, dAng = %f, dR = %f\n",
jim@4689
   271
		   event.mgesture.x,
jim@4689
   272
		   event.mgesture.y,
jim@4689
   273
		   event.mgesture.dTheta,
jim@4689
   274
		   event.mgesture.dDist);
jim@4689
   275
	    printf("MG: numDownTouch = %i\n",event.mgesture.numFingers);
jim@4689
   276
#endif
jim@4689
   277
	    knob.p.x = event.mgesture.x;
jim@4689
   278
	    knob.p.y = event.mgesture.y;
jim@4689
   279
	    knob.ang += event.mgesture.dTheta;
jim@4689
   280
	    knob.r += event.mgesture.dDist;
jim@4689
   281
	    break;
jim@4689
   282
	  case SDL_DOLLARGESTURE:
jim@4689
   283
	    printf("Gesture %"PRIs64" performed, error: %f\n",
jim@4689
   284
		   event.dgesture.gestureId,
jim@4689
   285
		   event.dgesture.error);
jim@4689
   286
	    break;
jim@4689
   287
	  case SDL_DOLLARRECORD:
jim@4689
   288
	    printf("Recorded gesture: %"PRIs64"\n",event.dgesture.gestureId);
jim@4689
   289
	    break;
jim@4689
   290
	  }
jim@4689
   291
      }
jim@4689
   292
    DrawScreen(screen);
jim@4689
   293
  }  
jim@4689
   294
  SDL_Quit();  
jim@4689
   295
  return 0;
jim@4689
   296
}
jim@4689
   297