test/testgesture.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Fri, 12 Dec 2014 21:00:25 +0100
changeset 9273 9f1c18388f2c
parent 9235 ba9988f0daec
child 9274 f95cc158ed4f
permissions -rw-r--r--
Removed global variable from test program.

On Android starting the application after a previous quit did not always work.
Android keeps VM processes for a faster restart and therefore the loaded *.so.
slouken@5535
     1
/*
slouken@8149
     2
  Copyright (C) 1997-2014 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
jim@4689
    18
#include <stdio.h>
jim@4689
    19
#include <math.h>
slouken@5456
    20
slouken@5456
    21
#include "SDL.h"
slouken@5456
    22
#include "SDL_touch.h"
slouken@5456
    23
#include "SDL_gesture.h"
jim@4689
    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
jim@4689
    42
typedef struct {
jim@4689
    43
  float x,y;
jim@4689
    44
} Point;
jim@4689
    45
jim@4689
    46
typedef struct {
jim@4689
    47
  float ang,r;
jim@4689
    48
  Point p;
jim@4689
    49
} Knob;
jim@4689
    50
slouken@5457
    51
static Knob knob;
jim@4689
    52
jim@4689
    53
void handler (int sig)
jim@4689
    54
{
slouken@5460
    55
  SDL_Log ("exiting...(%d)", sig);
jim@4689
    56
  exit (0);
jim@4689
    57
}
jim@4689
    58
jim@4689
    59
void perror_exit (char *error)
jim@4689
    60
{
jim@4689
    61
  perror (error);
jim@4689
    62
  handler (9);
jim@4689
    63
}
jim@4689
    64
slouken@4918
    65
void setpix(SDL_Surface *screen, float _x, float _y, unsigned int col)
jim@4689
    66
{
jim@4689
    67
  Uint32 *pixmem32;
jim@4689
    68
  Uint32 colour;
slouken@4918
    69
  Uint8 r,g,b;
slouken@4918
    70
  int x = (int)_x;
slouken@4918
    71
  int y = (int)_y;
slouken@4918
    72
  float a;
slouken@7191
    73
slouken@6790
    74
  if(x < 0 || x >= screen->w) return;
slouken@6790
    75
  if(y < 0 || y >= screen->h) return;
jim@4689
    76
jim@4689
    77
  pixmem32 = (Uint32*) screen->pixels  + y*screen->pitch/BPP + x;
slouken@7191
    78
slouken@4918
    79
  SDL_memcpy(&colour,pixmem32,screen->format->BytesPerPixel);
jim@4689
    80
jim@4689
    81
  SDL_GetRGB(colour,screen->format,&r,&g,&b);
gabomdq@7678
    82
  /* r = 0;g = 0; b = 0; */
slouken@4918
    83
  a = (float)((col>>24)&0xFF);
gabomdq@7678
    84
  if(a == 0) a = 0xFF; /* Hack, to make things easier. */
jim@4689
    85
  a /= 0xFF;
slouken@4918
    86
  r = (Uint8)(r*(1-a) + ((col>>16)&0xFF)*(a));
slouken@4918
    87
  g = (Uint8)(g*(1-a) + ((col>> 8)&0xFF)*(a));
slouken@4918
    88
  b = (Uint8)(b*(1-a) + ((col>> 0)&0xFF)*(a));
jim@4689
    89
  colour = SDL_MapRGB( screen->format,r, g, b);
slouken@7191
    90
jim@4689
    91
jim@4689
    92
  *pixmem32 = colour;
jim@4689
    93
}
jim@4689
    94
slouken@4918
    95
void drawLine(SDL_Surface *screen,float x0,float y0,float x1,float y1,unsigned int col) {
jim@4689
    96
  float t;
slouken@4918
    97
  for(t=0;t<1;t+=(float)(1.f/SDL_max(SDL_fabs(x0-x1),SDL_fabs(y0-y1))))
jim@4689
    98
    setpix(screen,x1+t*(x0-x1),y1+t*(y0-y1),col);
jim@4689
    99
}
jim@4689
   100
