Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/t_animation.h @ 3853

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

orxonox/trunk: doxygen-comments

File size: 7.2 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 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.
27template<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*/
72template<class T>
73tAnimation<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*/
95template<class T>
96tAnimation<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
111template<class T>
112void tAnimation<T>::rewind(void)
113{
114  this->currentKeyFrame = keyFrameList->firstElement();
115  this->nextKeyFrame = keyFrameList->nextElement(keyFrameList->firstElement());
116  this->localTime = 0.0;
117}
118
119template<class T>
120void tAnimation<T>::setFuncToAnim(T* object, void (T::*funcToAnim)(float))
121{
122  this->baseObject = this->object = object;
123  this->funcToAnim = funcToAnim;
124}
125
126template<class T>
127void 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
155template<class T>
156void 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->localTime = 0.0;
174                  this->bRunning = false;
175                  break;
176                case ANIM_INF_REWIND:
177                  break;
178                }
179            }
180          this->nextKeyFrame = this->keyFrameList->nextElement(this->nextKeyFrame);
181          printf("%p from:%f to:%f\n", this->currentKeyFrame,this->currentKeyFrame->value, this->nextKeyFrame->value);
182          this->setAnimFunc(this->currentKeyFrame->animFunc);     
183        }
184     
185      (this->object->*(funcToAnim))((this->*animFunc)(this->localTime));
186    }
187}
188
189
190template<class T>
191void tAnimation<T>::setAnimFunc(ANIM_FUNCTION animFunc)
192{
193  switch (animFunc)
194    {
195    default:
196    case ANIM_CONSTANT:
197      this->animFunc = &tAnimation<T>::constant;
198      break;
199    case ANIM_LINEAR:
200      this->animFunc = &tAnimation<T>::linear;
201      break;
202    case ANIM_SINE:
203      this->animFunc = &tAnimation<T>::sine;
204      break;
205    case ANIM_COSINE:
206      this->animFunc = &tAnimation<T>::cosine;
207      break;
208    case ANIM_EXP:
209      this->animFunc = &tAnimation<T>::exp;
210      break;
211    case ANIM_NEG_EXP:
212      {
213        this->animFunc = &tAnimation<T>::negExp;
214        float d = fabs(this->currentKeyFrame->value - this->nextKeyFrame->value);
215        expFactor =  - 1.0 / this->currentKeyFrame->duration * logf(DELTA_X);
216        break;
217      }
218    case ANIM_QUADRATIC:
219      this->animFunc = &tAnimation<T>::quadratic;
220      break;
221    case ANIM_RANDOM:
222      this->animFunc = &tAnimation<T>::random;
223      break;
224    }
225}
226
227
228// animation functions
229template<class T>
230float tAnimation<T>::random(float timePassed) const
231{
232  return (float)rand()/(float)RAND_MAX;
233}
234
235template<class T>
236float tAnimation<T>::constant(float timePassed) const
237{
238  return this->currentKeyFrame->value;
239}
240
241template<class T>
242float tAnimation<T>::linear(float timePassed) const 
243{
244  return this->currentKeyFrame->value + (this->nextKeyFrame->value - this->currentKeyFrame->value) 
245    * (timePassed / this->currentKeyFrame->duration);
246  //  PRINTF(0)("value is %f, %p %p\n", val, this->currentKeyFrame, this->nextKeyFrame);
247  //  return val;
248}
249
250template<class T>
251float tAnimation<T>::sine(float timePassed) const
252{
253  float d = this->currentKeyFrame->value - this->nextKeyFrame->value;
254  float e = 0.5 * d * (1 - cos(M_PI * timePassed / this->currentKeyFrame->duration));
255  return this->currentKeyFrame->value - e;
256  /*
257  return his->currentKeyFrame->value - (this->nextKeyFrame->value - this->currentKeyFrame->value)
258    * sin(timePassed / this->currentKeyFrame->duration * M_PI);
259  */
260}
261
262template<class T>
263float tAnimation<T>::cosine(float timePassed) const
264{
265  float d = this->currentKeyFrame->value - this->nextKeyFrame->value;
266  float e = 0.5 * d * (sin(M_PI * timePassed / this->currentKeyFrame->duration));
267  if( timePassed > 0.5*this->currentKeyFrame->duration) e = (d - e);
268  return this->currentKeyFrame->value - e;
269  /*
270  return this->currentKeyFrame->value - (this->nextKeyFrame->value - this->currentKeyFrame->value)
271    * cos(timePassed / this->currentKeyFrame->duration * M_PI);
272  */
273}
274
275template<class T>
276float tAnimation<T>::exp(float timePassed) const
277{
278}
279
280template<class T>
281float tAnimation<T>::negExp(float timePassed) const
282{
283  float d = this->currentKeyFrame->value - this->nextKeyFrame->value;
284  float e = d * (1.0 - expf(- timePassed * expFactor));
285  return  this->currentKeyFrame->value - e;
286}
287
288template<class T>
289float tAnimation<T>::quadratic(float timePassed) const
290{
291
292}
293
294
295#endif /* _T_ANIMATION_H */
Note: See TracBrowser for help on using the repository browser.