changeset 4920:a4032241deb5

Fixed massive stack memory usage in the gesture functions
author Sam Lantinga <slouken@libsdl.org>
date Tue, 30 Nov 2010 18:07:31 -0800
parents 716b2cbf4c9e
children 40a43c6d9220
files src/events/SDL_gesture.c
diffstat 1 files changed, 22 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/src/events/SDL_gesture.c	Tue Nov 30 17:58:51 2010 -0800
+++ b/src/events/SDL_gesture.c	Tue Nov 30 18:07:31 2010 -0800
@@ -294,7 +294,7 @@
 }
 
 //DollarPath contains raw points, plus (possibly) the calculated length
-int dollarNormalize(SDL_DollarPath path,SDL_FloatPoint *points) {
+int dollarNormalize(const SDL_DollarPath *path,SDL_FloatPoint *points) {
   int i;
   float interval;
   float dist;
@@ -303,34 +303,35 @@
   float xmin,xmax,ymin,ymax;
   float ang;
   float w,h;
+  float length = path->length;
 
   //Calculate length if it hasn't already been done
-  if(path.length <= 0) {
-    for(i=1;i<path.numPoints;i++) {
-      float dx = path.p[i  ].x - 
-                 path.p[i-1].x;
-      float dy = path.p[i  ].y - 
-                 path.p[i-1].y;
-      path.length += (float)(SDL_sqrt(dx*dx+dy*dy));
+  if(length <= 0) {
+    for(i=1;i<path->numPoints;i++) {
+      float dx = path->p[i  ].x - 
+                 path->p[i-1].x;
+      float dy = path->p[i  ].y - 
+                 path->p[i-1].y;
+      length += (float)(SDL_sqrt(dx*dx+dy*dy));
     }
   }
 
   //Resample
-  interval = path.length/(DOLLARNPOINTS - 1);
+  interval = length/(DOLLARNPOINTS - 1);
   dist = interval;
 
   centroid.x = 0;centroid.y = 0;
   
-  //printf("(%f,%f)\n",path.p[path.numPoints-1].x,path.p[path.numPoints-1].y);
-  for(i = 1;i < path.numPoints;i++) {
-    float d = (float)(SDL_sqrt((path.p[i-1].x-path.p[i].x)*(path.p[i-1].x-path.p[i].x)+
-                             (path.p[i-1].y-path.p[i].y)*(path.p[i-1].y-path.p[i].y)));
+  //printf("(%f,%f)\n",path->p[path->numPoints-1].x,path->p[path->numPoints-1].y);
+  for(i = 1;i < path->numPoints;i++) {
+    float d = (float)(SDL_sqrt((path->p[i-1].x-path->p[i].x)*(path->p[i-1].x-path->p[i].x)+
+                             (path->p[i-1].y-path->p[i].y)*(path->p[i-1].y-path->p[i].y)));
     //printf("d = %f dist = %f/%f\n",d,dist,interval);
     while(dist + d > interval) {
-      points[numPoints].x = path.p[i-1].x + 
-        ((interval-dist)/d)*(path.p[i].x-path.p[i-1].x);
-      points[numPoints].y = path.p[i-1].y + 
-        ((interval-dist)/d)*(path.p[i].y-path.p[i-1].y);
+      points[numPoints].x = path->p[i-1].x + 
+        ((interval-dist)/d)*(path->p[i].x-path->p[i-1].x);
+      points[numPoints].y = path->p[i-1].y + 
+        ((interval-dist)/d)*(path->p[i].y-path->p[i-1].y);
       centroid.x += points[numPoints].x;
       centroid.y += points[numPoints].y;
       numPoints++;
@@ -344,7 +345,7 @@
     return 0;
   }
   //copy the last point
-  points[DOLLARNPOINTS-1] = path.p[path.numPoints-1];
+  points[DOLLARNPOINTS-1] = path->p[path->numPoints-1];
   numPoints = DOLLARNPOINTS;
 
   centroid.x /= numPoints;
@@ -386,7 +387,7 @@
   return numPoints;
 }
 
-float dollarRecognize(SDL_DollarPath path,int *bestTempl,SDL_GestureTouch* touch) {
+float dollarRecognize(const SDL_DollarPath *path,int *bestTempl,SDL_GestureTouch* touch) {
         
         SDL_FloatPoint points[DOLLARNPOINTS];
         int numPoints = dollarNormalize(path,points);
@@ -528,7 +529,7 @@
 #ifdef ENABLE_DOLLAR
       if(inTouch->recording) {
         inTouch->recording = SDL_FALSE;        
-        dollarNormalize(inTouch->dollarPath,path);
+        dollarNormalize(&inTouch->dollarPath,path);
         //PrintPath(path);
         if(recordAll) {
           index = SDL_AddDollarGesture(NULL,path);
@@ -549,7 +550,7 @@
       else {        
         int bestTempl;
         float error;
-        error = dollarRecognize(inTouch->dollarPath,
+        error = dollarRecognize(&inTouch->dollarPath,
                                 &bestTempl,inTouch);
         if(bestTempl >= 0){
           //Send Event