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