2 Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
13 * Spacebar to begin recording a gesture on all touches.
14 * s to save all touches into "./gestureSave"
15 * l to load all touches from "./gestureSave"
19 #include <stdlib.h> /* for exit() */
22 #include <emscripten/emscripten.h>
30 /* MUST BE A POWER OF 2! */
31 #define EVENT_BUF_SIZE 256
36 static SDL_Event events[EVENT_BUF_SIZE];
37 static int eventWrite;
40 static int colors[7] = {0xFF,0xFF00,0xFF0000,0xFFFF00,0x00FFFF,0xFF00FF,0xFFFFFF};
44 SDL_bool quitting = SDL_FALSE;
57 void setpix(SDL_Surface *screen, float _x, float _y, unsigned int col)
66 if(x < 0 || x >= screen->w) return;
67 if(y < 0 || y >= screen->h) return;
69 pixmem32 = (Uint32*) screen->pixels + y*screen->pitch/BPP + x;
71 SDL_memcpy(&colour,pixmem32,screen->format->BytesPerPixel);
73 SDL_GetRGB(colour,screen->format,&r,&g,&b);
74 /* r = 0;g = 0; b = 0; */
75 a = (float)((col>>24)&0xFF);
76 if(a == 0) a = 0xFF; /* Hack, to make things easier. */
78 r = (Uint8)(r*(1-a) + ((col>>16)&0xFF)*(a));
79 g = (Uint8)(g*(1-a) + ((col>> 8)&0xFF)*(a));
80 b = (Uint8)(b*(1-a) + ((col>> 0)&0xFF)*(a));
81 colour = SDL_MapRGB( screen->format,r, g, b);
87 void drawLine(SDL_Surface *screen,float x0,float y0,float x1,float y1,unsigned int col) {
89 for(t=0;t<1;t+=(float)(1.f/SDL_max(SDL_fabs(x0-x1),SDL_fabs(y0-y1))))
90 setpix(screen,x1+t*(x0-x1),y1+t*(y0-y1),col);
93 void drawCircle(SDL_Surface* screen,float x,float y,float r,unsigned int c)
97 for(ty = (float)-SDL_fabs(r);ty <= (float)SDL_fabs((int)r);ty++) {
98 xr = (float)SDL_sqrt(r*r - ty*ty);
99 if(r > 0) { /* r > 0 ==> filled circle */
100 for(tx=-xr+.5f;tx<=xr-.5;tx++) {
101 setpix(screen,x+tx,y+ty,c);
105 setpix(screen,x-xr+.5f,y+ty,c);
106 setpix(screen,x+xr-.5f,y+ty,c);
111 void drawKnob(SDL_Surface* screen,Knob k) {
112 drawCircle(screen,k.p.x*screen->w,k.p.y*screen->h,k.r*screen->w,0xFFFFFF);
113 drawCircle(screen,(k.p.x+k.r/2*SDL_cosf(k.ang))*screen->w,
114 (k.p.y+k.r/2*SDL_sinf(k.ang))*screen->h,k.r/4*screen->w,0);
117 void DrawScreen(SDL_Surface* screen, SDL_Window* window)
121 SDL_FillRect(screen, NULL, 0);
124 for(y = 0;y < screen->h;y++)
125 for(x = 0;x < screen->w;x++)
126 setpix(screen,(float)x,(float)y,((x%255)<<16) + ((y%255)<<8) + (x+y)%255);
129 /* draw Touch History */
130 for(i = eventWrite; i < eventWrite+EVENT_BUF_SIZE; ++i) {
131 const SDL_Event *event = &events[i&(EVENT_BUF_SIZE-1)];
132 float age = (float)(i - eventWrite) / EVENT_BUF_SIZE;
136 if(event->type == SDL_FINGERMOTION ||
137 event->type == SDL_FINGERDOWN ||
138 event->type == SDL_FINGERUP) {
139 x = event->tfinger.x;
140 y = event->tfinger.y;
142 /* draw the touch: */
143 c = colors[event->tfinger.fingerId%7];
144 col = ((unsigned int)(c*(.1+.85))) | (unsigned int)(0xFF*age)<<24;
146 if(event->type == SDL_FINGERMOTION)
147 drawCircle(screen,x*screen->w,y*screen->h,5,col);
148 else if(event->type == SDL_FINGERDOWN)
149 drawCircle(screen,x*screen->w,y*screen->h,-10,col);
154 drawKnob(screen,knob);
156 SDL_UpdateWindowSurface(window);
159 /* Returns a new SDL_Window if window is NULL or window if not. */
160 SDL_Window* initWindow(SDL_Window *window, int width,int height)
163 window = SDL_CreateWindow("Gesture Test",
164 SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
165 width, height, SDL_WINDOW_RESIZABLE);
175 while(SDL_PollEvent(&event))
177 /* Record _all_ events */
178 events[eventWrite & (EVENT_BUF_SIZE-1)] = event;
187 switch (event.key.keysym.sym)
192 for (i = 0; i < SDL_GetNumTouchDevices(); ++i) {
193 SDL_TouchID id = SDL_GetTouchDevice(i);
194 SDL_Log("Fingers Down on device %"SDL_PRIs64": %d", id, SDL_GetNumTouchFingers(id));
199 SDL_RecordGesture(-1);
202 stream = SDL_RWFromFile("gestureSave", "w");
203 SDL_Log("Wrote %i templates", SDL_SaveAllDollarTemplates(stream));
207 stream = SDL_RWFromFile("gestureSave", "r");
208 SDL_Log("Loaded: %i", SDL_LoadDollarTemplates(-1, stream));
216 case SDL_WINDOWEVENT:
217 if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
218 if (!(window = initWindow(window, event.window.data1, event.window.data2)) ||
219 !(screen = SDL_GetWindowSurface(window)))
226 case SDL_FINGERMOTION:
228 SDL_Log("Finger: %"SDL_PRIs64",x: %f, y: %f",event.tfinger.fingerId,
229 event.tfinger.x,event.tfinger.y);
234 SDL_Log("Finger: %"SDL_PRIs64" down - x: %f, y: %f",
235 event.tfinger.fingerId,event.tfinger.x,event.tfinger.y);
240 SDL_Log("Finger: %"SDL_PRIs64" up - x: %f, y: %f",
241 event.tfinger.fingerId,event.tfinger.x,event.tfinger.y);
244 case SDL_MULTIGESTURE:
246 SDL_Log("Multi Gesture: x = %f, y = %f, dAng = %f, dR = %f",
249 event.mgesture.dTheta,
250 event.mgesture.dDist);
251 SDL_Log("MG: numDownTouch = %i",event.mgesture.numFingers);
253 knob.p.x = event.mgesture.x;
254 knob.p.y = event.mgesture.y;
255 knob.ang += event.mgesture.dTheta;
256 knob.r += event.mgesture.dDist;
258 case SDL_DOLLARGESTURE:
259 SDL_Log("Gesture %"SDL_PRIs64" performed, error: %f",
260 event.dgesture.gestureId,
261 event.dgesture.error);
263 case SDL_DOLLARRECORD:
264 SDL_Log("Recorded gesture: %"SDL_PRIs64"",event.dgesture.gestureId);
268 DrawScreen(screen, window);
270 #ifdef __EMSCRIPTEN__
272 emscripten_cancel_main_loop();
277 int main(int argc, char* argv[])
281 quitting = SDL_FALSE;
283 /* Enable standard application logging */
284 SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
286 /* gesture variables */
290 if (SDL_Init(SDL_INIT_VIDEO) < 0 ) return 1;
292 if (!(window = initWindow(window, WIDTH, HEIGHT)) ||
293 !(screen = SDL_GetWindowSurface(window)))
299 #ifdef __EMSCRIPTEN__
300 emscripten_set_main_loop(loop, 0, 1);