test/testgesture.c
changeset 4918 f5f70fed2c4c
parent 4689 f9ab8df6d45a
child 4921 40a43c6d9220
     1.1 --- a/test/testgesture.c	Tue Nov 30 12:04:01 2010 -0800
     1.2 +++ b/test/testgesture.c	Tue Nov 30 12:38:46 2010 -0800
     1.3 @@ -62,7 +62,7 @@
     1.4  
     1.5  void handler (int sig)
     1.6  {
     1.7 -  printf ("\exiting...(%d)\n", sig);
     1.8 +  printf ("exiting...(%d)\n", sig);
     1.9    exit (0);
    1.10  }
    1.11  
    1.12 @@ -72,93 +72,96 @@
    1.13    handler (9);
    1.14  }
    1.15  
    1.16 -void setpix(SDL_Surface *screen, int x, int y, unsigned int col)
    1.17 +void setpix(SDL_Surface *screen, float _x, float _y, unsigned int col)
    1.18  {
    1.19    Uint32 *pixmem32;
    1.20    Uint32 colour;
    1.21 +  Uint8 r,g,b;
    1.22 +  int x = (int)_x;
    1.23 +  int y = (int)_y;
    1.24 +  float a;
    1.25    
    1.26 -  if((unsigned)x > screen->w) return;
    1.27 -  if((unsigned)y > screen->h) return;
    1.28 +  if(x > screen->w) return;
    1.29 +  if(y > screen->h) return;
    1.30  
    1.31    pixmem32 = (Uint32*) screen->pixels  + y*screen->pitch/BPP + x;
    1.32    
    1.33 -  Uint8 r,g,b;
    1.34 -  float a;
    1.35 -  
    1.36 -  memcpy(&colour,pixmem32,screen->format->BytesPerPixel);
    1.37 +  SDL_memcpy(&colour,pixmem32,screen->format->BytesPerPixel);
    1.38  
    1.39    SDL_GetRGB(colour,screen->format,&r,&g,&b);
    1.40    //r = 0;g = 0; b = 0;
    1.41 -  a = (col>>24)&0xFF;
    1.42 +  a = (float)((col>>24)&0xFF);
    1.43    if(a == 0) a = 0xFF; //Hack, to make things easier.
    1.44    a /= 0xFF;
    1.45 -  r = r*(1-a) + ((col>>16)&0xFF)*(a);
    1.46 -  g = g*(1-a) + ((col>> 8)&0xFF)*(a);
    1.47 -  b = b*(1-a) + ((col>> 0)&0xFF)*(a);
    1.48 +  r = (Uint8)(r*(1-a) + ((col>>16)&0xFF)*(a));
    1.49 +  g = (Uint8)(g*(1-a) + ((col>> 8)&0xFF)*(a));
    1.50 +  b = (Uint8)(b*(1-a) + ((col>> 0)&0xFF)*(a));
    1.51    colour = SDL_MapRGB( screen->format,r, g, b);
    1.52    
    1.53  
    1.54    *pixmem32 = colour;
    1.55  }
    1.56  
    1.57 -void drawLine(SDL_Surface *screen,int x0,int y0,int x1,int y1,unsigned int col) {
    1.58 +void drawLine(SDL_Surface *screen,float x0,float y0,float x1,float y1,unsigned int col) {
    1.59    float t;
    1.60 -  for(t=0;t<1;t+=1.f/SDL_max(abs(x0-x1),abs(y0-y1)))
    1.61 +  for(t=0;t<1;t+=(float)(1.f/SDL_max(SDL_fabs(x0-x1),SDL_fabs(y0-y1))))
    1.62      setpix(screen,x1+t*(x0-x1),y1+t*(y0-y1),col);
    1.63  }
    1.64  
    1.65 -void drawCircle(SDL_Surface* screen,int x,int y,int r,unsigned int c)
    1.66 +void drawCircle(SDL_Surface* screen,float x,float y,float r,unsigned int c)
    1.67  {
    1.68 -  int tx,ty;
    1.69 +  float tx,ty;
    1.70    float xr;
    1.71 -  for(ty = -abs(r);ty <= abs(r);ty++) {
    1.72 -    xr = sqrt(r*r - ty*ty);
    1.73 +  for(ty = (float)-SDL_fabs(r);ty <= (float)SDL_fabs((int)r);ty++) {
    1.74 +    xr = (float)sqrt(r*r - ty*ty);
    1.75      if(r > 0) { //r > 0 ==> filled circle
    1.76 -      for(tx=-xr+.5;tx<=xr-.5;tx++) {
    1.77 +      for(tx=-xr+.5f;tx<=xr-.5;tx++) {
    1.78  	setpix(screen,x+tx,y+ty,c);
    1.79        }
    1.80      }
    1.81      else {
    1.82 -      setpix(screen,x-xr+.5,y+ty,c);
    1.83 -      setpix(screen,x+xr-.5,y+ty,c);
    1.84 +      setpix(screen,x-xr+.5f,y+ty,c);
    1.85 +      setpix(screen,x+xr-.5f,y+ty,c);
    1.86      }
    1.87    }
    1.88  }
    1.89  
    1.90  void drawKnob(SDL_Surface* screen,Knob k) {
    1.91    drawCircle(screen,k.p.x*screen->w,k.p.y*screen->h,k.r*screen->w,0xFFFFFF);  
    1.92 -  drawCircle(screen,(k.p.x+k.r/2*cos(k.ang))*screen->w,
    1.93 -  	            (k.p.y+k.r/2*sin(k.ang))*screen->h,k.r/4*screen->w,0);
    1.94 +  drawCircle(screen,(k.p.x+k.r/2*cosf(k.ang))*screen->w,
    1.95 +  	            (k.p.y+k.r/2*sinf(k.ang))*screen->h,k.r/4*screen->w,0);
    1.96  }
    1.97  
    1.98  void DrawScreen(SDL_Surface* screen)
    1.99  {
   1.100 -  int x, y;
   1.101 +  int x, y, i;
   1.102    if(SDL_MUSTLOCK(screen))
   1.103      {                                              
   1.104        if(SDL_LockSurface(screen) < 0) return;
   1.105      }
   1.106    for(y = 0;y < screen->h;y++)
   1.107      for(x = 0;x < screen->w;x++)
   1.108 -	setpix(screen,x,y,((x%255)<<16) + ((y%255)<<8) + (x+y)%255);
   1.109 +	setpix(screen,(float)x,(float)y,((x%255)<<16) + ((y%255)<<8) + (x+y)%255);
   1.110  
   1.111 -  int i;
   1.112    //draw Touch History
   1.113    for(i = SDL_max(0,eventWrite - EVENT_BUF_SIZE);i < eventWrite;i++) {
   1.114      SDL_Event event = events[i&(EVENT_BUF_SIZE-1)];
   1.115      int age = eventWrite - i - 1;
   1.116 +	float x, y;
   1.117 +	unsigned int c, col;
   1.118 +
   1.119      if(event.type == SDL_FINGERMOTION || 
   1.120         event.type == SDL_FINGERDOWN ||
   1.121         event.type == SDL_FINGERUP) {
   1.122        SDL_Touch* inTouch = SDL_GetTouch(event.tfinger.touchId);
   1.123        if(inTouch == NULL) continue;
   1.124  
   1.125 -      float x = ((float)event.tfinger.x)/inTouch->xres;
   1.126 -      float y = ((float)event.tfinger.y)/inTouch->yres;      
   1.127 +      x = ((float)event.tfinger.x)/inTouch->xres;
   1.128 +      y = ((float)event.tfinger.y)/inTouch->yres;      
   1.129        
   1.130        //draw the touch:      
   1.131 -      unsigned int c = colors[event.tfinger.touchId%7]; 
   1.132 -      unsigned int col = 
   1.133 +      c = colors[event.tfinger.touchId%7]; 
   1.134 +      col = 
   1.135  	((unsigned int)(c*(.1+.85))) |
   1.136  	((unsigned int)((0xFF*(1-((float)age)/EVENT_BUF_SIZE))) & 0xFF)<<24;
   1.137  
   1.138 @@ -186,15 +189,13 @@
   1.139  {  
   1.140    SDL_Surface *screen;
   1.141    SDL_Event event;
   1.142 +  SDL_bool quitting = SDL_FALSE;
   1.143 +  SDL_RWops *src;
   1.144  
   1.145    //gesture variables
   1.146 -  knob.r = .1;
   1.147 +  knob.r = .1f;
   1.148    knob.ang = 0;
   1.149  
   1.150 -  
   1.151 -  SDL_bool quitting = SDL_FALSE;
   1.152 -  SDL_RWops *src;
   1.153 -
   1.154    if (SDL_Init(SDL_INIT_VIDEO) < 0 ) return 1;
   1.155    
   1.156    if (!(screen = initScreen(WIDTH,HEIGHT)))
   1.157 @@ -244,14 +245,15 @@
   1.158  		return 1;
   1.159  	      }
   1.160  	    break;
   1.161 -	  case SDL_FINGERMOTION:    
   1.162 -	    ;
   1.163 +	  case SDL_FINGERMOTION:
   1.164  #if VERBOSE
   1.165  	    printf("Finger: %i,x: %i, y: %i\n",event.tfinger.fingerId,
   1.166  	    	   event.tfinger.x,event.tfinger.y);
   1.167  #endif
   1.168 -	    SDL_Touch* inTouch = SDL_GetTouch(event.tfinger.touchId);
   1.169 -	    SDL_Finger* inFinger = SDL_GetFinger(inTouch,event.tfinger.fingerId);
   1.170 +		{
   1.171 +			SDL_Touch* inTouch = SDL_GetTouch(event.tfinger.touchId);
   1.172 +			SDL_Finger* inFinger = SDL_GetFinger(inTouch,event.tfinger.fingerId);
   1.173 +		}
   1.174  	    break;	    
   1.175  	  case SDL_FINGERDOWN:
   1.176  #if VERBOSE