testgesture: cleaned up code formatting, etc.
authorRyan C. Gordon
Fri, 15 Mar 2019 22:39:31 -0400
changeset 12635cd9e8dc74ecd
parent 12634 6985acae187a
child 12636 cb8588b0b66a
testgesture: cleaned up code formatting, etc.
test/testgesture.c
     1.1 --- a/test/testgesture.c	Fri Mar 15 22:17:21 2019 -0400
     1.2 +++ b/test/testgesture.c	Fri Mar 15 22:39:31 2019 -0400
     1.3 @@ -9,6 +9,7 @@
     1.4    including commercial applications, and to alter it and redistribute it
     1.5    freely.
     1.6  */
     1.7 +
     1.8  /*  Usage:
     1.9   *  Spacebar to begin recording a gesture on all touches.
    1.10   *  s to save all touches into "./gestureSave"
    1.11 @@ -32,227 +33,229 @@
    1.12  /* MUST BE A POWER OF 2! */
    1.13  #define EVENT_BUF_SIZE 256
    1.14  
    1.15 -
    1.16  #define VERBOSE 0
    1.17  
    1.18  static SDLTest_CommonState *state;
    1.19  static SDL_Event events[EVENT_BUF_SIZE];
    1.20  static int eventWrite;
    1.21 -
    1.22 -
    1.23  static int colors[7] = {0xFF,0xFF00,0xFF0000,0xFFFF00,0x00FFFF,0xFF00FF,0xFFFFFF};
    1.24 -
    1.25  static int quitting = 0;
    1.26  
    1.27 -typedef struct {
    1.28 -  float x,y;
    1.29 +typedef struct
    1.30 +{
    1.31 +    float x, y;
    1.32  } Point;
    1.33  
    1.34 -typedef struct {
    1.35 -  float ang,r;
    1.36 -  Point p;
    1.37 +typedef struct
    1.38 +{
    1.39 +    float ang, r;
    1.40 +    Point p;
    1.41  } Knob;
    1.42  
    1.43 -static Knob knob;
    1.44 -
    1.45 -void setpix(SDL_Surface *screen, float _x, float _y, unsigned int col)
    1.46 -{
    1.47 -  Uint32 *pixmem32;
    1.48 -  Uint32 colour;
    1.49 -  Uint8 r,g,b;
    1.50 -  int x = (int)_x;
    1.51 -  int y = (int)_y;
    1.52 -  float a;
    1.53 -
    1.54 -  if(x < 0 || x >= screen->w) return;
    1.55 -  if(y < 0 || y >= screen->h) return;
    1.56 -
    1.57 -  pixmem32 = (Uint32*) screen->pixels  + y*screen->pitch/BPP + x;
    1.58 -
    1.59 -  SDL_memcpy(&colour,pixmem32,screen->format->BytesPerPixel);
    1.60 -
    1.61 -  SDL_GetRGB(colour,screen->format,&r,&g,&b);
    1.62 -  /* r = 0;g = 0; b = 0; */
    1.63 -  a = (float)((col>>24)&0xFF);
    1.64 -  if(a == 0) a = 0xFF; /* Hack, to make things easier. */
    1.65 -  a /= 0xFF;
    1.66 -  r = (Uint8)(r*(1-a) + ((col>>16)&0xFF)*(a));
    1.67 -  g = (Uint8)(g*(1-a) + ((col>> 8)&0xFF)*(a));
    1.68 -  b = (Uint8)(b*(1-a) + ((col>> 0)&0xFF)*(a));
    1.69 -  colour = SDL_MapRGB( screen->format,r, g, b);
    1.70 +static Knob knob = { 0.0f, 0.1f, { 0.0f, 0.0f } };
    1.71  
    1.72  
    1.73 -  *pixmem32 = colour;
    1.74 +static void
    1.75 +setpix(SDL_Surface *screen, float _x, float _y, unsigned int col)
    1.76 +{
    1.77 +    Uint32 *pixmem32;
    1.78 +    Uint32 colour;
    1.79 +    Uint8 r, g, b;
    1.80 +    const int x = (int)_x;
    1.81 +    const int y = (int)_y;
    1.82 +    float a;
    1.83 +
    1.84 +    if ( (x < 0) || (x >= screen->w) || (y < 0) || (y >= screen->h) ) {
    1.85 +        return;
    1.86 +    }
    1.87 +
    1.88 +    pixmem32 = (Uint32 *) screen->pixels + y * screen->pitch / BPP + x;
    1.89 +
    1.90 +    SDL_memcpy(&colour, pixmem32, screen->format->BytesPerPixel);
    1.91 +
    1.92 +    SDL_GetRGB(colour,screen->format,&r,&g,&b);
    1.93 +
    1.94 +    /* r = 0;g = 0; b = 0; */
    1.95 +    a = (float) ((col >> 24) & 0xFF);
    1.96 +    if (a == 0) {
    1.97 +        a = 0xFF; /* Hack, to make things easier. */
    1.98 +    }
    1.99 +
   1.100 +    a = (a == 0.0f) ? 1 : (a / 255.0f);
   1.101 +    r = (Uint8) (r * (1 - a) + ((col >> 16) & 0xFF) * a);
   1.102 +    g = (Uint8) (g * (1 - a) + ((col >> 8) & 0xFF) * a);
   1.103 +    b = (Uint8) (b * (1 - a) + ((col >> 0) & 0xFF) * a);
   1.104 +    colour = SDL_MapRGB(screen->format, r, g, b);
   1.105 +
   1.106 +    *pixmem32 = colour;
   1.107  }
   1.108  
   1.109 -void drawLine(SDL_Surface *screen,float x0,float y0,float x1,float y1,unsigned int col) {
   1.110 -  float t;
   1.111 -  for(t=0;t<1;t+=(float)(1.f/SDL_max(SDL_fabs(x0-x1),SDL_fabs(y0-y1))))
   1.112 -    setpix(screen,x1+t*(x0-x1),y1+t*(y0-y1),col);
   1.113 +static void
   1.114 +drawLine(SDL_Surface *screen, float x0, float y0, float x1, float y1, unsigned int col)
   1.115 +{
   1.116 +    float t;
   1.117 +    for (t = 0; t < 1; t += (float) (1.0f / SDL_max(SDL_fabs(x0 - x1), SDL_fabs(y0 - y1)))) {
   1.118 +        setpix(screen, x1 + t * (x0 - x1), y1 + t * (y0 - y1), col);
   1.119 +    }
   1.120  }
   1.121  
   1.122 -void drawCircle(SDL_Surface* screen,float x,float y,float r,unsigned int c)
   1.123 +static void
   1.124 +drawCircle(SDL_Surface *screen, float x, float y, float r, unsigned int c)
   1.125  {
   1.126 -  float tx,ty;
   1.127 -  float xr;
   1.128 -  for(ty = (float)-SDL_fabs(r);ty <= (float)SDL_fabs((int)r);ty++) {
   1.129 -    xr = (float)SDL_sqrt(r*r - ty*ty);
   1.130 -    if(r > 0) { /* r > 0 ==> filled circle */
   1.131 -      for(tx=-xr+.5f;tx<=xr-.5;tx++) {
   1.132 -    setpix(screen,x+tx,y+ty,c);
   1.133 -      }
   1.134 +    float tx,ty, xr;
   1.135 +    for (ty = (float) -SDL_fabs(r); ty <= (float) SDL_fabs((int) r); ty++) {
   1.136 +        xr = (float) SDL_sqrt(r * r - ty * ty);
   1.137 +        if (r > 0) { /* r > 0 ==> filled circle */
   1.138 +            for(tx = -xr + 0.5f; tx <= xr - 0.5f; tx++) {
   1.139 +                setpix(screen, x + tx, y + ty, c);
   1.140 +            }
   1.141 +        } else {
   1.142 +            setpix(screen, x - xr + 0.5f, y + ty, c);
   1.143 +            setpix(screen, x + xr - 0.5f, y + ty, c);
   1.144 +        }
   1.145      }
   1.146 -    else {
   1.147 -      setpix(screen,x-xr+.5f,y+ty,c);
   1.148 -      setpix(screen,x+xr-.5f,y+ty,c);
   1.149 -    }
   1.150 -  }
   1.151  }
   1.152  
   1.153 -void drawKnob(SDL_Surface* screen,Knob k) {
   1.154 -  drawCircle(screen,k.p.x*screen->w,k.p.y*screen->h,k.r*screen->w,0xFFFFFF);
   1.155 -  drawCircle(screen,(k.p.x+k.r/2*SDL_cosf(k.ang))*screen->w,
   1.156 -                (k.p.y+k.r/2*SDL_sinf(k.ang))*screen->h,k.r/4*screen->w,0);
   1.157 +static void
   1.158 +drawKnob(SDL_Surface *screen, const Knob *k)
   1.159 +{
   1.160 +    drawCircle(screen, k->p.x * screen->w, k->p.y * screen->h, k->r * screen->w, 0xFFFFFF);
   1.161 +    drawCircle(screen, (k->p.x + k->r / 2 * SDL_cosf(k->ang)) * screen->w,
   1.162 +               (k->p.y + k->r / 2 * SDL_sinf(k->ang)) * screen->h, k->r / 4 * screen->w, 0);
   1.163  }
   1.164  
   1.165 -void DrawScreen(SDL_Window* window)
   1.166 +static void
   1.167 +DrawScreen(SDL_Window *window)
   1.168  {
   1.169 -  SDL_Surface* screen = SDL_GetWindowSurface(window);
   1.170 -  int i;
   1.171 -
   1.172 -  if (!screen) {
   1.173 -    return;
   1.174 -  }
   1.175 +    SDL_Surface *screen = SDL_GetWindowSurface(window);
   1.176 +    int i;
   1.177  
   1.178 -#if 1
   1.179 -  SDL_FillRect(screen, NULL, 0);
   1.180 -#else
   1.181 -  int x, y;
   1.182 -  for(y = 0;y < screen->h;y++)
   1.183 -    for(x = 0;x < screen->w;x++)
   1.184 -    setpix(screen,(float)x,(float)y,((x%255)<<16) + ((y%255)<<8) + (x+y)%255);
   1.185 -#endif
   1.186 +    if (!screen) {
   1.187 +        return;
   1.188 +    }
   1.189 +
   1.190 +    SDL_FillRect(screen, NULL, 0);
   1.191 +
   1.192 +    /* draw Touch History */
   1.193 +    for (i = eventWrite; i < eventWrite + EVENT_BUF_SIZE; ++i) {
   1.194 +        const SDL_Event *event = &events[i & (EVENT_BUF_SIZE - 1)];
   1.195 +        const float age = (float)(i - eventWrite) / EVENT_BUF_SIZE;
   1.196 +        float x, y;
   1.197 +        unsigned int c, col;
   1.198  
   1.199 -  /* draw Touch History */
   1.200 -  for(i = eventWrite; i < eventWrite+EVENT_BUF_SIZE; ++i) {
   1.201 -    const SDL_Event *event = &events[i&(EVENT_BUF_SIZE-1)];
   1.202 -    float age = (float)(i - eventWrite) / EVENT_BUF_SIZE;
   1.203 -    float x, y;
   1.204 -    unsigned int c, col;
   1.205 +        if ( (event->type == SDL_FINGERMOTION) ||
   1.206 +             (event->type == SDL_FINGERDOWN) ||
   1.207 +             (event->type == SDL_FINGERUP) ) {
   1.208 +            x = event->tfinger.x;
   1.209 +            y = event->tfinger.y;
   1.210  
   1.211 -    if(event->type == SDL_FINGERMOTION ||
   1.212 -       event->type == SDL_FINGERDOWN ||
   1.213 -       event->type == SDL_FINGERUP) {
   1.214 -      x = event->tfinger.x;
   1.215 -      y = event->tfinger.y;
   1.216 +            /* draw the touch: */
   1.217 +            c = colors[event->tfinger.fingerId % 7];
   1.218 +            col = ((unsigned int) (c * (0.1f + 0.85f))) | (unsigned int) (0xFF * age) << 24;
   1.219  
   1.220 -      /* draw the touch: */
   1.221 -      c = colors[event->tfinger.fingerId%7];
   1.222 -      col = ((unsigned int)(c*(.1+.85))) | (unsigned int)(0xFF*age)<<24;
   1.223 +            if (event->type == SDL_FINGERMOTION) {
   1.224 +                drawCircle(screen, x * screen->w, y * screen->h, 5, col);
   1.225 +            } else if (event->type == SDL_FINGERDOWN) {
   1.226 +                drawCircle(screen, x * screen->w, y * screen->h, -10, col);
   1.227 +            }
   1.228 +        }
   1.229 +    }
   1.230  
   1.231 -      if(event->type == SDL_FINGERMOTION)
   1.232 -    drawCircle(screen,x*screen->w,y*screen->h,5,col);
   1.233 -      else if(event->type == SDL_FINGERDOWN)
   1.234 -    drawCircle(screen,x*screen->w,y*screen->h,-10,col);
   1.235 +    if (knob.p.x > 0) {
   1.236 +        drawKnob(screen, &knob);
   1.237      }
   1.238 -  }
   1.239  
   1.240 -  if(knob.p.x > 0)
   1.241 -    drawKnob(screen,knob);
   1.242 -
   1.243 -  SDL_UpdateWindowSurface(window);
   1.244 +    SDL_UpdateWindowSurface(window);
   1.245  }
   1.246  
   1.247 -void loop()
   1.248 +static void
   1.249 +loop(void)
   1.250  {
   1.251      SDL_Event event;
   1.252      SDL_RWops *stream;
   1.253      int i;
   1.254  
   1.255 -    while(SDL_PollEvent(&event))
   1.256 -    {
   1.257 +    while (SDL_PollEvent(&event)) {
   1.258          SDLTest_CommonEvent(state, &event, &quitting);
   1.259  
   1.260 -    /* Record _all_ events */
   1.261 -    events[eventWrite & (EVENT_BUF_SIZE-1)] = event;
   1.262 -    eventWrite++;
   1.263 +        /* Record _all_ events */
   1.264 +        events[eventWrite & (EVENT_BUF_SIZE-1)] = event;
   1.265 +        eventWrite++;
   1.266 +
   1.267 +        switch (event.type) {
   1.268 +            case SDL_KEYDOWN:
   1.269 +                switch (event.key.keysym.sym) {
   1.270 +                    case SDLK_i: {
   1.271 +                        for (i = 0; i < SDL_GetNumTouchDevices(); ++i) {
   1.272 +                            const SDL_TouchID id = SDL_GetTouchDevice(i);
   1.273 +                            SDL_Log("Fingers Down on device %"SDL_PRIs64": %d", id, SDL_GetNumTouchFingers(id));
   1.274 +                        }
   1.275 +                        break;
   1.276 +                    }
   1.277  
   1.278 -    switch (event.type)
   1.279 -      {
   1.280 -      case SDL_KEYDOWN:
   1.281 -        switch (event.key.keysym.sym)
   1.282 -          {
   1.283 -              case SDLK_i:
   1.284 -              {
   1.285 -                  int i;
   1.286 -                  for (i = 0; i < SDL_GetNumTouchDevices(); ++i) {
   1.287 -                      SDL_TouchID id = SDL_GetTouchDevice(i);
   1.288 -                      SDL_Log("Fingers Down on device %"SDL_PRIs64": %d", id, SDL_GetNumTouchFingers(id));
   1.289 -                  }
   1.290 -                  break;
   1.291 -              }
   1.292 -          case SDLK_SPACE:
   1.293 -        SDL_RecordGesture(-1);
   1.294 -        break;
   1.295 -          case SDLK_s:
   1.296 -        stream = SDL_RWFromFile("gestureSave", "w");
   1.297 -        SDL_Log("Wrote %i templates", SDL_SaveAllDollarTemplates(stream));
   1.298 -        SDL_RWclose(stream);
   1.299 -        break;
   1.300 -          case SDLK_l:
   1.301 -        stream = SDL_RWFromFile("gestureSave", "r");
   1.302 -        SDL_Log("Loaded: %i", SDL_LoadDollarTemplates(-1, stream));
   1.303 -        SDL_RWclose(stream);
   1.304 -        break;
   1.305 -        }
   1.306 -        break;
   1.307 +                    case SDLK_SPACE:
   1.308 +                        SDL_RecordGesture(-1);
   1.309 +                        break;
   1.310  
   1.311 -      case SDL_FINGERMOTION:
   1.312 +                    case SDLK_s:
   1.313 +                        stream = SDL_RWFromFile("gestureSave", "w");
   1.314 +                        SDL_Log("Wrote %i templates", SDL_SaveAllDollarTemplates(stream));
   1.315 +                        SDL_RWclose(stream);
   1.316 +                        break;
   1.317 +
   1.318 +                    case SDLK_l:
   1.319 +                        stream = SDL_RWFromFile("gestureSave", "r");
   1.320 +                        SDL_Log("Loaded: %i", SDL_LoadDollarTemplates(-1, stream));
   1.321 +                        SDL_RWclose(stream);
   1.322 +                        break;
   1.323 +                }
   1.324 +                break;
   1.325 +
   1.326  #if VERBOSE
   1.327 -        SDL_Log("Finger: %"SDL_PRIs64",x: %f, y: %f",event.tfinger.fingerId,
   1.328 -               event.tfinger.x,event.tfinger.y);
   1.329 -#endif
   1.330 -        break;
   1.331 -      case SDL_FINGERDOWN:
   1.332 -#if VERBOSE
   1.333 -        SDL_Log("Finger: %"SDL_PRIs64" down - x: %f, y: %f",
   1.334 -           event.tfinger.fingerId,event.tfinger.x,event.tfinger.y);
   1.335 +            case SDL_FINGERMOTION:
   1.336 +                SDL_Log("Finger: %"SDL_PRIs64",x: %f, y: %f",event.tfinger.fingerId,
   1.337 +                        event.tfinger.x,event.tfinger.y);
   1.338 +                break;
   1.339 +
   1.340 +            case SDL_FINGERDOWN:
   1.341 +                SDL_Log("Finger: %"SDL_PRIs64" down - x: %f, y: %f",
   1.342 +                        event.tfinger.fingerId,event.tfinger.x,event.tfinger.y);
   1.343 +                break;
   1.344 +
   1.345 +            case SDL_FINGERUP:
   1.346 +                SDL_Log("Finger: %"SDL_PRIs64" up - x: %f, y: %f",
   1.347 +                        event.tfinger.fingerId,event.tfinger.x,event.tfinger.y);
   1.348 +                break;
   1.349  #endif
   1.350 -        break;
   1.351 -      case SDL_FINGERUP:
   1.352 -#if VERBOSE
   1.353 -        SDL_Log("Finger: %"SDL_PRIs64" up - x: %f, y: %f",
   1.354 -               event.tfinger.fingerId,event.tfinger.x,event.tfinger.y);
   1.355 -#endif
   1.356 -        break;
   1.357 -      case SDL_MULTIGESTURE:
   1.358 +
   1.359 +            case SDL_MULTIGESTURE:
   1.360  #if VERBOSE
   1.361 -        SDL_Log("Multi Gesture: x = %f, y = %f, dAng = %f, dR = %f",
   1.362 -           event.mgesture.x,
   1.363 -           event.mgesture.y,
   1.364 -           event.mgesture.dTheta,
   1.365 -           event.mgesture.dDist);
   1.366 -        SDL_Log("MG: numDownTouch = %i",event.mgesture.numFingers);
   1.367 +                SDL_Log("Multi Gesture: x = %f, y = %f, dAng = %f, dR = %f",
   1.368 +                        event.mgesture.x, event.mgesture.y,
   1.369 +                        event.mgesture.dTheta, event.mgesture.dDist);
   1.370 +                SDL_Log("MG: numDownTouch = %i",event.mgesture.numFingers);
   1.371  #endif
   1.372 -        knob.p.x = event.mgesture.x;
   1.373 -        knob.p.y = event.mgesture.y;
   1.374 -        knob.ang += event.mgesture.dTheta;
   1.375 -        knob.r += event.mgesture.dDist;
   1.376 -        break;
   1.377 -      case SDL_DOLLARGESTURE:
   1.378 -        SDL_Log("Gesture %"SDL_PRIs64" performed, error: %f",
   1.379 -           event.dgesture.gestureId,
   1.380 -           event.dgesture.error);
   1.381 -        break;
   1.382 -      case SDL_DOLLARRECORD:
   1.383 -        SDL_Log("Recorded gesture: %"SDL_PRIs64"",event.dgesture.gestureId);
   1.384 -        break;
   1.385 -      }
   1.386 +
   1.387 +                knob.p.x = event.mgesture.x;
   1.388 +                knob.p.y = event.mgesture.y;
   1.389 +                knob.ang += event.mgesture.dTheta;
   1.390 +                knob.r += event.mgesture.dDist;
   1.391 +                break;
   1.392 +
   1.393 +            case SDL_DOLLARGESTURE:
   1.394 +                SDL_Log("Gesture %"SDL_PRIs64" performed, error: %f",
   1.395 +                        event.dgesture.gestureId, event.dgesture.error);
   1.396 +                break;
   1.397 +
   1.398 +            case SDL_DOLLARRECORD:
   1.399 +                SDL_Log("Recorded gesture: %"SDL_PRIs64"",event.dgesture.gestureId);
   1.400 +                break;
   1.401 +        }
   1.402      }
   1.403  
   1.404      for (i = 0; i < state->num_windows; ++i) {
   1.405          if (state->windows[i]) {
   1.406 -          DrawScreen(state->windows[i]);
   1.407 +            DrawScreen(state->windows[i]);
   1.408          }
   1.409      }
   1.410  
   1.411 @@ -265,43 +268,41 @@
   1.412  
   1.413  int main(int argc, char* argv[])
   1.414  {
   1.415 -  /* gesture variables */
   1.416 -  knob.r = .1f;
   1.417 -  knob.ang = 0;
   1.418 +    int i;
   1.419  
   1.420 -  /* !!! FIXME: there should be an SDLTest_CommonDefaultArgs() so apps don't need this. */
   1.421 -  state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
   1.422 -  if (!state) {
   1.423 -    return 1;
   1.424 -  }
   1.425 +    /* !!! FIXME: there should be an SDLTest_CommonDefaultArgs() so apps don't need this. */
   1.426 +    state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
   1.427 +    if (!state) {
   1.428 +        return 1;
   1.429 +    }
   1.430 +
   1.431 +    state->window_title = "Gesture Test";
   1.432 +    state->window_w = WIDTH;
   1.433 +    state->window_h = HEIGHT;
   1.434 +    state->skip_renderer = SDL_TRUE;
   1.435  
   1.436 -  state->window_title = "Gesture Test";
   1.437 -  state->window_w = WIDTH;
   1.438 -  state->window_h = HEIGHT;
   1.439 -  state->skip_renderer = SDL_TRUE;
   1.440 +    for (i = 1; i < argc;) {
   1.441 +        const int consumed = SDLTest_CommonArg(state, i);
   1.442 +        if (consumed == 0) {
   1.443 +            SDL_Log("Usage: %s %s\n", argv[0], SDLTest_CommonUsage(state));
   1.444 +            return 1;
   1.445 +        }
   1.446 +        i += consumed;
   1.447 +    }
   1.448  
   1.449 -  for (i = 1; i < argc;) {
   1.450 -    const int consumed = SDLTest_CommonArg(state, i);
   1.451 -    if (consumed == 0) {
   1.452 -      SDL_Log("Usage: %s %s\n", argv[0], SDLTest_CommonUsage(state));
   1.453 -      return 1;
   1.454 +    if (!SDLTest_CommonInit(state)) {
   1.455 +        return 1;
   1.456      }
   1.457 -    i += consumed;
   1.458 -  }
   1.459 -
   1.460 -  if (!SDLTest_CommonInit(state)) {
   1.461 -    return 1;
   1.462 -  }
   1.463  
   1.464  #ifdef __EMSCRIPTEN__
   1.465      emscripten_set_main_loop(loop, 0, 1);
   1.466  #else
   1.467 -    while(!quitting) {
   1.468 +    while (!quitting) {
   1.469          loop();
   1.470      }
   1.471  #endif
   1.472  
   1.473 -  SDLTest_CommonQuit(state);
   1.474 -  return 0;
   1.475 +    SDLTest_CommonQuit(state);
   1.476 +    return 0;
   1.477  }
   1.478