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