SDL  2.0
testgesture.c File Reference
#include "SDL.h"
#include <stdlib.h>
+ Include dependency graph for testgesture.c:

Go to the source code of this file.

Data Structures

struct  Point
struct  Knob

Macros

#define WIDTH   640
#define HEIGHT   480
#define BPP   4
#define EVENT_BUF_SIZE   256
#define VERBOSE   0

Functions

void setpix (SDL_Surface *screen, float _x, float _y, unsigned int col)
void drawLine (SDL_Surface *screen, float x0, float y0, float x1, float y1, unsigned int col)
void drawCircle (SDL_Surface *screen, float x, float y, float r, unsigned int c)
void drawKnob (SDL_Surface *screen, Knob k)
void DrawScreen (SDL_Surface *screen, SDL_Window *window)
SDL_WindowinitWindow (SDL_Window *window, int width, int height)
void loop ()
int main (int argc, char *argv[])

Variables

static SDL_Event events [EVENT_BUF_SIZE]
static int eventWrite
static int colors [7] = {0xFF,0xFF00,0xFF0000,0xFFFF00,0x00FFFF,0xFF00FF,0xFFFFFF}
SDL_Surfacescreen
SDL_Windowwindow
SDL_bool quitting = SDL_FALSE
static Knob knob

Macro Definition Documentation

#define BPP   4

Definition at line 27 of file testgesture.c.

Referenced by setpix().

#define EVENT_BUF_SIZE   256

Definition at line 30 of file testgesture.c.

Referenced by DrawScreen(), and loop().

#define HEIGHT   480

Definition at line 26 of file testgesture.c.

Referenced by main().

#define VERBOSE   0

Definition at line 33 of file testgesture.c.

#define WIDTH   640

Definition at line 25 of file testgesture.c.

Referenced by main().

Function Documentation

void drawCircle ( SDL_Surface screen,
float  x,
float  y,
float  r,
unsigned int  c 
)

Definition at line 92 of file testgesture.c.

References SDL_fabs, SDL_sqrt, and setpix().

Referenced by drawKnob(), and DrawScreen().

{
float tx,ty;
float xr;
for(ty = (float)-SDL_fabs(r);ty <= (float)SDL_fabs((int)r);ty++) {
xr = (float)SDL_sqrt(r*r - ty*ty);
if(r > 0) { /* r > 0 ==> filled circle */
for(tx=-xr+.5f;tx<=xr-.5;tx++) {
setpix(screen,x+tx,y+ty,c);
}
}
else {
setpix(screen,x-xr+.5f,y+ty,c);
setpix(screen,x+xr-.5f,y+ty,c);
}
}
}
void drawKnob ( SDL_Surface screen,
Knob  k 
)

Definition at line 110 of file testgesture.c.

References Knob::ang, drawCircle(), SDL_Surface::h, Knob::p, Knob::r, SDL_cosf, SDL_sinf, SDL_Surface::w, Point::x, and Point::y.

Referenced by DrawScreen().

{
drawCircle(screen,k.p.x*screen->w,k.p.y*screen->h,k.r*screen->w,0xFFFFFF);
drawCircle(screen,(k.p.x+k.r/2*SDL_cosf(k.ang))*screen->w,
(k.p.y+k.r/2*SDL_sinf(k.ang))*screen->h,k.r/4*screen->w,0);
}
void drawLine ( SDL_Surface screen,
float  x0,
float  y0,
float  x1,
float  y1,
unsigned int  col 
)

Definition at line 86 of file testgesture.c.

References SDL_fabs, SDL_max, and setpix().

{
float t;
for(t=0;t<1;t+=(float)(1.f/SDL_max(SDL_fabs(x0-x1),SDL_fabs(y0-y1))))
setpix(screen,x1+t*(x0-x1),y1+t*(y0-y1),col);
}
void DrawScreen ( SDL_Surface screen,
SDL_Window window 
)

Definition at line 116 of file testgesture.c.

References colors, drawCircle(), drawKnob(), EVENT_BUF_SIZE, eventWrite, SDL_Surface::h, i, NULL, Knob::p, SDL_FillRect, SDL_FINGERDOWN, SDL_FINGERMOTION, SDL_FINGERUP, SDL_UpdateWindowSurface, setpix(), SDL_Surface::w, and Point::x.

Referenced by loop().