slouken@4918
   101
void drawCircle(SDL_Surface* screen,float x,float y,float r,unsigned int c)
jim@4689
   102
{
slouken@4918
   103
  float tx,ty;
jim@4689
   104
  float xr;
slouken@4918
   105
  for(ty = (float)-SDL_fabs(r);ty <= (float)SDL_fabs((int)r);ty++) {
slouken@4918
   106
    xr = (float)sqrt(r*r - ty*ty);
gabomdq@7678
   107
    if(r > 0) { /* r > 0 ==> filled circle */
slouken@4918
   108
      for(tx=-xr+.5f;tx<=xr-.5;tx++) {
slouken@7191
   109
    setpix(screen,x+tx,y+ty,c);
jim@4689
   110
      }
jim@4689
   111
    }
jim@4689
   112
    else {
slouken@4918
   113
      setpix(screen,x-xr+.5f,y+ty,c);
slouken@4918
   114
      setpix(screen,x+xr-.5f,y+ty,c);
jim@4689
   115
    }
jim@4689
   116
  }
jim@4689
   117
}
jim@4689
   118
jim@4689
   119
void drawKnob(SDL_Surface* screen,Knob k) {
slouken@7191
   120
  drawCircle(screen,k.p.x*screen->w,k.p.y*screen->h,k.r*screen->w,0xFFFFFF);
philipp@7451
   121
  drawCircle(screen,(k.p.x+k.r/2*SDL_cosf(k.ang))*screen->w,
philipp@7451
   122
                (k.p.y+k.r/2*SDL_sinf(k.ang))*screen->h,k.r/4*screen->w,0);
jim@4689
   123
}
jim@4689
   124
philipp@9273
   125
void DrawScreen(SDL_Surface* screen, SDL_Window* window)
jim@4689
   126
{
slouken@5456
   127
  int i;
slouken@5456
   128
#if 1
slouken@5456
   129
  SDL_FillRect(screen, NULL, 0);
slouken@5456
   130
#else
slouken@5456
   131
  int x, y;
jim@4689
   132
  for(y = 0;y < screen->h;y++)
jim@4689
   133
    for(x = 0;x < screen->w;x++)
slouken@7191
   134
    setpix(screen,(float)x,(float)y,((x%255)<<16) + ((y%255)<<8) + (x+y)%255);
slouken@5456
   135
#endif
jim@4689
   136
gabomdq@7678
   137
  /* draw Touch History */
slouken@5460
   138
  for(i = eventWrite; i < eventWrite+EVENT_BUF_SIZE; ++i) {
slouken@5460
   139
    const SDL_Event *event = &events[i&(EVENT_BUF_SIZE-1)];
slouken@5460
   140
    float age = (float)(i - eventWrite) / EVENT_BUF_SIZE;
slouken@7191
   141
    float x, y;
slouken@7191
   142
    unsigned int c, col;
slouken@4918
   143
slouken@7191
   144
    if(event->type == SDL_FINGERMOTION ||
slouken@5460
   145
       event->type == SDL_FINGERDOWN ||
slouken@5460
   146
       event->type == SDL_FINGERUP) {
slouken@6951
   147
      x = event->tfinger.x;
slouken@6951
   148
      y = event->tfinger.y;
slouken@7191
   149
gabomdq@7678
   150
      /* draw the touch: */
slouken@7191
   151
      c = colors[event->tfinger.fingerId%7];
slouken@5460
   152
      col = ((unsigned int)(c*(.1+.85))) | (unsigned int)(0xFF*age)<<24;
jim@4689
   153
slouken@5460
   154
      if(event->type == SDL_FINGERMOTION)
slouken@7191
   155
    drawCircle(screen,x*screen->w,y*screen->h,5,col);
slouken@5460
   156
      else if(event->type == SDL_FINGERDOWN)
slouken@7191
   157
    drawCircle(screen,x*screen->w,y*screen->h,-10,col);
jim@4689
   158
    }
jim@4689
   159
  }
slouken@7191
   160
jim@4689
   161
  if(knob.p.x > 0)
jim@4689
   162
    drawKnob(screen,knob);
slouken@7191
   163
slouken@5456
   164
  SDL_UpdateWindowSurface(window);
jim@4689
   165
}
jim@4689
   166
