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 = last - f; 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 | } |
---|
235 | tmpKeyFrame->value = value; |
---|
236 | tmpKeyFrame->duration = duration; |
---|
237 | tmpKeyFrame->animFunc = animFunc; |
---|
238 | |
---|
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 | |
---|
264 | |
---|
265 | } |
---|
266 | |
---|
267 | (this->object->*(funcToAnim))((this->*animFunc)(this->localTime)); |
---|
268 | } |
---|
269 | } |
---|
270 | |
---|
271 | |
---|
272 | template<class T> |
---|
273 | void tAnim<T>::setAnimFunc(ANIM_FUNCTION animFunc) |
---|
274 | { |
---|
275 | switch (animFunc) |
---|
276 | { |
---|
277 | default: |
---|
278 | case ANIM_CONSTANT: |
---|
279 | this->animFunc = &tAnim<T>::constant; |
---|
280 | break; |
---|
281 | case ANIM_LINEAR: |
---|
282 | this->animFunc = &tAnim<T>::linear; |
---|
283 | break; |
---|
284 | case ANIM_SINE: |
---|
285 | this->animFunc = &tAnim<T>::sine; |
---|
286 | break; |
---|
287 | case ANIM_COSINE: |
---|
288 | this->animFunc = &tAnim<T>::cosine; |
---|
289 | break; |
---|
290 | case ANIM_EXP: |
---|
291 | this->animFunc = &tAnim<T>::exp; |
---|
292 | break; |
---|
293 | case ANIM_NEG_EXP: |
---|
294 | this->animFunc = &tAnim<T>::negExp; |
---|
295 | break; |
---|
296 | case ANIM_SINE: |
---|
297 | this->animFunc = &tAnim<T>::quadratic; |
---|
298 | break; |
---|
299 | case ANIM_RANDOM: |
---|
300 | this->animFunc = &tAnim<T>::random; |
---|
301 | break; |
---|
302 | } |
---|
303 | } |
---|
304 | |
---|
305 | |
---|
306 | // animation functions |
---|
307 | template<class T> |
---|
308 | float tAnim<T>::random(float timePassed) const |
---|
309 | { |
---|
310 | return (float)rand()/(float)RAND_MAX; |
---|
311 | } |
---|
312 | |
---|
313 | template<class T> |
---|
314 | float tAnim<T>::constant(float timePassed) const |
---|
315 | { |
---|
316 | return this->currentKeyFrame->value; |
---|
317 | } |
---|
318 | |
---|
319 | template<class T> |
---|
320 | float tAnim<T>::linear(float timePassed) const |
---|
321 | { |
---|
322 | return this->nextKeyFrame->value - (this->nextKeyFrame->value - this->currentKeyFrame->value) |
---|
323 | * (timePassed / this->currentKeyFrame->duration); |
---|
324 | // PRINTF(0)("value is %f, %p %p\n", val, this->currentKeyFrame, this->nextKeyFrame); |
---|
325 | // return val; |
---|
326 | } |
---|
327 | |
---|
328 | template<class T> |
---|
329 | float tAnim<T>::sine(float timePassed) const |
---|
330 | { |
---|
331 | return this->currentkeyFrame->value - (this->nextKeyFrame->value - this->currentKeyFrame->value) |
---|
332 | * sin(timePassed / this->currentKeyFrame->duration * M_PI); |
---|
333 | } |
---|
334 | |
---|
335 | template<class T> |
---|
336 | float tAnim<T>::cosine(float timePassed) const |
---|
337 | { |
---|
338 | return this->currentkeyFrame->value - (this->nextKeyFrame->value - this->currentKeyFrame->value) |
---|
339 | * cos(timePassed / this->currentKeyFrame->duration * M_PI); |
---|
340 | } |
---|
341 | |
---|
342 | template<class T> |
---|
343 | float tAnim<T>::exp(float timePassed) const |
---|
344 | { |
---|
345 | |
---|
346 | } |
---|
347 | |
---|
348 | template<class T> |
---|
349 | float tAnim<T>::negExp(float timePassed) const |
---|
350 | { |
---|
351 | |
---|
352 | } |
---|
353 | |
---|
354 | template<class T> |
---|
355 | float tAnim<T>::quadratic(float timePassed) const |
---|
356 | { |
---|
357 | |
---|
358 | } |
---|
359 | |
---|
360 | |
---|
361 | |
---|
362 | |
---|
363 | #endif /* _ANIMATION_H */ |
---|