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