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