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