test/testgesture.c
changeset 7191 75360622e65f
parent 6951 7833f01322b3
child 7451 2ce2d2c71156
     1.1 --- a/test/testgesture.c	Sat May 18 12:48:50 2013 -0700
     1.2 +++ b/test/testgesture.c	Sat May 18 14:17:52 2013 -0700
     1.3 @@ -93,12 +93,12 @@
     1.4    int x = (int)_x;
     1.5    int y = (int)_y;
     1.6    float a;
     1.7 -  
     1.8 +
     1.9    if(x < 0 || x >= screen->w) return;
    1.10    if(y < 0 || y >= screen->h) return;
    1.11  
    1.12    pixmem32 = (Uint32*) screen->pixels  + y*screen->pitch/BPP + x;
    1.13 -  
    1.14 +
    1.15    SDL_memcpy(&colour,pixmem32,screen->format->BytesPerPixel);
    1.16  
    1.17    SDL_GetRGB(colour,screen->format,&r,&g,&b);
    1.18 @@ -110,7 +110,7 @@
    1.19    g = (Uint8)(g*(1-a) + ((col>> 8)&0xFF)*(a));
    1.20    b = (Uint8)(b*(1-a) + ((col>> 0)&0xFF)*(a));
    1.21    colour = SDL_MapRGB( screen->format,r, g, b);
    1.22 -  
    1.23 +
    1.24  
    1.25    *pixmem32 = colour;
    1.26  }
    1.27 @@ -129,7 +129,7 @@
    1.28      xr = (float)sqrt(r*r - ty*ty);
    1.29      if(r > 0) { //r > 0 ==> filled circle
    1.30        for(tx=-xr+.5f;tx<=xr-.5;tx++) {
    1.31 -	setpix(screen,x+tx,y+ty,c);
    1.32 +    setpix(screen,x+tx,y+ty,c);
    1.33        }
    1.34      }
    1.35      else {
    1.36 @@ -140,9 +140,9 @@
    1.37  }
    1.38  
    1.39  void drawKnob(SDL_Surface* screen,Knob k) {
    1.40 -  drawCircle(screen,k.p.x*screen->w,k.p.y*screen->h,k.r*screen->w,0xFFFFFF);  
    1.41 +  drawCircle(screen,k.p.x*screen->w,k.p.y*screen->h,k.r*screen->w,0xFFFFFF);
    1.42    drawCircle(screen,(k.p.x+k.r/2*cosf(k.ang))*screen->w,
    1.43 -  	            (k.p.y+k.r/2*sinf(k.ang))*screen->h,k.r/4*screen->w,0);
    1.44 +                (k.p.y+k.r/2*sinf(k.ang))*screen->h,k.r/4*screen->w,0);
    1.45  }
    1.46  
    1.47  void DrawScreen(SDL_Surface* screen)
    1.48 @@ -154,36 +154,36 @@
    1.49    int x, y;
    1.50    for(y = 0;y < screen->h;y++)
    1.51      for(x = 0;x < screen->w;x++)
    1.52 -	setpix(screen,(float)x,(float)y,((x%255)<<16) + ((y%255)<<8) + (x+y)%255);
    1.53 +    setpix(screen,(float)x,(float)y,((x%255)<<16) + ((y%255)<<8) + (x+y)%255);
    1.54  #endif
    1.55  
    1.56    //draw Touch History
    1.57    for(i = eventWrite; i < eventWrite+EVENT_BUF_SIZE; ++i) {
    1.58      const SDL_Event *event = &events[i&(EVENT_BUF_SIZE-1)];
    1.59      float age = (float)(i - eventWrite) / EVENT_BUF_SIZE;
    1.60 -	float x, y;
    1.61 -	unsigned int c, col;
    1.62 +    float x, y;
    1.63 +    unsigned int c, col;
    1.64  
    1.65 -    if(event->type == SDL_FINGERMOTION || 
    1.66 +    if(event->type == SDL_FINGERMOTION ||
    1.67         event->type == SDL_FINGERDOWN ||
    1.68         event->type == SDL_FINGERUP) {
    1.69        x = event->tfinger.x;
    1.70        y = event->tfinger.y;
    1.71 -      
    1.72 -      //draw the touch:      
    1.73 -      c = colors[event->tfinger.fingerId%7]; 
    1.74 +
    1.75 +      //draw the touch:
    1.76 +      c = colors[event->tfinger.fingerId%7];
    1.77        col = ((unsigned int)(c*(.1+.85))) | (unsigned int)(0xFF*age)<<24;
    1.78  
    1.79        if(event->type == SDL_FINGERMOTION)
    1.80 -	drawCircle(screen,x*screen->w,y*screen->h,5,col);
    1.81 +    drawCircle(screen,x*screen->w,y*screen->h,5,col);
    1.82        else if(event->type == SDL_FINGERDOWN)
    1.83 -	drawCircle(screen,x*screen->w,y*screen->h,-10,col);     
    1.84 +    drawCircle(screen,x*screen->w,y*screen->h,-10,col);
    1.85      }
    1.86    }
    1.87 -  
    1.88 +
    1.89    if(knob.p.x > 0)
    1.90      drawKnob(screen,knob);
    1.91 -  
    1.92 +
    1.93    SDL_UpdateWindowSurface(window);
    1.94  }
    1.95  
    1.96 @@ -201,7 +201,7 @@
    1.97  }
    1.98  
    1.99  int main(int argc, char* argv[])
   1.100 -{  
   1.101 +{
   1.102    SDL_Surface *screen;
   1.103    SDL_Event event;
   1.104    SDL_bool quitting = SDL_FALSE;
   1.105 @@ -212,7 +212,7 @@
   1.106    knob.ang = 0;
   1.107  
   1.108    if (SDL_Init(SDL_INIT_VIDEO) < 0 ) return 1;
   1.109 -  
   1.110 +
   1.111    if (!(screen = initScreen(WIDTH,HEIGHT)))
   1.112      {
   1.113        SDL_Quit();
   1.114 @@ -220,92 +220,92 @@
   1.115      }
   1.116  
   1.117    while(!quitting) {
   1.118 -    while(SDL_PollEvent(&event)) 
   1.119 +    while(SDL_PollEvent(&event))
   1.120        {
   1.121 -	//Record _all_ events
   1.122 -	events[eventWrite & (EVENT_BUF_SIZE-1)] = event;
   1.123 -	eventWrite++;
   1.124 -	
   1.125 -	switch (event.type) 
   1.126 -	  {
   1.127 -	  case SDL_QUIT:
   1.128 -	    quitting = SDL_TRUE;
   1.129 -	    break;
   1.130 -	  case SDL_KEYDOWN:
   1.131 -	    switch (event.key.keysym.sym)
   1.132 -	      {
   1.133 -	      case SDLK_SPACE:
   1.134 -		SDL_RecordGesture(-1);
   1.135 -		break;
   1.136 -	      case SDLK_s:
   1.137 -		src = SDL_RWFromFile("gestureSave","w");
   1.138 -		SDL_Log("Wrote %i templates",SDL_SaveAllDollarTemplates(src));
   1.139 -		SDL_RWclose(src);
   1.140 -		break;
   1.141 -	      case SDLK_l:
   1.142 -		src = SDL_RWFromFile("gestureSave","r");
   1.143 -		SDL_Log("Loaded: %i",SDL_LoadDollarTemplates(-1,src));
   1.144 -		SDL_RWclose(src);
   1.145 -		break;
   1.146 -	      case SDLK_ESCAPE:
   1.147 -		quitting = SDL_TRUE;
   1.148 -		break;
   1.149 -	    }
   1.150 -	    break;
   1.151 -	  case SDL_WINDOWEVENT:
   1.152 +    //Record _all_ events
   1.153 +    events[eventWrite & (EVENT_BUF_SIZE-1)] = event;
   1.154 +    eventWrite++;
   1.155 +
   1.156 +    switch (event.type)
   1.157 +      {
   1.158 +      case SDL_QUIT:
   1.159 +        quitting = SDL_TRUE;
   1.160 +        break;
   1.161 +      case SDL_KEYDOWN:
   1.162 +        switch (event.key.keysym.sym)
   1.163 +          {
   1.164 +          case SDLK_SPACE:
   1.165 +        SDL_RecordGesture(-1);
   1.166 +        break;
   1.167 +          case SDLK_s:
   1.168 +        src = SDL_RWFromFile("gestureSave","w");
   1.169 +        SDL_Log("Wrote %i templates",SDL_SaveAllDollarTemplates(src));
   1.170 +        SDL_RWclose(src);
   1.171 +        break;
   1.172 +          case SDLK_l:
   1.173 +        src = SDL_RWFromFile("gestureSave","r");
   1.174 +        SDL_Log("Loaded: %i",SDL_LoadDollarTemplates(-1,src));
   1.175 +        SDL_RWclose(src);
   1.176 +        break;
   1.177 +          case SDLK_ESCAPE:
   1.178 +        quitting = SDL_TRUE;
   1.179 +        break;
   1.180 +        }
   1.181 +        break;
   1.182 +      case SDL_WINDOWEVENT:
   1.183              if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
   1.184 -	      if (!(screen = initScreen(event.window.data1, event.window.data2)))
   1.185 -	      {
   1.186 -		SDL_Quit();
   1.187 -		return 1;
   1.188 -	      }
   1.189 +          if (!(screen = initScreen(event.window.data1, event.window.data2)))
   1.190 +          {
   1.191 +        SDL_Quit();
   1.192 +        return 1;
   1.193 +          }
   1.194              }
   1.195 -	    break;
   1.196 -	  case SDL_FINGERMOTION:
   1.197 +        break;
   1.198 +      case SDL_FINGERMOTION:
   1.199  #if VERBOSE
   1.200 -	    SDL_Log("Finger: %i,x: %i, y: %i",event.tfinger.fingerId,
   1.201 -	    	   event.tfinger.x,event.tfinger.y);
   1.202 +        SDL_Log("Finger: %i,x: %i, y: %i",event.tfinger.fingerId,
   1.203 +               event.tfinger.x,event.tfinger.y);
   1.204  #endif
   1.205 -	    break;	    
   1.206 -	  case SDL_FINGERDOWN:
   1.207 +        break;
   1.208 +      case SDL_FINGERDOWN:
   1.209  #if VERBOSE
   1.210 -	    SDL_Log("Finger: %"PRIs64" down - x: %i, y: %i",
   1.211 -		   event.tfinger.fingerId,event.tfinger.x,event.tfinger.y);
   1.212 +        SDL_Log("Finger: %"PRIs64" down - x: %i, y: %i",
   1.213 +           event.tfinger.fingerId,event.tfinger.x,event.tfinger.y);
   1.214  #endif
   1.215 -	    break;
   1.216 -	  case SDL_FINGERUP:
   1.217 +        break;
   1.218 +      case SDL_FINGERUP:
   1.219  #if VERBOSE
   1.220 -	    SDL_Log("Finger: %"PRIs64" up - x: %i, y: %i",
   1.221 -	    	   event.tfinger.fingerId,event.tfinger.x,event.tfinger.y);
   1.222 +        SDL_Log("Finger: %"PRIs64" up - x: %i, y: %i",
   1.223 +               event.tfinger.fingerId,event.tfinger.x,event.tfinger.y);
   1.224  #endif
   1.225 -	    break;
   1.226 -	  case SDL_MULTIGESTURE:
   1.227 -#if VERBOSE	    
   1.228 -	    SDL_Log("Multi Gesture: x = %f, y = %f, dAng = %f, dR = %f",
   1.229 -		   event.mgesture.x,
   1.230 -		   event.mgesture.y,
   1.231 -		   event.mgesture.dTheta,
   1.232 -		   event.mgesture.dDist);
   1.233 -	    SDL_Log("MG: numDownTouch = %i",event.mgesture.numFingers);
   1.234 +        break;
   1.235 +      case SDL_MULTIGESTURE:
   1.236 +#if VERBOSE
   1.237 +        SDL_Log("Multi Gesture: x = %f, y = %f, dAng = %f, dR = %f",
   1.238 +           event.mgesture.x,
   1.239 +           event.mgesture.y,
   1.240 +           event.mgesture.dTheta,
   1.241 +           event.mgesture.dDist);
   1.242 +        SDL_Log("MG: numDownTouch = %i",event.mgesture.numFingers);
   1.243  #endif
   1.244 -	    knob.p.x = event.mgesture.x;
   1.245 -	    knob.p.y = event.mgesture.y;
   1.246 -	    knob.ang += event.mgesture.dTheta;
   1.247 -	    knob.r += event.mgesture.dDist;
   1.248 -	    break;
   1.249 -	  case SDL_DOLLARGESTURE:
   1.250 -	    SDL_Log("Gesture %"PRIs64" performed, error: %f",
   1.251 -		   event.dgesture.gestureId,
   1.252 -		   event.dgesture.error);
   1.253 -	    break;
   1.254 -	  case SDL_DOLLARRECORD:
   1.255 -	    SDL_Log("Recorded gesture: %"PRIs64"",event.dgesture.gestureId);
   1.256 -	    break;
   1.257 -	  }
   1.258 +        knob.p.x = event.mgesture.x;
   1.259 +        knob.p.y = event.mgesture.y;
   1.260 +        knob.ang += event.mgesture.dTheta;
   1.261 +        knob.r += event.mgesture.dDist;
   1.262 +        break;
   1.263 +      case SDL_DOLLARGESTURE:
   1.264 +        SDL_Log("Gesture %"PRIs64" performed, error: %f",
   1.265 +           event.dgesture.gestureId,
   1.266 +           event.dgesture.error);
   1.267 +        break;
   1.268 +      case SDL_DOLLARRECORD:
   1.269 +        SDL_Log("Recorded gesture: %"PRIs64"",event.dgesture.gestureId);
   1.270 +        break;
   1.271 +      }
   1.272        }
   1.273      DrawScreen(screen);
   1.274 -  }  
   1.275 -  SDL_Quit();  
   1.276 +  }
   1.277 +  SDL_Quit();
   1.278    return 0;
   1.279  }
   1.280