test/testgesture.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 09 Mar 2011 15:51:51 -0800
changeset 5460 43582fccdc0d
parent 5458 a96fcc853bef
child 5535 96594ac5fd1a
permissions -rw-r--r--
Removed newlines from log messages
Slightly speeded up event history processing each frame
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 <math.h>
slouken@5456
     9
slouken@5456
    10
#include "SDL.h"
slouken@5456
    11
#include "SDL_touch.h"
slouken@5456
    12
#include "SDL_gesture.h"
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
slouken@5086
    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
slouken@5086
    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
slouken@5458
    45
#define VERBOSE 0
jim@4689
    46
slouken@5457
    47
static SDL_Window *window;
slouken@5457
    48
static SDL_Event events[EVENT_BUF_SIZE];
slouken@5457
    49
static int eventWrite;
jim@4689
    50
slouken@5456
    51
slouken@5457
    52
static int colors[7] = {0xFF,0xFF00,0xFF0000,0xFFFF00,0x00FFFF,0xFF00FF,0xFFFFFF};
jim@4689
    53
jim@4689
    54
typedef struct {
jim@4689
    55
  float x,y;
jim@4689
    56
} Point;
jim@4689
    57
jim@4689
    58
typedef struct {
jim@4689
    59
  float ang,r;
jim@4689
    60
  Point p;
jim@4689
    61
} Knob;
jim@4689
    62
slouken@5457
    63
static Knob knob;
jim@4689
    64
jim@4689
    65
void handler (int sig)
jim@4689
    66
{
slouken@5460
    67
  SDL_Log ("exiting...(%d)", sig);
jim@4689
    68
  exit (0);
jim@4689
    69
}
jim@4689
    70
jim@4689
    71
void perror_exit (char *error)
jim@4689
    72
{
jim@4689
    73
  perror (error);
jim@4689
    74
  handler (9);
jim@4689
    75
}
jim@4689
    76
slouken@4918
    77
void setpix(SDL_Surface *screen, float _x, float _y, unsigned int col)
jim@4689
    78
{
jim@4689
    79
  Uint32 *pixmem32;
jim@4689
    80
  Uint32 colour;
slouken@4918
    81
  Uint8 r,g,b;
slouken@4918
    82
  int x = (int)_x;
slouken@4918
    83
  int y = (int)_y;
slouken@4918
    84
  float a;
jim@4689
    85
  
slouken@4921
    86
  if(x < 0 || x > screen->w) return;
slouken@4921
    87
  if(y < 0 || y > screen->h) return;
jim@4689
    88
jim@4689
    89
  pixmem32 = (Uint32*) screen->pixels  + y*screen->pitch/BPP + x;
jim@4689
    90
  
slouken@4918
    91
  SDL_memcpy(&colour,pixmem32,screen->format->BytesPerPixel);
jim@4689
    92
jim@4689
    93
  SDL_GetRGB(colour,screen->format,&r,&g,&b);
jim@4689
    94
  //r = 0;g = 0; b = 0;
slouken@4918
    95
  a = (float)((col>>24)&0xFF);
jim@4689
    96
  if(a == 0) a = 0xFF; //Hack, to make things easier.
jim@4689
    97
  a /= 0xFF;
slouken@4918
    98
  r = (Uint8)(r*(1-a) + ((col>>16)&0xFF)*(a));
slouken@4918
    99
  g = (Uint8)(g*(1-a) + ((col>> 8)&0xFF)*(a));
slouken@4918
   100
  b = (Uint8)(b*(1-a) + ((col>> 0)&0xFF)*(a));
jim@4689
   101
  colour = SDL_MapRGB( screen->format,r, g, b);
jim@4689
   102
  
jim@4689
   103
jim@4689
   104
  *pixmem32 = colour;
jim@4689
   105
}
jim@4689
   106
slouken@4918
   107
