test/testgesture.c
author Andreas Schiffler <aschiffler@ferzkopp.net>
Wed, 26 Dec 2012 22:26:44 -0800
changeset 6773 5ebc5a9e35b4
parent 5535 96594ac5fd1a
child 6790 475f0a544157
permissions -rw-r--r--
Add surface test suite; minor improvements to render suite; refactor image saving into test lib compare function; fix for Haiku build
slouken@5535
     1
/*
slouken@5535
     2
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
slouken@5535
     3
slouken@5535
     4
  This software is provided 'as-is', without any express or implied
slouken@5535
     5
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     6
  arising from the use of this software.
slouken@5535
     7
slouken@5535
     8
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
     9
  including commercial applications, and to alter it and redistribute it
slouken@5535
    10
  freely.
slouken@5535
    11
*/
jim@4689
    12
/*  Usage:
jim@4689
    13
 *  Spacebar to begin recording a gesture on all touches.
jim@4689
    14
 *  s to save all touches into "./gestureSave"
jim@4689
    15
 *  l to load all touches from "./gestureSave"
jim@4689
    16
 */
jim@4689
    17
jim@4689
    18
#include <stdio.h>
jim@4689
    19
#include <math.h>
slouken@5456
    20
slouken@5456
    21
#include "SDL.h"
slouken@5456
    22
#include "SDL_touch.h"
slouken@5456
    23
#include "SDL_gesture.h"
jim@4689
    24
jim@4689
    25
/* Make sure we have good macros for printing 32 and 64 bit values */
jim@4689
    26
#ifndef PRIs32
jim@4689
    27
#define PRIs32 "d"
jim@4689
    28
#endif
jim@4689
    29
#ifndef PRIu32
jim@4689
    30
#define PRIu32 "u"
jim@4689
    31
#endif
jim@4689
    32
#ifndef PRIs64
slouken@5086
    33
#ifdef __WIN32__
jim@4689
    34
#define PRIs64 "I64"
jim@4689
    35
#else
jim@4689
    36
#define PRIs64 "lld"
jim@4689
    37
#endif
jim@4689
    38
#endif
jim@4689
    39
#ifndef PRIu64
slouken@5086
    40
#ifdef __WIN32__
jim@4689
    41
#define PRIu64 "I64u"
jim@4689
    42
#else
jim@4689
    43
#define PRIu64 "llu"
jim@4689
    44
#endif
jim@4689
    45
#endif
jim@4689
    46
jim@4689
    47
#define WIDTH 640
jim@4689
    48
#define HEIGHT 480
jim@4689
    49
#define BPP 4
jim@4689
    50
#define DEPTH 32
jim@4689
    51
jim@4689
    52
//MUST BE A POWER OF 2!
jim@4689
    53
#define EVENT_BUF_SIZE 256
jim@4689
    54
jim@4689
    55
slouken@5458
    56
#define VERBOSE 0
jim@4689
    57
slouken@5457
    58
static SDL_Window *window;
slouken@5457
    59
static SDL_Event events[EVENT_BUF_SIZE];
slouken@5457
    60
static int eventWrite;
jim@4689
    61
slouken@5456
    62
slouken@5457
    63
static int colors[7] = {0xFF,0xFF00,0xFF0000,0xFFFF00,0x00FFFF,0xFF00FF,0xFFFFFF};
jim@4689
    64
jim@4689
    65
typedef struct {
jim@4689
    66
  float x,y;
jim@4689
    67
} Point;
jim@4689
    68
jim@4689
    69
typedef struct {
jim@4689
    70
  float ang,r;
jim@4689
    71
  Point p;
jim@4689
    72
} Knob;
jim@4689
    73
slouken@5457
    74
static Knob knob;
jim@4689
    75
jim@4689
    76
void handler (int sig)
jim@4689
    77
{
slouken@5460
    78
  SDL_Log ("exiting...(%d)", sig);
jim@4689
    79
  exit (0);
jim@4689
    80
}
jim@4689
    81
jim@4689
    82
void perror_exit (char *error)
jim@4689
    83
{
jim@4689
    84
  perror (error);
jim@4689
    85
  handler (9);
jim@4689
    86
}
jim@4689
    87
slouken@4918
    88
