[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(); |
---|
[3795] | 60 | // virtual void rewind(); |
---|
[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 | |
---|
[3783] | 93 | void setFuncToAnim(T* object, void (T::*funcToAnim)(float)); |
---|
[3795] | 94 | void addKeyFrame(float value, float duration, ANIM_FUNCTION animFunc = ANIM_LINEAR); |
---|
[2036] | 95 | |
---|
[3795] | 96 | |
---|
| 97 | // animation functions |
---|
| 98 | void setAnimFunc(ANIM_FUNCTION animFunc); |
---|
| 99 | |
---|
| 100 | float random(float timePassed) const; |
---|
| 101 | float constant(float timePassed) const; |
---|
| 102 | float linear(float timePassed) const; |
---|
| 103 | float sine(float timePassed) const; |
---|
| 104 | // ANIM_FUNCTION animFunc; |
---|
| 105 | float (tAnim<T>::*animFunc)(float) const; |
---|
| 106 | AnimKeyFrame* currentKeyFrame; |
---|
| 107 | AnimKeyFrame* nextKeyFrame; |
---|
| 108 | tList<AnimKeyFrame>* keyFrameList; |
---|
| 109 | |
---|
| 110 | |
---|
| 111 | |
---|
| 112 | |
---|
[3783] | 113 | virtual void tick(float time); |
---|
[3781] | 114 | |
---|
| 115 | private: |
---|
[3784] | 116 | T* object; |
---|
[3783] | 117 | void (T::*funcToAnim)(float); |
---|
[3781] | 118 | }; |
---|
| 119 | |
---|
| 120 | |
---|
| 121 | |
---|
[3329] | 122 | /** |
---|
[3781] | 123 | \brief standard constructor |
---|
| 124 | |
---|
[3329] | 125 | */ |
---|
[3781] | 126 | template<class T> |
---|
[3794] | 127 | tAnim<T>::tAnim (T* object, void (T::*funcToAnim)(float)) |
---|
[3781] | 128 | { |
---|
[3795] | 129 | // create a new List |
---|
| 130 | this->keyFrameList = new tList<AnimKeyFrame>(); |
---|
| 131 | AnimKeyFrame* tmpKeyFrame = new AnimKeyFrame; |
---|
| 132 | tmpKeyFrame->value = 0.0; |
---|
| 133 | tmpKeyFrame->duration = 1.0; |
---|
| 134 | keyFrameList->add(tmpKeyFrame); |
---|
| 135 | |
---|
| 136 | this->currentKeyFrame = tmpKeyFrame; |
---|
| 137 | this->nextKeyFrame = tmpKeyFrame; |
---|
| 138 | |
---|
| 139 | this->animFunc = &tAnim<T>::linear; |
---|
| 140 | |
---|
[3784] | 141 | this->setFuncToAnim(object, funcToAnim); |
---|
[3781] | 142 | } |
---|
[1853] | 143 | |
---|
| 144 | |
---|
[3781] | 145 | /** |
---|
| 146 | \brief standard deconstructor |
---|
[3245] | 147 | |
---|
[3781] | 148 | */ |
---|
| 149 | template<class T> |
---|
[3794] | 150 | tAnim<T>::~tAnim () |
---|
[3781] | 151 | { |
---|
[3795] | 152 | // delete all the KeyFrames |
---|
| 153 | tIterator<AnimKeyFrame>* itKF = keyFrameList->getIterator(); |
---|
| 154 | AnimKeyFrame* enumKF = itKF->nextElement(); |
---|
| 155 | while (enumKF) |
---|
| 156 | { |
---|
| 157 | delete enumKF; |
---|
| 158 | enumKF = itKF->nextElement(); |
---|
| 159 | } |
---|
| 160 | delete itKF; |
---|
| 161 | delete this->keyFrameList; |
---|
| 162 | |
---|
[3781] | 163 | } |
---|
[3245] | 164 | |
---|
[1853] | 165 | |
---|
[3781] | 166 | template<class T> |
---|
[3794] | 167 | void tAnim<T>::setFuncToAnim(T* object, void (T::*funcToAnim)(float)) |
---|
[3781] | 168 | { |
---|
| 169 | this->object = object; |
---|
[3783] | 170 | this->funcToAnim = funcToAnim; |
---|
[3781] | 171 | } |
---|
| 172 | |
---|
[3795] | 173 | template<class T> |
---|
| 174 | void tAnim<T>::addKeyFrame(float value, float duration, ANIM_FUNCTION animFunc) |
---|
| 175 | { |
---|
| 176 | // some small check |
---|
| 177 | if (duration <= 0.0) |
---|
| 178 | duration = 1.0; |
---|
| 179 | |
---|
[3781] | 180 | |
---|
[3795] | 181 | AnimKeyFrame* tmpKeyFrame; |
---|
| 182 | |
---|
| 183 | if (bHasKeys) |
---|
| 184 | { |
---|
| 185 | tmpKeyFrame = new AnimKeyFrame; |
---|
| 186 | if (this->currentKeyFrame == this->nextKeyFrame) |
---|
| 187 | this->nextKeyFrame = tmpKeyFrame; |
---|
| 188 | this->keyFrameList->add(tmpKeyFrame); |
---|
| 189 | |
---|
| 190 | } |
---|
| 191 | else |
---|
| 192 | { |
---|
| 193 | tmpKeyFrame = this->keyFrameList->firstElement(); |
---|
| 194 | bHasKeys = true; |
---|
| 195 | } |
---|
| 196 | tmpKeyFrame->value = value; |
---|
| 197 | tmpKeyFrame->duration = duration; |
---|
| 198 | tmpKeyFrame->animFunc = animFunc; |
---|
| 199 | |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | |
---|
[3781] | 203 | template<class T> |
---|
[3794] | 204 | void tAnim<T>::tick(float time) |
---|
[3781] | 205 | { |
---|
[3787] | 206 | if (this->bRunning) |
---|
[3786] | 207 | { |
---|
[3787] | 208 | this->localTime += time; |
---|
| 209 | if (localTime >= this->currentKeyFrame->duration) |
---|
| 210 | { |
---|
| 211 | this->localTime = 0; |
---|
| 212 | if (this->currentKeyFrame == this->keyFrameList->lastElement()) |
---|
| 213 | switch (this->postInfinity) |
---|
| 214 | { |
---|
| 215 | case ANIM_INF_CONSTANT: |
---|
| 216 | this->bRunning = false; |
---|
| 217 | break; |
---|
| 218 | case ANIM_INF_REWIND: |
---|
| 219 | break; |
---|
| 220 | } |
---|
| 221 | this->currentKeyFrame = this->keyFrameList->nextElement(this->currentKeyFrame); |
---|
| 222 | this->nextKeyFrame = this->keyFrameList->nextElement(this->nextKeyFrame); |
---|
| 223 | printf("%p from:%f to:%f\n", this->currentKeyFrame,this->currentKeyFrame->value, this->nextKeyFrame->value); |
---|
| 224 | |
---|
| 225 | |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | (this->object->*(funcToAnim))((this->*animFunc)(this->localTime)); |
---|
[3786] | 229 | } |
---|
[3781] | 230 | } |
---|
| 231 | |
---|
[3795] | 232 | |
---|
| 233 | template<class T> |
---|
| 234 | void tAnim<T>::setAnimFunc(ANIM_FUNCTION animFunc) |
---|
| 235 | { |
---|
| 236 | switch (animFunc) |
---|
| 237 | { |
---|
| 238 | default: |
---|
| 239 | case ANIM_CONSTANT: |
---|
| 240 | this->animFunc = &tAnim<T>::constant; |
---|
| 241 | break; |
---|
| 242 | case ANIM_LINEAR: |
---|
| 243 | this->animFunc = &tAnim<T>::linear; |
---|
| 244 | break; |
---|
| 245 | case ANIM_RANDOM: |
---|
| 246 | this->animFunc = &tAnim<T>::random; |
---|
| 247 | break; |
---|
| 248 | case ANIM_SINE: |
---|
| 249 | this->animFunc = &tAnim<T>::sine; |
---|
| 250 | break; |
---|
| 251 | } |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | |
---|
| 255 | // animation functions |
---|
| 256 | template<class T> |
---|
| 257 | float tAnim<T>::random(float timePassed) const |
---|
| 258 | { |
---|
| 259 | return (float)rand()/(float)RAND_MAX; |
---|
| 260 | } |
---|
| 261 | |
---|
| 262 | template<class T> |
---|
| 263 | float tAnim<T>::constant(float timePassed) const |
---|
| 264 | { |
---|
| 265 | return this->currentKeyFrame->value; |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | template<class T> |
---|
| 269 | float tAnim<T>::linear(float timePassed) const |
---|
| 270 | { |
---|
| 271 | return this->nextKeyFrame->value - (this->nextKeyFrame->value - this->currentKeyFrame->value) |
---|
| 272 | * (timePassed / this->currentKeyFrame->duration); |
---|
| 273 | // PRINTF(0)("value is %f, %p %p\n", val, this->currentKeyFrame, this->nextKeyFrame); |
---|
| 274 | // return val; |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | template<class T> |
---|
| 278 | float tAnim<T>::sine(float timePassed) const |
---|
| 279 | { |
---|
| 280 | |
---|
| 281 | } |
---|
| 282 | |
---|
[3781] | 283 | #endif /* _ANIMATION_H */ |
---|