void drawLine(SDL_Surface *screen,float x0,float y0,float x1,float y1,unsigned int col) {
jim@4689
   108
  float t;
slouken@4918
   109
  for(t=0;t<1;t+=(float)(1.f/SDL_max(SDL_fabs(x0-x1),SDL_fabs(y0-y1))))
jim@4689
   110
    setpix(screen,x1+t*(x0-x1),y1+t*(y0-y1),col);
jim@4689
   111
}
jim@4689
   112
slouken@4918
   113
void drawCircle(SDL_Surface* screen,float x,float y,float r,unsigned int c)
jim@4689
   114
{
slouken@4918
   115
  float tx,ty;
jim@4689
   116
  float xr;
slouken@4918
   117
  for(ty = (float)-SDL_fabs(r);ty <= (float)SDL_fabs((int)r);ty++) {
slouken@4918
   118
    xr = (float)sqrt(r*r - ty*ty);
jim@4689
   119
    if(r > 0) { //r > 0 ==> filled circle
slouken@4918
   120
      for(tx=-xr+.5f;tx<=xr-.5;tx++) {
jim@4689
   121
	setpix(screen,x+tx,y+ty,c);
jim@4689
   122
      }
jim@4689
   123
    }
jim@4689
   124
    else {
slouken@4918
   125
      setpix(screen,x-xr+.5f,y+ty,c);
slouken@4918
   126
      setpix(screen,x+xr-.5f,y+ty,c);
jim@4689
   127
    }
jim@4689
   128
  }
jim@4689
   129
}
jim@4689
   130
jim@4689
   131
void drawKnob(SDL_Surface* screen,Knob k) {
jim@4689
   132
  drawCircle(screen,k.p.x*screen->w,k.p.y*screen->h,k.r*screen->w,0xFFFFFF);  
slouken@4918
   133
  drawCircle(screen,(k.p.x+k.r/2*cosf(k.ang))*screen->w,
slouken@4918
   134
  	            (k.p.y+k.r/2*sinf(k.ang))*screen->h,k.r/4*screen->w,0);
jim@4689
   135
}
jim@4689
   136
jim@4689
   137
void DrawScreen(SDL_Surface* screen)
jim@4689
   138
{
slouken@5456
   139
  int i;
slouken@5456
   140
#if 1
slouken@5456
   141
  SDL_FillRect(screen, NULL, 0);
slouken@5456
   142
#else
slouken@5456
   143
  int x, y;
jim@4689
   144
  for(y = 0;y < screen->h;y++)
jim@4689
   145
    for(x = 0;x < screen->w;x++)
slouken@4918
   146
	setpix(screen,(float)x,(float)y,((x%255)<<16) + ((y%255)<<8) + (x+y)%255);
slouken@5456
   147
#endif
jim@4689
   148
jim@4689
   149
  //draw Touch History
slouken@5460
   150
  for(i = eventWrite; i < eventWrite+EVENT_BUF_SIZE; ++i) {
slouken@5460
   151
    const SDL_Event *event = &events[i&(EVENT_BUF_SIZE-1)];
slouken@5460
   152
    float age = (float)(i - eventWrite) / EVENT_BUF_SIZE;
slouken@4918
   153
	float x, y;
slouken@4918
   154
	unsigned int c, col;
slouken@4918
   155
slouken@5460
   156
    if(event->type == SDL_FINGERMOTION || 
slouken@5460
   157
       event->type == SDL_FINGERDOWN ||
slouken@5460
   158
       event->type == SDL_FINGERUP) {
slouken@5460
   159
      SDL_Touch* inTouch = SDL_GetTouch(event->tfinger.touchId);
jim@4689
   160
      if(inTouch == NULL) continue;
jim@4689
   161
slouken@5460
   162
      x = ((float)event->tfinger.x)/inTouch->xres;
slouken@5460
   163
      y = ((float)event->tfinger.y)/inTouch->yres;      
jim@4689
   164
      
jim@4689
   165
      //draw the touch:      
slouken@5460
   166
      c = colors[event->tfinger.fingerId%7]; 
slouken@5460
   167
      col = ((unsigned int)(c*(.1+.85))) | (unsigned int)(0xFF*age)<<24;
jim@4689
   168
slouken@5460
   169
      if(event->type == SDL_FINGERMOTION)
jim@4689
   170
	drawCircle(screen,x*screen->w,y*screen->h,5,col);
slouken@5460
   171
      else if(event->type == SDL_FINGERDOWN)
jim@4689
   172
	drawCircle(screen,x*screen->w,y*screen->h,-10,col);     
jim@4689
   173
    }
jim@4689
   174
  }
jim@4689
   175
  
jim@4689
   176
  if(knob.p.x > 0)
jim@4689
   177
    drawKnob(screen,knob);
jim@4689
   178
  
slouken@5456
   179
  SDL_UpdateWindowSurface(window);
jim@4689
   180
}
jim@4689
   181
