src/events/SDL_gesture.c
changeset 4665 c2493813a2f4
parent 4664 317a151b79ad
parent 4663 56a2d70de945
child 4678 f8431f66613d
     1.1 --- a/src/events/SDL_gesture.c	Fri Jul 16 20:48:43 2010 -0400
     1.2 +++ b/src/events/SDL_gesture.c	Fri Jul 16 20:53:44 2010 -0400
     1.3 @@ -110,8 +110,10 @@
     1.4    return hash;
     1.5  }
     1.6  
     1.7 -int SaveTemplate(DollarTemplate *templ, SDL_RWops * src) {
     1.8 +
     1.9 +static int SaveTemplate(DollarTemplate *templ, SDL_RWops * src) {
    1.10    if(src == NULL) return 0;
    1.11 +
    1.12    int i;
    1.13    
    1.14    //No Longer storing the Hash, rehash on load
    1.15 @@ -123,6 +125,7 @@
    1.16      fprintf(fp,"%i %i ",(int)templ->path[i].x,(int)templ->path[i].y);
    1.17    }
    1.18    fprintf(fp,"\n");
    1.19 +
    1.20    */
    1.21    if(SDL_RWwrite(src,templ->path,sizeof(templ->path[0]),DOLLARNPOINTS) != DOLLARNPOINTS) return 0;
    1.22    return 1;
    1.23 @@ -150,6 +153,36 @@
    1.24        }
    1.25      }
    1.26    }
    1.27 +  SDL_SetError("Unknown gestureId");
    1.28 +  return -1;
    1.29 +}
    1.30 +
    1.31 +//path is an already sampled set of points
    1.32 +//Returns the index of the gesture on success, or -1
    1.33 +static int SDL_AddDollarGesture(GestureTouch* inTouch,Point* path) {
    1.34 +  if(inTouch == NULL) {
    1.35 +    if(numGestureTouches == 0) return -1;
    1.36 +    int i = 0;
    1.37 +    for(i = 0;i < numGestureTouches; i++) {
    1.38 +      inTouch = &gestureTouch[i];
    1.39 +      if(inTouch->numDollarTemplates < MAXTEMPLATES) {
    1.40 +	DollarTemplate *templ = 
    1.41 +	  &inTouch->dollarTemplate[inTouch->numDollarTemplates];
    1.42 +	memcpy(templ->path,path,DOLLARNPOINTS*sizeof(Point));
    1.43 +	templ->hash = SDL_HashDollar(templ->path);
    1.44 +	inTouch->numDollarTemplates++;
    1.45 +      }
    1.46 +    }
    1.47 +    return inTouch->numDollarTemplates - 1;
    1.48 +  }else if(inTouch->numDollarTemplates < MAXTEMPLATES) {
    1.49 +    DollarTemplate *templ = 
    1.50 +      &inTouch->dollarTemplate[inTouch->numDollarTemplates];
    1.51 +    memcpy(templ->path,path,DOLLARNPOINTS*sizeof(Point));
    1.52 +    templ->hash = SDL_HashDollar(templ->path);
    1.53 +    inTouch->numDollarTemplates++;
    1.54 +    return inTouch->numDollarTemplates - 1;
    1.55 +  }
    1.56 +  return -1;
    1.57  }
    1.58  
    1.59  int SDL_LoadDollarTemplates(int touchId, SDL_RWops *src) {
    1.60 @@ -197,37 +230,6 @@
    1.61  }
    1.62  
    1.63  
    1.64 -//path is an already sampled set of points
    1.65 -//Returns the index of the gesture on success, or -1
    1.66 -int SDL_AddDollarGesture(GestureTouch* inTouch,Point* path) {
    1.67 -  if(inTouch == NULL) {
    1.68 -    if(numGestureTouches == 0) return -1;
    1.69 -    int i = 0;
    1.70 -    for(i = 0;i < numGestureTouches; i++) {
    1.71 -      inTouch = &gestureTouch[i];
    1.72 -      if(inTouch->numDollarTemplates < MAXTEMPLATES) {
    1.73 -	DollarTemplate *templ = 
    1.74 -	  &inTouch->dollarTemplate[inTouch->numDollarTemplates];
    1.75 -	memcpy(templ->path,path,DOLLARNPOINTS*sizeof(Point));
    1.76 -	templ->hash = SDL_HashDollar(templ->path);
    1.77 -	inTouch->numDollarTemplates++;
    1.78 -      }
    1.79 -    }
    1.80 -    return inTouch->numDollarTemplates - 1;
    1.81 -  }else if(inTouch->numDollarTemplates < MAXTEMPLATES) {
    1.82 -    DollarTemplate *templ = 
    1.83 -      &inTouch->dollarTemplate[inTouch->numDollarTemplates];
    1.84 -    memcpy(templ->path,path,DOLLARNPOINTS*sizeof(Point));
    1.85 -    templ->hash = SDL_HashDollar(templ->path);
    1.86 -    inTouch->numDollarTemplates++;
    1.87 -    return inTouch->numDollarTemplates - 1;
    1.88 -  }
    1.89 -  return -1;
    1.90 -}
    1.91 -
    1.92 -
    1.93 -
    1.94 -
    1.95  float dollarDifference(Point* points,Point* templ,float ang) {
    1.96    //  Point p[DOLLARNPOINTS];
    1.97    float dist = 0;
    1.98 @@ -279,21 +281,6 @@
    1.99    return SDL_min(f1,f2);  
   1.100  }
   1.101  
   1.102 -float dollarRecognize(DollarPath path,int *bestTempl,GestureTouch* touch) {
   1.103 -
   1.104 -  Point points[DOLLARNPOINTS];
   1.105 -  int numPoints = dollarNormalize(path,points);
   1.106 -  int i;
   1.107 - 
   1.108 -  int bestDiff = 10000;
   1.109 -  *bestTempl = -1;
   1.110 -  for(i = 0;i < touch->numDollarTemplates;i++) {
   1.111 -    int diff = bestDollarDifference(points,touch->dollarTemplate[i].path);
   1.112 -    if(diff < bestDiff) {bestDiff = diff; *bestTempl = i;}
   1.113 -  }
   1.114 -  return bestDiff;
   1.115 -}
   1.116 -
   1.117  //DollarPath contains raw points, plus (possibly) the calculated length
   1.118  int dollarNormalize(DollarPath path,Point *points) {
   1.119    int i;
   1.120 @@ -374,6 +361,21 @@
   1.121    return numPoints;
   1.122  }
   1.123  
   1.124 +float dollarRecognize(DollarPath path,int *bestTempl,GestureTouch* touch) {
   1.125 +	
   1.126 +	Point points[DOLLARNPOINTS];
   1.127 +	int numPoints = dollarNormalize(path,points);
   1.128 +	int i;
   1.129 +	
   1.130 +	int bestDiff = 10000;
   1.131 +	*bestTempl = -1;
   1.132 +	for(i = 0;i < touch->numDollarTemplates;i++) {
   1.133 +		int diff = bestDollarDifference(points,touch->dollarTemplate[i].path);
   1.134 +		if(diff < bestDiff) {bestDiff = diff; *bestTempl = i;}
   1.135 +	}
   1.136 +	return bestDiff;
   1.137 +}
   1.138 +
   1.139  int SDL_GestureAddTouch(SDL_Touch* touch) { 
   1.140    if(numGestureTouches >= MAXTOUCHES) return -1;
   1.141