Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: play/rewind and so on work perfectly. (you can try them out on [1-5] keys
1 play
2 stop
3 pause
4 replay
5 rewind

File size: 6.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 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  this->localTime = 0.0;
173}
174
175template<class T>
176void tAnim<T>::setFuncToAnim(T* object, void (T::*funcToAnim)(float))
177{
178  this->object = object;
179  this->funcToAnim = funcToAnim;
180}
181
182template<class T>
183void tAnim<T>::addKeyFrame(float value, float duration, ANIM_FUNCTION animFunc)
184{
185  // some small check
186  if (duration <= 0.0)
187    duration = 1.0;
188 
189
190  AnimKeyFrame* tmpKeyFrame;
191   
192  if (bHasKeys)
193    {
194      tmpKeyFrame = new AnimKeyFrame;
195      if (this->currentKeyFrame == this->nextKeyFrame)
196        this->nextKeyFrame = tmpKeyFrame;
197      this->keyFrameList->add(tmpKeyFrame);
198
199    }
200  else
201    {
202      tmpKeyFrame = this->keyFrameList->firstElement();
203      bHasKeys = true;
204    }
205  tmpKeyFrame->value = value;
206  tmpKeyFrame->duration = duration;
207  tmpKeyFrame->animFunc = animFunc;
208 
209}
210
211
212template<class T>
213void tAnim<T>::tick(float time)
214{
215  if (this->bRunning)
216    {
217      this->localTime += time;
218      if (localTime >= this->currentKeyFrame->duration)
219        {
220          this->localTime = 0;
221          if (this->currentKeyFrame == this->keyFrameList->lastElement())
222            switch (this->postInfinity)
223              {
224              case ANIM_INF_CONSTANT:
225                this->bRunning = false;
226                break;
227              case ANIM_INF_REWIND:
228                break;
229              }
230          this->currentKeyFrame = this->keyFrameList->nextElement(this->currentKeyFrame);
231          this->nextKeyFrame = this->keyFrameList->nextElement(this->nextKeyFrame);
232          printf("%p from:%f to:%f\n", this->currentKeyFrame,this->currentKeyFrame->value, this->nextKeyFrame->value);
233         
234         
235        }
236     
237      (this->object->*(funcToAnim))((this->*animFunc)(this->localTime));
238    }
239}
240
241
242template<class T>
243void tAnim<T>::setAnimFunc(ANIM_FUNCTION animFunc)
244{
245  switch (animFunc)
246    {
247    default:
248    case ANIM_CONSTANT:
249      this->animFunc = &tAnim<T>::constant;
250      break;
251    case ANIM_LINEAR:
252      this->animFunc = &tAnim<T>::linear;
253      break;
254    case ANIM_RANDOM:
255      this->animFunc = &tAnim<T>::random;
256      break;
257    case ANIM_SINE:
258      this->animFunc = &tAnim<T>::sine;
259      break;
260    }
261}
262
263
264// animation functions
265template<class T>
266float tAnim<T>::random(float timePassed) const
267{
268  return (float)rand()/(float)RAND_MAX;
269}
270
271template<class T>
272float tAnim<T>::constant(float timePassed) const
273{
274  return this->currentKeyFrame->value;
275}
276
277template<class T>
278float tAnim<T>::linear(float timePassed) const 
279{
280  return this->nextKeyFrame->value - (this->nextKeyFrame->value - this->currentKeyFrame->value) 
281    * (timePassed / this->currentKeyFrame->duration);
282  //  PRINTF(0)("value is %f, %p %p\n", val, this->currentKeyFrame, this->nextKeyFrame);
283  //  return val;
284}
285
286template<class T>
287float tAnim<T>::sine(float timePassed) const
288{
289 
290}
291
292#endif /* _ANIMATION_H */
Note: See TracBrowser for help on using the repository browser.