{
int i;
#if 1
SDL_FillRect(screen, NULL, 0);
#else
int x, y;
for(y = 0;y < screen->h;y++)
for(x = 0;x < screen->w;x++)
setpix(screen,(float)x,(float)y,((x%255)<<16) + ((y%255)<<8) + (x+y)%255);
#endif
/* draw Touch History */
const SDL_Event *event = &events[i&(EVENT_BUF_SIZE-1)];
float age = (float)(i - eventWrite) / EVENT_BUF_SIZE;
float x, y;
unsigned int c, col;
if(event->type == SDL_FINGERMOTION ||
event->type == SDL_FINGERDOWN ||
event->type == SDL_FINGERUP) {
x = event->tfinger.x;
y = event->tfinger.y;
/* draw the touch: */
c = colors[event->tfinger.fingerId%7];
col = ((unsigned int)(c*(.1+.85))) | (unsigned int)(0xFF*age)<<24;
if(event->type == SDL_FINGERMOTION)
drawCircle(screen,x*screen->w,y*screen->h,5,col);
else if(event->type == SDL_FINGERDOWN)
drawCircle(screen,x*screen->w,y*screen->h,-10,col);
}
}
if(knob.p.x > 0)
drawKnob(screen,knob);
}
SDL_Window* initWindow ( SDL_Window window,
int  width,
int  height 
)

Definition at line 159 of file testgesture.c.

References SDL_CreateWindow, SDL_WINDOW_RESIZABLE, and SDL_WINDOWPOS_CENTERED.

Referenced by loop(), and main().

