Advertisement
Le_BuG63

Untitled

Feb 15th, 2015
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.00 KB | None | 0 0
  1. #include "animation.h"
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <SDL\SDL.h>
  6.  
  7. #include "memory.h"
  8.  
  9. int
  10. Anim_load(const char *filePath, ANIMATION *anim) {
  11.     if (filePath != NULL) {
  12.         FILE *file = NULL;
  13.  
  14.         file = fopen(filePath, "r");
  15.  
  16.         if (file != NULL) {
  17.             fscanf(file, "%u %d\n", &anim->delay, &anim->totalFrames);
  18.  
  19.             anim->frames = xmalloc(anim->totalFrames * sizeof(int));
  20.             anim->actFrame = 0;
  21.  
  22.             anim->time.act = 0;
  23.             anim->time.old = 0;
  24.  
  25.             for (int frame = 0; frame < anim->totalFrames; ++frame) {
  26.                 fscanf(file, "%d", &anim->frames[frame]);
  27.             }
  28.             return 0;
  29.         }
  30.     }
  31.     return 1;
  32. }
  33.  
  34. void
  35. Anim_free(ANIMATION *anim) {
  36.     xfree(anim->frames);
  37. }
  38.  
  39. void
  40. Anim_play(ANIMATION *anim, int *entityState) {
  41.     anim->time.act = SDL_GetTicks();
  42.  
  43.     if ((anim->time.act - anim->time.old) > anim->delay) {
  44.         *entityState = anim->frames[anim->actFrame++];
  45.  
  46.         anim->time.old = anim->time.act++;
  47.  
  48.         if (anim->actFrame >= anim->totalFrames) {
  49.             anim->actFrame = 0;
  50.         }
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement