Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3824 was 3820, checked in by bensch, 20 years ago

orxonox/trunk: more descriptive comments

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