[3849] | 1 | |
---|
| 2 | |
---|
| 3 | |
---|
| 4 | #include "animation.h" |
---|
| 5 | |
---|
| 6 | //! A Class to handle some animation for single floated values. |
---|
| 7 | template<class T> class tAnimation : public Animation |
---|
| 8 | { |
---|
| 9 | public: |
---|
| 10 | tAnimation(T* object = NULL, void (T::*funcToAnim)(float) = NULL); |
---|
| 11 | virtual ~tAnimation(); |
---|
| 12 | |
---|
| 13 | virtual void rewind(); |
---|
| 14 | |
---|
| 15 | void setFuncToAnim(T* object, void (T::*funcToAnim)(float)); |
---|
| 16 | void addKeyFrame(float value, float duration, ANIM_FUNCTION animFunc = ANIM_LINEAR); |
---|
| 17 | |
---|
| 18 | virtual void tick(float time); |
---|
| 19 | |
---|
| 20 | // animation functions |
---|
| 21 | void setAnimFunc(ANIM_FUNCTION animFunc); |
---|
| 22 | |
---|
| 23 | float constant(float timePassed) const; |
---|
| 24 | float linear(float timePassed) const; |
---|
| 25 | float sine(float timePassed) const; |
---|
| 26 | float cosine(float timePassed) const; |
---|
| 27 | float exp(float timePassed) const; |
---|
| 28 | float negExp(float timePassed) const; |
---|
| 29 | float quadratic(float timePassed) const; |
---|
| 30 | float random(float timePassed) const; |
---|
| 31 | // ANIM_FUNCTION animFunc; |
---|
| 32 | float (tAnimation<T>::*animFunc)(float) const; |
---|
| 33 | KeyFrameF* currentKeyFrame; |
---|
| 34 | KeyFrameF* nextKeyFrame; |
---|
| 35 | tList<KeyFrameF>* keyFrameList; |
---|
| 36 | |
---|
| 37 | |
---|
| 38 | |
---|
| 39 | |
---|
| 40 | private: |
---|
| 41 | float expFactor; |
---|
| 42 | T* object; |
---|
| 43 | void (T::*funcToAnim)(float); |
---|
| 44 | }; |
---|
| 45 | |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | /** |
---|
| 49 | \brief standard constructor |
---|
| 50 | |
---|
| 51 | */ |
---|
| 52 | template<class T> |
---|
| 53 | tAnimation<T>::tAnimation (T* object, void (T::*funcToAnim)(float)) |
---|
| 54 | { |
---|
| 55 | // create a new List |
---|
| 56 | this->keyFrameList = new tList<KeyFrameF>(); |
---|
| 57 | KeyFrameF* tmpKeyFrame = new KeyFrameF; |
---|
| 58 | tmpKeyFrame->value = 0.0; |
---|
| 59 | tmpKeyFrame->duration = 1.0; |
---|
| 60 | keyFrameList->add(tmpKeyFrame); |
---|
| 61 | |
---|
| 62 | this->currentKeyFrame = tmpKeyFrame; |
---|
| 63 | this->nextKeyFrame = tmpKeyFrame; |
---|
| 64 | |
---|
| 65 | this->animFunc = &tAnimation<T>::linear; |
---|
| 66 | |
---|
| 67 | this->setFuncToAnim(object, funcToAnim); |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | |
---|
| 71 | /** |
---|
| 72 | \brief standard deconstructor |
---|
| 73 | |
---|
| 74 | */ |
---|
| 75 | template<class T> |
---|
| 76 | tAnimation<T>::~tAnimation () |
---|
| 77 | { |
---|
| 78 | // delete all the KeyFrames |
---|
| 79 | tIterator<KeyFrameF>* itKF = keyFrameList->getIterator(); |
---|
| 80 | KeyFrameF* enumKF = itKF->nextElement(); |
---|
| 81 | while (enumKF) |
---|
| 82 | { |
---|
| 83 | delete enumKF; |
---|
| 84 | enumKF = itKF->nextElement(); |
---|
| 85 | } |
---|
| 86 | delete itKF; |
---|
| 87 | delete this->keyFrameList; |
---|
| 88 | |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | template<class T> |
---|
| 92 | void tAnimation<T>::rewind(void) |
---|
| 93 | { |
---|
| 94 | this->currentKeyFrame = keyFrameList->firstElement(); |
---|
| 95 | this->nextKeyFrame = keyFrameList->nextElement(keyFrameList->firstElement()); |
---|
| 96 | this->localTime = 0.0; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | template<class T> |
---|
| 100 | void tAnimation<T>::setFuncToAnim(T* object, void (T::*funcToAnim)(float)) |
---|
| 101 | { |
---|
| 102 | this->baseObject = this->object = object; |
---|
| 103 | this->funcToAnim = funcToAnim; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | template<class T> |
---|
| 107 | void tAnimation<T>::addKeyFrame(float value, float duration, ANIM_FUNCTION animFunc) |
---|
| 108 | { |
---|
| 109 | // some small check |
---|
| 110 | if (duration <= 0.0) |
---|
| 111 | duration = 1.0; |
---|
| 112 | |
---|
| 113 | |
---|
| 114 | KeyFrameF* tmpKeyFrame; |
---|
| 115 | |
---|
| 116 | if (bHasKeys) |
---|
| 117 | { |
---|
| 118 | tmpKeyFrame = new KeyFrameF; |
---|
| 119 | if (this->currentKeyFrame == this->nextKeyFrame) |
---|
| 120 | this->nextKeyFrame = tmpKeyFrame; |
---|
| 121 | this->keyFrameList->add(tmpKeyFrame); |
---|
| 122 | |
---|
| 123 | } |
---|
| 124 | else |
---|
| 125 | { |
---|
| 126 | tmpKeyFrame = this->keyFrameList->firstElement(); |
---|
| 127 | bHasKeys = true; |
---|
| 128 | this->setAnimFunc(animFunc); |
---|
| 129 | } |
---|
| 130 | tmpKeyFrame->value = value; |
---|
| 131 | tmpKeyFrame->duration = duration; |
---|
| 132 | tmpKeyFrame->animFunc = animFunc; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | |
---|
| 136 | template<class T> |
---|
| 137 | void tAnimation<T>::tick(float time) |
---|
| 138 | { |
---|
| 139 | if (this->bRunning) |
---|
| 140 | { |
---|
| 141 | this->localTime += time; |
---|
| 142 | if (localTime >= this->currentKeyFrame->duration) |
---|
| 143 | { |
---|
| 144 | // switching to the next Key-Frame |
---|
| 145 | this->currentKeyFrame = this->nextKeyFrame; |
---|
| 146 | this->localTime = 0; |
---|
| 147 | // checking, if we should still Play the animation |
---|
| 148 | if (this->currentKeyFrame == this->keyFrameList->lastElement()) |
---|
| 149 | { |
---|
| 150 | switch (this->postInfinity) |
---|
| 151 | { |
---|
| 152 | case ANIM_INF_CONSTANT: |
---|
| 153 | this->bRunning = false; |
---|
| 154 | break; |
---|
| 155 | case ANIM_INF_REWIND: |
---|
| 156 | break; |
---|
| 157 | } |
---|
| 158 | } |
---|
| 159 | this->nextKeyFrame = this->keyFrameList->nextElement(this->nextKeyFrame); |
---|
| 160 | printf("%p from:%f to:%f\n", this->currentKeyFrame,this->currentKeyFrame->value, this->nextKeyFrame->value); |
---|
| 161 | this->setAnimFunc(this->currentKeyFrame->animFunc); |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | (this->object->*(funcToAnim))((this->*animFunc)(this->localTime)); |
---|
| 165 | } |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | |
---|
| 169 | template<class T> |
---|
| 170 | void tAnimation<T>::setAnimFunc(ANIM_FUNCTION animFunc) |
---|
| 171 | { |
---|
| 172 | switch (animFunc) |
---|
| 173 | { |
---|
| 174 | default: |
---|
| 175 | case ANIM_CONSTANT: |
---|
| 176 | this->animFunc = &tAnimation<T>::constant; |
---|
| 177 | break; |
---|
| 178 | case ANIM_LINEAR: |
---|
| 179 | this->animFunc = &tAnimation<T>::linear; |
---|
| 180 | break; |
---|
| 181 | case ANIM_SINE: |
---|
| 182 | this->animFunc = &tAnimation<T>::sine; |
---|
| 183 | break; |
---|
| 184 | case ANIM_COSINE: |
---|
| 185 | this->animFunc = &tAnimation<T>::cosine; |
---|
| 186 | break; |
---|
| 187 | case ANIM_EXP: |
---|
| 188 | this->animFunc = &tAnimation<T>::exp; |
---|
| 189 | break; |
---|
| 190 | case ANIM_NEG_EXP: |
---|
| 191 | { |
---|
| 192 | this->animFunc = &tAnimation<T>::negExp; |
---|
| 193 | float d = fabs(this->currentKeyFrame->value - this->nextKeyFrame->value); |
---|
| 194 | expFactor = - 1.0 / this->currentKeyFrame->duration * logf(DELTA_X); |
---|
| 195 | break; |
---|
| 196 | } |
---|
| 197 | case ANIM_QUADRATIC: |
---|
| 198 | this->animFunc = &tAnimation<T>::quadratic; |
---|
| 199 | break; |
---|
| 200 | case ANIM_RANDOM: |
---|
| 201 | this->animFunc = &tAnimation<T>::random; |
---|
| 202 | break; |
---|
| 203 | } |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | |
---|
| 207 | // animation functions |
---|
| 208 | template<class T> |
---|
| 209 | float tAnimation<T>::random(float timePassed) const |
---|
| 210 | { |
---|
| 211 | return (float)rand()/(float)RAND_MAX; |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | template<class T> |
---|
| 215 | float tAnimation<T>::constant(float timePassed) const |
---|
| 216 | { |
---|
| 217 | return this->currentKeyFrame->value; |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | template<class T> |
---|
| 221 | float tAnimation<T>::linear(float timePassed) const |
---|
| 222 | { |
---|
| 223 | return this->currentKeyFrame->value + (this->nextKeyFrame->value - this->currentKeyFrame->value) |
---|
| 224 | * (timePassed / this->currentKeyFrame->duration); |
---|
| 225 | // PRINTF(0)("value is %f, %p %p\n", val, this->currentKeyFrame, this->nextKeyFrame); |
---|
| 226 | // return val; |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | template<class T> |
---|
| 230 | float tAnimation<T>::sine(float timePassed) const |
---|
| 231 | { |
---|
| 232 | float d = this->currentKeyFrame->value - this->nextKeyFrame->value; |
---|
| 233 | float e = 0.5 * d * (1 - cos(M_PI * timePassed / this->currentKeyFrame->duration)); |
---|
| 234 | return this->currentKeyFrame->value - e; |
---|
| 235 | /* |
---|
| 236 | return his->currentKeyFrame->value - (this->nextKeyFrame->value - this->currentKeyFrame->value) |
---|
| 237 | * sin(timePassed / this->currentKeyFrame->duration * M_PI); |
---|
| 238 | */ |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | template<class T> |
---|
| 242 | float tAnimation<T>::cosine(float timePassed) const |
---|
| 243 | { |
---|
| 244 | float d = this->currentKeyFrame->value - this->nextKeyFrame->value; |
---|
| 245 | float e = 0.5 * d * (sin(M_PI * timePassed / this->currentKeyFrame->duration)); |
---|
| 246 | if( timePassed > 0.5*this->currentKeyFrame->duration) e = (d - e); |
---|
| 247 | return this->currentKeyFrame->value - e; |
---|
| 248 | /* |
---|
| 249 | return this->currentKeyFrame->value - (this->nextKeyFrame->value - this->currentKeyFrame->value) |
---|
| 250 | * cos(timePassed / this->currentKeyFrame->duration * M_PI); |
---|
| 251 | */ |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | template<class T> |
---|
| 255 | float tAnimation<T>::exp(float timePassed) const |
---|
| 256 | { |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | template<class T> |
---|
| 260 | float tAnimation<T>::negExp(float timePassed) const |
---|
| 261 | { |
---|
| 262 | float d = this->currentKeyFrame->value - this->nextKeyFrame->value; |
---|
| 263 | float e = d * (1.0 - expf(- timePassed * expFactor)); |
---|
| 264 | return this->currentKeyFrame->value - e; |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | template<class T> |
---|
| 268 | float tAnimation<T>::quadratic(float timePassed) const |
---|
| 269 | { |
---|
| 270 | |
---|
| 271 | } |
---|