Refactor SDL_AddDollarGesture
authorKees Bakker <kees@ijzerbout.nl>
Thu, 27 Oct 2011 22:52:03 +0200
changeset 60679a8faef6fdbe
parent 6066 1d66a8dce3ef
child 6068 c0a928f55a19
Refactor SDL_AddDollarGesture
src/events/SDL_gesture.c
     1.1 --- a/src/events/SDL_gesture.c	Thu Oct 27 22:23:09 2011 +0200
     1.2 +++ b/src/events/SDL_gesture.c	Thu Oct 27 22:52:03 2011 +0200
     1.3 @@ -162,52 +162,47 @@
     1.4  
     1.5  //path is an already sampled set of points
     1.6  //Returns the index of the gesture on success, or -1
     1.7 -static int SDL_AddDollarGesture(SDL_GestureTouch* inTouch,SDL_FloatPoint* path)
     1.8 +static int SDL_AddDollarGesture_one(SDL_GestureTouch* inTouch, SDL_FloatPoint* path)
     1.9  {
    1.10      SDL_DollarTemplate* dollarTemplate;
    1.11      SDL_DollarTemplate *templ;
    1.12 +    int index;
    1.13 +
    1.14 +    index = inTouch->numDollarTemplates;
    1.15 +    dollarTemplate =
    1.16 +        (SDL_DollarTemplate *)SDL_realloc(inTouch->dollarTemplate,
    1.17 +                                          (index + 1) *
    1.18 +                                          sizeof(SDL_DollarTemplate));
    1.19 +    if (!dollarTemplate) {
    1.20 +        SDL_OutOfMemory();
    1.21 +        return -1;
    1.22 +    }
    1.23 +    inTouch->dollarTemplate = dollarTemplate;
    1.24 +
    1.25 +    templ = &inTouch->dollarTemplate[index];
    1.26 +    SDL_memcpy(templ->path, path, DOLLARNPOINTS*sizeof(SDL_FloatPoint));
    1.27 +    templ->hash = SDL_HashDollar(templ->path);
    1.28 +    inTouch->numDollarTemplates++;
    1.29 +
    1.30 +    return index;
    1.31 +}
    1.32 +
    1.33 +static int SDL_AddDollarGesture(SDL_GestureTouch* inTouch, SDL_FloatPoint* path)
    1.34 +{
    1.35 +    int index;
    1.36      int i = 0;
    1.37      if (inTouch == NULL) {
    1.38          if (SDL_numGestureTouches == 0) return -1;
    1.39          for (i = 0; i < SDL_numGestureTouches; i++) {
    1.40              inTouch = &SDL_gestureTouch[i];
    1.41 -
    1.42 -            dollarTemplate =
    1.43 -                (SDL_DollarTemplate *)SDL_realloc(inTouch->dollarTemplate,
    1.44 -                                                  (inTouch->numDollarTemplates + 1) *
    1.45 -                                                  sizeof(SDL_DollarTemplate));
    1.46 -            if (!dollarTemplate) {
    1.47 -                SDL_OutOfMemory();
    1.48 +            index = SDL_AddDollarGesture_one(inTouch, path);
    1.49 +            if (index < 0)
    1.50                  return -1;
    1.51 -            }
    1.52 -
    1.53 -            inTouch->dollarTemplate = dollarTemplate;
    1.54 -
    1.55 -            templ =
    1.56 -                &inTouch->dollarTemplate[inTouch->numDollarTemplates];
    1.57 -            SDL_memcpy(templ->path,path,DOLLARNPOINTS*sizeof(SDL_FloatPoint));
    1.58 -            templ->hash = SDL_HashDollar(templ->path);
    1.59 -            inTouch->numDollarTemplates++;
    1.60          }
    1.61 -        return inTouch->numDollarTemplates - 1;
    1.62 +        // Use the index of the last one added.
    1.63 +        return index;
    1.64      } else {
    1.65 -        SDL_DollarTemplate* dollarTemplate =
    1.66 -            ( SDL_DollarTemplate *)SDL_realloc(inTouch->dollarTemplate,
    1.67 -                                               (inTouch->numDollarTemplates + 1) *
    1.68 -                                               sizeof(SDL_DollarTemplate));
    1.69 -        if (!dollarTemplate) {
    1.70 -            SDL_OutOfMemory();
    1.71 -            return -1;
    1.72 -        }
    1.73 -
    1.74 -        inTouch->dollarTemplate = dollarTemplate;
    1.75 -
    1.76 -        templ =
    1.77 -            &inTouch->dollarTemplate[inTouch->numDollarTemplates];
    1.78 -        SDL_memcpy(templ->path,path,DOLLARNPOINTS*sizeof(SDL_FloatPoint));
    1.79 -        templ->hash = SDL_HashDollar(templ->path);
    1.80 -        inTouch->numDollarTemplates++;
    1.81 -        return inTouch->numDollarTemplates - 1;
    1.82 +        return SDL_AddDollarGesture_one(inTouch, path);
    1.83      }
    1.84      return -1;
    1.85  }