Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: added the time-functions to the amimation

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