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