test/testgesture.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 31 Dec 2012 14:08:43 -0800
changeset 6790 475f0a544157
parent 5535 96594ac5fd1a
child 6951 7833f01322b3
permissions -rw-r--r--
Fixed bug 1672 - Found: seg-fault with testgesture

automata 2012-12-23 22:05:21 PST

An incorrect guard resulted in segmentation faults with the
SDL/test/testgesture executable. The attached patch fixes this issue, and also
allows the program's window to properly handle resize events.

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