Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 3849 in orxonox.OLD


Ignore:
Timestamp:
Apr 17, 2005, 2:23:53 AM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: taken tAnimation into a file of its own

Location:
orxonox/trunk/src
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • orxonox/trunk/src/animation.h

    r3848 r3849  
    1 /*
    2    orxonox - the future of 3D-vertical-scrollers
    3 
    4    Copyright (C) 2004 orx
    5 
    6    This program is free software; you can redistribute it and/or modify
    7    it under the terms of the GNU General Public License as published by
    8    the Free Software Foundation; either version 2, or (at your option)
    9    any later version.
    10 
    11    ### File Specific:
    12    main-programmer: Benjamin Grauer
    13    co-programmer: ...
    14 */
    15 
    16 
    171/*!
    182    \file animation.h
    19     A Set of functions to animate some floats inside of an Object
    20 
    21     We apologize, that most part of the Function-Definitions are located
    22     inside this h-file, but this must be like this because it is a template
    23     function.
     3    A Subclass for all animations in orxonox
    244*/
    255
     
    10181
    10282
    103 //! A Class to handle some animation for single floated values.
    104 template<class T> class tAnimation : public Animation
    105 {
    106  public:
    107   tAnimation(T* object = NULL, void (T::*funcToAnim)(float) = NULL);
    108   virtual ~tAnimation();
    109 
    110   virtual void rewind();
    111 
    112   void setFuncToAnim(T* object, void (T::*funcToAnim)(float));
    113   void addKeyFrame(float value, float duration, ANIM_FUNCTION animFunc = ANIM_LINEAR);
    114 
    115   virtual void tick(float time);
    116 
    117   // animation functions
    118   void setAnimFunc(ANIM_FUNCTION animFunc);
    119 
    120   float constant(float timePassed) const;
    121   float linear(float timePassed) const;
    122   float sine(float timePassed) const;
    123   float cosine(float timePassed) const;
    124   float exp(float timePassed) const;
    125   float negExp(float timePassed) const;
    126   float quadratic(float timePassed) const;
    127   float random(float timePassed) const;
    128   //  ANIM_FUNCTION animFunc;
    129   float (tAnimation<T>::*animFunc)(float) const;
    130   KeyFrameF* currentKeyFrame;
    131   KeyFrameF* nextKeyFrame;
    132   tList<KeyFrameF>* keyFrameList;
    133 
    134 
    135 
    136 
    137  private:
    138   float expFactor;
    139   T* object;
    140   void (T::*funcToAnim)(float);
    141 };
    142 
    143 
    144 
    145 /**
    146    \brief standard constructor
    147 
    148 */
    149 template<class T>
    150 tAnimation<T>::tAnimation (T* object, void (T::*funcToAnim)(float))
    151 {
    152   // create a new List
    153   this->keyFrameList = new tList<KeyFrameF>();
    154   KeyFrameF* tmpKeyFrame = new KeyFrameF;
    155   tmpKeyFrame->value = 0.0;
    156   tmpKeyFrame->duration = 1.0;
    157   keyFrameList->add(tmpKeyFrame);
    158 
    159   this->currentKeyFrame = tmpKeyFrame;
    160   this->nextKeyFrame = tmpKeyFrame;
    161 
    162   this->animFunc = &tAnimation<T>::linear;
    163 
    164   this->setFuncToAnim(object, funcToAnim);
    165 }
    166 
    167 
    168 /**
    169    \brief standard deconstructor
    170 
    171 */
    172 template<class T>
    173 tAnimation<T>::~tAnimation ()
    174 {
    175   // delete all the KeyFrames
    176   tIterator<KeyFrameF>* itKF = keyFrameList->getIterator();
    177   KeyFrameF*  enumKF = itKF->nextElement();
    178   while (enumKF)
    179     {
    180       delete enumKF;
    181       enumKF = itKF->nextElement();
    182     }
    183   delete itKF;
    184   delete this->keyFrameList;
    185 
    186 }
    187 
    188 template<class T>
    189 void tAnimation<T>::rewind(void)
    190 {
    191   this->currentKeyFrame = keyFrameList->firstElement();
    192   this->nextKeyFrame = keyFrameList->nextElement(keyFrameList->firstElement());
    193   this->localTime = 0.0;
    194 }
    195 
    196 template<class T>
    197 void tAnimation<T>::setFuncToAnim(T* object, void (T::*funcToAnim)(float))
    198 {
    199   this->baseObject = this->object = object;
    200   this->funcToAnim = funcToAnim;
    201 }
    202 
    203 template<class T>
    204 void tAnimation<T>::addKeyFrame(float value, float duration, ANIM_FUNCTION animFunc)
    205 {
    206   // some small check
    207   if (duration <= 0.0)
    208     duration = 1.0;
    209  
    210 
    211   KeyFrameF* tmpKeyFrame;
    212    
    213   if (bHasKeys)
    214     {
    215       tmpKeyFrame = new KeyFrameF;
    216       if (this->currentKeyFrame == this->nextKeyFrame)
    217         this->nextKeyFrame = tmpKeyFrame;
    218       this->keyFrameList->add(tmpKeyFrame);
    219 
    220     }
    221   else
    222     {
    223       tmpKeyFrame = this->keyFrameList->firstElement();
    224       bHasKeys = true;
    225       this->setAnimFunc(animFunc);
    226     }
    227   tmpKeyFrame->value = value;
    228   tmpKeyFrame->duration = duration;
    229   tmpKeyFrame->animFunc = animFunc;
    230 }
    231 
    232 
    233 template<class T>
    234 void tAnimation<T>::tick(float time)
    235 {
    236   if (this->bRunning)
    237     {
    238       this->localTime += time;
    239       if (localTime >= this->currentKeyFrame->duration)
    240         {
    241           // switching to the next Key-Frame
    242           this->currentKeyFrame = this->nextKeyFrame;
    243           this->localTime = 0;
    244           // checking, if we should still Play the animation
    245           if (this->currentKeyFrame == this->keyFrameList->lastElement())
    246             {
    247               switch (this->postInfinity)
    248                 {
    249                 case ANIM_INF_CONSTANT:
    250                   this->bRunning = false;
    251                   break;
    252                 case ANIM_INF_REWIND:
    253                   break;
    254                 }
    255             }
    256           this->nextKeyFrame = this->keyFrameList->nextElement(this->nextKeyFrame);
    257           printf("%p from:%f to:%f\n", this->currentKeyFrame,this->currentKeyFrame->value, this->nextKeyFrame->value);
    258           this->setAnimFunc(this->currentKeyFrame->animFunc);     
    259         }
    260      
    261       (this->object->*(funcToAnim))((this->*animFunc)(this->localTime));
    262     }
    263 }
    264 
    265 
    266 template<class T>
    267 void tAnimation<T>::setAnimFunc(ANIM_FUNCTION animFunc)
    268 {
    269   switch (animFunc)
    270     {
    271     default:
    272     case ANIM_CONSTANT:
    273       this->animFunc = &tAnimation<T>::constant;
    274       break;
    275     case ANIM_LINEAR:
    276       this->animFunc = &tAnimation<T>::linear;
    277       break;
    278     case ANIM_SINE:
    279       this->animFunc = &tAnimation<T>::sine;
    280       break;
    281     case ANIM_COSINE:
    282       this->animFunc = &tAnimation<T>::cosine;
    283       break;
    284     case ANIM_EXP:
    285       this->animFunc = &tAnimation<T>::exp;
    286       break;
    287     case ANIM_NEG_EXP:
    288       {
    289         this->animFunc = &tAnimation<T>::negExp;
    290         float d = fabs(this->currentKeyFrame->value - this->nextKeyFrame->value);
    291         expFactor =  - 1.0 / this->currentKeyFrame->duration * logf(DELTA_X);
    292         break;
    293       }
    294     case ANIM_QUADRATIC:
    295       this->animFunc = &tAnimation<T>::quadratic;
    296       break;
    297     case ANIM_RANDOM:
    298       this->animFunc = &tAnimation<T>::random;
    299       break;
    300     }
    301 }
    302 
    303 
    304 // animation functions
    305 template<class T>
    306 float tAnimation<T>::random(float timePassed) const
    307 {
    308   return (float)rand()/(float)RAND_MAX;
    309 }
    310 
    311 template<class T>
    312 float tAnimation<T>::constant(float timePassed) const
    313 {
    314   return this->currentKeyFrame->value;
    315 }
    316 
    317 template<class T>
    318 float tAnimation<T>::linear(float timePassed) const
    319 {
    320   return this->currentKeyFrame->value + (this->nextKeyFrame->value - this->currentKeyFrame->value)
    321     * (timePassed / this->currentKeyFrame->duration);
    322   //  PRINTF(0)("value is %f, %p %p\n", val, this->currentKeyFrame, this->nextKeyFrame);
    323   //  return val;
    324 }
    325 
    326 template<class T>
    327 float tAnimation<T>::sine(float timePassed) const
    328 {
    329   float d = this->currentKeyFrame->value - this->nextKeyFrame->value;
    330   float e = 0.5 * d * (1 - cos(M_PI * timePassed / this->currentKeyFrame->duration));
    331   return this->currentKeyFrame->value - e;
    332   /*
    333   return his->currentKeyFrame->value - (this->nextKeyFrame->value - this->currentKeyFrame->value)
    334     * sin(timePassed / this->currentKeyFrame->duration * M_PI);
    335   */
    336 }
    337 
    338 template<class T>
    339 float tAnimation<T>::cosine(float timePassed) const
    340 {
    341   float d = this->currentKeyFrame->value - this->nextKeyFrame->value;
    342   float e = 0.5 * d * (sin(M_PI * timePassed / this->currentKeyFrame->duration));
    343   if( timePassed > 0.5*this->currentKeyFrame->duration) e = (d - e);
    344   return this->currentKeyFrame->value - e;
    345   /*
    346   return this->currentKeyFrame->value - (this->nextKeyFrame->value - this->currentKeyFrame->value)
    347     * cos(timePassed / this->currentKeyFrame->duration * M_PI);
    348   */
    349 }
    350 
    351 template<class T>
    352 float tAnimation<T>::exp(float timePassed) const
    353 {
    354 }
    355 
    356 template<class T>
    357 float tAnimation<T>::negExp(float timePassed) const
    358 {
    359   float d = this->currentKeyFrame->value - this->nextKeyFrame->value;
    360   float e = d * (1.0 - expf(- timePassed * expFactor));
    361   return  this->currentKeyFrame->value - e;
    362 }
    363 
    364 template<class T>
    365 float tAnimation<T>::quadratic(float timePassed) const
    366 {
    367 
    368 }
    369 
    370 
    371 
    372 
    373 
    37483/**********************TEST*******************************/
    37584class aTest
  • orxonox/trunk/src/track_manager.cc

    r3847 r3849  
    2424#include "list.h"
    2525#include "text_engine.h"
    26 #include "animation.h"
     26#include "t_animation.h"
    2727
    2828#include <stdarg.h>
Note: See TracChangeset for help on using the changeset viewer.