Added README.touch and README.gesture. Moved touchtest/gestureSDLTest to test/testgesture
authorJim Grandpre <jim.tla@gmail.com>
Sun, 15 Aug 2010 00:36:28 -0400
changeset 4689f9ab8df6d45a
parent 4688 494f71f57a80
child 4690 baf8195aeb92
Added README.touch and README.gesture. Moved touchtest/gestureSDLTest to test/testgesture
README.gesture
README.iphoneos
README.touch
include/SDL_events.h
include/SDL_touch.h
src/events/SDL_gesture.c
src/events/SDL_touch.c
test/Makefile.in
test/testgesture.c
touchTest/gestureSDLTest.c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/README.gesture	Sun Aug 15 00:36:28 2010 -0400
     1.3 @@ -0,0 +1,73 @@
     1.4 +===========================================================================
     1.5 +Dollar Gestures
     1.6 +===========================================================================
     1.7 +SDL Provides an implementation of the $1 gesture recognition system. This allows for recording, saving, loading, and performing single stroke gestures.
     1.8 +
     1.9 +Gestures can be performed with any number of fingers (the centroid of the fingers must follow the path of the gesture), but the number of fingers must be constant (a finger cannot go down in the middle of a gesture). The path of a gesture is considered the path from the time when the final finger went down, to the first time any finger comes up. 
    1.10 +
    1.11 +Dollar gestures are assigned an Id based on a hash function. This is guaranteed to remain constant for a given gesture. There is a (small) chance that two different gestures will be assigned the same ID. In this simply re-recording on of the gestures should result in a different ID.
    1.12 +
    1.13 +Recording:
    1.14 +----------
    1.15 +To begin recording on a touch device call:
    1.16 +SDL_RecordGesture(SDL_TouchID touchId), where touchId is the id of the touch device you wish to record on, or -1 to record on all connected devices.
    1.17 +
    1.18 +Recording terminates as soon as a finger comes up. Recording is acknowledged by an SDL_DOLLARRECORD event.
    1.19 +A SDL_DOLLARRECORD event is a dgesture with the following fields:
    1.20 +
    1.21 +event.dgesture.touchId   - the Id of the touch used to record the gesture.
    1.22 +event.dgesture.gestureId - the unique id of the recoreded gesture.
    1.23 +
    1.24 +
    1.25 +
    1.26 +Performing:
    1.27 +-----------
    1.28 +As long as there is a dollar gesture assigned to a touch, every finger-up event will also cause an SDL_DOLLARGESTURE event with the following fields:
    1.29 +
    1.30 +event.dgesture.touchId    - the Id of the touch which performed the gesture.
    1.31 +event.dgesture.gestureId  - the unique id of the closest gesture to the performed stroke.
    1.32 +event.dgesture.error      - the difference between the gesture template and the actual performed gesture. Lower error is a better match.
    1.33 +event.dgesture.numFingers - the number of fingers used to draw the stroke.
    1.34 +
    1.35 +Most programs will want to define an appropriate error threshold and check to be sure taht the error of a gesture is not abnormally high (an indicator that no gesture was performed). 
    1.36 +
    1.37 +
    1.38 +
    1.39 +Saving:
    1.40 +-------
    1.41 +To save a template, call SDL_SaveDollarTemplate(gestureId, src) where gestureId is the id of the gesture you want to save, and src is an SDL_RWops pointer to the file where the gesture will be stored.
    1.42 +
    1.43 +To save all currently loaded templatesm, call SDL_SaveAllDollarTemplates(src) where source is an SDL_RWops pointer to the file where the gesture will be stored.
    1.44 +
    1.45 +Both functions return the number of gestures sucessfully saved.
    1.46 +
    1.47 +
    1.48 +Loading:
    1.49 +--------
    1.50 +To load templates from a file, call SDL_LoadDollarTemplates(touchId,src) where touchId is the id of the touch to load to (or -1 to load to all touch devices), and src is an SDL_RWops pointer to a gesture save file. 
    1.51 +
    1.52 +SDL_LoadDollarTemplates returns the number of templates sucessfully loaded. 
    1.53 +
    1.54 +
    1.55 +
    1.56 +===========================================================================
    1.57 +Multi Gestures
    1.58 +===========================================================================
    1.59 +SDL provides simple support to pinch/rotate/swipe gestures. 
    1.60 +Every time a finger is moved an SDL_MULTIGESTURE event is sent with the following fields:
    1.61 +
    1.62 +event.mgesture.touchId - the Id of the touch on which the gesture was performed.
    1.63 +event.mgesture.x       - the normalized x cooridinate of the gesture. (0..1)
    1.64 +event.mgesture.y       - the normalized y cooridinate of the gesture. (0..1)
    1.65 +event.mgesture.dTheta  - the amount that the fingers rotated during this motion.
    1.66 +event.mgesture.dDist   - the amount that the fingers pinched during this motion.
    1.67 +event.mgesture.numFingers - the number of fingers used in the gesture.
    1.68 +
    1.69 +
    1.70 +===========================================================================
    1.71 +Notes
    1.72 +===========================================================================
    1.73 +For a complete example see test/testgesture.c
    1.74 +
    1.75 +Please direct questions/comments to:
    1.76 +   jim.tla+sdl_touch@gmail.com
    1.77 \ No newline at end of file
     2.1 --- a/README.iphoneos	Fri Aug 13 23:08:32 2010 -0400
     2.2 +++ b/README.iphoneos	Sun Aug 15 00:36:28 2010 -0400
     2.3 @@ -34,14 +34,6 @@
     2.4  5.  Delete the contents of main.m and program your app as a regular SDL program instead.  You may replace main.m with your own main.c, but you must tell XCode not to use the project prefix file, as it includes Objective-C code.
     2.5  
     2.6  ==============================================================================
     2.7 -Notes -- Touch Input
     2.8 -==============================================================================
     2.9 -
    2.10 -Touch input in SDL for iPhone OS is presently exposed through SDL's mouse input API.  Multi-touch input is reported as multiple mice, with each touch associated with a specific mouse.  This association stays coherent from the time the touch starts to the time a touch ends.
    2.11 -
    2.12 -By default, multi-touch is turned ON.  This requires some care, because if you simply respond to mouse events without checking which mouse caused the event, you may end up fetching data from the wrong mouse, ie, from an incorrect or invalid touch.  To turn multi-touch OFF, you can recompile SDL for iPhone with the macro SDL_IPHONE_MULTIPLE_MICE (found in SDL_config_iphoneos.h) set to 0.
    2.13 -
    2.14 -==============================================================================
    2.15  Notes -- Accelerometer as Joystick
    2.16  ==============================================================================
    2.17  
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/README.touch	Sun Aug 15 00:36:28 2010 -0400
     3.3 @@ -0,0 +1,77 @@
     3.4 +===========================================================================
     3.5 +Events
     3.6 +===========================================================================
     3.7 +SDL_FINGERDOWN:
     3.8 +Sent when a finger (or stylus) is placed on a touch device.
     3.9 +Fields:
    3.10 +event.tfinger.touchId  - the Id of the touch device.
    3.11 +event.tfinger.fingerId - the Id of the finger which just went down.
    3.12 +event.tfinger.x        - the x coordinate of the touch (0..touch.xres)
    3.13 +event.tfinger.y        - the y coordinate of the touch (0..touch.yres)
    3.14 +event.tfinger.pressure - the pressure of the touch (0..touch.pressureres)
    3.15 +
    3.16 +SDL_FINGERMOTION:
    3.17 +Sent when a finger (or stylus) is moved on the touch device.
    3.18 +Fields:
    3.19 +Same as FINGERDOWN but with additional:
    3.20 +event.tfginer.dx       - chagne in x coordinate during this motion event.
    3.21 +event.tfginer.dy       - chagne in y coordinate during this motion event.
    3.22 +
    3.23 +SDL_FINGERMOTION:
    3.24 +Sent when a finger (or stylus) is lifted from the touch device.
    3.25 +Fields:
    3.26 +Same as FINGERDOWN.
    3.27 +
    3.28 +
    3.29 +===========================================================================
    3.30 +Functions
    3.31 +===========================================================================
    3.32 +SDL provides the ability to access the underlying Touch and Finger structures.
    3.33 +These structures should _never_ be modified.
    3.34 +
    3.35 +The following functions are included from SDL_Touch.h
    3.36 +
    3.37 +To get a SDL_Touch device call SDL_GetTouch(touchId). 
    3.38 +This returns an SDL_Touch*. 
    3.39 +IMPORTANT: If the touch has been removed, or there is no touch with the given ID, SDL_GetTouch will return null. Be sure to check for this!
    3.40 +
    3.41 +An SDL_Touch has the following fields:
    3.42 +>pressure_max, pressure_min, x_max, x_min, y_max, y_min
    3.43 +	Which give, respectively, the maximum and minumum values that the touch digitizer can return for pressure, x coordiniate, and y coordinate AS REPORTED BY THE OPERATING SYSTEM.
    3.44 +On Mac/iPhone systems _max will always be 0, and _min will always be 1. 
    3.45 +
    3.46 +>xres,yres,pressures:
    3.47 +	The resolution at which x,y, and pressure values are reported. Currently these will always be equal to 2^15, but this may not always be the case. 
    3.48 +
    3.49 +>native_xres,native_yres,native_pressureres:
    3.50 +	The native resolution of the touch device AS REPORTED BY THE OPERATING SYSTEM.
    3.51 +On Mac/iPhone systems these will always be 1.
    3.52 +
    3.53 +>num_fingers:
    3.54 +	The number of fingers currently down on the device.
    3.55 +
    3.56 +>fingers:
    3.57 +	An array of pointers to the fingers which are on the device.
    3.58 +
    3.59 +
    3.60 +The most common reason to get a touch device is to normalize inputs. This would look something like:
    3.61 +
    3.62 +      SDL_Touch* inTouch = SDL_GetTouch(event.tfinger.touchId);
    3.63 +      if(inTouch == NULL) continue;
    3.64 +
    3.65 +      float x = ((float)event.tfinger.x)/inTouch->xres;
    3.66 +      float y = ((float)event.tfinger.y)/inTouch->yres;      
    3.67 +
    3.68 +
    3.69 +To get an SDL_Finger, call SDL_GetFinger(touch,fingerId), where touch is a pointer to an SDL_Touch device, and fingerId is the id of the requested finger.
    3.70 +This returns an SDL_Finger*, or null if the finger does not exist, or has been removed.
    3.71 +An SDL_Finger is guaranteed to be persistent for the duration of a touch, but it will be de-allocated as soon as the finger is removed. This occurs when the SDL_FINGERUP event is _added_ to the event queue, and thus BEFORE the FINGERUP event is seen. 
    3.72 +As a result, be very careful to check for null return values.
    3.73 +
    3.74 +An SDL_Finger has the following fields:
    3.75 +>x,y,pressure:
    3.76 +	The current coordinates of the touch.
    3.77 +>xdelta,ydelta: 
    3.78 +	The change in position resulting from the last finger motion.
    3.79 +>last_x, last_y, last_pressure:
    3.80 +	 The previous coordinates of the touch.
    3.81 \ No newline at end of file
     4.1 --- a/include/SDL_events.h	Fri Aug 13 23:08:32 2010 -0400
     4.2 +++ b/include/SDL_events.h	Sun Aug 15 00:36:28 2010 -0400
     4.3 @@ -340,6 +340,7 @@
     4.4      Uint32 windowID;    /**< The window with mouse focus, if any */
     4.5      SDL_TouchID touchId;        /**< The touch device index */
     4.6      SDL_GestureID gestureId;
     4.7 +    Uint32 numFingers;
     4.8      float error;
     4.9    /*
    4.10      //TODO: Enable to give location?
     5.1 --- a/include/SDL_touch.h	Fri Aug 13 23:08:32 2010 -0400
     5.2 +++ b/include/SDL_touch.h	Sun Aug 15 00:36:28 2010 -0400
     5.3 @@ -50,11 +50,11 @@
     5.4    SDL_FingerID id;
     5.5    Uint16 x;
     5.6    Uint16 y;
     5.7 +  Uint16 pressure;
     5.8    Uint16 xdelta;
     5.9    Uint16 ydelta;
    5.10    Uint16 last_x, last_y,last_pressure;  /* the last reported coordinates */
    5.11    SDL_bool down;
    5.12 -  Uint16 pressure;
    5.13  };
    5.14  
    5.15  typedef struct SDL_Touch SDL_Touch;
     6.1 --- a/src/events/SDL_gesture.c	Fri Aug 13 23:08:32 2010 -0400
     6.2 +++ b/src/events/SDL_gesture.c	Sun Aug 15 00:36:28 2010 -0400
     6.3 @@ -462,6 +462,8 @@
     6.4    */
     6.5    event.dgesture.gestureId = gestureId;
     6.6    event.dgesture.error = error;  
     6.7 +  //A finger came up to trigger this event.
     6.8 +  event.dgesture.numFingers = touch->numDownFingers + 1; 
     6.9    return SDL_PushEvent(&event) > 0;
    6.10  }
    6.11  
    6.12 @@ -471,7 +473,6 @@
    6.13    event.dgesture.type = SDL_DOLLARRECORD;
    6.14    event.dgesture.touchId = touch->id;
    6.15    event.dgesture.gestureId = gestureId;
    6.16 -
    6.17    return SDL_PushEvent(&event) > 0;
    6.18  }
    6.19  
     7.1 --- a/src/events/SDL_touch.c	Fri Aug 13 23:08:32 2010 -0400
     7.2 +++ b/src/events/SDL_touch.c	Sun Aug 15 00:36:28 2010 -0400
     7.3 @@ -351,6 +351,7 @@
     7.4  	    event.tfinger.touchId = id;
     7.5  	    event.tfinger.x = x;
     7.6  	    event.tfinger.y = y;
     7.7 +	    event.tfinger.pressure = pressure;
     7.8  	    event.tfinger.state = touch->buttonstate;
     7.9  	    event.tfinger.windowID = touch->focus ? touch->focus->id : 0;
    7.10  	    event.tfinger.fingerId = fingerid;
    7.11 @@ -470,8 +471,7 @@
    7.12  	    event.tfinger.x = x;
    7.13  	    event.tfinger.y = y;
    7.14  	    event.tfinger.dx = xrel;
    7.15 -	    event.tfinger.dy = yrel;
    7.16 -	    
    7.17 +	    event.tfinger.dy = yrel;	    
    7.18  		
    7.19  	    event.tfinger.pressure = pressure;
    7.20  	    event.tfinger.state = touch->buttonstate;
     8.1 --- a/test/Makefile.in	Fri Aug 13 23:08:32 2010 -0400
     8.2 +++ b/test/Makefile.in	Sun Aug 15 00:36:28 2010 -0400
     8.3 @@ -7,7 +7,7 @@
     8.4  CFLAGS  = @CFLAGS@
     8.5  LIBS	= @LIBS@
     8.6  
     8.7 -TARGETS = checkkeys$(EXE) graywin$(EXE) loopwave$(EXE) testalpha$(EXE) testatomic$(EXE) testaudioinfo$(EXE) testbitmap$(EXE) testblitspeed$(EXE) testcursor$(EXE) testdraw2$(EXE) testdyngles$(EXE) testdyngl$(EXE) testerror$(EXE) testfile$(EXE) testfill$(EXE) testgamma$(EXE) testgl2$(EXE) testgles$(EXE) testgl$(EXE) testhaptic$(EXE) testhread$(EXE) testiconv$(EXE) testime$(EXE) testintersections$(EXE) testjoystick$(EXE) testkeys$(EXE) testloadso$(EXE) testlock$(EXE) testmultiaudio$(EXE) testoverlay2$(EXE) testoverlay$(EXE) testpalette$(EXE) testplatform$(EXE) testpower$(EXE) testresample$(EXE) testsem$(EXE) testsprite2$(EXE) testsprite$(EXE) testspriteminimal$(EXE) testtimer$(EXE) testver$(EXE) testvidinfo$(EXE) testwin$(EXE) testwm2$(EXE) testwm$(EXE) threadwin$(EXE) torturethread$(EXE)
     8.8 +TARGETS = checkkeys$(EXE) graywin$(EXE) loopwave$(EXE) testalpha$(EXE) testatomic$(EXE) testaudioinfo$(EXE) testbitmap$(EXE) testblitspeed$(EXE) testcursor$(EXE) testdraw2$(EXE) testdyngles$(EXE) testdyngl$(EXE) testerror$(EXE) testfile$(EXE) testfill$(EXE) testgamma$(EXE) testgl2$(EXE) testgles$(EXE) testgl$(EXE) testhaptic$(EXE) testhread$(EXE) testiconv$(EXE) testime$(EXE) testintersections$(EXE) testjoystick$(EXE) testkeys$(EXE) testloadso$(EXE) testlock$(EXE) testmultiaudio$(EXE) testoverlay2$(EXE) testoverlay$(EXE) testpalette$(EXE) testplatform$(EXE) testpower$(EXE) testresample$(EXE) testsem$(EXE) testsprite2$(EXE) testsprite$(EXE) testspriteminimal$(EXE) testtimer$(EXE) testver$(EXE) testvidinfo$(EXE) testwin$(EXE) testwm2$(EXE) testwm$(EXE) threadwin$(EXE) torturethread$(EXE) testgesture$(EXE)
     8.9  
    8.10  all: Makefile $(TARGETS)
    8.11  
    8.12 @@ -151,6 +151,9 @@
    8.13  
    8.14  testatomic$(EXE): $(srcdir)/testatomic.c
    8.15  	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
    8.16 +
    8.17 +testgesture$(EXE): $(srcdir)/testgesture.c
    8.18 +	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
    8.19   
    8.20  testime$(EXE): $(srcdir)/testime.c
    8.21  	$(CC) -o $@ $? $(CFLAGS) $(LIBS) @SDL_TTF_LIB@
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/test/testgesture.c	Sun Aug 15 00:36:28 2010 -0400
     9.3 @@ -0,0 +1,297 @@
     9.4 +/*  Usage:
     9.5 + *  Spacebar to begin recording a gesture on all touches.
     9.6 + *  s to save all touches into "./gestureSave"
     9.7 + *  l to load all touches from "./gestureSave"
     9.8 + */
     9.9 +
    9.10 +#include <stdio.h>
    9.11 +#include <SDL.h>
    9.12 +#include <math.h>
    9.13 +#include <SDL_touch.h>
    9.14 +#include <SDL_gesture.h>
    9.15 +
    9.16 +
    9.17 +/* Make sure we have good macros for printing 32 and 64 bit values */
    9.18 +#ifndef PRIs32
    9.19 +#define PRIs32 "d"
    9.20 +#endif
    9.21 +#ifndef PRIu32
    9.22 +#define PRIu32 "u"
    9.23 +#endif
    9.24 +#ifndef PRIs64
    9.25 +#ifdef __WIN32__
    9.26 +#define PRIs64 "I64"
    9.27 +#else
    9.28 +#define PRIs64 "lld"
    9.29 +#endif
    9.30 +#endif
    9.31 +#ifndef PRIu64
    9.32 +#ifdef __WIN32__
    9.33 +#define PRIu64 "I64u"
    9.34 +#else
    9.35 +#define PRIu64 "llu"
    9.36 +#endif
    9.37 +#endif
    9.38 +
    9.39 +#define WIDTH 640
    9.40 +#define HEIGHT 480
    9.41 +#define BPP 4
    9.42 +#define DEPTH 32
    9.43 +
    9.44 +//MUST BE A POWER OF 2!
    9.45 +#define EVENT_BUF_SIZE 256
    9.46 +
    9.47 +
    9.48 +#define VERBOSE SDL_FALSE
    9.49 +
    9.50 +SDL_Event events[EVENT_BUF_SIZE];
    9.51 +int eventWrite;
    9.52 +
    9.53 +int colors[7] = {0xFF,0xFF00,0xFF0000,0xFFFF00,0x00FFFF,0xFF00FF,0xFFFFFF};
    9.54 +
    9.55 +typedef struct {
    9.56 +  float x,y;
    9.57 +} Point;
    9.58 +
    9.59 +typedef struct {
    9.60 +  float ang,r;
    9.61 +  Point p;
    9.62 +} Knob;
    9.63 +
    9.64 +Knob knob;
    9.65 +
    9.66 +void handler (int sig)
    9.67 +{
    9.68 +  printf ("\exiting...(%d)\n", sig);
    9.69 +  exit (0);
    9.70 +}
    9.71 +
    9.72 +void perror_exit (char *error)
    9.73 +{
    9.74 +  perror (error);
    9.75 +  handler (9);
    9.76 +}
    9.77 +
    9.78 +void setpix(SDL_Surface *screen, int x, int y, unsigned int col)
    9.79 +{
    9.80 +  Uint32 *pixmem32;
    9.81 +  Uint32 colour;
    9.82 +  
    9.83 +  if((unsigned)x > screen->w) return;
    9.84 +  if((unsigned)y > screen->h) return;
    9.85 +
    9.86 +  pixmem32 = (Uint32*) screen->pixels  + y*screen->pitch/BPP + x;
    9.87 +  
    9.88 +  Uint8 r,g,b;
    9.89 +  float a;
    9.90 +  
    9.91 +  memcpy(&colour,pixmem32,screen->format->BytesPerPixel);
    9.92 +
    9.93 +  SDL_GetRGB(colour,screen->format,&r,&g,&b);
    9.94 +  //r = 0;g = 0; b = 0;
    9.95 +  a = (col>>24)&0xFF;
    9.96 +  if(a == 0) a = 0xFF; //Hack, to make things easier.
    9.97 +  a /= 0xFF;
    9.98 +  r = r*(1-a) + ((col>>16)&0xFF)*(a);
    9.99 +  g = g*(1-a) + ((col>> 8)&0xFF)*(a);
   9.100 +  b = b*(1-a) + ((col>> 0)&0xFF)*(a);
   9.101 +  colour = SDL_MapRGB( screen->format,r, g, b);
   9.102 +  
   9.103 +
   9.104 +  *pixmem32 = colour;
   9.105 +}
   9.106 +
   9.107 +void drawLine(SDL_Surface *screen,int x0,int y0,int x1,int y1,unsigned int col) {
   9.108 +  float t;
   9.109 +  for(t=0;t<1;t+=1.f/SDL_max(abs(x0-x1),abs(y0-y1)))
   9.110 +    setpix(screen,x1+t*(x0-x1),y1+t*(y0-y1),col);
   9.111 +}
   9.112 +
   9.113 +void drawCircle(SDL_Surface* screen,int x,int y,int r,unsigned int c)
   9.114 +{
   9.115 +  int tx,ty;
   9.116 +  float xr;
   9.117 +  for(ty = -abs(r);ty <= abs(r);ty++) {
   9.118 +    xr = sqrt(r*r - ty*ty);
   9.119 +    if(r > 0) { //r > 0 ==> filled circle
   9.120 +      for(tx=-xr+.5;tx<=xr-.5;tx++) {
   9.121 +	setpix(screen,x+tx,y+ty,c);
   9.122 +      }
   9.123 +    }
   9.124 +    else {
   9.125 +      setpix(screen,x-xr+.5,y+ty,c);
   9.126 +      setpix(screen,x+xr-.5,y+ty,c);
   9.127 +    }
   9.128 +  }
   9.129 +}
   9.130 +
   9.131 +void drawKnob(SDL_Surface* screen,Knob k) {
   9.132 +  drawCircle(screen,k.p.x*screen->w,k.p.y*screen->h,k.r*screen->w,0xFFFFFF);  
   9.133 +  drawCircle(screen,(k.p.x+k.r/2*cos(k.ang))*screen->w,
   9.134 +  	            (k.p.y+k.r/2*sin(k.ang))*screen->h,k.r/4*screen->w,0);
   9.135 +}
   9.136 +
   9.137 +void DrawScreen(SDL_Surface* screen)
   9.138 +{
   9.139 +  int x, y;
   9.140 +  if(SDL_MUSTLOCK(screen))
   9.141 +    {                                              
   9.142 +      if(SDL_LockSurface(screen) < 0) return;
   9.143 +    }
   9.144 +  for(y = 0;y < screen->h;y++)
   9.145 +    for(x = 0;x < screen->w;x++)
   9.146 +	setpix(screen,x,y,((x%255)<<16) + ((y%255)<<8) + (x+y)%255);
   9.147 +
   9.148 +  int i;
   9.149 +  //draw Touch History
   9.150 +  for(i = SDL_max(0,eventWrite - EVENT_BUF_SIZE);i < eventWrite;i++) {
   9.151 +    SDL_Event event = events[i&(EVENT_BUF_SIZE-1)];
   9.152 +    int age = eventWrite - i - 1;
   9.153 +    if(event.type == SDL_FINGERMOTION || 
   9.154 +       event.type == SDL_FINGERDOWN ||
   9.155 +       event.type == SDL_FINGERUP) {
   9.156 +      SDL_Touch* inTouch = SDL_GetTouch(event.tfinger.touchId);
   9.157 +      if(inTouch == NULL) continue;
   9.158 +
   9.159 +      float x = ((float)event.tfinger.x)/inTouch->xres;
   9.160 +      float y = ((float)event.tfinger.y)/inTouch->yres;      
   9.161 +      
   9.162 +      //draw the touch:      
   9.163 +      unsigned int c = colors[event.tfinger.touchId%7]; 
   9.164 +      unsigned int col = 
   9.165 +	((unsigned int)(c*(.1+.85))) |
   9.166 +	((unsigned int)((0xFF*(1-((float)age)/EVENT_BUF_SIZE))) & 0xFF)<<24;
   9.167 +
   9.168 +      if(event.type == SDL_FINGERMOTION)
   9.169 +	drawCircle(screen,x*screen->w,y*screen->h,5,col);
   9.170 +      else if(event.type == SDL_FINGERDOWN)
   9.171 +	drawCircle(screen,x*screen->w,y*screen->h,-10,col);     
   9.172 +    }
   9.173 +  }
   9.174 +  
   9.175 +  if(knob.p.x > 0)
   9.176 +    drawKnob(screen,knob);
   9.177 +  
   9.178 +  if(SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen);
   9.179 +  SDL_Flip(screen);
   9.180 +}
   9.181 +
   9.182 +SDL_Surface* initScreen(int width,int height)
   9.183 +{
   9.184 +  return SDL_SetVideoMode(width, height, DEPTH,
   9.185 +			  SDL_HWSURFACE | SDL_RESIZABLE);
   9.186 +}
   9.187 +
   9.188 +int main(int argc, char* argv[])
   9.189 +{  
   9.190 +  SDL_Surface *screen;
   9.191 +  SDL_Event event;
   9.192 +
   9.193 +  //gesture variables
   9.194 +  knob.r = .1;
   9.195 +  knob.ang = 0;
   9.196 +
   9.197 +  
   9.198 +  SDL_bool quitting = SDL_FALSE;
   9.199 +  SDL_RWops *src;
   9.200 +
   9.201 +  if (SDL_Init(SDL_INIT_VIDEO) < 0 ) return 1;
   9.202 +  
   9.203 +  if (!(screen = initScreen(WIDTH,HEIGHT)))
   9.204 +    {
   9.205 +      SDL_Quit();
   9.206 +      return 1;
   9.207 +    }
   9.208 +
   9.209 +  while(!quitting) {
   9.210 +    while(SDL_PollEvent(&event)) 
   9.211 +      {
   9.212 +	//Record _all_ events
   9.213 +	events[eventWrite & (EVENT_BUF_SIZE-1)] = event;
   9.214 +	eventWrite++;
   9.215 +	
   9.216 +	switch (event.type) 
   9.217 +	  {
   9.218 +	  case SDL_QUIT:
   9.219 +	    quitting = SDL_TRUE;
   9.220 +	    break;
   9.221 +	  case SDL_KEYDOWN:
   9.222 +	    switch (event.key.keysym.sym)
   9.223 +	      {
   9.224 +	      case SDLK_SPACE:
   9.225 +		SDL_RecordGesture(-1);
   9.226 +		break;
   9.227 +	      case SDLK_s:
   9.228 +		src = SDL_RWFromFile("gestureSave","w");
   9.229 +		printf("Wrote %i templates\n",SDL_SaveAllDollarTemplates(src));
   9.230 +		SDL_RWclose(src);
   9.231 +		break;
   9.232 +	      case SDLK_l:
   9.233 +		src = SDL_RWFromFile("gestureSave","r");
   9.234 +		printf("Loaded: %i\n",SDL_LoadDollarTemplates(-1,src));
   9.235 +		SDL_RWclose(src);
   9.236 +		break;
   9.237 +	      case SDLK_ESCAPE:
   9.238 +		quitting = SDL_TRUE;
   9.239 +		break;
   9.240 +	    }
   9.241 +	    break;
   9.242 +	  case SDL_VIDEORESIZE:
   9.243 +	    if (!(screen = initScreen(event.resize.w,
   9.244 +				      event.resize.h)))
   9.245 +	      {
   9.246 +		SDL_Quit();
   9.247 +		return 1;
   9.248 +	      }
   9.249 +	    break;
   9.250 +	  case SDL_FINGERMOTION:    
   9.251 +	    ;
   9.252 +#if VERBOSE
   9.253 +	    printf("Finger: %i,x: %i, y: %i\n",event.tfinger.fingerId,
   9.254 +	    	   event.tfinger.x,event.tfinger.y);
   9.255 +#endif
   9.256 +	    SDL_Touch* inTouch = SDL_GetTouch(event.tfinger.touchId);
   9.257 +	    SDL_Finger* inFinger = SDL_GetFinger(inTouch,event.tfinger.fingerId);
   9.258 +	    break;	    
   9.259 +	  case SDL_FINGERDOWN:
   9.260 +#if VERBOSE
   9.261 +	    printf("Finger: %"PRIs64" down - x: %i, y: %i\n",
   9.262 +		   event.tfinger.fingerId,event.tfinger.x,event.tfinger.y);
   9.263 +#endif
   9.264 +	    break;
   9.265 +	  case SDL_FINGERUP:
   9.266 +#if VERBOSE
   9.267 +	    printf("Finger: %"PRIs64" up - x: %i, y: %i\n",
   9.268 +	    	   event.tfinger.fingerId,event.tfinger.x,event.tfinger.y);
   9.269 +#endif
   9.270 +	    break;
   9.271 +	  case SDL_MULTIGESTURE:
   9.272 +#if VERBOSE	    
   9.273 +	    printf("Multi Gesture: x = %f, y = %f, dAng = %f, dR = %f\n",
   9.274 +		   event.mgesture.x,
   9.275 +		   event.mgesture.y,
   9.276 +		   event.mgesture.dTheta,
   9.277 +		   event.mgesture.dDist);
   9.278 +	    printf("MG: numDownTouch = %i\n",event.mgesture.numFingers);
   9.279 +#endif
   9.280 +	    knob.p.x = event.mgesture.x;
   9.281 +	    knob.p.y = event.mgesture.y;
   9.282 +	    knob.ang += event.mgesture.dTheta;
   9.283 +	    knob.r += event.mgesture.dDist;
   9.284 +	    break;
   9.285 +	  case SDL_DOLLARGESTURE:
   9.286 +	    printf("Gesture %"PRIs64" performed, error: %f\n",
   9.287 +		   event.dgesture.gestureId,
   9.288 +		   event.dgesture.error);
   9.289 +	    break;
   9.290 +	  case SDL_DOLLARRECORD:
   9.291 +	    printf("Recorded gesture: %"PRIs64"\n",event.dgesture.gestureId);
   9.292 +	    break;
   9.293 +	  }
   9.294 +      }
   9.295 +    DrawScreen(screen);
   9.296 +  }  
   9.297 +  SDL_Quit();  
   9.298 +  return 0;
   9.299 +}
   9.300 +
    10.1 --- a/touchTest/gestureSDLTest.c	Fri Aug 13 23:08:32 2010 -0400
    10.2 +++ b/touchTest/gestureSDLTest.c	Sun Aug 15 00:36:28 2010 -0400
    10.3 @@ -1,9 +1,16 @@
    10.4 +/*  Usage:
    10.5 + *  Spacebar to begin recording a gesture on all touches.
    10.6 + *  s to save all touches into "./gestureSave"
    10.7 + *  l to load all touches from "./gestureSave"
    10.8 + */
    10.9 +
   10.10  #include <stdio.h>
   10.11  #include <SDL.h>
   10.12  #include <math.h>
   10.13  #include <SDL_touch.h>
   10.14  #include <SDL_gesture.h>
   10.15  
   10.16 +
   10.17  /* Make sure we have good macros for printing 32 and 64 bit values */
   10.18  #ifndef PRIs32
   10.19  #define PRIs32 "d"