[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 | |
---|
[1853] | 29 | |
---|
[3782] | 30 | // FORWARD DEFINITION |
---|
| 31 | template<class T> class tList; |
---|
| 32 | |
---|
[3784] | 33 | typedef enum ANIM_FUNCTION {ANIM_CONSTANT, |
---|
| 34 | ANIM_LINEAR, |
---|
| 35 | ANIM_SINE, |
---|
| 36 | ANIM_RANDOM}; |
---|
[3787] | 37 | |
---|
[3784] | 38 | typedef enum ANIM_INFINITY {ANIM_INF_CONSTANT, |
---|
| 39 | ANIM_INF_LINEAR, |
---|
| 40 | ANIM_INF_PINGPONG, |
---|
| 41 | ANIM_INF_REWIND};//, ANIM_DELETE} |
---|
[3543] | 42 | |
---|
[3784] | 43 | struct AnimKeyFrame |
---|
| 44 | { |
---|
| 45 | float duration; |
---|
| 46 | float value; |
---|
| 47 | ANIM_FUNCTION animFunc; |
---|
| 48 | }; |
---|
| 49 | |
---|
[3782] | 50 | class Anim |
---|
| 51 | { |
---|
[3785] | 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 | |
---|
[3782] | 58 | protected: |
---|
[3784] | 59 | Anim(void); |
---|
| 60 | virtual ~Anim(void); |
---|
[3782] | 61 | |
---|
| 62 | static tList<Anim>* animatorList; |
---|
[3783] | 63 | |
---|
| 64 | virtual void tick(float time) = 0; |
---|
[3784] | 65 | |
---|
| 66 | |
---|
| 67 | |
---|
| 68 | // animation functions |
---|
[3786] | 69 | float random(float timePassed) const; |
---|
| 70 | float constant(float timePassed) const; |
---|
| 71 | float linear(float timePassed) const; |
---|
| 72 | float sine(float timePassed) const; |
---|
[3784] | 73 | |
---|
| 74 | |
---|
| 75 | // variables |
---|
| 76 | // ANIM_FUNCTION animFunc; |
---|
[3785] | 77 | float (Anim::*animFunc)(float) const; |
---|
[3784] | 78 | ANIM_INFINITY preInfinity; |
---|
| 79 | ANIM_INFINITY postInfinity; |
---|
| 80 | |
---|
| 81 | bool bHasKeys; |
---|
[3787] | 82 | bool bRunning; |
---|
[3785] | 83 | |
---|
[3786] | 84 | float localTime; |
---|
[3785] | 85 | AnimKeyFrame* currentKeyFrame; |
---|
| 86 | AnimKeyFrame* nextKeyFrame; |
---|
[3784] | 87 | tList<AnimKeyFrame>* keyFrameList; |
---|
[3782] | 88 | }; |
---|
| 89 | |
---|
| 90 | |
---|
[3781] | 91 | //! A Class to handle some animation for single floated values. |
---|
[3782] | 92 | template<class T> class Animation : public Anim |
---|
[3781] | 93 | { |
---|
| 94 | public: |
---|
[3784] | 95 | Animation(T* object = NULL, void (T::*funcToAnim)(float) = NULL); |
---|
[3781] | 96 | virtual ~Animation(); |
---|
[3543] | 97 | |
---|
[3783] | 98 | void setFuncToAnim(T* object, void (T::*funcToAnim)(float)); |
---|
[2036] | 99 | |
---|
[3783] | 100 | virtual void tick(float time); |
---|
[3781] | 101 | |
---|
| 102 | private: |
---|
[3784] | 103 | T* object; |
---|
[3783] | 104 | void (T::*funcToAnim)(float); |
---|
[3781] | 105 | }; |
---|
| 106 | |
---|
| 107 | |
---|
| 108 | |
---|
[3329] | 109 | /** |
---|
[3781] | 110 | \brief standard constructor |
---|
| 111 | |
---|
[3329] | 112 | */ |
---|
[3781] | 113 | template<class T> |
---|
[3784] | 114 | Animation<T>::Animation (T* object, void (T::*funcToAnim)(float)) |
---|
[3781] | 115 | { |
---|
[3784] | 116 | this->setFuncToAnim(object, funcToAnim); |
---|
[3781] | 117 | } |
---|
[1853] | 118 | |
---|
| 119 | |
---|
[3781] | 120 | /** |
---|
| 121 | \brief standard deconstructor |
---|
[3245] | 122 | |
---|
[3781] | 123 | */ |
---|
| 124 | template<class T> |
---|
| 125 | Animation<T>::~Animation () |
---|
| 126 | { |
---|
| 127 | // delete what has to be deleted here |
---|
| 128 | } |
---|
[3245] | 129 | |
---|
[1853] | 130 | |
---|
[3781] | 131 | template<class T> |
---|
[3783] | 132 | void Animation<T>::setFuncToAnim(T* object, void (T::*funcToAnim)(float)) |
---|
[3781] | 133 | { |
---|
| 134 | this->object = object; |
---|
[3783] | 135 | this->funcToAnim = funcToAnim; |
---|
[3781] | 136 | } |
---|
| 137 | |
---|
| 138 | |
---|
| 139 | template<class T> |
---|
| 140 | void Animation<T>::tick(float time) |
---|
| 141 | { |
---|
[3787] | 142 | if (this->bRunning) |
---|
[3786] | 143 | { |
---|
[3787] | 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)); |
---|
[3786] | 165 | } |
---|
[3781] | 166 | } |
---|
| 167 | |
---|
| 168 | #endif /* _ANIMATION_H */ |
---|