Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 3795 in orxonox.OLD for orxonox/trunk/src/animation.h


Ignore:
Timestamp:
Apr 13, 2005, 4:32:53 PM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: moved some functions around in the Animation-classes

File:
1 edited

Legend:

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

    r3794 r3795  
    2727#define _ANIMATION_H
    2828
    29 
     29#include "list.h"
    3030// FORWARD DEFINITION
    31 template<class T> class tList;
    3231
    3332typedef enum ANIM_FUNCTION {ANIM_CONSTANT,
     
    5554  void setInfinity(ANIM_INFINITY postInfinity = ANIM_INF_CONSTANT);
    5655
    57   void addKeyFrame(float value, float duration, ANIM_FUNCTION animFunc = ANIM_LINEAR);
    58   void setAnimFunc(ANIM_FUNCTION animFunc);
    59 
    6056  void play(); // equals resume();
    6157  void stop();
    6258  void pause();
    6359  void replay();
     60  //  virtual void rewind();
    6461
    6562  virtual void tick(float time) = 0;
    6663
     64  /* implement in subclasses:
     65   *
     66   * De-/Constructor
     67   * Animation Functions
     68   * virtual tick
     69   * List of keyFrames
     70   * currentKeyFrame/nextKeyFrame
     71   * virtual rewind, to go to the first Keyframe. (other functions will call this one)
     72   */
    6773 protected:
    6874  Anim(void);
    6975
    70   static tList<Anim>* animatorList;
     76  // variables
     77
     78  float localTime;
     79  ANIM_INFINITY postInfinity;
     80
     81  bool bHasKeys;
     82  bool bRunning;
     83};
     84
     85
     86//! A Class to handle some animation for single floated values.
     87template<class T> class tAnim : public Anim
     88{
     89 public:
     90  tAnim(T* object = NULL, void (T::*funcToAnim)(float) = NULL);
     91  virtual ~tAnim();
     92
     93  void setFuncToAnim(T* object, void (T::*funcToAnim)(float));
     94  void addKeyFrame(float value, float duration, ANIM_FUNCTION animFunc = ANIM_LINEAR);
     95
    7196
    7297  // animation functions
     98  void setAnimFunc(ANIM_FUNCTION animFunc);
     99
    73100  float random(float timePassed) const;
    74101  float constant(float timePassed) const;
    75102  float linear(float timePassed) const;
    76103  float sine(float timePassed) const;
    77 
    78 
    79   // variables
    80104  //  ANIM_FUNCTION animFunc;
    81   float (Anim::*animFunc)(float) const;
    82 
    83   ANIM_INFINITY postInfinity;
    84 
    85   bool bHasKeys;
    86   bool bRunning;
    87 
    88   float localTime;
     105  float (tAnim<T>::*animFunc)(float) const;
    89106  AnimKeyFrame* currentKeyFrame;
    90107  AnimKeyFrame* nextKeyFrame;
    91108  tList<AnimKeyFrame>* keyFrameList;
    92 };
    93 
    94 
    95 //! A Class to handle some animation for single floated values.
    96 template<class T> class tAnim : public Anim
    97 {
    98  public:
    99   tAnim(T* object = NULL, void (T::*funcToAnim)(float) = NULL);
    100   virtual ~tAnim();
    101 
    102   void setFuncToAnim(T* object, void (T::*funcToAnim)(float));
     109
     110
     111
    103112
    104113  virtual void tick(float time);
     
    118127tAnim<T>::tAnim (T* object, void (T::*funcToAnim)(float))
    119128{
     129  // create a new List
     130  this->keyFrameList = new tList<AnimKeyFrame>();
     131  AnimKeyFrame* tmpKeyFrame = new AnimKeyFrame;
     132  tmpKeyFrame->value = 0.0;
     133  tmpKeyFrame->duration = 1.0;
     134  keyFrameList->add(tmpKeyFrame);
     135
     136  this->currentKeyFrame = tmpKeyFrame;
     137  this->nextKeyFrame = tmpKeyFrame;
     138
     139  this->animFunc = &tAnim<T>::linear;
     140
    120141  this->setFuncToAnim(object, funcToAnim);
    121142}
     
    129150tAnim<T>::~tAnim ()
    130151{
    131   // delete what has to be deleted here
     152  // delete all the KeyFrames
     153  tIterator<AnimKeyFrame>* itKF = keyFrameList->getIterator();
     154  AnimKeyFrame*  enumKF = itKF->nextElement();
     155  while (enumKF)
     156    {
     157      delete enumKF;
     158      enumKF = itKF->nextElement();
     159    }
     160  delete itKF;
     161  delete this->keyFrameList;
     162
    132163}
    133164
     
    138169  this->object = object;
    139170  this->funcToAnim = funcToAnim;
     171}
     172
     173template<class T>
     174void tAnim<T>::addKeyFrame(float value, float duration, ANIM_FUNCTION animFunc)
     175{
     176  // some small check
     177  if (duration <= 0.0)
     178    duration = 1.0;
     179 
     180
     181  AnimKeyFrame* tmpKeyFrame;
     182   
     183  if (bHasKeys)
     184    {
     185      tmpKeyFrame = new AnimKeyFrame;
     186      if (this->currentKeyFrame == this->nextKeyFrame)
     187        this->nextKeyFrame = tmpKeyFrame;
     188      this->keyFrameList->add(tmpKeyFrame);
     189
     190    }
     191  else
     192    {
     193      tmpKeyFrame = this->keyFrameList->firstElement();
     194      bHasKeys = true;
     195    }
     196  tmpKeyFrame->value = value;
     197  tmpKeyFrame->duration = duration;
     198  tmpKeyFrame->animFunc = animFunc;
     199 
    140200}
    141201
     
    170230}
    171231
     232
     233template<class T>
     234void tAnim<T>::setAnimFunc(ANIM_FUNCTION animFunc)
     235{
     236  switch (animFunc)
     237    {
     238    default:
     239    case ANIM_CONSTANT:
     240      this->animFunc = &tAnim<T>::constant;
     241      break;
     242    case ANIM_LINEAR:
     243      this->animFunc = &tAnim<T>::linear;
     244      break;
     245    case ANIM_RANDOM:
     246      this->animFunc = &tAnim<T>::random;
     247      break;
     248    case ANIM_SINE:
     249      this->animFunc = &tAnim<T>::sine;
     250      break;
     251    }
     252}
     253
     254
     255// animation functions
     256template<class T>
     257float tAnim<T>::random(float timePassed) const
     258{
     259  return (float)rand()/(float)RAND_MAX;
     260}
     261
     262template<class T>
     263float tAnim<T>::constant(float timePassed) const
     264{
     265  return this->currentKeyFrame->value;
     266}
     267
     268template<class T>
     269float tAnim<T>::linear(float timePassed) const
     270{
     271  return this->nextKeyFrame->value - (this->nextKeyFrame->value - this->currentKeyFrame->value)
     272    * (timePassed / this->currentKeyFrame->duration);
     273  //  PRINTF(0)("value is %f, %p %p\n", val, this->currentKeyFrame, this->nextKeyFrame);
     274  //  return val;
     275}
     276
     277template<class T>
     278float tAnim<T>::sine(float timePassed) const
     279{
     280 
     281}
     282
    172283#endif /* _ANIMATION_H */
Note: See TracChangeset for help on using the changeset viewer.