[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: |
---|
[995] | 45 | include "core/Executor.h" |
---|
| 46 | |
---|
[496] | 47 | ClassName::ClassName() |
---|
| 48 | { |
---|
[995] | 49 | myTimer.setTimer(interval_in_seconds, bLoop, this, createExecutor(createFunctor(&ClassName::functionName))); |
---|
[496] | 50 | } |
---|
| 51 | |
---|
| 52 | void ClassName::functionName() |
---|
| 53 | { |
---|
| 54 | whateveryouwant(); |
---|
| 55 | something(else); |
---|
| 56 | } |
---|
| 57 | */ |
---|
| 58 | |
---|
[384] | 59 | #ifndef _Timer_H__ |
---|
| 60 | #define _Timer_H__ |
---|
| 61 | |
---|
[708] | 62 | #include <OgreFrameListener.h> |
---|
[995] | 63 | #include "OrxonoxPrereqs.h" |
---|
| 64 | #include "core/CorePrereqs.h" |
---|
[708] | 65 | |
---|
[384] | 66 | namespace orxonox |
---|
| 67 | { |
---|
[995] | 68 | class StaticTimer; |
---|
| 69 | void delay(float delay, const std::string& command); |
---|
| 70 | void executeDelayedCommand(StaticTimer* timer, const std::string& command); |
---|
| 71 | |
---|
[496] | 72 | //! TimerBase is the parent of the Timer class. |
---|
[729] | 73 | class _OrxonoxExport TimerBase : public OrxonoxClass |
---|
[384] | 74 | { |
---|
| 75 | friend class TimerFrameListener; |
---|
| 76 | |
---|
| 77 | public: |
---|
[995] | 78 | ~TimerBase(); |
---|
[384] | 79 | |
---|
[995] | 80 | void run() const; |
---|
[384] | 81 | |
---|
[496] | 82 | /** @brief Starts the Timer: Function-call after 'interval' seconds. */ |
---|
[995] | 83 | inline void startTimer() |
---|
| 84 | { this->bActive_ = true; this->time_ = this->interval_; } |
---|
[496] | 85 | /** @brief Stops the Timer. */ |
---|
[995] | 86 | inline void stopTimer() |
---|
| 87 | { this->bActive_ = false; this->time_ = this->interval_; } |
---|
[496] | 88 | /** @brief Pauses the Timer - it will continue with the actual state if you unpause it. */ |
---|
[995] | 89 | inline void pauseTimer() |
---|
| 90 | { this->bActive_ = false; } |
---|
[496] | 91 | /** @brief Unpauses the Timer - continues with the given state. */ |
---|
[995] | 92 | inline void unpauseTimer() |
---|
| 93 | { this->bActive_ = true; } |
---|
[871] | 94 | /** @brief Returns true if the Timer is active (= not stoped, not paused). @return True = Time is active */ |
---|
[995] | 95 | inline bool isActive() const |
---|
| 96 | { return this->bActive_; } |
---|
[957] | 97 | /** @brief Gives the Timer some extra time. @param time The amount of extra time in seconds */ |
---|
[995] | 98 | inline void addTime(float time) |
---|
| 99 | { this->time_ += time; } |
---|
[957] | 100 | /** @brief Decreases the remaining time of the Timer. @param time The amount of time to remove */ |
---|
[995] | 101 | inline void removeTime(float time) |
---|
| 102 | { this->time_ -= time; } |
---|
| 103 | /** @brief Sets the interval of the Timer. @param interval The interval */ |
---|
| 104 | inline void setInterval(float interval) |
---|
| 105 | { this->interval_ = interval; } |
---|
| 106 | /** @brief Sets bLoop to a given value. @param bLoop True = loop */ |
---|
| 107 | inline void setLoop(bool bLoop) |
---|
| 108 | { this->bLoop_ = bLoop; } |
---|
[384] | 109 | |
---|
| 110 | protected: |
---|
[995] | 111 | TimerBase(); |
---|
[384] | 112 | |
---|
[995] | 113 | Executor* executor_; //!< The executor of the function that should be called when the time expires |
---|
| 114 | |
---|
| 115 | float interval_; //!< The time-interval in seconds |
---|
| 116 | bool bLoop_; //!< If true, the function gets called every 'interval' seconds |
---|
| 117 | bool bActive_; //!< If true, the Timer ticks and calls the function if the time's up |
---|
| 118 | |
---|
| 119 | float time_; //!< Internal variable, counting the time till the next function-call |
---|
[384] | 120 | }; |
---|
| 121 | |
---|
[496] | 122 | //! The Timer is a callback-object, calling a given function after a given time-interval. |
---|
[384] | 123 | template <class T = BaseObject> |
---|
| 124 | class Timer : public TimerBase |
---|
| 125 | { |
---|
| 126 | public: |
---|
[995] | 127 | Timer() {} |
---|
[384] | 128 | |
---|
[496] | 129 | /** |
---|
| 130 | @brief Constructor: Initializes the Timer with given values. |
---|
| 131 | @param interval The timer-interval in seconds |
---|
| 132 | @param bLoop If true, the function gets called every 'interval' seconds |
---|
| 133 | @param object The object owning the timer and the function |
---|
[995] | 134 | @param exeuctor A executor of the function to call |
---|
[496] | 135 | */ |
---|
[995] | 136 | Timer(float interval, bool bLoop, T* object, ExecutorMember<T>* exeuctor) |
---|
[384] | 137 | { |
---|
[995] | 138 | this->setTimer(interval, bLoop, object, exeuctor); |
---|
[384] | 139 | } |
---|
| 140 | |
---|
[496] | 141 | /** |
---|
| 142 | @brief Initializes the Timer with given values. |
---|
| 143 | @param interval The timer-interval in seconds |
---|
| 144 | @param bLoop If true, the function gets called every 'interval' seconds |
---|
| 145 | @param object The object owning the timer and the function |
---|
[995] | 146 | @param exeuctor A executor of the function to call |
---|
[496] | 147 | */ |
---|
[995] | 148 | void setTimer(float interval, bool bLoop, T* object, ExecutorMember<T>* executor) |
---|
[384] | 149 | { |
---|
| 150 | this->interval_ = interval; |
---|
| 151 | this->bLoop_ = bLoop; |
---|
[995] | 152 | executor->setObject(object); |
---|
| 153 | this->executor_ = (Executor*)executor; |
---|
[384] | 154 | this->bActive_ = true; |
---|
| 155 | |
---|
| 156 | this->time_ = interval; |
---|
| 157 | } |
---|
[995] | 158 | }; |
---|
[384] | 159 | |
---|
[995] | 160 | //! The StaticTimer is a callback-object, calling a static function after a given time-interval. |
---|
| 161 | class StaticTimer : public TimerBase |
---|
| 162 | { |
---|
| 163 | public: |
---|
| 164 | StaticTimer() {} |
---|
| 165 | |
---|
| 166 | /** |
---|
| 167 | @brief Constructor: Initializes the Timer with given values. |
---|
| 168 | @param interval The timer-interval in seconds |
---|
| 169 | @param bLoop If true, the function gets called every 'interval' seconds |
---|
| 170 | @param exeuctor A executor of the function to call |
---|
| 171 | */ |
---|
| 172 | StaticTimer(float interval, bool bLoop, ExecutorStatic* executor) |
---|
[384] | 173 | { |
---|
[995] | 174 | this->setTimer(interval, bLoop, executor); |
---|
[384] | 175 | } |
---|
| 176 | |
---|
[995] | 177 | /** |
---|
| 178 | @brief Initializes the Timer with given values. |
---|
| 179 | @param interval The timer-interval in seconds |
---|
| 180 | @param bLoop If true, the function gets called every 'interval' seconds |
---|
| 181 | @param object The object owning the timer and the function |
---|
| 182 | @param executor A executor of the function to call |
---|
| 183 | */ |
---|
| 184 | void setTimer(float interval, bool bLoop, ExecutorStatic* executor) |
---|
| 185 | { |
---|
| 186 | this->interval_ = interval; |
---|
| 187 | this->bLoop_ = bLoop; |
---|
| 188 | this->executor_ = (Executor*)executor; |
---|
| 189 | this->bActive_ = true; |
---|
| 190 | |
---|
| 191 | this->time_ = interval; |
---|
| 192 | } |
---|
[384] | 193 | }; |
---|
| 194 | |
---|
[496] | 195 | //! The TimerFrameListener manages all Timers in the game. |
---|
[384] | 196 | class TimerFrameListener : public Ogre::FrameListener |
---|
| 197 | { |
---|
| 198 | private: |
---|
[496] | 199 | /** @brief Gets called before a frame gets rendered. */ |
---|
[384] | 200 | bool frameStarted(const Ogre::FrameEvent &evt) |
---|
| 201 | { |
---|
[496] | 202 | // Iterate through all Timers |
---|
[646] | 203 | for (Iterator<TimerBase> it = ObjectList<TimerBase>::start(); it; ) |
---|
[384] | 204 | { |
---|
| 205 | if (it->isActive()) |
---|
| 206 | { |
---|
[496] | 207 | // If active: Decrease the timer by the duration of the last frame |
---|
[384] | 208 | it->time_ -= evt.timeSinceLastFrame; |
---|
| 209 | |
---|
| 210 | if (it->time_ <= 0) |
---|
| 211 | { |
---|
[496] | 212 | // It's time to call the function |
---|
[384] | 213 | if (it->bLoop_) |
---|
[970] | 214 | { |
---|
[496] | 215 | it->time_ += it->interval_; // Q: Why '+=' and not '='? A: Think about it. It's more accurate like that. Seriously. |
---|
[970] | 216 | while (it->time_ <= 0) |
---|
| 217 | { |
---|
| 218 | // The interval was shorter than one tick, so execute the function more than once |
---|
| 219 | it->run(); |
---|
| 220 | it->time_ += it->interval_; |
---|
| 221 | } |
---|
| 222 | } |
---|
[384] | 223 | else |
---|
[496] | 224 | it->stopTimer(); // Stop the timer if we don't want to loop |
---|
[384] | 225 | |
---|
[646] | 226 | (it++)->run(); |
---|
[384] | 227 | } |
---|
[646] | 228 | else |
---|
| 229 | ++it; |
---|
[384] | 230 | } |
---|
[646] | 231 | else |
---|
| 232 | ++it; |
---|
[384] | 233 | } |
---|
| 234 | |
---|
| 235 | return FrameListener::frameStarted(evt); |
---|
| 236 | } |
---|
| 237 | }; |
---|
| 238 | } |
---|
| 239 | |
---|
[673] | 240 | #endif /* _Timer_H__ */ |
---|