home *** CD-ROM | disk | FTP | other *** search
- #include "Animator.h"
-
- #include "log.h"
- #include "SDL.h"
- #include "Tokenizer.h"
- #include <stdlib.h>
- #include <math.h>
-
- Animator::Animator(const char* filename){
- currentAnimation = -1;
- currentFrame = 0.0f;
- lastCalcMillis = SDL_GetTicks();
- running = false;
- looping = false;
- playBackwards = false;
-
- for(int i=0;i<NUM_ANIMATIONS;i++){
- animations[i].firstKeyframe = 0;
- animations[i].lastKeyframe = 0;
- animations[i].firstLoopKeyframe = 0;
- animations[i].lastLoopKeyframe = 0;
- animations[i].millisPerKeyframe = 0;
- animations[i].numLoopKeyframes = 0;
- }
-
- File* f=new File(filename, "rt");
- if(f->isOpen()){
- readFromFile(f);
- f->close();
- log("Animator loaded: '%s'.\n", filename);
- }else{
- error("(in Animator::Animator()): Couldn't open file '%s'.\n\n", filename);
- }
-
- }
-
- Animator::Animator(const Animator& animator){
- currentAnimation = -1;
- currentFrame = 0.0f;
- lastCalcMillis = SDL_GetTicks();
- running = false;
- looping = false;
- playBackwards = false;
-
- for(int i=0;i<NUM_ANIMATIONS;i++){
- this->animations[i] = animator.animations[i];
- }
- }
-
- Animator::~Animator(){
- }
-
- void Animator::setCurrentAnimation(int animationId){
- currentAnimation = animationId;
- if(currentAnimation == -1)
- currentFrame = 0.0f;
- else
- currentFrame = (float)animations[currentAnimation].firstKeyframe;
- }
-
- int Animator::getCurrentAnimation(){
- return currentAnimation;
- }
-
- void Animator::setLooping(bool looping){
- this->looping = looping;
- }
-
- void Animator::start(){
- running = true;
- }
-
- void Animator::pause(){
- running = false;
- }
-
- void Animator::stop(){
- if(currentAnimation == -1)
- currentFrame = 0.0f;
- else
- currentFrame = (float)animations[currentAnimation].firstKeyframe;
-
- running = false;
- }
-
- void Animator::setPlayBackwards(bool newPlayBackwards){
- playBackwards = newPlayBackwards;
- }
-
- bool Animator::getPlayBackwards(){
- return playBackwards;
- }
-
- float Animator::calcCurrentFrame(){
- unsigned long currentMillis = SDL_GetTicks();
- unsigned long deltaTMillis = currentMillis - lastCalcMillis;
-
- lastCalcMillis = currentMillis;
-
- if( currentAnimation == -1 ){
- return 0.0;
- }
-
- animation_t* ca = &animations[currentAnimation];
-
- if(!running){
- return currentFrame;
- }
-
- if( !playBackwards ){
- float f = currentFrame + deltaTMillis/(float)ca->millisPerKeyframe;
- // printf("dt: %d: mpf: %d f: %f df: %f\n", deltaTMillis, ca->millisPerKeyframe, f, deltaTMillis/(float)ca->millisPerKeyframe);
-
- if(looping){
-
- if(f > ca->firstLoopKeyframe){
- if(ca->numLoopKeyframes == 0){
- currentFrame = (float)ca->firstKeyframe;
- // return currentFrame;
- }
- f = f - ca->firstLoopKeyframe;
- f = (float)fmod(f, ca->numLoopKeyframes);
- f = f + ca->firstLoopKeyframe;
- currentFrame = f;
- }else{
- currentFrame = f;
- }
-
- }else{
- if(f > ca->lastKeyframe){
- currentFrame = (float)ca->lastKeyframe;
- // return currentFrame;
- }else{
- currentFrame = f;
- }
- }
- }else{ // playing backwards
- float f = currentFrame - deltaTMillis/(float)ca->millisPerKeyframe;
- // printf("dt: %d: mpf: %d f: %f df: %f\n", deltaTMillis, ca->millisPerKeyframe, f, deltaTMillis/(float)ca->millisPerKeyframe);
-
- if(looping){
- if(f < ca->lastLoopKeyframe){ // in the loop
- if(ca->numLoopKeyframes == 0){
- currentFrame = (float)ca->firstKeyframe;
- }
- f = ca->lastLoopKeyframe - f;
- f = (float)fmod(f, ca->numLoopKeyframes);
- f = ca->lastLoopKeyframe - f;
- currentFrame = f;
- }else{
- currentFrame = f;
- }
-
- }else{
- if(f < ca->firstKeyframe){
- currentFrame = (float)ca->firstKeyframe;
- // return currentFrame;
- }else{
- currentFrame = f;
- }
- }
- }
-
- return currentFrame;
- }
-
- void Animator::setCurrentFrame(float newCurrentFrame){
- this->currentFrame = newCurrentFrame;
- }
-
- float Animator::getCurrentFrame(){
- return currentFrame;
- }
-
- bool Animator::readFromFile(File* f){
- char buff[256];
- Tokenizer t(" =\t\n\r\"", "\"");
-
- while(f->readLine(256, buff, true) != -1){
-
- t.tokenize(buff);
-
- if(t.tokc == 0)
- continue;
-
- if(t.tokc != 6){
- warn("(in Animator::readFromFile()): Wrong number of tokens in line %i (needed 6 but read %i).\n\n", f->line, t.tokc);
- continue;
- }
-
- int animationId = getAnimationIdByName(t.tokv[0]);
- if(animationId == -1){
- warn("(in Animator::readFromFile()): Unknown animation: '%s'. Ignoring\n\n", t.tokv[0]);
- continue;
- }
-
- animations[animationId].firstKeyframe = atoi(t.tokv[1]);
- animations[animationId].lastKeyframe = atoi(t.tokv[2]);
- animations[animationId].firstLoopKeyframe = atoi(t.tokv[3]);
- animations[animationId].lastLoopKeyframe = atoi(t.tokv[4]);
- animations[animationId].millisPerKeyframe = atoi(t.tokv[5]);
-
- animations[animationId].numLoopKeyframes = animations[animationId].lastLoopKeyframe - animations[animationId].firstLoopKeyframe;
- }
-
- return true;
- }
-
-
- int Animator::getAnimationIdByName(const char* name){
- if(streq(name, "LEGS_FORWARD")){
- return ANIMATION_LEGS_FORWARD;
- }else if(streq(name, "LEGS_BACKWARD")){
- return ANIMATION_LEGS_BACKWARD;
- }else if(streq(name, "WEAPON_RELOAD")){
- return ANIMATION_WEAPON_RELOAD;
- }else{
- return -1;
- }
- }
-