Bug fixes, now using RWops instead of File pointers.
authorJim Grandpre
Fri, 16 Jul 2010 20:48:43 -0400
changeset 4664317a151b79ad
parent 4662 3c4e0130c9b1
child 4665 c2493813a2f4
Bug fixes, now using RWops instead of File pointers.
include/SDL_gesture.h
src/events/SDL_gesture.c
touchTest/gestureSDLTest.c
     1.1 --- a/include/SDL_gesture.h	Tue Jul 13 18:31:09 2010 -0400
     1.2 +++ b/include/SDL_gesture.h	Fri Jul 16 20:48:43 2010 -0400
     1.3 @@ -57,7 +57,7 @@
     1.4   *
     1.5   *
     1.6   */
     1.7 -  extern DECLSPEC int SDLCALL SDL_SaveAllDollarTemplates(FILE *fp);
     1.8 +  extern DECLSPEC int SDLCALL SDL_SaveAllDollarTemplates(SDL_RWops *src);
     1.9  
    1.10  /**
    1.11   *  \brief Save a currently loaded Dollar Gesture template
    1.12 @@ -65,7 +65,7 @@
    1.13   *
    1.14   */
    1.15    extern DECLSPEC int 
    1.16 -  SDLCALL SDL_SaveDollarTemplate(unsigned long gestureId,FILE *fp);
    1.17 +  SDLCALL SDL_SaveDollarTemplate(unsigned long gestureId,SDL_RWops *src);
    1.18  
    1.19  
    1.20  /**
    1.21 @@ -73,7 +73,7 @@
    1.22   *
    1.23   *
    1.24   */
    1.25 -  extern DECLSPEC int SDLCALL SDL_LoadDollarTemplates(int touchId, FILE *fp);
    1.26 +  extern DECLSPEC int SDLCALL SDL_LoadDollarTemplates(int touchId, SDL_RWops *src);
    1.27  
    1.28  
    1.29  
     2.1 --- a/src/events/SDL_gesture.c	Tue Jul 13 18:31:09 2010 -0400
     2.2 +++ b/src/events/SDL_gesture.c	Fri Jul 16 20:48:43 2010 -0400
     2.3 @@ -110,40 +110,50 @@
     2.4    return hash;
     2.5  }
     2.6  
     2.7 -int SaveTemplate(DollarTemplate *templ, FILE *fp) {
     2.8 +int SaveTemplate(DollarTemplate *templ, SDL_RWops * src) {
     2.9 +  if(src == NULL) return 0;
    2.10    int i;
    2.11 -  fprintf(fp,"%lu ",templ->hash);
    2.12 +  
    2.13 +  //No Longer storing the Hash, rehash on load
    2.14 +  //fprintf(fp,"%lu ",templ->hash);
    2.15 +  //if(SDL_RWops.write(src,&(templ->hash),sizeof(templ->hash),1) != 1) return 0;
    2.16 +  
    2.17 +  /*
    2.18    for(i = 0;i < DOLLARNPOINTS;i++) {
    2.19      fprintf(fp,"%i %i ",(int)templ->path[i].x,(int)templ->path[i].y);
    2.20    }
    2.21    fprintf(fp,"\n");
    2.22 +  */
    2.23 +  if(SDL_RWwrite(src,templ->path,sizeof(templ->path[0]),DOLLARNPOINTS) != DOLLARNPOINTS) return 0;
    2.24 +  return 1;
    2.25  }
    2.26  
    2.27  
    2.28 -int SDL_SaveAllDollarTemplates(FILE *fp) {  
    2.29 +int SDL_SaveAllDollarTemplates(SDL_RWops *src) {  
    2.30    int i,j,rtrn = 0;
    2.31    for(i = 0; i < numGestureTouches; i++) {
    2.32      GestureTouch* touch = &gestureTouch[i];
    2.33      for(j = 0;j < touch->numDollarTemplates; j++) {
    2.34 -	rtrn += SaveTemplate(&touch->dollarTemplate[i],fp);
    2.35 +	rtrn += SaveTemplate(&touch->dollarTemplate[i],src);
    2.36      }
    2.37    }
    2.38    return rtrn;  
    2.39  }
    2.40  
    2.41 -int SDL_SaveDollarTemplate(unsigned long gestureId, FILE *fp) {
    2.42 +int SDL_SaveDollarTemplate(unsigned long gestureId, SDL_RWops *src) {
    2.43    int i,j;
    2.44    for(i = 0; i < numGestureTouches; i++) {
    2.45      GestureTouch* touch = &gestureTouch[i];
    2.46      for(j = 0;j < touch->numDollarTemplates; j++) {
    2.47        if(touch->dollarTemplate[i].hash == gestureId) {
    2.48 -	return SaveTemplate(&touch->dollarTemplate[i],fp);
    2.49 +	return SaveTemplate(&touch->dollarTemplate[i],src);
    2.50        }
    2.51      }
    2.52    }
    2.53  }
    2.54  
    2.55 -int SDL_LoadDollarTemplates(int touchId, FILE *fp) {
    2.56 +int SDL_LoadDollarTemplates(int touchId, SDL_RWops *src) {
    2.57 +  if(src == NULL) return 0;
    2.58    int i,loaded = 0;
    2.59    GestureTouch *touch = NULL;
    2.60    if(touchId >= 0) {
    2.61 @@ -153,9 +163,10 @@
    2.62      if(touch == NULL) return -1;
    2.63    }
    2.64  
    2.65 -  while(!feof(fp)) {
    2.66 +  while(1) {
    2.67      DollarTemplate templ;
    2.68 -    fscanf(fp,"%lu ",&templ.hash);
    2.69 +    //fscanf(fp,"%lu ",&templ.hash);
    2.70 +    /*
    2.71      for(i = 0;i < DOLLARNPOINTS; i++) {		
    2.72        int x,y;
    2.73        if(fscanf(fp,"%i %i ",&x,&y) != 2) break;
    2.74 @@ -163,22 +174,26 @@
    2.75        templ.path[i].y = y;
    2.76      }
    2.77      fscanf(fp,"\n");
    2.78 +    */
    2.79 +    if(SDL_RWread(src,templ.path,sizeof(templ.path[0]),DOLLARNPOINTS) < DOLLARNPOINTS) break;
    2.80  
    2.81      if(touchId >= 0) {
    2.82 -      if(SDL_AddDollarGesture(touch,templ)) loaded++;
    2.83 +      printf("Adding loaded gesture to 1 touch\n");
    2.84 +      if(SDL_AddDollarGesture(touch,templ.path)) loaded++;
    2.85      }
    2.86      else {
    2.87 +      printf("Adding to: %i touches\n",numGestureTouches);
    2.88        for(i = 0;i < numGestureTouches; i++) {
    2.89 -	if(gestureTouch[i].id == touchId) {
    2.90 -	  touch = &gestureTouch[i];
    2.91 -	  SDL_AddDollarGesture(touch,templ);
    2.92 -	}
    2.93 +	touch = &gestureTouch[i];
    2.94 +	printf("Adding loaded gesture to + touches\n");
    2.95 +	//TODO: What if this fails?
    2.96 +	SDL_AddDollarGesture(touch,templ.path);	
    2.97        }
    2.98        loaded++;
    2.99      }
   2.100    }
   2.101  
   2.102 -  return 1; 
   2.103 +  return loaded; 
   2.104  }
   2.105  
   2.106  
     3.1 --- a/touchTest/gestureSDLTest.c	Tue Jul 13 18:31:09 2010 -0400
     3.2 +++ b/touchTest/gestureSDLTest.c	Fri Jul 16 20:48:43 2010 -0400
     3.3 @@ -305,16 +305,21 @@
     3.4  	      SDL_RecordGesture(-1);
     3.5  	    }
     3.6  	    else if(event.key.keysym.sym == 115) {
     3.7 -	      FILE *fp;
     3.8 -	      fp = fopen("gestureSave","w");
     3.9 -	      SDL_SaveAllDollarTemplates(fp);
    3.10 -	      fclose(fp);
    3.11 +	      SDL_RWops *src;
    3.12 +	      //fp = fopen("gestureSave","w");
    3.13 +	      src = SDL_RWFromFile("gestureSave","w");
    3.14 +	      
    3.15 +	      printf("Wrote %i templates\n",SDL_SaveAllDollarTemplates(src));
    3.16 +	      //fclose(fp);
    3.17 +	      SDL_RWclose(src);
    3.18  	    }
    3.19  	    else if(event.key.keysym.sym == 108) {
    3.20 -	      FILE *fp;
    3.21 -	      fp = fopen("gestureSave","r");
    3.22 -	      printf("Loaded: %i\n",SDL_LoadDollarTemplates(-1,fp));
    3.23 -	      fclose(fp);
    3.24 +	      SDL_RWops *src;
    3.25 +	      //fp = fopen("gestureSave","r");
    3.26 +	      src = SDL_RWFromFile("gestureSave","r");
    3.27 +	      printf("Loaded: %i\n",SDL_LoadDollarTemplates(-1,src));
    3.28 +	      //fclose(fp);
    3.29 +	      SDL_RWclose(src);
    3.30  	    }
    3.31  	    
    3.32  	    //keypress = 1;