Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/animation.h @ 3825

Last change on this file since 3825 was 3825, checked in by bensch, 19 years ago

orxonox/trunk: some more animation functions

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