jim@4689
   182
SDL_Surface* initScreen(int width,int height)
jim@4689
   183
{
slouken@5456
   184
  if (!window) {
slouken@5456
   185
    window = SDL_CreateWindow("Gesture Test",
slouken@5456
   186
                              SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
slouken@5456
   187
                              WIDTH, HEIGHT, SDL_WINDOW_RESIZABLE);
slouken@5456
   188
  }
slouken@5456
   189
  if (!window) {
slouken@5456
   190
    return NULL;
slouken@5456
   191
  }
slouken@5456
   192
  return SDL_GetWindowSurface(window);
jim@4689
   193
}
jim@4689
   194
jim@4689
   195
int main(int argc, char* argv[])
jim@4689
   196
{  
jim@4689
   197
  SDL_Surface *screen;
jim@4689
   198
  SDL_Event event;
slouken@4918
   199
  SDL_bool quitting = SDL_FALSE;
slouken@4918
   200
  SDL_RWops *src;
jim@4689
   201
jim@4689
   202
  //gesture variables
slouken@4918
   203
  knob.r = .1f;
jim@4689
   204
  knob.ang = 0;
jim@4689
   205
jim@4689
   206
  if (SDL_Init(SDL_INIT_VIDEO) < 0 ) return 1;
jim@4689
   207
  
jim@4689
   208
  if (!(screen = initScreen(WIDTH,HEIGHT)))
jim@4689
   209
    {
jim@4689
   210
      SDL_Quit();
jim@4689
   211
      return 1;
jim@4689
   212
    }
jim@4689
   213
jim@4689
   214
  while(!quitting) {
jim@4689
   215
    while(SDL_PollEvent(&event)) 
jim@4689
   216
      {
jim@4689
   217
	//Record _all_ events
jim@4689
   218
	events[eventWrite & (EVENT_BUF_SIZE-1)] = event;
jim@4689
   219
	eventWrite++;
jim@4689
   220
	
jim@4689
   221
	switch (event.type) 
jim@4689
   222
	  {
jim@4689
   223
	  case SDL_QUIT:
jim@4689
   224
	    quitting = SDL_TRUE;
jim@4689
   225
	    break;
jim@4689
   226
	  case SDL_KEYDOWN:
jim@4689
   227
	    switch (event.key.keysym.sym)
jim@4689
   228
	      {
jim@4689
   229
	      case SDLK_SPACE:
jim@4689
   230
		SDL_RecordGesture(-1);
jim@4689
   231
		break;
jim@4689
   232
	      case SDLK_s:
jim@4689
   233
		src = SDL_RWFromFile("gestureSave","w");
slouken@5460
   234
		SDL_Log("Wrote %i templates",SDL_SaveAllDollarTemplates(src));
jim@4689
   235
		SDL_RWclose(src);
jim@4689
   236
		break;
jim@4689
   237
	      case SDLK_l:
jim@4689
   238
		src = SDL_RWFromFile("gestureSave","r");
slouken@5460
   239
		SDL_Log("Loaded: %i",SDL_LoadDollarTemplates(-1,src));
jim@4689
   240
		SDL_RWclose(src);
jim@4689
   241
		break;
jim@4689
   242
	      case SDLK_ESCAPE:
jim@4689
   243
		quitting = SDL_TRUE;
jim@4689
   244
		break;
jim@4689
   245
	    }
jim@4689
   246
	    break;
slouken@5456
   247
	  case SDL_WINDOWEVENT:
slouken@5456
   248
            if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
slouken@5456
   249
	      if (!(screen = initScreen(0, 0)))
jim@4689
   250
	      {
jim@4689
   251
		SDL_Quit();
jim@4689
   252
		return 1;
jim@4689
   253
	      }
slouken@5456
   254
            }
jim@4689
   255
	    break;
slouken@4918
   256
	  case SDL_FINGERMOTION:
jim@4689
   257
#if VERBOSE
slouken@5460
   258
	    SDL_Log("Finger: %i,x: %i, y: %i",event.tfinger.fingerId,
jim@4689
   259
	    	   event.tfinger.x,event.tfinger.y);
jim@4689
   260
#endif
slouken@4918
   261
		{
slouken@4918
   262
			SDL_Touch* inTouch = SDL_GetTouch(event.tfinger.touchId);
slouken@4918
   263
			SDL_Finger* inFinger = SDL_GetFinger(inTouch,event.tfinger.fingerId);
slouken@4918
   264
		}
jim@4689
   265
	    break;	    
jim@4689
   266
	  case SDL_FINGERDOWN:
jim@4689
   267
#if VERBOSE
slouken@5460
   268
	    SDL_Log("Finger: %"PRIs64" down - x: %i, y: %i",
jim@4689
   269
		   event.tfinger.fingerId,event.tfinger.x,event.tfinger.y);
jim@4689
   270
#endif
jim@4689
   271
	    break;
jim@4689
   272
	  case SDL_FINGERUP:
jim@4689
   273
#if VERBOSE
slouken@5460
   274
	    SDL_Log("Finger: %"PRIs64" up - x: %i, y: %i",
jim@4689
   275
	    	   event.tfinger.fingerId,event.tfinger.x,event.tfinger.y);
jim@4689
   276
#endif
jim@4689
   277
	    break;
jim@4689
   278
	  case SDL_MULTIGESTURE:
jim@4689
   279
#if VERBOSE	    
slouken@5460
   280
	    SDL_Log("Multi Gesture: x = %f, y = %f, dAng = %f, dR = %f",
jim@4689
   281
		   event.mgesture.x,
jim@4689
   282
		   event.mgesture.y,
jim@4689
   283
		   event.mgesture.dTheta,
jim@4689
   284
		   event.mgesture.dDist);
slouken@5460
   285
	    SDL_Log("MG: numDownTouch = %i",event.mgesture.numFingers);
jim@4689
   286
#endif
jim@4689
   287
	    knob.p.x = event.mgesture.x;
jim@4689
   288
	    knob.p.y = event.mgesture.y;
jim@4689
   289
	    knob.ang += event.mgesture.dTheta;
jim@4689
   290
	    knob.r += event.mgesture.dDist;
jim@4689
   291
	    break;
jim@4689
   292
	  case SDL_DOLLARGESTURE:
slouken@5460
   293
	    SDL_Log("Gesture %"PRIs64" performed, error: %f",
jim@4689
   294
		   event.dgesture.gestureId,
jim@4689
   295
		   event.dgesture.error);
jim@4689
   296
	    break;
jim@4689
   297
	  case SDL_DOLLARRECORD:
slouken@5460
   298
	    SDL_Log("Recorded gesture: %"PRIs64"",event.dgesture.gestureId);
jim@4689
   299
	    break;
jim@4689
   300
	  }
jim@4689
   301
      }
jim@4689
   302
    DrawScreen(screen);
jim@4689
   303
  }  
jim@4689
   304
  SDL_Quit();  
jim@4689
   305
  return 0;
jim@4689
   306
}
jim@4689
   307