philipp@9273
   167
/* Returns a new SDL_Window if window is NULL or window if not. */
philipp@9273
   168
SDL_Window* initWindow(SDL_Window *window, int width,int height)
jim@4689
   169
{
slouken@5456
   170
  if (!window) {
slouken@5456
   171
    window = SDL_CreateWindow("Gesture Test",
slouken@5456
   172
                              SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
slouken@6790
   173
                              width, height, SDL_WINDOW_RESIZABLE);
slouken@5456
   174
  }
slouken@5456
   175
  if (!window) {
slouken@5456
   176
    return NULL;
slouken@5456
   177
  }
philipp@9273
   178
  return window;
jim@4689
   179
}
jim@4689
   180
jim@4689
   181
int main(int argc, char* argv[])
slouken@7191
   182
{
philipp@9273
   183
  SDL_Window *window = NULL;
jim@4689
   184
  SDL_Surface *screen;
jim@4689
   185
  SDL_Event event;
slouken@4918
   186
  SDL_bool quitting = SDL_FALSE;
philipp@7900
   187
  SDL_RWops *stream;
jim@4689
   188
aschiffler@7639
   189
  /* Enable standard application logging */
aschiffler@7639
   190
  SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
aschiffler@7639
   191
gabomdq@7678
   192
  /* gesture variables */
slouken@4918
   193
  knob.r = .1f;
jim@4689
   194
  knob.ang = 0;
jim@4689
   195
jim@4689
   196
  if (SDL_Init(SDL_INIT_VIDEO) < 0 ) return 1;
slouken@7191
   197
philipp@9273
   198
  if (!(window = initWindow(window, WIDTH, HEIGHT)) ||
philipp@9273
   199
      !(screen = SDL_GetWindowSurface(window)))
jim@4689
   200
    {
jim@4689
   201
      SDL_Quit();
jim@4689
   202
      return 1;
jim@4689
   203
    }
jim@4689
   204
jim@4689
   205
  while(!quitting) {
slouken@7191
   206
    while(SDL_PollEvent(&event))
jim@4689
   207
      {
gabomdq@7678
   208
    /* Record _all_ events */
slouken@7191
   209
    events[eventWrite & (EVENT_BUF_SIZE-1)] = event;
slouken@7191
   210
    eventWrite++;
slouken@7191
   211
slouken@7191
   212
    switch (event.type)
slouken@7191
   213
      {
slouken@7191
   214
      case SDL_QUIT:
slouken@7191
   215
        quitting = SDL_TRUE;
slouken@7191
   216
        break;
slouken@7191
   217
      case SDL_KEYDOWN:
slouken@7191
   218
        switch (event.key.keysym.sym)
slouken@7191
   219
          {
urkle@9235
   220
              case SDLK_i:
urkle@9235
   221
              {
urkle@9235
   222
                  int i;
urkle@9235
   223
                  for (i = 0; i < SDL_GetNumTouchDevices(); ++i) {
urkle@9235
   224
                      SDL_TouchID id = SDL_GetTouchDevice(i);
urkle@9235
   225
                      SDL_Log("Fingers Down on device %"SDL_PRIs64": %d", id, SDL_GetNumTouchFingers(id));
urkle@9235
   226
                  }
urkle@9235
   227
                  break;
urkle@9235
   228
              }
slouken@7191
   229
          case SDLK_SPACE:
slouken@7191
   230
        SDL_RecordGesture(-1);
slouken@7191
   231
        break;
slouken@7191
   232
          case SDLK_s:
philipp@7900
   233
        stream = SDL_RWFromFile("gestureSave", "w");
philipp@7900
   234
        SDL_Log("Wrote %i templates", SDL_SaveAllDollarTemplates(stream));
philipp@7900
   235
        SDL_RWclose(stream);
slouken@7191
   236
        break;
slouken@7191
   237
          case SDLK_l:
philipp@7900
   238
        stream = SDL_RWFromFile("gestureSave", "r");
philipp@7900
   239
        SDL_Log("Loaded: %i", SDL_LoadDollarTemplates(-1, stream));
philipp@7900
   240
        SDL_RWclose(stream);
slouken@7191
   241
        break;
slouken@7191
   242
          case SDLK_ESCAPE:
slouken@7191
   243
        quitting = SDL_TRUE;
slouken@7191
   244
        break;
slouken@7191
   245
        }
slouken@7191
   246
        break;
slouken@7191
   247
      case SDL_WINDOWEVENT:
slouken@5456
   248
            if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
philipp@9273
   249
          if (!(window = initWindow(window, event.window.data1, event.window.data2)) ||
philipp@9273
   250
              !(screen = SDL_GetWindowSurface(window)))
slouken@7191
   251
          {
slouken@7191
   252
        SDL_Quit();
slouken@7191
   253
        return 1;
slouken@7191
   254
          }
slouken@5456
   255
            }
slouken@7191
   256
        break;
slouken@7191
   257
      case SDL_FINGERMOTION:
jim@4689
   258
#if VERBOSE
slouken@8917
   259
        SDL_Log("Finger: %"SDL_PRIs64",x: %f, y: %f",event.tfinger.fingerId,
slouken@7191
   260
               event.tfinger.x,event.tfinger.y);
jim@4689
   261
#endif
slouken@7191
   262
        break;
slouken@7191
   263
      case SDL_FINGERDOWN:
jim@4689
   264
#if VERBOSE
slouken@8917
   265
        SDL_Log("Finger: %"SDL_PRIs64" down - x: %f, y: %f",
slouken@7191
   266
           event.tfinger.fingerId,event.tfinger.x,event.tfinger.y);
jim@4689
   267
#endif
slouken@7191
   268
        break;
slouken@7191
   269
      case SDL_FINGERUP:
jim@4689
   270
#if VERBOSE
slouken@8917
   271
        SDL_Log("Finger: %"SDL_PRIs64" up - x: %f, y: %f",
slouken@7191
   272
               event.tfinger.fingerId,event.tfinger.x,event.tfinger.y);
jim@4689
   273
#endif
slouken@7191
   274
        break;
slouken@7191
   275
      case SDL_MULTIGESTURE:
slouken@7191
   276
#if VERBOSE
slouken@7191
   277
        SDL_Log("Multi Gesture: x = %f, y = %f, dAng = %f, dR = %f",
slouken@7191
   278
           event.mgesture.x,
slouken@7191
   279
           event.mgesture.y,
slouken@7191
   280
           event.mgesture.dTheta,
slouken@7191
   281
           event.mgesture.dDist);
slouken@7191
   282
        SDL_Log("MG: numDownTouch = %i",event.mgesture.numFingers);
jim@4689
   283
#endif
slouken@7191
   284
        knob.p.x = event.mgesture.x;
slouken@7191
   285
        knob.p.y = event.mgesture.y;
slouken@7191
   286
        knob.ang += event.mgesture.dTheta;
slouken@7191
   287
        knob.r += event.mgesture.dDist;
slouken@7191
   288
        break;
slouken@7191
   289
      case SDL_DOLLARGESTURE:
slouken@8917
   290
        SDL_Log("Gesture %"SDL_PRIs64" performed, error: %f",
slouken@7191
   291
           event.dgesture.gestureId,
slouken@7191
   292
           event.dgesture.error);
slouken@7191
   293
        break;
slouken@7191
   294
      case SDL_DOLLARRECORD:
slouken@8917
   295
        SDL_Log("Recorded gesture: %"SDL_PRIs64"",event.dgesture.gestureId);
slouken@7191
   296
        break;
slouken@7191
   297
      }
jim@4689
   298
      }
philipp@9273
   299
    DrawScreen(screen, window);
slouken@7191
   300
  }
slouken@7191
   301
  SDL_Quit();
jim@4689
   302
  return 0;
jim@4689
   303
}
jim@4689
   304