Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 10618 in orxonox.OLD for trunk/src/util/animation/t_animation.h


Ignore:
Timestamp:
Apr 4, 2007, 12:13:53 PM (17 years ago)
Author:
bknecht
Message:

merged cleanup into trunk (only improvements)

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

    • Property svn:ignore
      •  

        old new  
        1616OrxonoxPlayability.kdevses
        1717OrxonoxPlayability.kdevelop.pcs
         18orxonox.backtrace
         19orxonox.kdevses
         20orxonox.kdevelop.pcs
  • trunk/src/util/animation/t_animation.h

    r8315 r10618  
    11/*
    22   orxonox - the future of 3D-vertical-scrollers
    3 
     3 
    44   Copyright (C) 2004 orx
    5 
     5 
    66   This program is free software; you can redistribute it and/or modify
    77   it under the terms of the GNU General Public License as published by
    88   the Free Software Foundation; either version 2, or (at your option)
    99   any later version.
    10 
     10 
    1111   ### File Specific:
    1212   main-programmer: Benjamin Grauer
     
    3737template<class T> class tAnimation : public Animation
    3838{
    39  public:
     39public:
    4040  tAnimation(T* object = NULL, void (T::*funcToAnim)(float) = NULL);
    41   virtual ~tAnimation();
    4241
    4342  void setFuncToAnim(T* object, void (T::*funcToAnim)(float));
     
    4847  virtual void tick(float dt);
    4948
    50  private:
     49private:
    5150  // animation functions
    5251  void setAnimFunc(ANIM_FUNCTION animFunc);
     
    6160
    6261
     62private:
     63  typedef std::list<KeyFrameF> KeyFrameList;
     64  typedef typename KeyFrameList::iterator KeyFrameIterator;
     65
    6366  //  ANIM_FUNCTION animFunc
    6467  float (tAnimation<T>::*animFunc)(float) const;  //!< A Function for the AnimationType
    6568
    66   KeyFrameF* currentKeyFrame;                     //!< The current KeyFrame
    67   KeyFrameF* nextKeyFrame;                        //!< The KeyFrame we iterate to
    68   tList<KeyFrameF>* keyFrameList;                 //!< The KeyFrameList
     69  KeyFrameIterator  currentKeyFrame;                     //!< The current KeyFrame
     70  KeyFrameIterator  nextKeyFrame;                        //!< The KeyFrame we iterate to
     71  KeyFrameList      keyFrameList;                 //!< The KeyFrameList
    6972
    7073  T* object;                                      //!< The Object from which to Animate something
     
    8386{
    8487  // create a new List
    85   this->keyFrameList = new tList<KeyFrameF>();
    86   KeyFrameF* tmpKeyFrame = new KeyFrameF;
    87   tmpKeyFrame->value = 0.0;
    88   tmpKeyFrame->duration = 1.0;
    89   keyFrameList->add(tmpKeyFrame);
    90 
    91   this->currentKeyFrame = tmpKeyFrame;
    92   this->nextKeyFrame = tmpKeyFrame;
     88  KeyFrameF tmpKeyFrame;
     89  tmpKeyFrame.value = 0.0;
     90  tmpKeyFrame.duration = 1.0;
     91  keyFrameList.push_back(tmpKeyFrame);
     92
     93  this->currentKeyFrame = keyFrameList.begin();
     94  this->nextKeyFrame = keyFrameList.begin();
    9395
    9496  this->animFunc = &tAnimation<T>::linear;
     
    99101
    100102/**
    101  *  standard deconstructor
    102 
    103    deletes all the Keyframes
    104 */
    105 template<class T>
    106 tAnimation<T>::~tAnimation ()
    107 {
    108   // delete all the KeyFrames
    109   tIterator<KeyFrameF>* itKF = keyFrameList->getIterator();
    110   KeyFrameF*  enumKF = itKF->firstElement();
    111   while (enumKF)
    112     {
    113       delete enumKF;
    114       enumKF = itKF->nextElement();
    115     }
    116   delete itKF;
    117   delete this->keyFrameList;
    118 }
    119 
    120 /**
    121103 *  rewinds the Animation to the beginning (first KeyFrame and time == 0)
    122104*/
     
    124106void tAnimation<T>::rewind()
    125107{
    126   this->currentKeyFrame = keyFrameList->firstElement();
    127   this->nextKeyFrame = keyFrameList->nextElement(keyFrameList->firstElement());
     108  this->currentKeyFrame = keyFrameList.begin();
     109  this->nextKeyFrame = ++keyFrameList.begin();
    128110  this->localTime = 0.0;
    129   this->setAnimFunc(this->currentKeyFrame->animFunc);
     111  this->setAnimFunc((*this->currentKeyFrame).animFunc);
    130112}
    131113
     
    157139    animFunc = ANIM_DEFAULT_FUNCTION;
    158140
    159   KeyFrameF* tmpKeyFrame;
     141  KeyFrameF& tmpKeyFrame = keyFrameList.front();
    160142
    161143  // when adding the first frame
    162144  if (this->keyFrameCount == 0)
    163     {
    164       tmpKeyFrame = this->keyFrameList->firstElement();
    165       this->setAnimFunc(animFunc);
    166     }
     145  {
     146    this->setAnimFunc(animFunc);
     147  }
    167148  else
    168     {
    169       tmpKeyFrame = new KeyFrameF;
    170       // when adding the second frame
    171       if (this->currentKeyFrame == this->nextKeyFrame)
    172         this->nextKeyFrame = tmpKeyFrame;
    173       this->keyFrameList->add(tmpKeyFrame);
    174     }
    175 
    176   tmpKeyFrame->value = value;
    177   tmpKeyFrame->duration = duration;
    178   tmpKeyFrame->animFunc = animFunc;
     149  {
     150    this->keyFrameList.push_back(KeyFrameF());
     151    tmpKeyFrame = keyFrameList.back();
     152    // when adding the second frame
     153    if (this->currentKeyFrame == this->nextKeyFrame)
     154      this->nextKeyFrame = --keyFrameList.end();
     155  }
     156
     157  tmpKeyFrame.value = value;
     158  tmpKeyFrame.duration = duration;
     159  tmpKeyFrame.animFunc = animFunc;
    179160  this->keyFrameCount++;
    180161}
     
    188169{
    189170  if (this->bRunning)
     171  {
     172    this->localTime += dt;
     173    if (localTime >= this->currentKeyFrame->duration)
    190174    {
    191       this->localTime += dt;
    192       if (localTime >= this->currentKeyFrame->duration)
    193         {
    194           if (likely(this->keyFramesToPlay != 0))
    195             {
    196               if (unlikely(this->keyFramesToPlay > 0))
    197                 --this->keyFramesToPlay;
    198               // switching to the next Key-Frame
    199               this->localTime -= this->currentKeyFrame->duration;
    200 
    201               this->currentKeyFrame = this->nextKeyFrame;
    202               // checking, if we should still Play the animation
    203               if (this->currentKeyFrame == this->keyFrameList->lastElement())
    204                 this->handleInfinity();
    205               this->nextKeyFrame = this->keyFrameList->nextElement(this->currentKeyFrame);
    206 
    207               //printf("%p from:%f to:%f\n", this->currentKeyFrame,this->currentKeyFrame->value, this->nextKeyFrame->value);
    208               this->setAnimFunc(this->currentKeyFrame->animFunc);
    209             }
    210           else
    211             this->pause();
    212         }
    213 
    214       (this->object->*(funcToAnim))((this->*animFunc)(this->localTime));
     175      if (likely(this->keyFramesToPlay != 0))
     176      {
     177        if (unlikely(this->keyFramesToPlay > 0))
     178          --this->keyFramesToPlay;
     179        // switching to the next Key-Frame
     180        this->localTime -= this->currentKeyFrame->duration;
     181
     182        this->currentKeyFrame = this->nextKeyFrame;
     183        // checking, if we should still Play the animation
     184        if (this->currentKeyFrame == --this->keyFrameList.end())
     185          this->handleInfinity();
     186        this->nextKeyFrame = this->currentKeyFrame;
     187        this->nextKeyFrame++;
     188
     189        //printf("%p from:%f to:%f\n", this->currentKeyFrame,this->currentKeyFrame->value, this->nextKeyFrame->value);
     190        this->setAnimFunc(this->currentKeyFrame->animFunc);
     191      }
     192      else
     193        this->pause();
    215194    }
     195
     196    (this->object->*(funcToAnim))((this->*animFunc)(this->localTime));
     197  }
    216198}
    217199
     
    224206{
    225207  switch (animFunc)
    226     {
    227     default:
    228     case ANIM_CONSTANT:
    229       this->animFunc = &tAnimation<T>::constant;
    230       break;
    231     case ANIM_LINEAR:
    232       this->animFunc = &tAnimation<T>::linear;
    233       break;
    234     case ANIM_SINE:
    235       this->animFunc = &tAnimation<T>::sine;
    236       break;
    237     case ANIM_COSINE:
    238       this->animFunc = &tAnimation<T>::cosine;
    239       break;
    240     case ANIM_EXP:
    241       this->animFunc = &tAnimation<T>::exp;
    242       break;
    243     case ANIM_NEG_EXP:
    244         this->animFunc = &tAnimation<T>::negExp;
    245         expFactor =  - 1.0 / this->currentKeyFrame->duration * logf(DELTA_X);
    246         break;
    247     case ANIM_QUADRATIC:
    248       this->animFunc = &tAnimation<T>::quadratic;
    249       break;
    250     case ANIM_RANDOM:
    251       this->animFunc = &tAnimation<T>::random;
    252       break;
    253     }
     208  {
     209  default:
     210  case ANIM_CONSTANT:
     211    this->animFunc = &tAnimation<T>::constant;
     212    break;
     213  case ANIM_LINEAR:
     214    this->animFunc = &tAnimation<T>::linear;
     215    break;
     216  case ANIM_SINE:
     217    this->animFunc = &tAnimation<T>::sine;
     218    break;
     219  case ANIM_COSINE:
     220    this->animFunc = &tAnimation<T>::cosine;
     221    break;
     222  case ANIM_EXP:
     223    this->animFunc = &tAnimation<T>::exp;
     224    break;
     225  case ANIM_NEG_EXP:
     226    this->animFunc = &tAnimation<T>::negExp;
     227    expFactor =  - 1.0 / this->currentKeyFrame->duration * logf(DELTA_X);
     228    break;
     229  case ANIM_QUADRATIC:
     230    this->animFunc = &tAnimation<T>::quadratic;
     231    break;
     232  case ANIM_RANDOM:
     233    this->animFunc = &tAnimation<T>::random;
     234    break;
     235  }
    254236}
    255237
     
    274256{
    275257  return this->currentKeyFrame->value + (this->nextKeyFrame->value - this->currentKeyFrame->value)
    276     * (timePassed / this->currentKeyFrame->duration);
     258         * (timePassed / this->currentKeyFrame->duration);
    277259}
    278260
     
    286268  if (timePassed * 2.0 < this->currentKeyFrame->duration)
    287269    return this->currentKeyFrame->value + (this->nextKeyFrame->value - this->currentKeyFrame->value)
    288       * sin( M_PI * timePassed / this->currentKeyFrame->duration)/2;
     270           * sin( M_PI * timePassed / this->currentKeyFrame->duration)/2;
    289271  else
    290272    return this->nextKeyFrame->value - (this->nextKeyFrame->value - this->currentKeyFrame->value)
    291       * sin( M_PI * (1.0 - timePassed / this->currentKeyFrame->duration))/2;
     273           * sin( M_PI * (1.0 - timePassed / this->currentKeyFrame->duration))/2;
    292274  /*
    293275  printf("::%f::::%f::\n",timePassed/this->currentKeyFrame->duration,retVal);
     
    304286{
    305287  return ((this->nextKeyFrame->value + this->currentKeyFrame->value) +
    306     (this->currentKeyFrame->value - this->nextKeyFrame->value) *
    307     cos( M_PI * timePassed / this->currentKeyFrame->duration))/2;
     288          (this->currentKeyFrame->value - this->nextKeyFrame->value) *
     289          cos( M_PI * timePassed / this->currentKeyFrame->duration))/2;
    308290}
    309291
     
    348330{
    349331  return this->currentKeyFrame->value +
    350     (this->nextKeyFrame->value - this->currentKeyFrame->value) *
    351     (float)rand()/(float)RAND_MAX;
     332         (this->nextKeyFrame->value - this->currentKeyFrame->value) *
     333         (float)rand()/(float)RAND_MAX;
    352334}
    353335
Note: See TracChangeset for help on using the changeset viewer.