home *** CD-ROM | disk | FTP | other *** search
/ Enter 2005 March / ENTER.ISO / files / fwp-0.0.6-win32-installer.exe / Animator.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2005-01-01  |  5.2 KB  |  221 lines

  1. #include "Animator.h"
  2.  
  3. #include "log.h"
  4. #include "SDL.h"
  5. #include "Tokenizer.h"
  6. #include <stdlib.h>
  7. #include <math.h>
  8.  
  9. Animator::Animator(const char* filename){
  10.     currentAnimation = -1;
  11.     currentFrame = 0.0f;
  12.     lastCalcMillis = SDL_GetTicks();
  13.     running = false;
  14.     looping = false;
  15.     playBackwards = false;
  16.  
  17.     for(int i=0;i<NUM_ANIMATIONS;i++){
  18.         animations[i].firstKeyframe = 0;
  19.         animations[i].lastKeyframe = 0;
  20.         animations[i].firstLoopKeyframe = 0;
  21.         animations[i].lastLoopKeyframe = 0;
  22.         animations[i].millisPerKeyframe = 0;
  23.         animations[i].numLoopKeyframes = 0;
  24.     }
  25.  
  26.     File* f=new File(filename, "rt");
  27.     if(f->isOpen()){
  28.         readFromFile(f);
  29.         f->close();
  30.         log("Animator loaded: '%s'.\n", filename);
  31.     }else{
  32.         error("(in Animator::Animator()): Couldn't open file '%s'.\n\n", filename);
  33.     }
  34.  
  35. }
  36.  
  37. Animator::Animator(const Animator& animator){
  38.     currentAnimation = -1;
  39.     currentFrame = 0.0f;
  40.     lastCalcMillis = SDL_GetTicks();
  41.     running = false;
  42.     looping = false;
  43.     playBackwards = false;
  44.  
  45.     for(int i=0;i<NUM_ANIMATIONS;i++){
  46.         this->animations[i] = animator.animations[i];
  47.     }
  48. }
  49.  
  50. Animator::~Animator(){
  51. }
  52.  
  53. void Animator::setCurrentAnimation(int animationId){
  54.     currentAnimation = animationId;
  55.     if(currentAnimation == -1)
  56.         currentFrame = 0.0f;
  57.     else
  58.         currentFrame = (float)animations[currentAnimation].firstKeyframe;
  59. }
  60.  
  61. int Animator::getCurrentAnimation(){
  62.     return currentAnimation;
  63. }
  64.  
  65. void Animator::setLooping(bool looping){
  66.     this->looping = looping;
  67. }
  68.  
  69. void Animator::start(){
  70.     running = true;
  71. }
  72.  
  73. void Animator::pause(){
  74.     running = false;
  75. }
  76.  
  77. void Animator::stop(){
  78.     if(currentAnimation == -1)
  79.         currentFrame = 0.0f;
  80.     else
  81.         currentFrame = (float)animations[currentAnimation].firstKeyframe;
  82.     
  83.     running = false;
  84. }
  85.  
  86. void Animator::setPlayBackwards(bool newPlayBackwards){
  87.     playBackwards = newPlayBackwards;
  88. }
  89.  
  90. bool Animator::getPlayBackwards(){
  91.     return playBackwards;
  92. }
  93.  
  94. float Animator::calcCurrentFrame(){
  95.     unsigned long currentMillis = SDL_GetTicks();
  96.     unsigned long deltaTMillis = currentMillis - lastCalcMillis;
  97.  
  98.     lastCalcMillis = currentMillis;
  99.  
  100.     if( currentAnimation == -1 ){
  101.         return 0.0;
  102.     }
  103.  
  104.     animation_t* ca = &animations[currentAnimation];
  105.  
  106.     if(!running){
  107.         return currentFrame;
  108.     }
  109.  
  110.     if( !playBackwards ){
  111.         float f = currentFrame + deltaTMillis/(float)ca->millisPerKeyframe;
  112. //    printf("dt: %d:  mpf: %d  f: %f   df: %f\n", deltaTMillis, ca->millisPerKeyframe, f, deltaTMillis/(float)ca->millisPerKeyframe);
  113.  
  114.         if(looping){
  115.  
  116.             if(f > ca->firstLoopKeyframe){
  117.                 if(ca->numLoopKeyframes == 0){
  118.                     currentFrame = (float)ca->firstKeyframe;
  119. //                    return currentFrame;
  120.                 }
  121.                 f = f - ca->firstLoopKeyframe;
  122.                 f = (float)fmod(f, ca->numLoopKeyframes);
  123.                 f = f + ca->firstLoopKeyframe;
  124.                 currentFrame = f;
  125.             }else{
  126.                 currentFrame = f;
  127.             }
  128.  
  129.         }else{
  130.             if(f > ca->lastKeyframe){
  131.                 currentFrame = (float)ca->lastKeyframe;
  132. //                return currentFrame;
  133.             }else{
  134.                 currentFrame = f;
  135.             }
  136.         }
  137.     }else{        // playing backwards
  138.         float f = currentFrame - deltaTMillis/(float)ca->millisPerKeyframe;
  139. //    printf("dt: %d:  mpf: %d  f: %f   df: %f\n", deltaTMillis, ca->millisPerKeyframe, f, deltaTMillis/(float)ca->millisPerKeyframe);
  140.  
  141.         if(looping){
  142.             if(f < ca->lastLoopKeyframe){    // in the loop
  143.                 if(ca->numLoopKeyframes == 0){
  144.                     currentFrame = (float)ca->firstKeyframe;
  145.                 }
  146.                 f = ca->lastLoopKeyframe - f;
  147.                 f = (float)fmod(f, ca->numLoopKeyframes);
  148.                 f = ca->lastLoopKeyframe - f;
  149.                 currentFrame = f;
  150.             }else{
  151.                 currentFrame = f;
  152.             }
  153.  
  154.         }else{
  155.             if(f < ca->firstKeyframe){
  156.                 currentFrame = (float)ca->firstKeyframe;
  157. //                return currentFrame;
  158.             }else{
  159.                 currentFrame = f;
  160.             }
  161.         }
  162.     }
  163.  
  164.     return currentFrame;
  165. }
  166.  
  167. void Animator::setCurrentFrame(float newCurrentFrame){
  168.     this->currentFrame = newCurrentFrame;
  169. }
  170.  
  171. float Animator::getCurrentFrame(){
  172.     return currentFrame;
  173. }
  174.  
  175. bool Animator::readFromFile(File* f){
  176.     char buff[256];
  177.     Tokenizer t(" =\t\n\r\"", "\"");
  178.  
  179.     while(f->readLine(256, buff, true) != -1){
  180.         
  181.         t.tokenize(buff);
  182.  
  183.         if(t.tokc == 0)
  184.             continue;
  185.  
  186.         if(t.tokc != 6){
  187.             warn("(in Animator::readFromFile()): Wrong number of tokens in line %i (needed 6 but read %i).\n\n", f->line, t.tokc);
  188.             continue;
  189.         }
  190.  
  191.         int animationId = getAnimationIdByName(t.tokv[0]);
  192.         if(animationId == -1){
  193.             warn("(in Animator::readFromFile()): Unknown animation: '%s'. Ignoring\n\n", t.tokv[0]);
  194.             continue;
  195.         }
  196.  
  197.         animations[animationId].firstKeyframe = atoi(t.tokv[1]);
  198.         animations[animationId].lastKeyframe = atoi(t.tokv[2]);
  199.         animations[animationId].firstLoopKeyframe = atoi(t.tokv[3]);
  200.         animations[animationId].lastLoopKeyframe = atoi(t.tokv[4]);
  201.         animations[animationId].millisPerKeyframe = atoi(t.tokv[5]);
  202.  
  203.         animations[animationId].numLoopKeyframes = animations[animationId].lastLoopKeyframe - animations[animationId].firstLoopKeyframe;
  204.     }
  205.  
  206.     return true;
  207. }
  208.  
  209.  
  210. int Animator::getAnimationIdByName(const char* name){
  211.     if(streq(name, "LEGS_FORWARD")){
  212.         return ANIMATION_LEGS_FORWARD;
  213.     }else if(streq(name, "LEGS_BACKWARD")){
  214.         return ANIMATION_LEGS_BACKWARD;
  215.     }else if(streq(name, "WEAPON_RELOAD")){
  216.         return ANIMATION_WEAPON_RELOAD;
  217.     }else{
  218.         return -1;
  219.     }
  220. }
  221.