void setpix(SDL_Surface *screen, float _x, float _y, unsigned int col)
jim@4689
    89
{
jim@4689
    90
  Uint32 *pixmem32;
jim@4689
    91
  Uint32 colour;
slouken@4918
    92
  Uint8 r,g,b;
slouken@4918
    93
  int x = (int)_x;
slouken@4918
    94
  int y = (int)_y;
slouken@4918
    95
  float a;
jim@4689
    96
  
slouken@4921
    97
  if(x < 0 || x > screen->w) return;
slouken@4921
    98
  if(y < 0 || y > screen->h) return;
jim@4689
    99
jim@4689
   100
  pixmem32 = (Uint32*) screen->pixels  + y*screen->pitch/BPP + x;
jim@4689
   101
  
slouken@4918
   102
  SDL_memcpy(&colour,pixmem32,screen->format->BytesPerPixel);
jim@4689
   103
jim@4689
   104
  SDL_GetRGB(colour,screen->format,&r,&g,&b);
jim@4689
   105
  //r = 0;g = 0; b = 0;
slouken@4918
   106
  a = (float)((col>>24)&0xFF);
jim@4689
   107
  if(a == 0) a = 0xFF; //Hack, to make things easier.
jim@4689
   108
  a /= 0xFF;
slouken@4918
   109
  r = (Uint8)(r*(1-a) + ((col>>16)&0xFF)*(a));
slouken@4918
   110
  g = (Uint8)(g*(1-a) + ((col>> 8)&0xFF)*(a));
slouken@4918
   111
  b = (Uint8)(b*(1-a) + ((col>> 0)&0xFF)*(a));
jim@4689
   112
  colour = SDL_MapRGB( screen->format,r, g, b);
jim@4689
   113
  
jim@4689
   114
jim@4689
   115
  *pixmem32 = colour;
jim@4689
   116
}
jim@4689
   117
slouken@4918
   118
void drawLine(SDL_Surface *screen,float x0,float y0,float x1,float y1,unsigned int col) {
jim@4689
   119
  float t;
slouken@4918
   120
  for(t=0;t<1;t+=(float)(1.f/SDL_max(SDL_fabs(x0-x1),SDL_fabs(y0-y1))))
jim@4689
   121
    setpix(screen,x1+t*(x0-x1),y1+t*(y0-y1),col);
jim@4689
   122
}
jim@4689
   123
slouken@4918
   124
void drawCircle(SDL_Surface* screen,float x,float y,float r,unsigned int c)
jim@4689
   125
{
slouken@4918
   126
  float tx,ty;
jim@4689
   127
  float xr;
slouken@4918
   128
  for(ty = (float)-SDL_fabs(r);ty <= (float)SDL_fabs((int)r);ty++) {
slouken@4918
   129
    xr = (float)sqrt(r*r - ty*ty);
jim@4689
   130
    if(r > 0) { //r > 0 ==> filled circle
slouken@4918
   131
      for(tx=-xr+.5f;tx<=xr-.5;tx++) {
jim@4689
   132
	setpix(screen,x+tx,y+ty,c);
jim@4689
   133
      }
jim@4689
   134
    }
jim@4689
   135
    else {
slouken@4918
   136
      setpix(screen,x-xr+.5f,y+ty,c);
slouken@4918
   137
      setpix(screen,x+xr-.5f,y+ty,c);
jim@4689
   138
    }
jim@4689
   139
  }
jim@4689
   140
}
jim@4689
   141
jim@4689
   142
void drawKnob(SDL_Surface* screen,Knob k) {
jim@4689
   143
  drawCircle(screen,k.p.x*screen->w,k.p.y*screen->h,k.r*screen->w,0xFFFFFF);  
slouken@4918
   144
  drawCircle(screen,(k.p.x+k.r/2*cosf(k.ang))*screen->w,
slouken@4918
   145
  	            (k.p.y+k.r/2*sinf(k.ang))*screen->h,k.r/4*screen->w,0);
jim@4689
   146
}
jim@4689
   147
jim@4689
   148
