| 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 | /*!  | 
|---|
| 17 |     \file t_animation.h | 
|---|
| 18 | */ | 
|---|
| 19 |  | 
|---|
| 20 | #ifndef _T_ANIMATION_H | 
|---|
| 21 | #define _T_ANIMATION_H | 
|---|
| 22 |  | 
|---|
| 23 | #include "animation.h" | 
|---|
| 24 |  | 
|---|
| 25 | #define DELTA_X 0.05  //!< the percentag of the distance that doesnt have to be done by neg_exp (asymptotical) ~ maschinendelta | 
|---|
| 26 |  | 
|---|
| 27 | //! A Class to handle some animation for single floated values. | 
|---|
| 28 | template<class T> class tAnimation : public Animation | 
|---|
| 29 | { | 
|---|
| 30 |  public: | 
|---|
| 31 |   tAnimation(T* object = NULL, void (T::*funcToAnim)(float) = NULL); | 
|---|
| 32 |   virtual ~tAnimation(); | 
|---|
| 33 |  | 
|---|
| 34 |   void setFuncToAnim(T* object, void (T::*funcToAnim)(float)); | 
|---|
| 35 |  | 
|---|
| 36 |   void addKeyFrame(float value, float duration, ANIM_FUNCTION animFunc = ANIM_LINEAR); | 
|---|
| 37 |  | 
|---|
| 38 |   virtual void rewind(); | 
|---|
| 39 |   virtual void tick(float dt); | 
|---|
| 40 |  | 
|---|
| 41 |  private: | 
|---|
| 42 |   // animation functions | 
|---|
| 43 |   void setAnimFunc(ANIM_FUNCTION animFunc); | 
|---|
| 44 |   float constant(float timePassed) const; | 
|---|
| 45 |   float linear(float timePassed) const; | 
|---|
| 46 |   float sine(float timePassed) const; | 
|---|
| 47 |   float cosine(float timePassed) const; | 
|---|
| 48 |   float exp(float timePassed) const; | 
|---|
| 49 |   float negExp(float timePassed) const; | 
|---|
| 50 |   float quadratic(float timePassed) const; | 
|---|
| 51 |   float random(float timePassed) const; | 
|---|
| 52 |  | 
|---|
| 53 |  | 
|---|
| 54 |   //  ANIM_FUNCTION animFunc; | 
|---|
| 55 |   float (tAnimation<T>::*animFunc)(float) const;  //!< A Function for the AnimationType | 
|---|
| 56 |  | 
|---|
| 57 |   KeyFrameF* currentKeyFrame;                     //!< The current KeyFrame | 
|---|
| 58 |   KeyFrameF* nextKeyFrame;                        //!< The KeyFrame we iterate to | 
|---|
| 59 |   tList<KeyFrameF>* keyFrameList;                 //!< The KeyFrameList | 
|---|
| 60 |  | 
|---|
| 61 |   T* object;                                      //!< The Object from which to Animate something | 
|---|
| 62 |   void (T::*funcToAnim)(float);                   //!< The function to Animate | 
|---|
| 63 |  | 
|---|
| 64 |   float expFactor;                                //!< some factors | 
|---|
| 65 | }; | 
|---|
| 66 |  | 
|---|
| 67 |  | 
|---|
| 68 |  | 
|---|
| 69 | /** | 
|---|
| 70 |    \brief standard constructor | 
|---|
| 71 | */ | 
|---|
| 72 | template<class T> | 
|---|
| 73 | tAnimation<T>::tAnimation (T* object, void (T::*funcToAnim)(float))  | 
|---|
| 74 | { | 
|---|
| 75 |   // create a new List | 
|---|
| 76 |   this->keyFrameList = new tList<KeyFrameF>(); | 
|---|
| 77 |   KeyFrameF* tmpKeyFrame = new KeyFrameF; | 
|---|
| 78 |   tmpKeyFrame->value = 0.0; | 
|---|
| 79 |   tmpKeyFrame->duration = 1.0; | 
|---|
| 80 |   keyFrameList->add(tmpKeyFrame); | 
|---|
| 81 |  | 
|---|
| 82 |   this->currentKeyFrame = tmpKeyFrame; | 
|---|
| 83 |   this->nextKeyFrame = tmpKeyFrame; | 
|---|
| 84 |  | 
|---|
| 85 |   this->animFunc = &tAnimation<T>::linear; | 
|---|
| 86 |  | 
|---|
| 87 |   this->setFuncToAnim(object, funcToAnim); | 
|---|
| 88 | } | 
|---|
| 89 |  | 
|---|
| 90 |  | 
|---|
| 91 | /** | 
|---|
| 92 |    \brief standard deconstructor | 
|---|
| 93 |     | 
|---|
| 94 |    deletes all the Keyframes | 
|---|
| 95 | */ | 
|---|
| 96 | template<class T> | 
|---|
| 97 | tAnimation<T>::~tAnimation ()  | 
|---|
| 98 | { | 
|---|
| 99 |   // delete all the KeyFrames | 
|---|
| 100 |   tIterator<KeyFrameF>* itKF = keyFrameList->getIterator(); | 
|---|
| 101 |   KeyFrameF*  enumKF = itKF->nextElement(); | 
|---|
| 102 |   while (enumKF) | 
|---|
| 103 |     { | 
|---|
| 104 |       delete enumKF; | 
|---|
| 105 |       enumKF = itKF->nextElement(); | 
|---|
| 106 |     } | 
|---|
| 107 |   delete itKF; | 
|---|
| 108 |   delete this->keyFrameList; | 
|---|
| 109 | } | 
|---|
| 110 |  | 
|---|
| 111 | /** | 
|---|
| 112 |    \brief rewinds the Animation to the beginning (first KeyFrame and time == 0) | 
|---|
| 113 | */ | 
|---|
| 114 | template<class T> | 
|---|
| 115 | void tAnimation<T>::rewind(void) | 
|---|
| 116 | { | 
|---|
| 117 |   this->currentKeyFrame = keyFrameList->firstElement(); | 
|---|
| 118 |   this->nextKeyFrame = keyFrameList->nextElement(keyFrameList->firstElement()); | 
|---|
| 119 |   this->localTime = 0.0; | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|
| 122 | /** | 
|---|
| 123 |    \brief sets the Function we want to animate | 
|---|
| 124 |    \param object from what object do we want to animate | 
|---|
| 125 |    \param funcToAnim which function | 
|---|
| 126 | */ | 
|---|
| 127 | template<class T> | 
|---|
| 128 | void tAnimation<T>::setFuncToAnim(T* object, void (T::*funcToAnim)(float)) | 
|---|
| 129 | { | 
|---|
| 130 |   this->baseObject = this->object = object; | 
|---|
| 131 |   this->funcToAnim = funcToAnim; | 
|---|
| 132 | } | 
|---|
| 133 |  | 
|---|
| 134 | /** | 
|---|
| 135 |    \brief Appends a new Keyframe | 
|---|
| 136 |    \param value the value of the new KeyFrame | 
|---|
| 137 |    \param duration The duration from the new KeyFrame to the next one | 
|---|
| 138 |    \param animFunc The function to animate between this keyFrame and the next one | 
|---|
| 139 | */ | 
|---|
| 140 | template<class T> | 
|---|
| 141 | void tAnimation<T>::addKeyFrame(float value, float duration, ANIM_FUNCTION animFunc) | 
|---|
| 142 | { | 
|---|
| 143 |   // some small check | 
|---|
| 144 |   if (duration <= 0.0) | 
|---|
| 145 |     duration = 1.0; | 
|---|
| 146 |  | 
|---|
| 147 |   KeyFrameF* tmpKeyFrame; | 
|---|
| 148 |      | 
|---|
| 149 |   if (bHasKeys) | 
|---|
| 150 |     { | 
|---|
| 151 |       tmpKeyFrame = new KeyFrameF; | 
|---|
| 152 |       if (this->currentKeyFrame == this->nextKeyFrame) | 
|---|
| 153 |         this->nextKeyFrame = tmpKeyFrame; | 
|---|
| 154 |       this->keyFrameList->add(tmpKeyFrame); | 
|---|
| 155 |  | 
|---|
| 156 |     } | 
|---|
| 157 |   else | 
|---|
| 158 |     { | 
|---|
| 159 |       tmpKeyFrame = this->keyFrameList->firstElement(); | 
|---|
| 160 |       bHasKeys = true; | 
|---|
| 161 |       this->setAnimFunc(animFunc); | 
|---|
| 162 |     } | 
|---|
| 163 |   tmpKeyFrame->value = value; | 
|---|
| 164 |   tmpKeyFrame->duration = duration; | 
|---|
| 165 |   tmpKeyFrame->animFunc = animFunc; | 
|---|
| 166 | } | 
|---|
| 167 |  | 
|---|
| 168 | /** | 
|---|
| 169 |    \brief ticks the Animation | 
|---|
| 170 |    \param dt how much time to tick | 
|---|
| 171 | */ | 
|---|
| 172 | template<class T> | 
|---|
| 173 | void tAnimation<T>::tick(float dt) | 
|---|
| 174 | { | 
|---|
| 175 |   if (this->bRunning) | 
|---|
| 176 |     { | 
|---|
| 177 |       this->localTime += dt; | 
|---|
| 178 |       if (localTime >= this->currentKeyFrame->duration) | 
|---|
| 179 |         { | 
|---|
| 180 |           // switching to the next Key-Frame | 
|---|
| 181 |           this->localTime -= this->currentKeyFrame->duration; | 
|---|
| 182 |  | 
|---|
| 183 |           this->currentKeyFrame = this->nextKeyFrame; | 
|---|
| 184 |           // checking, if we should still Play the animation | 
|---|
| 185 |           if (this->currentKeyFrame == this->keyFrameList->lastElement()) | 
|---|
| 186 |             this->handleInfinity(); | 
|---|
| 187 |           this->nextKeyFrame = this->keyFrameList->nextElement(this->currentKeyFrame); | 
|---|
| 188 |           printf("%p from:%f to:%f\n", this->currentKeyFrame,this->currentKeyFrame->value, this->nextKeyFrame->value); | 
|---|
| 189 |           this->setAnimFunc(this->currentKeyFrame->animFunc);      | 
|---|
| 190 |         } | 
|---|
| 191 |        | 
|---|
| 192 |       (this->object->*(funcToAnim))((this->*animFunc)(this->localTime)); | 
|---|
| 193 |     } | 
|---|
| 194 | } | 
|---|
| 195 |  | 
|---|
| 196 | /** | 
|---|
| 197 |    \brief Sets The kind of Animation between this keyframe and the next one | 
|---|
| 198 |    \param animFunc The Type of Animation to set | 
|---|
| 199 | */ | 
|---|
| 200 | template<class T> | 
|---|
| 201 | void tAnimation<T>::setAnimFunc(ANIM_FUNCTION animFunc) | 
|---|
| 202 | { | 
|---|
| 203 |   switch (animFunc) | 
|---|
| 204 |     { | 
|---|
| 205 |     default: | 
|---|
| 206 |     case ANIM_CONSTANT: | 
|---|
| 207 |       this->animFunc = &tAnimation<T>::constant; | 
|---|
| 208 |       break; | 
|---|
| 209 |     case ANIM_LINEAR: | 
|---|
| 210 |       this->animFunc = &tAnimation<T>::linear; | 
|---|
| 211 |       break; | 
|---|
| 212 |     case ANIM_SINE: | 
|---|
| 213 |       this->animFunc = &tAnimation<T>::sine; | 
|---|
| 214 |       break; | 
|---|
| 215 |     case ANIM_COSINE: | 
|---|
| 216 |       this->animFunc = &tAnimation<T>::cosine; | 
|---|
| 217 |       break; | 
|---|
| 218 |     case ANIM_EXP: | 
|---|
| 219 |       this->animFunc = &tAnimation<T>::exp; | 
|---|
| 220 |       break; | 
|---|
| 221 |     case ANIM_NEG_EXP: | 
|---|
| 222 |       { | 
|---|
| 223 |         this->animFunc = &tAnimation<T>::negExp; | 
|---|
| 224 |         float d = fabs(this->currentKeyFrame->value - this->nextKeyFrame->value); | 
|---|
| 225 |         expFactor =  - 1.0 / this->currentKeyFrame->duration * logf(DELTA_X); | 
|---|
| 226 |         break; | 
|---|
| 227 |       } | 
|---|
| 228 |     case ANIM_QUADRATIC: | 
|---|
| 229 |       this->animFunc = &tAnimation<T>::quadratic; | 
|---|
| 230 |       break; | 
|---|
| 231 |     case ANIM_RANDOM: | 
|---|
| 232 |       this->animFunc = &tAnimation<T>::random; | 
|---|
| 233 |       break; | 
|---|
| 234 |     } | 
|---|
| 235 | } | 
|---|
| 236 |  | 
|---|
| 237 |  | 
|---|
| 238 | // animation functions | 
|---|
| 239 | /** | 
|---|
| 240 |    \brief stays at the value of the currentKeyFrame | 
|---|
| 241 |    \param timePassed The time passed since this Keyframe began | 
|---|
| 242 | */ | 
|---|
| 243 | template<class T> | 
|---|
| 244 | float tAnimation<T>::constant(float timePassed) const | 
|---|
| 245 | { | 
|---|
| 246 |   return this->currentKeyFrame->value; | 
|---|
| 247 | } | 
|---|
| 248 |  | 
|---|
| 249 | /** | 
|---|
| 250 |    \brief linear interpolation between this keyframe and the next one | 
|---|
| 251 |    \param timePassed The time passed since this Keyframe began | 
|---|
| 252 | */ | 
|---|
| 253 | template<class T> | 
|---|
| 254 | float tAnimation<T>::linear(float timePassed) const  | 
|---|
| 255 | { | 
|---|
| 256 |   return this->currentKeyFrame->value + (this->nextKeyFrame->value - this->currentKeyFrame->value)  | 
|---|
| 257 |     * (timePassed / this->currentKeyFrame->duration); | 
|---|
| 258 |   //  PRINTF(0)("value is %f, %p %p\n", val, this->currentKeyFrame, this->nextKeyFrame); | 
|---|
| 259 |   //  return val; | 
|---|
| 260 | } | 
|---|
| 261 |  | 
|---|
| 262 | /** | 
|---|
| 263 |    \brief a Sinusodial Interpolation between this keyframe and the next one | 
|---|
| 264 |    \param timePassed The time passed since this Keyframe began | 
|---|
| 265 | */ | 
|---|
| 266 | template<class T> | 
|---|
| 267 | float tAnimation<T>::sine(float timePassed) const | 
|---|
| 268 | { | 
|---|
| 269 |   float d = this->currentKeyFrame->value - this->nextKeyFrame->value; | 
|---|
| 270 |   float e = 0.5 * d * (1 - cos(M_PI * timePassed / this->currentKeyFrame->duration)); | 
|---|
| 271 |   return this->currentKeyFrame->value - e; | 
|---|
| 272 |   /* | 
|---|
| 273 |   return his->currentKeyFrame->value - (this->nextKeyFrame->value - this->currentKeyFrame->value) | 
|---|
| 274 |     * sin(timePassed / this->currentKeyFrame->duration * M_PI); | 
|---|
| 275 |   */ | 
|---|
| 276 | } | 
|---|
| 277 |  | 
|---|
| 278 | /** | 
|---|
| 279 |    \brief a cosine interpolation between this keyframe and the next one | 
|---|
| 280 |    \param timePassed The time passed since this Keyframe began | 
|---|
| 281 | */ | 
|---|
| 282 | template<class T> | 
|---|
| 283 | float tAnimation<T>::cosine(float timePassed) const | 
|---|
| 284 | { | 
|---|
| 285 |   float d = this->currentKeyFrame->value - this->nextKeyFrame->value; | 
|---|
| 286 |   float e = 0.5 * d * (sin(M_PI * timePassed / this->currentKeyFrame->duration)); | 
|---|
| 287 |   if( timePassed > 0.5*this->currentKeyFrame->duration) e = (d - e); | 
|---|
| 288 |   return this->currentKeyFrame->value - e; | 
|---|
| 289 |   /* | 
|---|
| 290 |   return this->currentKeyFrame->value - (this->nextKeyFrame->value - this->currentKeyFrame->value) | 
|---|
| 291 |     * cos(timePassed / this->currentKeyFrame->duration * M_PI); | 
|---|
| 292 |   */ | 
|---|
| 293 | } | 
|---|
| 294 |  | 
|---|
| 295 | /** | 
|---|
| 296 |    \brief an exponential interpolation between this keyframe and the next one | 
|---|
| 297 |    \param timePassed The time passed since this Keyframe began | 
|---|
| 298 | */ | 
|---|
| 299 | template<class T> | 
|---|
| 300 | float tAnimation<T>::exp(float timePassed) const | 
|---|
| 301 | { | 
|---|
| 302 | } | 
|---|
| 303 |  | 
|---|
| 304 | /** | 
|---|
| 305 |    \brief a negative exponential interpolation between this keyframe and the next one | 
|---|
| 306 |    \param timePassed The time passed since this Keyframe began | 
|---|
| 307 | */ | 
|---|
| 308 | template<class T> | 
|---|
| 309 | float tAnimation<T>::negExp(float timePassed) const | 
|---|
| 310 | { | 
|---|
| 311 |   float d = this->currentKeyFrame->value - this->nextKeyFrame->value; | 
|---|
| 312 |   float e = d * (1.0 - expf(- timePassed * expFactor)); | 
|---|
| 313 |   return  this->currentKeyFrame->value - e; | 
|---|
| 314 | } | 
|---|
| 315 |  | 
|---|
| 316 | /** | 
|---|
| 317 |    \brief a quadratic interpolation between this keyframe and the next one | 
|---|
| 318 |    \param timePassed The time passed since this Keyframe began | 
|---|
| 319 | */ | 
|---|
| 320 | template<class T> | 
|---|
| 321 | float tAnimation<T>::quadratic(float timePassed) const | 
|---|
| 322 | { | 
|---|
| 323 |   this->linear(timePassed); | 
|---|
| 324 | } | 
|---|
| 325 |  | 
|---|
| 326 | /** | 
|---|
| 327 |    \brief some random animation (fluctuating) | 
|---|
| 328 |    \param timePassed The time passed since this Keyframe began | 
|---|
| 329 | */ | 
|---|
| 330 | template<class T> | 
|---|
| 331 | float tAnimation<T>::random(float timePassed) const | 
|---|
| 332 | { | 
|---|
| 333 |   return this->currentKeyFrame->value * (float)rand()/(float)RAND_MAX; | 
|---|
| 334 | } | 
|---|
| 335 |  | 
|---|
| 336 | #endif /* _T_ANIMATION_H */ | 
|---|