Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: definition of animation

File size: 3.5 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
30// FORWARD DEFINITION
31template<class T> class tList;
32
33typedef enum ANIM_FUNCTION {ANIM_CONSTANT,
34                            ANIM_LINEAR,
35                            ANIM_SINE,
36                            ANIM_RANDOM};
37
38typedef enum ANIM_INFINITY {ANIM_INF_CONSTANT,
39                            ANIM_INF_LINEAR,
40                            ANIM_INF_PINGPONG,
41                            ANIM_INF_REWIND};//, ANIM_DELETE}
42
43struct AnimKeyFrame
44{
45  float duration;
46  float value;
47  ANIM_FUNCTION animFunc;
48};
49
50class Anim
51{
52 public:
53  virtual ~Anim(void);
54
55  void setInfinity(ANIM_INFINITY postInfinity = ANIM_INF_CONSTANT);
56
57  void addKeyFrame(float value, float duration, ANIM_FUNCTION animFunc = ANIM_LINEAR);
58  void setAnimFunc(ANIM_FUNCTION animFunc);
59
60  void play(); // equals resume();
61  void stop();
62  void pause();
63  void replay();
64
65  virtual void tick(float time) = 0;
66
67 protected:
68  Anim(void);
69
70  static tList<Anim>* animatorList;
71
72  // animation functions
73  float random(float timePassed) const;
74  float constant(float timePassed) const;
75  float linear(float timePassed) const;
76  float sine(float timePassed) const;
77
78
79  // variables
80  //  ANIM_FUNCTION animFunc;
81  float (Anim::*animFunc)(float) const;
82
83  ANIM_INFINITY postInfinity;
84
85  bool bHasKeys;
86  bool bRunning;
87
88  float localTime;
89  AnimKeyFrame* currentKeyFrame;
90  AnimKeyFrame* nextKeyFrame;
91  tList<AnimKeyFrame>* keyFrameList;
92};
93
94
95//! A Class to handle some animation for single floated values.
96template<class T> class tAnim : public Anim
97{
98 public:
99  tAnim(T* object = NULL, void (T::*funcToAnim)(float) = NULL);
100  virtual ~tAnim();
101
102  void setFuncToAnim(T* object, void (T::*funcToAnim)(float));
103
104  virtual void tick(float time);
105
106 private:
107  T* object;
108  void (T::*funcToAnim)(float);
109};
110
111
112
113/**
114   \brief standard constructor
115
116*/
117template<class T>
118tAnim<T>::tAnim (T* object, void (T::*funcToAnim)(float)) 
119{
120  this->setFuncToAnim(object, funcToAnim);
121}
122
123
124/**
125   \brief standard deconstructor
126
127*/
128template<class T>
129tAnim<T>::~tAnim () 
130{
131  // delete what has to be deleted here
132}
133
134
135template<class T>
136void tAnim<T>::setFuncToAnim(T* object, void (T::*funcToAnim)(float))
137{
138  this->object = object;
139  this->funcToAnim = funcToAnim;
140}
141
142
143template<class T>
144void tAnim<T>::tick(float time)
145{
146  if (this->bRunning)
147    {
148      this->localTime += time;
149      if (localTime >= this->currentKeyFrame->duration)
150        {
151          this->localTime = 0;
152          if (this->currentKeyFrame == this->keyFrameList->lastElement())
153            switch (this->postInfinity)
154              {
155              case ANIM_INF_CONSTANT:
156                this->bRunning = false;
157                break;
158              case ANIM_INF_REWIND:
159                break;
160              }
161          this->currentKeyFrame = this->keyFrameList->nextElement(this->currentKeyFrame);
162          this->nextKeyFrame = this->keyFrameList->nextElement(this->nextKeyFrame);
163          printf("%p from:%f to:%f\n", this->currentKeyFrame,this->currentKeyFrame->value, this->nextKeyFrame->value);
164         
165         
166        }
167     
168      (this->object->*(funcToAnim))((this->*animFunc)(this->localTime));
169    }
170}
171
172#endif /* _ANIMATION_H */
Note: See TracBrowser for help on using the repository browser.