void DrawScreen(SDL_Surface* screen)
jim@4689
   149
{
slouken@5456
   150
  int i;
slouken@5456
   151
#if 1
slouken@5456
   152
  SDL_FillRect(screen, NULL, 0);
slouken@5456
   153
#else
slouken@5456
   154
  int x, y;
jim@4689
   155
  for(y = 0;y < screen->h;y++)
jim@4689
   156
    for(x = 0;x < screen->w;x++)
slouken@4918
   157
	setpix(screen,(float)x,(float)y,((x%255)<<16) + ((y%255)<<8) + (x+y)%255);
slouken@5456
   158
#endif
jim@4689
   159
jim@4689
   160
  //draw Touch History
slouken@5460
   161
  for(i = eventWrite; i < eventWrite+EVENT_BUF_SIZE; ++i) {
slouken@5460
   162
    const SDL_Event *event = &events[i&(EVENT_BUF_SIZE-1)];
slouken@5460
   163
    float age = (float)(i - eventWrite) / EVENT_BUF_SIZE;
slouken@4918
   164
	float x, y;
slouken@4918
   165
	unsigned int c, col;
slouken@4918
   166
slouken@5460
   167
    if(event->type == SDL_FINGERMOTION || 
slouken@5460
   168
       event->type == SDL_FINGERDOWN ||
slouken@5460
   169
       event->type == SDL_FINGERUP) {
slouken@5460
   170
      SDL_Touch* inTouch = SDL_GetTouch(event->tfinger.touchId);
jim@4689
   171
      if(inTouch == NULL) continue;
jim@4689
   172
slouken@5460
   173
      x = ((float)event->tfinger.x)/inTouch->xres;
slouken@5460
   174
      y = ((float)event->tfinger.y)/inTouch->yres;      
jim@4689
   175
      
jim@4689
   176
      //draw the touch:      
slouken@5460
   177
      c = colors[event->tfinger.fingerId%7]; 
slouken@5460
   178
      col = ((unsigned int)(c*(.1+.85))) | (unsigned int)(0xFF*age)<<24;
jim@4689
   179
slouken@5460
   180
      if(event->type == SDL_FINGERMOTION)
jim@4689
   181
	drawCircle(screen,x*screen->w,y*screen->h,5,col);
slouken@5460
   182
      else if(event->type == SDL_FINGERDOWN)
jim@4689
   183
	drawCircle(screen,x*screen->w,y*screen->h,-10,col);     
jim@4689
   184
    }
jim@4689
   185
  }
jim@4689
   186
  
jim@4689
   187
  if(knob.p.x > 0)
jim@4689
   188
    drawKnob(screen,knob);
jim@4689
   189
  
slouken@5456
   190
  SDL_UpdateWindowSurface(window);
jim@4689
   191
}
jim@4689
   192
jim@4689
   193
SDL_Surface* initScreen(int width,int height)
jim@4689
   194
{
slouken@5456
   195
  if (!window) {
slouken@5456
   196
    window = SDL_CreateWindow("Gesture Test",
slouken@5456
   197
                              SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
slouken@5456
   198
                              WIDTH, HEIGHT, SDL_WINDOW_RESIZABLE);
slouken@5456
   199
  }
slouken@5456
   200
  if (!window) {
slouken@5456
   201
    return NULL;
slouken@5456
   202
  }
slouken@5456
   203
  return SDL_GetWindowSurface(window);
jim@4689
   204
}
jim@4689
   205
jim@4689
   206
