Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: moved some functions around in the Animation-classes

File size: 5.9 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();
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  void setFuncToAnim(T* object, void (T::*funcToAnim)(float));
94  void addKeyFrame(float value, float duration, ANIM_FUNCTION animFunc = ANIM_LINEAR);
95
96
97  // animation functions
98  void setAnimFunc(ANIM_FUNCTION animFunc);
99
100  float random(float timePassed) const;
101  float constant(float timePassed) const;
102  float linear(float timePassed) const;
103  float sine(float timePassed) const;
104  //  ANIM_FUNCTION animFunc;
105  float (tAnim<T>::*animFunc)(float) const;
106  AnimKeyFrame* currentKeyFrame;
107  AnimKeyFrame* nextKeyFrame;
108  tList<AnimKeyFrame>* keyFrameList;
109
110
111
112
113  virtual void tick(float time);
114
115 private:
116  T* object;
117  void (T::*funcToAnim)(float);
118};
119
120
121
122/**
123   \brief standard constructor
124
125*/
126template<class T>
127tAnim<T>::tAnim (T* object, void (T::*funcToAnim)(float)) 
128{
129  // create a new List
130  this->keyFrameList = new tList<AnimKeyFrame>();
131  AnimKeyFrame* tmpKeyFrame = new AnimKeyFrame;
132  tmpKeyFrame->value = 0.0;
133  tmpKeyFrame->duration = 1.0;
134  keyFrameList->add(tmpKeyFrame);
135
136  this->currentKeyFrame = tmpKeyFrame;
137  this->nextKeyFrame = tmpKeyFrame;
138
139  this->animFunc = &tAnim<T>::linear;
140
141  this->setFuncToAnim(object, funcToAnim);
142}
143
144
145/**
146   \brief standard deconstructor
147
148*/
149template<class T>
150tAnim<T>::~tAnim () 
151{
152  // delete all the KeyFrames
153  tIterator<AnimKeyFrame>* itKF = keyFrameList->getIterator();
154  AnimKeyFrame*  enumKF = itKF->nextElement();
155  while (enumKF)
156    {
157      delete enumKF;
158      enumKF = itKF->nextElement();
159    }
160  delete itKF;
161  delete this->keyFrameList;
162
163}
164
165
166template<class T>
167void tAnim<T>::setFuncToAnim(T* object, void (T::*funcToAnim)(float))
168{
169  this->object = object;
170  this->funcToAnim = funcToAnim;
171}
172
173template<class T>
174void tAnim<T>::addKeyFrame(float value, float duration, ANIM_FUNCTION animFunc)
175{
176  // some small check
177  if (duration <= 0.0)
178    duration = 1.0;
179 
180
181  AnimKeyFrame* tmpKeyFrame;
182   
183  if (bHasKeys)
184    {
185      tmpKeyFrame = new AnimKeyFrame;
186      if (this->currentKeyFrame == this->nextKeyFrame)
187        this->nextKeyFrame = tmpKeyFrame;
188      this->keyFrameList->add(tmpKeyFrame);
189
190    }
191  else
192    {
193      tmpKeyFrame = this->keyFrameList->firstElement();
194      bHasKeys = true;
195    }
196  tmpKeyFrame->value = value;
197  tmpKeyFrame->duration = duration;
198  tmpKeyFrame->animFunc = animFunc;
199 
200}
201
202
203template<class T>
204void tAnim<T>::tick(float time)
205{
206  if (this->bRunning)
207    {
208      this->localTime += time;
209      if (localTime >= this->currentKeyFrame->duration)
210        {
211          this->localTime = 0;
212          if (this->currentKeyFrame == this->keyFrameList->lastElement())
213            switch (this->postInfinity)
214              {
215              case ANIM_INF_CONSTANT:
216                this->bRunning = false;
217                break;
218              case ANIM_INF_REWIND:
219                break;
220              }
221          this->currentKeyFrame = this->keyFrameList->nextElement(this->currentKeyFrame);
222          this->nextKeyFrame = this->keyFrameList->nextElement(this->nextKeyFrame);
223          printf("%p from:%f to:%f\n", this->currentKeyFrame,this->currentKeyFrame->value, this->nextKeyFrame->value);
224         
225         
226        }
227     
228      (this->object->*(funcToAnim))((this->*animFunc)(this->localTime));
229    }
230}
231
232
233template<class T>
234void tAnim<T>::setAnimFunc(ANIM_FUNCTION animFunc)
235{
236  switch (animFunc)
237    {
238    default:
239    case ANIM_CONSTANT:
240      this->animFunc = &tAnim<T>::constant;
241      break;
242    case ANIM_LINEAR:
243      this->animFunc = &tAnim<T>::linear;
244      break;
245    case ANIM_RANDOM:
246      this->animFunc = &tAnim<T>::random;
247      break;
248    case ANIM_SINE:
249      this->animFunc = &tAnim<T>::sine;
250      break;
251    }
252}
253
254
255// animation functions
256template<class T>
257float tAnim<T>::random(float timePassed) const
258{
259  return (float)rand()/(float)RAND_MAX;
260}
261
262template<class T>
263float tAnim<T>::constant(float timePassed) const
264{
265  return this->currentKeyFrame->value;
266}
267
268template<class T>
269float tAnim<T>::linear(float timePassed) const 
270{
271  return this->nextKeyFrame->value - (this->nextKeyFrame->value - this->currentKeyFrame->value) 
272    * (timePassed / this->currentKeyFrame->duration);
273  //  PRINTF(0)("value is %f, %p %p\n", val, this->currentKeyFrame, this->nextKeyFrame);
274  //  return val;
275}
276
277template<class T>
278float tAnim<T>::sine(float timePassed) const
279{
280 
281}
282
283#endif /* _ANIMATION_H */
Note: See TracBrowser for help on using the repository browser.