[3781] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
[3329] | 3 | |
---|
[3781] | 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: ... |
---|
[3245] | 14 | */ |
---|
[1853] | 15 | |
---|
| 16 | |
---|
[3781] | 17 | /*! |
---|
| 18 | \file animation.h |
---|
| 19 | A Set of functions to animate some floats inside of an Object |
---|
[3784] | 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. |
---|
[3781] | 24 | */ |
---|
| 25 | |
---|
| 26 | #ifndef _ANIMATION_H |
---|
| 27 | #define _ANIMATION_H |
---|
| 28 | |
---|
[3795] | 29 | #include "list.h" |
---|
[3782] | 30 | // FORWARD DEFINITION |
---|
| 31 | |
---|
[3784] | 32 | typedef enum ANIM_FUNCTION {ANIM_CONSTANT, |
---|
| 33 | ANIM_LINEAR, |
---|
| 34 | ANIM_SINE, |
---|
| 35 | ANIM_RANDOM}; |
---|
[3787] | 36 | |
---|
[3784] | 37 | typedef enum ANIM_INFINITY {ANIM_INF_CONSTANT, |
---|
| 38 | ANIM_INF_LINEAR, |
---|
| 39 | ANIM_INF_PINGPONG, |
---|
| 40 | ANIM_INF_REWIND};//, ANIM_DELETE} |
---|
[3543] | 41 | |
---|
[3784] | 42 | struct AnimKeyFrame |
---|
| 43 | { |
---|
| 44 | float duration; |
---|
| 45 | float value; |
---|
| 46 | ANIM_FUNCTION animFunc; |
---|
| 47 | }; |
---|
| 48 | |
---|
[3782] | 49 | class Anim |
---|
| 50 | { |
---|
[3785] | 51 | public: |
---|
[3794] | 52 | virtual ~Anim(void); |
---|
| 53 | |
---|
| 54 | void setInfinity(ANIM_INFINITY postInfinity = ANIM_INF_CONSTANT); |
---|
| 55 | |
---|
| 56 | void play(); // equals resume(); |
---|
| 57 | void stop(); |
---|
| 58 | void pause(); |
---|
| 59 | void replay(); |
---|
[3797] | 60 | virtual void rewind() = 0; |
---|
[3794] | 61 | |
---|
| 62 | virtual void tick(float time) = 0; |
---|
| 63 | |
---|
[3795] | 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 | */ |
---|
[3782] | 73 | protected: |
---|
[3784] | 74 | Anim(void); |
---|
[3782] | 75 | |
---|
[3784] | 76 | // variables |
---|
[3794] | 77 | |
---|
[3795] | 78 | float localTime; |
---|
[3784] | 79 | ANIM_INFINITY postInfinity; |
---|
| 80 | |
---|
| 81 | bool bHasKeys; |
---|
[3787] | 82 | bool bRunning; |
---|
[3782] | 83 | }; |
---|
| 84 | |
---|
| 85 | |
---|
[3781] | 86 | //! A Class to handle some animation for single floated values. |
---|
[3794] | 87 | template<class T> class tAnim : public Anim |
---|
[3781] | 88 | { |
---|
| 89 | public: |
---|
[3794] | 90 | tAnim(T* object = NULL, void (T::*funcToAnim)(float) = NULL); |
---|
| 91 | virtual ~tAnim(); |
---|
[3543] | 92 | |
---|
[3797] | 93 | virtual void rewind(); |
---|
| 94 | |
---|
[3783] | 95 | void setFuncToAnim(T* object, void (T::*funcToAnim)(float)); |
---|
[3795] | 96 | void addKeyFrame(float value, float duration, ANIM_FUNCTION animFunc = ANIM_LINEAR); |
---|
[2036] | 97 | |
---|
[3797] | 98 | virtual void tick(float time); |
---|
[3795] | 99 | |
---|
| 100 | // animation functions |
---|
| 101 | void setAnimFunc(ANIM_FUNCTION animFunc); |
---|
| 102 | |
---|
| 103 | float random(float timePassed) const; |
---|
| 104 | float constant(float timePassed) const; |
---|
| 105 | float linear(float timePassed) const; |
---|
| 106 | float sine(float timePassed) const; |
---|
| 107 | // ANIM_FUNCTION animFunc; |
---|
| 108 | float (tAnim<T>::*animFunc)(float) const; |
---|
| 109 | AnimKeyFrame* currentKeyFrame; |
---|
| 110 | AnimKeyFrame* nextKeyFrame; |
---|
| 111 | tList<AnimKeyFrame>* keyFrameList; |
---|
| 112 | |
---|
| 113 | |
---|
| 114 | |
---|
| 115 | |
---|
[3781] | 116 | |
---|
| 117 | private: |
---|
[3784] | 118 | T* object; |
---|
[3783] | 119 | void (T::*funcToAnim)(float); |
---|
[3781] | 120 | }; |
---|
| 121 | |
---|
| 122 | |
---|
| 123 | |
---|
[3329] | 124 | /** |
---|
[3781] | 125 | \brief standard constructor |
---|
| 126 | |
---|
[3329] | 127 | */ |
---|
[3781] | 128 | template<class T> |
---|
[3794] | 129 | tAnim<T>::tAnim (T* object, void (T::*funcToAnim)(float)) |
---|
[3781] | 130 | { |
---|
[3795] | 131 | // create a new List |
---|
| 132 | this->keyFrameList = new tList<AnimKeyFrame>(); |
---|
| 133 | AnimKeyFrame* tmpKeyFrame = new AnimKeyFrame; |
---|
| 134 | tmpKeyFrame->value = 0.0; |
---|
| 135 | tmpKeyFrame->duration = 1.0; |
---|
| 136 | keyFrameList->add(tmpKeyFrame); |
---|
| 137 | |
---|
| 138 | this->currentKeyFrame = tmpKeyFrame; |
---|
| 139 | this->nextKeyFrame = tmpKeyFrame; |
---|
| 140 | |
---|
| 141 | this->animFunc = &tAnim<T>::linear; |
---|
| 142 | |
---|
[3784] | 143 | this->setFuncToAnim(object, funcToAnim); |
---|
[3781] | 144 | } |
---|
[1853] | 145 | |
---|
| 146 | |
---|
[3781] | 147 | /** |
---|
| 148 | \brief standard deconstructor |
---|
[3245] | 149 | |
---|
[3781] | 150 | */ |
---|
| 151 | template<class T> |
---|
[3794] | 152 | tAnim<T>::~tAnim () |
---|
[3781] | 153 | { |
---|
[3795] | 154 | // delete all the KeyFrames |
---|
| 155 | tIterator<AnimKeyFrame>* itKF = keyFrameList->getIterator(); |
---|
| 156 | AnimKeyFrame* enumKF = itKF->nextElement(); |
---|
| 157 | while (enumKF) |
---|
| 158 | { |
---|
| 159 | delete enumKF; |
---|
| 160 | enumKF = itKF->nextElement(); |
---|
| 161 | } |
---|
| 162 | delete itKF; |
---|
| 163 | delete this->keyFrameList; |
---|
| 164 | |
---|
[3781] | 165 | } |
---|
[3245] | 166 | |
---|
[3797] | 167 | template<class T> |
---|
| 168 | void tAnim<T>::rewind(void) |
---|
| 169 | { |
---|
| 170 | this->currentKeyFrame = keyFrameList->firstElement(); |
---|
| 171 | this->nextKeyFrame = keyFrameList->nextElement(keyFrameList->firstElement()); |
---|
[3798] | 172 | this->localTime = 0.0; |
---|
[3797] | 173 | } |
---|
[1853] | 174 | |
---|
[3781] | 175 | template<class T> |
---|
[3794] | 176 | void tAnim<T>::setFuncToAnim(T* object, void (T::*funcToAnim)(float)) |
---|
[3781] | 177 | { |
---|
| 178 | this->object = object; |
---|
[3783] | 179 | this->funcToAnim = funcToAnim; |
---|
[3781] | 180 | } |
---|
| 181 | |
---|
[3795] | 182 | template<class T> |
---|
| 183 | void tAnim<T>::addKeyFrame(float value, float duration, ANIM_FUNCTION animFunc) |
---|
| 184 | { |
---|
| 185 | // some small check |
---|
| 186 | if (duration <= 0.0) |
---|
| 187 | duration = 1.0; |
---|
| 188 | |
---|
[3781] | 189 | |
---|
[3795] | 190 | AnimKeyFrame* tmpKeyFrame; |
---|
| 191 | |
---|
| 192 | if (bHasKeys) |
---|
| 193 | { |
---|
| 194 | tmpKeyFrame = new AnimKeyFrame; |
---|
| 195 | if (this->currentKeyFrame == this->nextKeyFrame) |
---|
| 196 | this->nextKeyFrame = tmpKeyFrame; |
---|
| 197 | this->keyFrameList->add(tmpKeyFrame); |
---|
| 198 | |
---|
| 199 | } |
---|
| 200 | else |
---|
| 201 | { |
---|
| 202 | tmpKeyFrame = this->keyFrameList->firstElement(); |
---|
| 203 | bHasKeys = true; |
---|
| 204 | } |
---|
| 205 | tmpKeyFrame->value = value; |
---|
| 206 | tmpKeyFrame->duration = duration; |
---|
| 207 | tmpKeyFrame->animFunc = animFunc; |
---|
| 208 | |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | |
---|
[3781] | 212 | template<class T> |
---|
[3794] | 213 | void tAnim<T>::tick(float time) |
---|
[3781] | 214 | { |
---|
[3787] | 215 | if (this->bRunning) |
---|
[3786] | 216 | { |
---|
[3787] | 217 | this->localTime += time; |
---|
| 218 | if (localTime >= this->currentKeyFrame->duration) |
---|
| 219 | { |
---|
| 220 | this->localTime = 0; |
---|
| 221 | if (this->currentKeyFrame == this->keyFrameList->lastElement()) |
---|
| 222 | switch (this->postInfinity) |
---|
| 223 | { |
---|
| 224 | case ANIM_INF_CONSTANT: |
---|
| 225 | this->bRunning = false; |
---|
| 226 | break; |
---|
| 227 | case ANIM_INF_REWIND: |
---|
| 228 | break; |
---|
| 229 | } |
---|
| 230 | this->currentKeyFrame = this->keyFrameList->nextElement(this->currentKeyFrame); |
---|
| 231 | this->nextKeyFrame = this->keyFrameList->nextElement(this->nextKeyFrame); |
---|
| 232 | printf("%p from:%f to:%f\n", this->currentKeyFrame,this->currentKeyFrame->value, this->nextKeyFrame->value); |
---|
| 233 | |
---|
| 234 | |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | (this->object->*(funcToAnim))((this->*animFunc)(this->localTime)); |
---|
[3786] | 238 | } |
---|
[3781] | 239 | } |
---|
| 240 | |
---|
[3795] | 241 | |
---|
| 242 | template<class T> |
---|
| 243 | void tAnim<T>::setAnimFunc(ANIM_FUNCTION animFunc) |
---|
| 244 | { |
---|
| 245 | switch (animFunc) |
---|
| 246 | { |
---|
| 247 | default: |
---|
| 248 | case ANIM_CONSTANT: |
---|
| 249 | this->animFunc = &tAnim<T>::constant; |
---|
| 250 | break; |
---|
| 251 | case ANIM_LINEAR: |
---|
| 252 | this->animFunc = &tAnim<T>::linear; |
---|
| 253 | break; |
---|
| 254 | case ANIM_RANDOM: |
---|
| 255 | this->animFunc = &tAnim<T>::random; |
---|
| 256 | break; |
---|
| 257 | case ANIM_SINE: |
---|
| 258 | this->animFunc = &tAnim<T>::sine; |
---|
| 259 | break; |
---|
| 260 | } |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | |
---|
| 264 | // animation functions |
---|
| 265 | template<class T> |
---|
| 266 | float tAnim<T>::random(float timePassed) const |
---|
| 267 | { |
---|
| 268 | return (float)rand()/(float)RAND_MAX; |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | template<class T> |
---|
| 272 | float tAnim<T>::constant(float timePassed) const |
---|
| 273 | { |
---|
| 274 | return this->currentKeyFrame->value; |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | template<class T> |
---|
| 278 | float tAnim<T>::linear(float timePassed) const |
---|
| 279 | { |
---|
| 280 | return this->nextKeyFrame->value - (this->nextKeyFrame->value - this->currentKeyFrame->value) |
---|
| 281 | * (timePassed / this->currentKeyFrame->duration); |
---|
| 282 | // PRINTF(0)("value is %f, %p %p\n", val, this->currentKeyFrame, this->nextKeyFrame); |
---|
| 283 | // return val; |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | template<class T> |
---|
| 287 | float tAnim<T>::sine(float timePassed) const |
---|
| 288 | { |
---|
| 289 | |
---|
| 290 | } |
---|
| 291 | |
---|
[3781] | 292 | #endif /* _ANIMATION_H */ |
---|