int main(int argc, char* argv[])
jim@4689
   207
{  
jim@4689
   208
  SDL_Surface *screen;
jim@4689
   209
  SDL_Event event;
slouken@4918
   210
  SDL_bool quitting = SDL_FALSE;
slouken@4918
   211
  SDL_RWops *src;
jim@4689
   212
jim@4689
   213
  //gesture variables
slouken@4918
   214
  knob.r = .1f;
jim@4689
   215
  knob.ang = 0;
jim@4689
   216
jim@4689
   217
  if (SDL_Init(SDL_INIT_VIDEO) < 0 ) return 1;
jim@4689
   218
  
jim@4689
   219
  if (!(screen = initScreen(WIDTH,HEIGHT)))
jim@4689
   220
    {
jim@4689
   221
      SDL_Quit();
jim@4689
   222
      return 1;
jim@4689
   223
    }
jim@4689
   224
jim@4689
   225
  while(!quitting) {
jim@4689
   226
    while(SDL_PollEvent(&event)) 
jim@4689
   227
      {
jim@4689
   228
	//Record _all_ events
jim@4689
   229
	events[eventWrite & (EVENT_BUF_SIZE-1)] = event;
jim@4689
   230
	eventWrite++;
jim@4689
   231
	
jim@4689
   232
	switch (event.type) 
jim@4689
   233
	  {
jim@4689
   234
	  case SDL_QUIT:
jim@4689
   235
	    quitting = SDL_TRUE;
jim@4689
   236
	    break;
jim@4689
   237
	  case SDL_KEYDOWN:
jim@4689
   238
	    switch (event.key.keysym.sym)
jim@4689
   239
	      {
jim@4689
   240
	      case SDLK_SPACE:
jim@4689
   241
		SDL_RecordGesture(-1);
jim@4689
   242
		break;
jim@4689
   243
	      case SDLK_s:
jim@4689
   244
		src = SDL_RWFromFile("gestureSave","w");
slouken@5460
   245
		SDL_Log("Wrote %i templates",SDL_SaveAllDollarTemplates(src));
jim@4689
   246
		SDL_RWclose(src);
jim@4689
   247
		break;
jim@4689
   248
	      case SDLK_l:
jim@4689
   249
		src = SDL_RWFromFile("gestureSave","r");
slouken@5460
   250
		SDL_Log("Loaded: %i",SDL_LoadDollarTemplates(-1,src));
jim@4689
   251
		SDL_RWclose(src);
jim@4689
   252
		break;
jim@4689
   253
	      case SDLK_ESCAPE:
jim@4689
   254
		quitting = SDL_TRUE;
jim@4689
   255
		break;
jim@4689
   256
	    }
jim@4689
   257
	    break;
slouken@5456
   258
	  case SDL_WINDOWEVENT:
slouken@5456
   259
            if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
slouken@5456
   260
	      if (!(screen = initScreen(0, 0)))
jim@4689
   261
	      {
jim@4689
   262
		SDL_Quit();
jim@4689
   263
		return 1;
jim@4689
   264
	      }
slouken@5456
   265
            }
jim@4689
   266
	    break;
slouken@4918
   267
	  case SDL_FINGERMOTION:
jim@4689
   268
#if VERBOSE
slouken@5460
   269
	    SDL_Log("Finger: %i,x: %i, y: %i",event.tfinger.fingerId,
jim@4689
   270
	    	   event.tfinger.x,event.tfinger.y);
jim@4689
   271
#endif
slouken@4918
   272
		{
slouken@4918
   273
			SDL_Touch* inTouch = SDL_GetTouch(event.tfinger.touchId);
slouken@4918
   274
			SDL_Finger* inFinger = SDL_GetFinger(inTouch,event.tfinger.fingerId);
slouken@4918
   275
		}
jim@4689
   276
	    break;	    
jim@4689
   277
	  case SDL_FINGERDOWN:
jim@4689
   278
#if VERBOSE
slouken@5460
   279
	    SDL_Log("Finger: %"PRIs64" down - x: %i, y: %i",
jim@4689
   280
		   event.tfinger.fingerId,event.tfinger.x,event.tfinger.y);
jim@4689
   281
#endif
jim@4689
   282
	    break;
jim@4689
   283
	  case SDL_FINGERUP:
jim@4689
   284
#if VERBOSE
slouken@5460
   285
	    SDL_Log("Finger: %"PRIs64" up - x: %i, y: %i",
jim@4689
   286
	    	   event.tfinger.fingerId,event.tfinger.x,event.tfinger.y);
jim@4689
   287
#endif
jim@4689
   288
	    break;
jim@4689
   289
	  case SDL_MULTIGESTURE:
jim@4689
   290
#if VERBOSE	    
slouken@5460
   291
	    SDL_Log("Multi Gesture: x = %f, y = %f, dAng = %f, dR = %f",
jim@4689
   292
		   event.mgesture.x,
jim@4689
   293
		   event.mgesture.y,
jim@4689
   294
		   event.mgesture.dTheta,
jim@4689
   295
		   event.mgesture.dDist);
slouken@5460
   296
	    SDL_Log("MG: numDownTouch = %i",event.mgesture.numFingers);
jim@4689
   297
#endif
jim@4689
   298
	    knob.p.x = event.mgesture.x;
jim@4689
   299
	    knob.p.y = event.mgesture.y;
jim@4689
   300
	    knob.ang += event.mgesture.dTheta;
jim@4689
   301
	    knob.r += event.mgesture.dDist;
jim@4689
   302
	    break;
jim@4689
   303
	  case SDL_DOLLARGESTURE:
slouken@5460
   304
	    SDL_Log("Gesture %"PRIs64" performed, error: %f",
jim@4689
   305
		   event.dgesture.gestureId,
jim@4689
   306
		   event.dgesture.error);
jim@4689
   307
	    break;
jim@4689
   308
	  case SDL_DOLLARRECORD:
slouken@5460
   309
	    SDL_Log("Recorded gesture: %"PRIs64"",event.dgesture.gestureId);
jim@4689
   310
	    break;
jim@4689
   311
	  }
jim@4689
   312
      }
jim@4689
   313
    DrawScreen(screen);
jim@4689
   314
  }  
jim@4689
   315
  SDL_Quit();  
jim@4689
   316
  return 0;
jim@4689
   317
}
jim@4689
   318