{
if (!window) {
window = SDL_CreateWindow("Gesture Test",
}
return window;
}
void loop ( )

Definition at line 169 of file testgesture.c.

References Knob::ang, SDL_WindowEvent::data1, SDL_WindowEvent::data2, SDL_MultiGestureEvent::dDist, SDL_Event::dgesture, DrawScreen(), SDL_MultiGestureEvent::dTheta, SDL_DollarGestureEvent::error, SDL_WindowEvent::event, EVENT_BUF_SIZE, eventWrite, SDL_TouchFingerEvent::fingerId, SDL_DollarGestureEvent::gestureId, i, initWindow(), SDL_Event::key, SDL_KeyboardEvent::keysym, SDL_Event::mgesture, SDL_MultiGestureEvent::numFingers, Knob::p, quitting, Knob::r, SDL_DOLLARGESTURE, SDL_DOLLARRECORD, SDL_FINGERDOWN, SDL_FINGERMOTION, SDL_FINGERUP, SDL_GetNumTouchDevices, SDL_GetNumTouchFingers, SDL_GetTouchDevice, SDL_GetWindowSurface, SDL_KEYDOWN, SDL_LoadDollarTemplates, SDL_Log, SDL_MULTIGESTURE, SDL_PollEvent, SDL_PRIs64, SDL_Quit, SDL_QUIT, SDL_RecordGesture, SDL_RWclose, SDL_RWFromFile, SDL_SaveAllDollarTemplates, SDL_TRUE, SDL_WINDOWEVENT, SDL_WINDOWEVENT_RESIZED, SDLK_ESCAPE, SDLK_i, SDLK_l, SDLK_s, SDLK_SPACE, SDL_Keysym::sym, SDL_Event::tfinger, SDL_Event::type, SDL_Event::window, Point::x, SDL_TouchFingerEvent::x, SDL_MultiGestureEvent::x, Point::y, SDL_TouchFingerEvent::y, and SDL_MultiGestureEvent::y.

{
while(SDL_PollEvent(&event))
{
/* Record _all_ events */
switch (event.type)
{
case SDL_QUIT:
break;
switch (event.key.keysym.sym)
{
case SDLK_i:
{
int i;
for (i = 0; i < SDL_GetNumTouchDevices(); ++i) {
SDL_Log("Fingers Down on device %"SDL_PRIs64": %d", id, SDL_GetNumTouchFingers(id));
}
break;
}
case SDLK_SPACE:
break;
case SDLK_s:
stream = SDL_RWFromFile("gestureSave", "w");
SDL_Log("Wrote %i templates", SDL_SaveAllDollarTemplates(stream));
SDL_RWclose(stream);
break;
case SDLK_l:
stream = SDL_RWFromFile("gestureSave", "r");
SDL_Log("Loaded: %i", SDL_LoadDollarTemplates(-1, stream));
SDL_RWclose(stream);
break;
break;
}
break;
if (!(window = initWindow(window, event.window.data1, event.window.data2)) ||
{
exit(1);
}
}
break;
#if VERBOSE
SDL_Log("Finger: %"SDL_PRIs64",x: %f, y: %f",event.tfinger.fingerId,
event.tfinger.x,event.tfinger.y);
#endif
break;
#if VERBOSE
SDL_Log("Finger: %"SDL_PRIs64" down - x: %f, y: %f",
event.tfinger.fingerId,event.tfinger.x,event.tfinger.y);
#endif
break;
#if VERBOSE
SDL_Log("Finger: %"SDL_PRIs64" up - x: %f, y: %f",
event.tfinger.fingerId,event.tfinger.x,event.tfinger.y);
#endif
break;
#if VERBOSE
SDL_Log("Multi Gesture: x = %f, y = %f, dAng = %f, dR = %f",
event.mgesture.x,
event.mgesture.y,
event.mgesture.dDist);
SDL_Log("MG: numDownTouch = %i",event.mgesture.numFingers);
#endif
knob.p.x = event.mgesture.x;
knob.p.y = event.mgesture.y;
knob.ang += event.mgesture.dTheta;
knob.r += event.mgesture.dDist;
break;
SDL_Log("Gesture %"SDL_PRIs64" performed, error: %f",
event.dgesture.error);
break;
SDL_Log("Recorded gesture: %"SDL_PRIs64"",event.dgesture.gestureId);
break;
}
}
#ifdef __EMSCRIPTEN__
if (quitting) {
emscripten_cancel_main_loop();
}
#endif
}
int main ( int  argc,
char *  argv[] 
)

Definition at line 276 of file testgesture.c.

References Knob::ang, HEIGHT, initWindow(), loop(), NULL, quitting, Knob::r, SDL_FALSE, SDL_GetWindowSurface, SDL_Init, SDL_INIT_VIDEO, SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, SDL_LogSetPriority, SDL_Quit, and WIDTH.

{
/* Enable standard application logging */
/* gesture variables */
knob.r = .1f;
knob.ang = 0;
if (SDL_Init(SDL_INIT_VIDEO) < 0 ) return 1;
{
return 1;
}
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop(loop, 0, 1);
#else
while(!quitting) {
loop();
}
#endif
return 0;
}
void setpix ( SDL_Surface screen,
float  _x,
float  _y,
unsigned int  col 
)

Definition at line 56 of file testgesture.c.

References BPP, SDL_PixelFormat::BytesPerPixel, SDL_Surface::format, SDL_Surface::h, SDL_Surface::pitch, SDL_Surface::pixels, SDL_GetRGB, SDL_MapRGB, SDL_memcpy, and SDL_Surface::w.

Referenced by drawCircle(), drawLine(), and DrawScreen().

{
Uint32 *pixmem32;
Uint32 colour;
int x = (int)_x;
int y = (int)_y;
float a;
if(x < 0 || x >= screen->w) return;
if(y < 0 || y >= screen->h) return;
pixmem32 = (Uint32*) screen->pixels + y*screen->pitch/BPP + x;
SDL_memcpy(&colour,pixmem32,screen->format->BytesPerPixel);
SDL_GetRGB(colour,screen->format,&r,&g,&b);
/* r = 0;g = 0; b = 0; */
a = (float)((col>>24)&0xFF);
if(a == 0) a = 0xFF; /* Hack, to make things easier. */
a /= 0xFF;
r = (Uint8)(r*(1-a) + ((col>>16)&0xFF)*(a));
g = (Uint8)(g*(1-a) + ((col>> 8)&0xFF)*(a));
b = (Uint8)(b*(1-a) + ((col>> 0)&0xFF)*(a));
colour = SDL_MapRGB( screen->format,r, g, b);
*pixmem32 = colour;
}

Variable Documentation

int colors[7] = {0xFF,0xFF00,0xFF0000,0xFFFF00,0x00FFFF,0xFF00FF,0xFFFFFF}
static

Definition at line 39 of file testgesture.c.

Referenced by DrawScreen(), MapNto1(), SDL_LoadBMP_RW(), and SDL_SaveBMP_RW().

SDL_Event events[EVENT_BUF_SIZE]
static

Definition at line 35 of file testgesture.c.

Referenced by IsSDLWindowEventPending(), and UpdateEventsForDeviceRemoval().

int eventWrite
static

Definition at line 36 of file testgesture.c.

Referenced by DrawScreen(), and loop().

Knob knob
static

Definition at line 54 of file testgesture.c.

SDL_bool quitting = SDL_FALSE

Definition at line 43 of file testgesture.c.

Referenced by loop(), and main().

SDL_Surface* screen

Definition at line 41 of file testgesture.c.

Definition at line 42 of file testgesture.c.