| 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 | /*! |
|---|
| 18 | \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. |
|---|
| 24 | */ |
|---|
| 25 | |
|---|
| 26 | #ifndef _ANIMATION_H |
|---|
| 27 | #define _ANIMATION_H |
|---|
| 28 | |
|---|
| 29 | #include "list.h" |
|---|
| 30 | // FORWARD DEFINITION |
|---|
| 31 | |
|---|
| 32 | typedef enum ANIM_FUNCTION {ANIM_CONSTANT, |
|---|
| 33 | ANIM_LINEAR, |
|---|
| 34 | ANIM_SINE, |
|---|
| 35 | ANIM_COSINE, |
|---|
| 36 | ANIM_EXP, |
|---|
| 37 | ANIM_NEG_EXP, |
|---|
| 38 | ANIM_QUADRATIC, |
|---|
| 39 | ANIM_RANDOM}; |
|---|
| 40 | |
|---|
| 41 | typedef enum ANIM_INFINITY {ANIM_INF_CONSTANT, |
|---|
| 42 | ANIM_INF_LINEAR, |
|---|
| 43 | ANIM_INF_PINGPONG, |
|---|
| 44 | ANIM_INF_REWIND};//, ANIM_DELETE} |
|---|
| 45 | |
|---|
| 46 | typedef struct AnimKeyFrame |
|---|
| 47 | { |
|---|
| 48 | float duration; |
|---|
| 49 | float value; |
|---|
| 50 | ANIM_FUNCTION animFunc; |
|---|
| 51 | }; |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | /**********************TEST*******************************/ |
|---|
| 55 | class aTest |
|---|
| 56 | { |
|---|
| 57 | public: |
|---|
| 58 | aTest() { last = 0.0;} |
|---|
| 59 | ~aTest() {} |
|---|
| 60 | void littleDebug(float f) { diff = f - last; printf("f=%f, diff=%f\n", f,diff); last = f;} |
|---|
| 61 | private: |
|---|
| 62 | float diff; |
|---|
| 63 | float last; |
|---|
| 64 | }; |
|---|
| 65 | |
|---|
| 66 | //aTest::aTest() {} |
|---|
| 67 | //aTest::~aTest() {} |
|---|
| 68 | |
|---|
| 69 | //void aTest::littleDebug(float f) |
|---|
| 70 | |
|---|
| 71 | /**********************TEST*******************************/ |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | class Anim |
|---|
| 75 | { |
|---|
| 76 | public: |
|---|
| 77 | virtual ~Anim(void); |
|---|
| 78 | void doNotHandle(void); |
|---|
| 79 | |
|---|
| 80 | void setInfinity(ANIM_INFINITY postInfinity = ANIM_INF_CONSTANT); |
|---|
| 81 | |
|---|
| 82 | void play(); // equals resume(); |
|---|
| 83 | void stop(); |
|---|
| 84 | void pause(); |
|---|
| 85 | void replay(); |
|---|
| 86 | virtual void rewind() = 0; |
|---|
| 87 | |
|---|
| 88 | virtual void tick(float time) = 0; |
|---|
| 89 | |
|---|
| 90 | /* implement in subclasses: |
|---|
| 91 | * |
|---|
| 92 | * De-/Constructor |
|---|
| 93 | * Animation Functions |
|---|
| 94 | * virtual tick |
|---|
| 95 | * List of keyFrames |
|---|
| 96 | * currentKeyFrame/nextKeyFrame |
|---|
| 97 | * virtual rewind, to go to the first Keyframe. (other functions will call this one) |
|---|
| 98 | */ |
|---|
| 99 | protected: |
|---|
| 100 | Anim(void); |
|---|
| 101 | |
|---|
| 102 | // variables |
|---|
| 103 | |
|---|
| 104 | float localTime; |
|---|
| 105 | ANIM_INFINITY postInfinity; |
|---|
| 106 | |
|---|
| 107 | bool bHasKeys; |
|---|
| 108 | bool bHandled; //!< If this Animation is handled by the AnimationPlayer. |
|---|
| 109 | bool bRunning; |
|---|
| 110 | }; |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | //! A Class to handle some animation for single floated values. |
|---|
| 114 | template<class T> class tAnim : public Anim |
|---|
| 115 | { |
|---|
| 116 | public: |
|---|
| 117 | tAnim(T* object = NULL, void (T::*funcToAnim)(float) = NULL); |
|---|
| 118 | virtual ~tAnim(); |
|---|
| 119 | |
|---|
| 120 | virtual void rewind(); |
|---|
| 121 | |
|---|
| 122 | void setFuncToAnim(T* object, void (T::*funcToAnim)(float)); |
|---|
| 123 | void addKeyFrame(float value, float duration, ANIM_FUNCTION animFunc = ANIM_LINEAR); |
|---|
| 124 | |
|---|
| 125 | virtual void tick(float time); |
|---|
| 126 | |
|---|
| 127 | // animation functions |
|---|
| 128 | void setAnimFunc(ANIM_FUNCTION animFunc); |
|---|
| 129 | |
|---|
| 130 | float constant(float timePassed) const; |
|---|
| 131 | float linear(float timePassed) const; |
|---|
| 132 | float sine(float timePassed) const; |
|---|
| 133 | float cosine(float timePassed) const; |
|---|
| 134 | float exp(float timePassed) const; |
|---|
| 135 | float negExp(float timePassed) const; |
|---|
| 136 | float quadratic(float timePassed) const; |
|---|
| 137 | float random(float timePassed) const; |
|---|
| 138 | // ANIM_FUNCTION animFunc; |
|---|
| 139 | float (tAnim<T>::*animFunc)(float) const; |
|---|
| 140 | AnimKeyFrame* currentKeyFrame; |
|---|
| 141 | AnimKeyFrame* nextKeyFrame; |
|---|
| 142 | tList<AnimKeyFrame>* keyFrameList; |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | private: |
|---|
| 148 | T* object; |
|---|
| 149 | void (T::*funcToAnim)(float); |
|---|
| 150 | }; |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | /** |
|---|
| 155 | \brief standard constructor |
|---|
| 156 | |
|---|
| 157 | */ |
|---|
| 158 | template<class T> |
|---|
| 159 | tAnim<T>::tAnim (T* object, void (T::*funcToAnim)(float)) |
|---|
| 160 | { |
|---|
| 161 | // create a new List |
|---|
| 162 | this->keyFrameList = new tList<AnimKeyFrame>(); |
|---|
| 163 | AnimKeyFrame* tmpKeyFrame = new AnimKeyFrame; |
|---|
| 164 | tmpKeyFrame->value = 0.0; |
|---|
| 165 | tmpKeyFrame->duration = 1.0; |
|---|
| 166 | keyFrameList->add(tmpKeyFrame); |
|---|
| 167 | |
|---|
| 168 | this->currentKeyFrame = tmpKeyFrame; |
|---|
| 169 | this->nextKeyFrame = tmpKeyFrame; |
|---|
| 170 | |
|---|
| 171 | this->animFunc = &tAnim<T>::linear; |
|---|
| 172 | |
|---|
| 173 | this->setFuncToAnim(object, funcToAnim); |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | /** |
|---|
| 178 | \brief standard deconstructor |
|---|
| 179 | |
|---|
| 180 | */ |
|---|
| 181 | template<class T> |
|---|
| 182 | tAnim<T>::~tAnim () |
|---|
| 183 | { |
|---|
| 184 | // delete all the KeyFrames |
|---|
| 185 | tIterator<AnimKeyFrame>* itKF = keyFrameList->getIterator(); |
|---|
| 186 | AnimKeyFrame* enumKF = itKF->nextElement(); |
|---|
| 187 | while (enumKF) |
|---|
| 188 | { |
|---|
| 189 | delete enumKF; |
|---|
| 190 | enumKF = itKF->nextElement(); |
|---|
| 191 | } |
|---|
| 192 | delete itKF; |
|---|
| 193 | delete this->keyFrameList; |
|---|
| 194 | |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | template<class T> |
|---|
| 198 | void tAnim<T>::rewind(void) |
|---|
| 199 | { |
|---|
| 200 | this->currentKeyFrame = keyFrameList->firstElement(); |
|---|
| 201 | this->nextKeyFrame = keyFrameList->nextElement(keyFrameList->firstElement()); |
|---|
| 202 | this->localTime = 0.0; |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | template<class T> |
|---|
| 206 | void tAnim<T>::setFuncToAnim(T* object, void (T::*funcToAnim)(float)) |
|---|
| 207 | { |
|---|
| 208 | this->object = object; |
|---|
| 209 | this->funcToAnim = funcToAnim; |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | template<class T> |
|---|
| 213 | void tAnim<T>::addKeyFrame(float value, float duration, ANIM_FUNCTION animFunc) |
|---|
| 214 | { |
|---|
| 215 | // some small check |
|---|
| 216 | if (duration <= 0.0) |
|---|
| 217 | duration = 1.0; |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | AnimKeyFrame* tmpKeyFrame; |
|---|
| 221 | |
|---|
| 222 | if (bHasKeys) |
|---|
| 223 | { |
|---|
| 224 | tmpKeyFrame = new AnimKeyFrame; |
|---|
| 225 | if (this->currentKeyFrame == this->nextKeyFrame) |
|---|
| 226 | this->nextKeyFrame = tmpKeyFrame; |
|---|
| 227 | this->keyFrameList->add(tmpKeyFrame); |
|---|
| 228 | |
|---|
| 229 | } |
|---|
| 230 | else |
|---|
| 231 | { |
|---|
| 232 | tmpKeyFrame = this->keyFrameList->firstElement(); |
|---|
| 233 | bHasKeys = true; |
|---|
| 234 | this->setAnimFunc(animFunc); |
|---|
| 235 | } |
|---|
| 236 | tmpKeyFrame->value = value; |
|---|
| 237 | tmpKeyFrame->duration = duration; |
|---|
| 238 | tmpKeyFrame->animFunc = animFunc; |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | template<class T> |
|---|
| 243 | void tAnim<T>::tick(float time) |
|---|
| 244 | { |
|---|
| 245 | if (this->bRunning) |
|---|
| 246 | { |
|---|
| 247 | this->localTime += time; |
|---|
| 248 | if (localTime >= this->currentKeyFrame->duration) |
|---|
| 249 | { |
|---|
| 250 | this->localTime = 0; |
|---|
| 251 | if (this->currentKeyFrame == this->keyFrameList->lastElement()) |
|---|
| 252 | switch (this->postInfinity) |
|---|
| 253 | { |
|---|
| 254 | case ANIM_INF_CONSTANT: |
|---|
| 255 | this->bRunning = false; |
|---|
| 256 | break; |
|---|
| 257 | case ANIM_INF_REWIND: |
|---|
| 258 | break; |
|---|
| 259 | } |
|---|
| 260 | this->currentKeyFrame = this->keyFrameList->nextElement(this->currentKeyFrame); |
|---|
| 261 | this->nextKeyFrame = this->keyFrameList->nextElement(this->nextKeyFrame); |
|---|
| 262 | printf("%p from:%f to:%f\n", this->currentKeyFrame,this->currentKeyFrame->value, this->nextKeyFrame->value); |
|---|
| 263 | this->setAnimFunc(this->currentKeyFrame->animFunc); |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | (this->object->*(funcToAnim))((this->*animFunc)(this->localTime)); |
|---|
| 267 | } |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | |
|---|
| 271 | template<class T> |
|---|
| 272 | void tAnim<T>::setAnimFunc(ANIM_FUNCTION animFunc) |
|---|
| 273 | { |
|---|
| 274 | switch (animFunc) |
|---|
| 275 | { |
|---|
| 276 | default: |
|---|
| 277 | case ANIM_CONSTANT: |
|---|
| 278 | this->animFunc = &tAnim<T>::constant; |
|---|
| 279 | break; |
|---|
| 280 | case ANIM_LINEAR: |
|---|
| 281 | this->animFunc = &tAnim<T>::linear; |
|---|
| 282 | break; |
|---|
| 283 | case ANIM_SINE: |
|---|
| 284 | this->animFunc = &tAnim<T>::sine; |
|---|
| 285 | break; |
|---|
| 286 | case ANIM_COSINE: |
|---|
| 287 | this->animFunc = &tAnim<T>::cosine; |
|---|
| 288 | break; |
|---|
| 289 | case ANIM_EXP: |
|---|
| 290 | this->animFunc = &tAnim<T>::exp; |
|---|
| 291 | break; |
|---|
| 292 | case ANIM_NEG_EXP: |
|---|
| 293 | this->animFunc = &tAnim<T>::negExp; |
|---|
| 294 | break; |
|---|
| 295 | case ANIM_QUADRATIC: |
|---|
| 296 | this->animFunc = &tAnim<T>::quadratic; |
|---|
| 297 | break; |
|---|
| 298 | case ANIM_RANDOM: |
|---|
| 299 | this->animFunc = &tAnim<T>::random; |
|---|
| 300 | break; |
|---|
| 301 | } |
|---|
| 302 | } |
|---|
| 303 | |
|---|
| 304 | |
|---|
| 305 | // animation functions |
|---|
| 306 | template<class T> |
|---|
| 307 | float tAnim<T>::random(float timePassed) const |
|---|
| 308 | { |
|---|
| 309 | return (float)rand()/(float)RAND_MAX; |
|---|
| 310 | } |
|---|
| 311 | |
|---|
| 312 | template<class T> |
|---|
| 313 | float tAnim<T>::constant(float timePassed) const |
|---|
| 314 | { |
|---|
| 315 | return this->currentKeyFrame->value; |
|---|
| 316 | } |
|---|
| 317 | |
|---|
| 318 | template<class T> |
|---|
| 319 | float tAnim<T>::linear(float timePassed) const |
|---|
| 320 | { |
|---|
| 321 | return this->nextKeyFrame->value - (this->nextKeyFrame->value - this->currentKeyFrame->value) |
|---|
| 322 | * (timePassed / this->currentKeyFrame->duration); |
|---|
| 323 | // PRINTF(0)("value is %f, %p %p\n", val, this->currentKeyFrame, this->nextKeyFrame); |
|---|
| 324 | // return val; |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | template<class T> |
|---|
| 328 | float tAnim<T>::sine(float timePassed) const |
|---|
| 329 | { |
|---|
| 330 | float d = this->currentKeyFrame->value - this->nextKeyFrame->value; |
|---|
| 331 | float e = 0.5 * d * (1 - cos(M_PI * timePassed / this->currentKeyFrame->duration)); |
|---|
| 332 | //printf("d=%f, t=%f, T=%f\n", d, timePassed, this->currentKeyFrame->duration); |
|---|
| 333 | return this->currentKeyFrame->value - e; |
|---|
| 334 | /* |
|---|
| 335 | return this->currentKeyFrame->value - (this->nextKeyFrame->value - this->currentKeyFrame->value) |
|---|
| 336 | * sin(timePassed / this->currentKeyFrame->duration * M_PI); |
|---|
| 337 | */ |
|---|
| 338 | } |
|---|
| 339 | |
|---|
| 340 | template<class T> |
|---|
| 341 | float tAnim<T>::cosine(float timePassed) const |
|---|
| 342 | { |
|---|
| 343 | return this->currentKeyFrame->value - (this->nextKeyFrame->value - this->currentKeyFrame->value) |
|---|
| 344 | * cos(timePassed / this->currentKeyFrame->duration * M_PI); |
|---|
| 345 | } |
|---|
| 346 | |
|---|
| 347 | template<class T> |
|---|
| 348 | float tAnim<T>::exp(float timePassed) const |
|---|
| 349 | { |
|---|
| 350 | |
|---|
| 351 | } |
|---|
| 352 | |
|---|
| 353 | template<class T> |
|---|
| 354 | float tAnim<T>::negExp(float timePassed) const |
|---|
| 355 | { |
|---|
| 356 | |
|---|
| 357 | } |
|---|
| 358 | |
|---|
| 359 | template<class T> |
|---|
| 360 | float tAnim<T>::quadratic(float timePassed) const |
|---|
| 361 | { |
|---|
| 362 | |
|---|
| 363 | } |
|---|
| 364 | |
|---|
| 365 | |
|---|
| 366 | |
|---|
| 367 | |
|---|
| 368 | #endif /* _ANIMATION_H */ |
|---|