[670] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * |
---|
| 4 | * |
---|
| 5 | * License notice: |
---|
| 6 | * |
---|
| 7 | * This program is free software; you can redistribute it and/or |
---|
| 8 | * modify it under the terms of the GNU General Public License |
---|
| 9 | * as published by the Free Software Foundation; either version 2 |
---|
| 10 | * of the License, or (at your option) any later version. |
---|
| 11 | * |
---|
| 12 | * This program is distributed in the hope that it will be useful, |
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | * GNU General Public License for more details. |
---|
| 16 | * |
---|
| 17 | * You should have received a copy of the GNU General Public License |
---|
| 18 | * along with this program; if not, write to the Free Software |
---|
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 20 | * |
---|
| 21 | * Author: |
---|
| 22 | * Fabian 'x3n' Landau |
---|
| 23 | * Co-authors: |
---|
| 24 | * ... |
---|
| 25 | * |
---|
| 26 | */ |
---|
| 27 | |
---|
[496] | 28 | /*! |
---|
| 29 | @file Timer.h |
---|
| 30 | @brief Definition and Implementation of the Timer class. |
---|
| 31 | |
---|
| 32 | The Timer is a callback-object, calling a given function after a given time-interval. |
---|
| 33 | |
---|
| 34 | Usage: |
---|
| 35 | header.h: |
---|
| 36 | class ClassName |
---|
| 37 | { |
---|
| 38 | public: |
---|
| 39 | ClassName(); |
---|
| 40 | void functionName(); |
---|
[640] | 41 | Timer<ClassName> myTimer; |
---|
[496] | 42 | }; |
---|
| 43 | |
---|
| 44 | source.cc: |
---|
| 45 | ClassName::ClassName() |
---|
| 46 | { |
---|
| 47 | myTimer.setTimer(interval_in_seconds, bLoop, this, &ClassName::functionName); |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | void ClassName::functionName() |
---|
| 51 | { |
---|
| 52 | whateveryouwant(); |
---|
| 53 | something(else); |
---|
| 54 | } |
---|
| 55 | */ |
---|
| 56 | |
---|
[384] | 57 | #ifndef _Timer_H__ |
---|
| 58 | #define _Timer_H__ |
---|
| 59 | |
---|
[708] | 60 | #include <OgreFrameListener.h> |
---|
| 61 | |
---|
| 62 | #include "../OrxonoxPrereqs.h" |
---|
| 63 | |
---|
[496] | 64 | #include "../core/CoreIncludes.h" |
---|
[384] | 65 | |
---|
| 66 | namespace orxonox |
---|
| 67 | { |
---|
[496] | 68 | //! TimerBase is the parent of the Timer class. |
---|
[729] | 69 | class _OrxonoxExport TimerBase : public OrxonoxClass |
---|
[384] | 70 | { |
---|
| 71 | friend class TimerFrameListener; |
---|
| 72 | |
---|
| 73 | public: |
---|
[496] | 74 | /** @brief Constructor: Sets the default-values. */ |
---|
[384] | 75 | TimerBase() |
---|
| 76 | { |
---|
| 77 | RegisterRootObject(TimerBase); |
---|
| 78 | |
---|
| 79 | this->interval_ = 0; |
---|
| 80 | this->bLoop_ = false; |
---|
| 81 | this->bActive_ = false; |
---|
| 82 | |
---|
| 83 | this->time_ = 0; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | virtual void run() const = 0; |
---|
| 87 | |
---|
[496] | 88 | /** @brief Starts the Timer: Function-call after 'interval' seconds. */ |
---|
[384] | 89 | inline void startTimer() { this->bActive_ = true; this->time_ = this->interval_; } |
---|
[496] | 90 | /** @brief Stops the Timer. */ |
---|
[384] | 91 | inline void stopTimer() { this->bActive_ = false; this->time_ = this->interval_; } |
---|
[496] | 92 | /** @brief Pauses the Timer - it will continue with the actual state if you unpause it. */ |
---|
[384] | 93 | inline void pauseTimer() { this->bActive_ = false; } |
---|
[496] | 94 | /** @brief Unpauses the Timer - continues with the given state. */ |
---|
[384] | 95 | inline void unpauseTimer() { this->bActive_ = true; } |
---|
[496] | 96 | /** @returns true if the Timer is active (= not stoped, not paused). */ |
---|
[384] | 97 | inline bool isActive() const { return this->bActive_; } |
---|
| 98 | |
---|
| 99 | protected: |
---|
[496] | 100 | float interval_; //!< The time-interval in seconds |
---|
| 101 | bool bLoop_; //!< If true, the function gets called every 'interval' seconds |
---|
| 102 | bool bActive_; //!< If true, the Timer ticks and calls the function if the time's up |
---|
[384] | 103 | |
---|
[496] | 104 | float time_; //!< Internal variable, counting the time till the next function-call |
---|
[384] | 105 | }; |
---|
[729] | 106 | ExportAbstractClass(TimerBase, Orxonox); |
---|
[384] | 107 | |
---|
[496] | 108 | //! The Timer is a callback-object, calling a given function after a given time-interval. |
---|
[384] | 109 | template <class T = BaseObject> |
---|
| 110 | class Timer : public TimerBase |
---|
| 111 | { |
---|
| 112 | public: |
---|
[496] | 113 | /** @brief Constructor: Sets the default-values. */ |
---|
[384] | 114 | Timer() |
---|
| 115 | { |
---|
| 116 | this->timerFunction_ = 0; |
---|
| 117 | this->object_ = 0; |
---|
| 118 | } |
---|
| 119 | |
---|
[496] | 120 | /** |
---|
| 121 | @brief Constructor: Initializes the Timer with given values. |
---|
| 122 | @param interval The timer-interval in seconds |
---|
| 123 | @param bLoop If true, the function gets called every 'interval' seconds |
---|
| 124 | @param object The object owning the timer and the function |
---|
| 125 | @param timerFunction A function pointer to the function to call |
---|
| 126 | */ |
---|
[384] | 127 | Timer(float interval, bool bLoop, T* object, void (T::*timerFunction)()) |
---|
| 128 | { |
---|
| 129 | this->setTimer(interval, bLoop, timerFunction, object); |
---|
| 130 | } |
---|
| 131 | |
---|
[496] | 132 | /** |
---|
| 133 | @brief Initializes the Timer with given values. |
---|
| 134 | @param interval The timer-interval in seconds |
---|
| 135 | @param bLoop If true, the function gets called every 'interval' seconds |
---|
| 136 | @param object The object owning the timer and the function |
---|
| 137 | @param timerFunction A function pointer to the function to call |
---|
| 138 | */ |
---|
[384] | 139 | void setTimer(float interval, bool bLoop, T* object, void (T::*timerFunction)()) |
---|
| 140 | { |
---|
| 141 | this->interval_ = interval; |
---|
| 142 | this->bLoop_ = bLoop; |
---|
| 143 | this->timerFunction_ = timerFunction; |
---|
| 144 | this->object_ = object; |
---|
| 145 | this->bActive_ = true; |
---|
| 146 | |
---|
| 147 | this->time_ = interval; |
---|
| 148 | } |
---|
| 149 | |
---|
[496] | 150 | /** @brief Calls the given function of the given object. */ |
---|
[384] | 151 | void run() const |
---|
| 152 | { |
---|
| 153 | ((*this->object_).*timerFunction_)(); |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | private: |
---|
| 157 | void (T::*timerFunction_)(); |
---|
| 158 | T* object_; |
---|
| 159 | }; |
---|
[729] | 160 | ExportAbstractClass(Timer<BaseObject>, Orxonox); |
---|
[384] | 161 | |
---|
[496] | 162 | //! The TimerFrameListener manages all Timers in the game. |
---|
[384] | 163 | class TimerFrameListener : public Ogre::FrameListener |
---|
| 164 | { |
---|
| 165 | private: |
---|
[496] | 166 | /** @brief Gets called before a frame gets rendered. */ |
---|
[384] | 167 | bool frameStarted(const Ogre::FrameEvent &evt) |
---|
| 168 | { |
---|
[496] | 169 | // Iterate through all Timers |
---|
[646] | 170 | for (Iterator<TimerBase> it = ObjectList<TimerBase>::start(); it; ) |
---|
[384] | 171 | { |
---|
| 172 | if (it->isActive()) |
---|
| 173 | { |
---|
[496] | 174 | // If active: Decrease the timer by the duration of the last frame |
---|
[384] | 175 | it->time_ -= evt.timeSinceLastFrame; |
---|
| 176 | |
---|
| 177 | if (it->time_ <= 0) |
---|
| 178 | { |
---|
[496] | 179 | // It's time to call the function |
---|
[384] | 180 | if (it->bLoop_) |
---|
[496] | 181 | it->time_ += it->interval_; // Q: Why '+=' and not '='? A: Think about it. It's more accurate like that. Seriously. |
---|
[384] | 182 | else |
---|
[496] | 183 | it->stopTimer(); // Stop the timer if we don't want to loop |
---|
[384] | 184 | |
---|
[646] | 185 | (it++)->run(); |
---|
[384] | 186 | } |
---|
[646] | 187 | else |
---|
| 188 | ++it; |
---|
[384] | 189 | } |
---|
[646] | 190 | else |
---|
| 191 | ++it; |
---|
[384] | 192 | } |
---|
| 193 | |
---|
| 194 | return FrameListener::frameStarted(evt); |
---|
| 195 | } |
---|
| 196 | }; |
---|
| 197 | } |
---|
| 198 | |
---|
[673] | 199 | #endif /* _Timer_H__ */ |
---|