Update VS2010 project to add new files; update new files so code builds on Win32/Win64
2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2010 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software Founation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "SDL_config.h"
24 /* General mouse handling code for SDL */
26 #include "SDL_events.h"
27 #include "SDL_events_c.h"
28 #include "SDL_gesture_c.h"
35 //TODO: Replace with malloc
37 #define MAXPATHSIZE 1024
42 #define DOLLARNPOINTS 64
43 #define DOLLARSIZE 256
47 //PHI = ((sqrt(5)-1)/2)
48 #define PHI 0.618033989
58 SDL_FloatPoint p[MAXPATHSIZE];
62 SDL_FloatPoint path[DOLLARNPOINTS];
69 SDL_FloatPoint centroid;
70 SDL_DollarPath dollarPath;
71 Uint16 numDownFingers;
73 int numDollarTemplates;
74 SDL_DollarTemplate *dollarTemplate;
79 SDL_GestureTouch *SDL_gestureTouch;
80 int SDL_numGestureTouches = 0;
83 void SDL_PrintPath(SDL_FloatPoint *path) {
86 for(i=0;i<DOLLARNPOINTS;i++) {
87 printf(" (%f,%f)",path[i].x,path[i].y);
92 int SDL_RecordGesture(SDL_TouchID touchId) {
94 if(touchId < 0) recordAll = SDL_TRUE;
95 for(i = 0;i < SDL_numGestureTouches; i++) {
96 if((touchId < 0) || (SDL_gestureTouch[i].id == touchId)) {
97 SDL_gestureTouch[i].recording = SDL_TRUE;
102 return (touchId < 0);
105 unsigned long SDL_HashDollar(SDL_FloatPoint* points) {
106 unsigned long hash = 5381;
108 for(i = 0;i < DOLLARNPOINTS; i++) {
109 hash = ((hash<<5) + hash) + (unsigned long)points[i].x;
110 hash = ((hash<<5) + hash) + (unsigned long)points[i].y;
116 static int SaveTemplate(SDL_DollarTemplate *templ, SDL_RWops * src) {
117 if(src == NULL) return 0;
120 //No Longer storing the Hash, rehash on load
121 //if(SDL_RWops.write(src,&(templ->hash),sizeof(templ->hash),1) != 1) return 0;
123 if(SDL_RWwrite(src,templ->path,
124 sizeof(templ->path[0]),DOLLARNPOINTS) != DOLLARNPOINTS)
131 int SDL_SaveAllDollarTemplates(SDL_RWops *src) {
133 for(i = 0; i < SDL_numGestureTouches; i++) {
134 SDL_GestureTouch* touch = &SDL_gestureTouch[i];
135 for(j = 0;j < touch->numDollarTemplates; j++) {
136 rtrn += SaveTemplate(&touch->dollarTemplate[i],src);
142 int SDL_SaveDollarTemplate(SDL_GestureID gestureId, SDL_RWops *src) {
144 for(i = 0; i < SDL_numGestureTouches; i++) {
145 SDL_GestureTouch* touch = &SDL_gestureTouch[i];
146 for(j = 0;j < touch->numDollarTemplates; j++) {
147 if(touch->dollarTemplate[i].hash == gestureId) {
148 return SaveTemplate(&touch->dollarTemplate[i],src);
152 SDL_SetError("Unknown gestureId");
156 //path is an already sampled set of points
157 //Returns the index of the gesture on success, or -1
158 static int SDL_AddDollarGesture(SDL_GestureTouch* inTouch,SDL_FloatPoint* path) {
159 SDL_DollarTemplate* dollarTemplate;
160 SDL_DollarTemplate *templ;
162 if(inTouch == NULL) {
163 if(SDL_numGestureTouches == 0) return -1;
164 for(i = 0;i < SDL_numGestureTouches; i++) {
165 inTouch = &SDL_gestureTouch[i];
168 (SDL_DollarTemplate *)SDL_realloc(inTouch->dollarTemplate,
169 (inTouch->numDollarTemplates + 1) *
170 sizeof(SDL_DollarTemplate));
171 if(!dollarTemplate) {
176 inTouch->dollarTemplate = dollarTemplate;
179 &inTouch->dollarTemplate[inTouch->numDollarTemplates];
180 memcpy(templ->path,path,DOLLARNPOINTS*sizeof(SDL_FloatPoint));
181 templ->hash = SDL_HashDollar(templ->path);
182 inTouch->numDollarTemplates++;
184 return inTouch->numDollarTemplates - 1;
186 SDL_DollarTemplate* dollarTemplate =
187 ( SDL_DollarTemplate *)SDL_realloc(inTouch->dollarTemplate,
188 (inTouch->numDollarTemplates + 1) *
189 sizeof(SDL_DollarTemplate));
190 if(!dollarTemplate) {
195 inTouch->dollarTemplate = dollarTemplate;
198 &inTouch->dollarTemplate[inTouch->numDollarTemplates];
199 memcpy(templ->path,path,DOLLARNPOINTS*sizeof(SDL_FloatPoint));
200 templ->hash = SDL_HashDollar(templ->path);
201 inTouch->numDollarTemplates++;
202 return inTouch->numDollarTemplates - 1;
207 int SDL_LoadDollarTemplates(SDL_TouchID touchId, SDL_RWops *src) {
209 SDL_GestureTouch *touch = NULL;
210 if(src == NULL) return 0;
212 for(i = 0;i < SDL_numGestureTouches; i++)
213 if(SDL_gestureTouch[i].id == touchId)
214 touch = &SDL_gestureTouch[i];
215 if(touch == NULL) return -1;
219 SDL_DollarTemplate templ;
221 if(SDL_RWread(src,templ.path,sizeof(templ.path[0]),DOLLARNPOINTS) <
222 DOLLARNPOINTS) break;
225 printf("Adding loaded gesture to 1 touch\n");
226 if(SDL_AddDollarGesture(touch,templ.path)) loaded++;
229 printf("Adding to: %i touches\n",SDL_numGestureTouches);
230 for(i = 0;i < SDL_numGestureTouches; i++) {
231 touch = &SDL_gestureTouch[i];
232 printf("Adding loaded gesture to + touches\n");
233 //TODO: What if this fails?
234 SDL_AddDollarGesture(touch,templ.path);
244 float dollarDifference(SDL_FloatPoint* points,SDL_FloatPoint* templ,float ang) {
245 // SDL_FloatPoint p[DOLLARNPOINTS];
249 for(i = 0; i < DOLLARNPOINTS; i++) {
250 p.x = (float)(points[i].x * cos(ang) - points[i].y * sin(ang));
251 p.y = (float)(points[i].x * sin(ang) + points[i].y * cos(ang));
252 dist += (float)(sqrt((p.x-templ[i].x)*(p.x-templ[i].x)+
253 (p.y-templ[i].y)*(p.y-templ[i].y)));
255 return dist/DOLLARNPOINTS;
259 float bestDollarDifference(SDL_FloatPoint* points,SDL_FloatPoint* templ) {
260 //------------BEGIN DOLLAR BLACKBOX----------------//
261 //-TRANSLATED DIRECTLY FROM PSUDEO-CODE AVAILABLE AT-//
262 //-"http://depts.washington.edu/aimgroup/proj/dollar/"-//
266 float x1 = (float)(PHI*ta + (1-PHI)*tb);
267 float f1 = dollarDifference(points,templ,x1);
268 float x2 = (float)((1-PHI)*ta + PHI*tb);
269 float f2 = dollarDifference(points,templ,x2);
270 while(fabs(ta-tb) > dt) {
275 x1 = (float)(PHI*ta + (1-PHI)*tb);
276 f1 = dollarDifference(points,templ,x1);
282 x2 = (float)((1-PHI)*ta + PHI*tb);
283 f2 = dollarDifference(points,templ,x2);
288 printf("Min angle (x1): %f\n",x1);
290 printf("Min angle (x2): %f\n",x2);
292 return SDL_min(f1,f2);
295 //DollarPath contains raw points, plus (possibly) the calculated length
296 int dollarNormalize(SDL_DollarPath path,SDL_FloatPoint *points) {
301 SDL_FloatPoint centroid;
302 float xmin,xmax,ymin,ymax;
306 //Calculate length if it hasn't already been done
307 if(path.length <= 0) {
308 for(i=1;i<path.numPoints;i++) {
309 float dx = path.p[i ].x -
311 float dy = path.p[i ].y -
313 path.length += (float)(sqrt(dx*dx+dy*dy));
318 interval = path.length/(DOLLARNPOINTS - 1);
321 centroid.x = 0;centroid.y = 0;
323 //printf("(%f,%f)\n",path.p[path.numPoints-1].x,path.p[path.numPoints-1].y);
324 for(i = 1;i < path.numPoints;i++) {
325 float d = (float)(sqrt((path.p[i-1].x-path.p[i].x)*(path.p[i-1].x-path.p[i].x)+
326 (path.p[i-1].y-path.p[i].y)*(path.p[i-1].y-path.p[i].y)));
327 //printf("d = %f dist = %f/%f\n",d,dist,interval);
328 while(dist + d > interval) {
329 points[numPoints].x = path.p[i-1].x +
330 ((interval-dist)/d)*(path.p[i].x-path.p[i-1].x);
331 points[numPoints].y = path.p[i-1].y +
332 ((interval-dist)/d)*(path.p[i].y-path.p[i-1].y);
333 centroid.x += points[numPoints].x;
334 centroid.y += points[numPoints].y;
341 if(numPoints < DOLLARNPOINTS-1) {
342 printf("ERROR: NumPoints = %i\n",numPoints);
345 //copy the last point
346 points[DOLLARNPOINTS-1] = path.p[path.numPoints-1];
347 numPoints = DOLLARNPOINTS;
349 centroid.x /= numPoints;
350 centroid.y /= numPoints;
352 //printf("Centroid (%f,%f)",centroid.x,centroid.y);
353 //Rotate Points so point 0 is left of centroid and solve for the bounding box
359 ang = (float)(atan2(centroid.y - points[0].y,
360 centroid.x - points[0].x));
362 for(i = 0;i<numPoints;i++) {
363 float px = points[i].x;
364 float py = points[i].y;
365 points[i].x = (float)((px - centroid.x)*cos(ang) -
366 (py - centroid.y)*sin(ang) + centroid.x);
367 points[i].y = (float)((px - centroid.x)*sin(ang) +
368 (py - centroid.y)*cos(ang) + centroid.y);
371 if(points[i].x < xmin) xmin = points[i].x;
372 if(points[i].x > xmax) xmax = points[i].x;
373 if(points[i].y < ymin) ymin = points[i].y;
374 if(points[i].y > ymax) ymax = points[i].y;
377 //Scale points to DOLLARSIZE, and translate to the origin
381 for(i=0;i<numPoints;i++) {
382 points[i].x = (points[i].x - centroid.x)*DOLLARSIZE/w;
383 points[i].y = (points[i].y - centroid.y)*DOLLARSIZE/h;
388 float dollarRecognize(SDL_DollarPath path,int *bestTempl,SDL_GestureTouch* touch) {
390 SDL_FloatPoint points[DOLLARNPOINTS];
391 int numPoints = dollarNormalize(path,points);
392 //SDL_PrintPath(points);
395 float bestDiff = 10000;
397 for(i = 0;i < touch->numDollarTemplates;i++) {
398 float diff = bestDollarDifference(points,touch->dollarTemplate[i].path);
399 if(diff < bestDiff) {bestDiff = diff; *bestTempl = i;}
404 int SDL_GestureAddTouch(SDL_Touch* touch) {
405 SDL_GestureTouch *gestureTouch = (SDL_GestureTouch *)SDL_realloc(SDL_gestureTouch,
406 (SDL_numGestureTouches + 1) *
407 sizeof(SDL_GestureTouch));
414 SDL_gestureTouch = gestureTouch;
416 SDL_gestureTouch[SDL_numGestureTouches].res.x = touch->xres;
417 SDL_gestureTouch[SDL_numGestureTouches].res.y = touch->yres;
418 SDL_gestureTouch[SDL_numGestureTouches].numDownFingers = 0;
420 SDL_gestureTouch[SDL_numGestureTouches].res.x = touch->xres;
421 SDL_gestureTouch[SDL_numGestureTouches].id = touch->id;
423 SDL_gestureTouch[SDL_numGestureTouches].numDollarTemplates = 0;
425 SDL_gestureTouch[SDL_numGestureTouches].recording = SDL_FALSE;
427 SDL_numGestureTouches++;
431 int SDL_GestureRemoveTouch(SDL_TouchID id) {
433 for(i = 0;i < SDL_numGestureTouches; i++) {
434 if(SDL_gestureTouch[i].id == id) {
435 SDL_numGestureTouches--;
436 SDL_gestureTouch[i] = SDL_gestureTouch[SDL_numGestureTouches];
444 SDL_GestureTouch * SDL_GetGestureTouch(SDL_TouchID id) {
446 for(i = 0;i < SDL_numGestureTouches; i++) {
447 //printf("%i ?= %i\n",SDL_gestureTouch[i].id,id);
448 if(SDL_gestureTouch[i].id == id) return &SDL_gestureTouch[i];
453 int SDL_SendGestureMulti(SDL_GestureTouch* touch,float dTheta,float dDist) {
455 event.mgesture.type = SDL_MULTIGESTURE;
456 event.mgesture.touchId = touch->id;
457 event.mgesture.x = touch->centroid.x;
458 event.mgesture.y = touch->centroid.y;
459 event.mgesture.dTheta = dTheta;
460 event.mgesture.dDist = dDist;
461 event.mgesture.numFingers = touch->numDownFingers;
462 return SDL_PushEvent(&event) > 0;
465 int SDL_SendGestureDollar(SDL_GestureTouch* touch,
466 SDL_GestureID gestureId,float error) {
468 event.dgesture.type = SDL_DOLLARGESTURE;
469 event.dgesture.touchId = touch->id;
471 //TODO: Add this to give location of gesture?
472 event.mgesture.x = touch->centroid.x;
473 event.mgesture.y = touch->centroid.y;
475 event.dgesture.gestureId = gestureId;
476 event.dgesture.error = error;
477 //A finger came up to trigger this event.
478 event.dgesture.numFingers = touch->numDownFingers + 1;
479 return SDL_PushEvent(&event) > 0;
483 int SDL_SendDollarRecord(SDL_GestureTouch* touch,SDL_GestureID gestureId) {
485 event.dgesture.type = SDL_DOLLARRECORD;
486 event.dgesture.touchId = touch->id;
487 event.dgesture.gestureId = gestureId;
488 return SDL_PushEvent(&event) > 0;
492 void SDL_GestureProcessEvent(SDL_Event* event)
495 SDL_FloatPoint path[DOLLARNPOINTS];
498 float pathDx, pathDy;
499 SDL_FloatPoint lastP;
500 SDL_FloatPoint lastCentroid;
506 if(event->type == SDL_FINGERMOTION ||
507 event->type == SDL_FINGERDOWN ||
508 event->type == SDL_FINGERUP) {
509 SDL_GestureTouch* inTouch = SDL_GetGestureTouch(event->tfinger.touchId);
511 //Shouldn't be possible
512 if(inTouch == NULL) return;
514 //printf("@ (%i,%i) with res: (%i,%i)\n",(int)event->tfinger.x,
515 // (int)event->tfinger.y,
516 // (int)inTouch->res.x,(int)inTouch->res.y);
519 x = ((float)event->tfinger.x)/(float)inTouch->res.x;
520 y = ((float)event->tfinger.y)/(float)inTouch->res.y;
524 if(event->type == SDL_FINGERUP) {
525 inTouch->numDownFingers--;
528 if(inTouch->recording) {
529 inTouch->recording = SDL_FALSE;
530 dollarNormalize(inTouch->dollarPath,path);
531 //SDL_PrintPath(path);
533 index = SDL_AddDollarGesture(NULL,path);
534 for(i = 0;i < SDL_numGestureTouches; i++)
535 SDL_gestureTouch[i].recording = SDL_FALSE;
538 index = SDL_AddDollarGesture(inTouch,path);
542 SDL_SendDollarRecord(inTouch,inTouch->dollarTemplate[index].hash);
545 SDL_SendDollarRecord(inTouch,-1);
551 error = dollarRecognize(inTouch->dollarPath,
555 unsigned long gestureId = inTouch->dollarTemplate[bestTempl].hash;
556 SDL_SendGestureDollar(inTouch,gestureId,error);
557 //printf ("%s\n",);("Dollar error: %f\n",error);
561 //inTouch->gestureLast[j] = inTouch->gestureLast[inTouch->numDownFingers];
562 if(inTouch->numDownFingers > 0) {
563 inTouch->centroid.x = (inTouch->centroid.x*(inTouch->numDownFingers+1)-
564 x)/inTouch->numDownFingers;
565 inTouch->centroid.y = (inTouch->centroid.y*(inTouch->numDownFingers+1)-
566 y)/inTouch->numDownFingers;
569 else if(event->type == SDL_FINGERMOTION) {
570 float dx = ((float)event->tfinger.dx)/(float)inTouch->res.x;
571 float dy = ((float)event->tfinger.dy)/(float)inTouch->res.y;
572 //printf("dx,dy: (%f,%f)\n",dx,dy);
574 SDL_DollarPath* path = &inTouch->dollarPath;
575 if(path->numPoints < MAXPATHSIZE) {
576 path->p[path->numPoints].x = inTouch->centroid.x;
577 path->p[path->numPoints].y = inTouch->centroid.y;
579 (path->p[path->numPoints].x-path->p[path->numPoints-1].x);
581 (path->p[path->numPoints].y-path->p[path->numPoints-1].y);
582 path->length += (float)sqrt(pathDx*pathDx + pathDy*pathDy);
588 lastCentroid = inTouch->centroid;
590 inTouch->centroid.x += dx/inTouch->numDownFingers;
591 inTouch->centroid.y += dy/inTouch->numDownFingers;
592 //printf("Centrid : (%f,%f)\n",inTouch->centroid.x,inTouch->centroid.y);
593 if(inTouch->numDownFingers > 1) {
594 SDL_FloatPoint lv; //Vector from centroid to last x,y position
595 SDL_FloatPoint v; //Vector from centroid to current x,y position
596 //lv = inTouch->gestureLast[j].cv;
597 lv.x = lastP.x - lastCentroid.x;
598 lv.y = lastP.y - lastCentroid.y;
599 lDist = (float)sqrt(lv.x*lv.x + lv.y*lv.y);
600 //printf("lDist = %f\n",lDist);
601 v.x = x - inTouch->centroid.x;
602 v.y = y - inTouch->centroid.y;
603 //inTouch->gestureLast[j].cv = v;
604 Dist = (float)sqrt(v.x*v.x+v.y*v.y);
605 // cos(dTheta) = (v . lv)/(|v| * |lv|)
607 //Normalize Vectors to simplify angle calculation
612 dtheta = (float)atan2(lv.x*v.y - lv.y*v.x,lv.x*v.x + lv.y*v.y);
614 dDist = (Dist - lDist);
615 if(lDist == 0) {dDist = 0;dtheta = 0;} //To avoid impossible values
617 //inTouch->gestureLast[j].dDist = dDist;
618 //inTouch->gestureLast[j].dtheta = dtheta;
620 //printf("dDist = %f, dTheta = %f\n",dDist,dtheta);
621 //gdtheta = gdtheta*.9 + dtheta*.1;
622 //gdDist = gdDist*.9 + dDist*.1
623 //knob.r += dDist/numDownFingers;
624 //knob.ang += dtheta;
625 //printf("thetaSum = %f, distSum = %f\n",gdtheta,gdDist);
626 //printf("id: %i dTheta = %f, dDist = %f\n",j,dtheta,dDist);
627 SDL_SendGestureMulti(inTouch,dtheta,dDist);
630 //inTouch->gestureLast[j].dDist = 0;
631 //inTouch->gestureLast[j].dtheta = 0;
632 //inTouch->gestureLast[j].cv.x = 0;
633 //inTouch->gestureLast[j].cv.y = 0;
635 //inTouch->gestureLast[j].f.p.x = x;
636 //inTouch->gestureLast[j].f.p.y = y;
641 if(event->type == SDL_FINGERDOWN) {
643 inTouch->numDownFingers++;
644 inTouch->centroid.x = (inTouch->centroid.x*(inTouch->numDownFingers - 1)+
645 x)/inTouch->numDownFingers;
646 inTouch->centroid.y = (inTouch->centroid.y*(inTouch->numDownFingers - 1)+
647 y)/inTouch->numDownFingers;
648 //printf("Finger Down: (%f,%f). Centroid: (%f,%f\n",x,y,
649 // inTouch->centroid.x,inTouch->centroid.y);
652 inTouch->dollarPath.length = 0;
653 inTouch->dollarPath.p[0].x = x;
654 inTouch->dollarPath.p[0].y = y;
655 inTouch->dollarPath.numPoints = 1;
661 /* vi: set ts=4 sw=4 expandtab: */