| 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 | |
|---|
| 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(); |
|---|
| 41 | Timer<ClassName> myTimer; |
|---|
| 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 | |
|---|
| 57 | #ifndef _Timer_H__ |
|---|
| 58 | #define _Timer_H__ |
|---|
| 59 | |
|---|
| 60 | #include "../core/CoreIncludes.h" |
|---|
| 61 | #include "OgreFrameListener.h" |
|---|
| 62 | |
|---|
| 63 | namespace orxonox |
|---|
| 64 | { |
|---|
| 65 | //! TimerBase is the parent of the Timer class. |
|---|
| 66 | class TimerBase : public OrxonoxClass |
|---|
| 67 | { |
|---|
| 68 | friend class TimerFrameListener; |
|---|
| 69 | |
|---|
| 70 | public: |
|---|
| 71 | /** @brief Constructor: Sets the default-values. */ |
|---|
| 72 | TimerBase() |
|---|
| 73 | { |
|---|
| 74 | RegisterRootObject(TimerBase); |
|---|
| 75 | |
|---|
| 76 | this->interval_ = 0; |
|---|
| 77 | this->bLoop_ = false; |
|---|
| 78 | this->bActive_ = false; |
|---|
| 79 | |
|---|
| 80 | this->time_ = 0; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | virtual void run() const = 0; |
|---|
| 84 | |
|---|
| 85 | /** @brief Starts the Timer: Function-call after 'interval' seconds. */ |
|---|
| 86 | inline void startTimer() { this->bActive_ = true; this->time_ = this->interval_; } |
|---|
| 87 | /** @brief Stops the Timer. */ |
|---|
| 88 | inline void stopTimer() { this->bActive_ = false; this->time_ = this->interval_; } |
|---|
| 89 | /** @brief Pauses the Timer - it will continue with the actual state if you unpause it. */ |
|---|
| 90 | inline void pauseTimer() { this->bActive_ = false; } |
|---|
| 91 | /** @brief Unpauses the Timer - continues with the given state. */ |
|---|
| 92 | inline void unpauseTimer() { this->bActive_ = true; } |
|---|
| 93 | /** @returns true if the Timer is active (= not stoped, not paused). */ |
|---|
| 94 | inline bool isActive() const { return this->bActive_; } |
|---|
| 95 | |
|---|
| 96 | protected: |
|---|
| 97 | float interval_; //!< The time-interval in seconds |
|---|
| 98 | bool bLoop_; //!< If true, the function gets called every 'interval' seconds |
|---|
| 99 | bool bActive_; //!< If true, the Timer ticks and calls the function if the time's up |
|---|
| 100 | |
|---|
| 101 | float time_; //!< Internal variable, counting the time till the next function-call |
|---|
| 102 | }; |
|---|
| 103 | |
|---|
| 104 | //! The Timer is a callback-object, calling a given function after a given time-interval. |
|---|
| 105 | template <class T = BaseObject> |
|---|
| 106 | class Timer : public TimerBase |
|---|
| 107 | { |
|---|
| 108 | public: |
|---|
| 109 | /** @brief Constructor: Sets the default-values. */ |
|---|
| 110 | Timer() |
|---|
| 111 | { |
|---|
| 112 | this->timerFunction_ = 0; |
|---|
| 113 | this->object_ = 0; |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | /** |
|---|
| 117 | @brief Constructor: Initializes the Timer with given values. |
|---|
| 118 | @param interval The timer-interval in seconds |
|---|
| 119 | @param bLoop If true, the function gets called every 'interval' seconds |
|---|
| 120 | @param object The object owning the timer and the function |
|---|
| 121 | @param timerFunction A function pointer to the function to call |
|---|
| 122 | */ |
|---|
| 123 | Timer(float interval, bool bLoop, T* object, void (T::*timerFunction)()) |
|---|
| 124 | { |
|---|
| 125 | this->setTimer(interval, bLoop, timerFunction, object); |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | /** |
|---|
| 129 | @brief Initializes the Timer with given values. |
|---|
| 130 | @param interval The timer-interval in seconds |
|---|
| 131 | @param bLoop If true, the function gets called every 'interval' seconds |
|---|
| 132 | @param object The object owning the timer and the function |
|---|
| 133 | @param timerFunction A function pointer to the function to call |
|---|
| 134 | */ |
|---|
| 135 | void setTimer(float interval, bool bLoop, T* object, void (T::*timerFunction)()) |
|---|
| 136 | { |
|---|
| 137 | this->interval_ = interval; |
|---|
| 138 | this->bLoop_ = bLoop; |
|---|
| 139 | this->timerFunction_ = timerFunction; |
|---|
| 140 | this->object_ = object; |
|---|
| 141 | this->bActive_ = true; |
|---|
| 142 | |
|---|
| 143 | this->time_ = interval; |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | /** @brief Calls the given function of the given object. */ |
|---|
| 147 | void run() const |
|---|
| 148 | { |
|---|
| 149 | ((*this->object_).*timerFunction_)(); |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | private: |
|---|
| 153 | void (T::*timerFunction_)(); |
|---|
| 154 | T* object_; |
|---|
| 155 | }; |
|---|
| 156 | |
|---|
| 157 | //! The TimerFrameListener manages all Timers in the game. |
|---|
| 158 | class TimerFrameListener : public Ogre::FrameListener |
|---|
| 159 | { |
|---|
| 160 | private: |
|---|
| 161 | /** @brief Gets called before a frame gets rendered. */ |
|---|
| 162 | bool frameStarted(const Ogre::FrameEvent &evt) |
|---|
| 163 | { |
|---|
| 164 | // Iterate through all Timers |
|---|
| 165 | for (Iterator<TimerBase> it = ObjectList<TimerBase>::start(); it; ) |
|---|
| 166 | { |
|---|
| 167 | if (it->isActive()) |
|---|
| 168 | { |
|---|
| 169 | // If active: Decrease the timer by the duration of the last frame |
|---|
| 170 | it->time_ -= evt.timeSinceLastFrame; |
|---|
| 171 | |
|---|
| 172 | if (it->time_ <= 0) |
|---|
| 173 | { |
|---|
| 174 | // It's time to call the function |
|---|
| 175 | if (it->bLoop_) |
|---|
| 176 | it->time_ += it->interval_; // Q: Why '+=' and not '='? A: Think about it. It's more accurate like that. Seriously. |
|---|
| 177 | else |
|---|
| 178 | it->stopTimer(); // Stop the timer if we don't want to loop |
|---|
| 179 | |
|---|
| 180 | (it++)->run(); |
|---|
| 181 | } |
|---|
| 182 | else |
|---|
| 183 | ++it; |
|---|
| 184 | } |
|---|
| 185 | else |
|---|
| 186 | ++it; |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | return FrameListener::frameStarted(evt); |
|---|
| 190 | } |
|---|
| 191 | }; |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | #endif /* _Timer_H__ */ |
|---|