[1505] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Fabian 'x3n' Landau |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | /*! |
---|
[2171] | 30 | @file |
---|
[1505] | 31 | @brief Definition and Implementation of the Timer class. |
---|
| 32 | |
---|
| 33 | The Timer is a callback-object, calling a given function after a given time-interval. |
---|
| 34 | |
---|
| 35 | Usage: |
---|
| 36 | header.h: |
---|
| 37 | class ClassName |
---|
| 38 | { |
---|
| 39 | public: |
---|
| 40 | ClassName(); |
---|
| 41 | void functionName(); |
---|
| 42 | Timer<ClassName> myTimer; |
---|
| 43 | }; |
---|
| 44 | |
---|
| 45 | source.cc: |
---|
| 46 | include "core/Executor.h" |
---|
| 47 | |
---|
| 48 | ClassName::ClassName() |
---|
| 49 | { |
---|
| 50 | myTimer.setTimer(interval_in_seconds, bLoop, this, createExecutor(createFunctor(&ClassName::functionName))); |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | void ClassName::functionName() |
---|
| 54 | { |
---|
| 55 | whateveryouwant(); |
---|
| 56 | something(else); |
---|
| 57 | } |
---|
| 58 | */ |
---|
| 59 | |
---|
| 60 | #ifndef _Timer_H__ |
---|
| 61 | #define _Timer_H__ |
---|
| 62 | |
---|
| 63 | #include "OrxonoxPrereqs.h" |
---|
[3131] | 64 | |
---|
[2662] | 65 | #include "core/Executor.h" |
---|
[1755] | 66 | #include "core/OrxonoxClass.h" |
---|
[2896] | 67 | #include "tools/TimeFactorListener.h" |
---|
[1505] | 68 | |
---|
| 69 | namespace orxonox |
---|
| 70 | { |
---|
| 71 | class StaticTimer; |
---|
| 72 | void delay(float delay, const std::string& command); |
---|
| 73 | void killdelays(); |
---|
| 74 | void executeDelayedCommand(StaticTimer* timer, const std::string& command); |
---|
| 75 | |
---|
| 76 | //! TimerBase is the parent of the Timer class. |
---|
[2662] | 77 | class _OrxonoxExport TimerBase : public TimeFactorListener |
---|
[1505] | 78 | { |
---|
| 79 | public: |
---|
| 80 | ~TimerBase(); |
---|
| 81 | |
---|
| 82 | void run() const; |
---|
[1552] | 83 | void deleteExecutor(); |
---|
[1505] | 84 | |
---|
| 85 | /** @brief Starts the Timer: Function-call after 'interval' seconds. */ |
---|
| 86 | inline void startTimer() |
---|
| 87 | { this->bActive_ = true; this->time_ = this->interval_; } |
---|
| 88 | /** @brief Stops the Timer. */ |
---|
| 89 | inline void stopTimer() |
---|
| 90 | { this->bActive_ = false; this->time_ = this->interval_; } |
---|
| 91 | /** @brief Pauses the Timer - it will continue with the actual state if you unpause it. */ |
---|
| 92 | inline void pauseTimer() |
---|
| 93 | { this->bActive_ = false; } |
---|
| 94 | /** @brief Unpauses the Timer - continues with the given state. */ |
---|
| 95 | inline void unpauseTimer() |
---|
| 96 | { this->bActive_ = true; } |
---|
| 97 | /** @brief Returns true if the Timer is active (= not stoped, not paused). @return True = Time is active */ |
---|
| 98 | inline bool isActive() const |
---|
| 99 | { return this->bActive_; } |
---|
[1608] | 100 | /** @brief Returns the remaining time until the Timer calls the function. @return The remaining time */ |
---|
| 101 | inline float getRemainingTime() const |
---|
[1755] | 102 | { return (float)this->time_ / 1000000.0f; } |
---|
[1505] | 103 | /** @brief Gives the Timer some extra time. @param time The amount of extra time in seconds */ |
---|
| 104 | inline void addTime(float time) |
---|
[1755] | 105 | { if (time > 0.0f) this->time_ += (long long)(time * 1000000.0f); } |
---|
[1505] | 106 | /** @brief Decreases the remaining time of the Timer. @param time The amount of time to remove */ |
---|
| 107 | inline void removeTime(float time) |
---|
[1755] | 108 | { if (time > 0.0f) this->time_ -= (long long)(time * 1000000.0f); } |
---|
[1505] | 109 | /** @brief Sets the interval of the Timer. @param interval The interval */ |
---|
| 110 | inline void setInterval(float interval) |
---|
[1755] | 111 | { this->interval_ = (long long)(interval * 1000000.0f); } |
---|
[1505] | 112 | /** @brief Sets bLoop to a given value. @param bLoop True = loop */ |
---|
| 113 | inline void setLoop(bool bLoop) |
---|
| 114 | { this->bLoop_ = bLoop; } |
---|
| 115 | |
---|
[1755] | 116 | void tick(const Clock& time); |
---|
[1505] | 117 | |
---|
| 118 | protected: |
---|
| 119 | TimerBase(); |
---|
| 120 | |
---|
[2087] | 121 | Executor* executor_; //!< The executor of the function that should be called when the time expires |
---|
[1505] | 122 | |
---|
[2087] | 123 | long long interval_; //!< The time-interval in micro seconds |
---|
| 124 | bool bLoop_; //!< If true, the function gets called every 'interval' seconds |
---|
| 125 | bool bActive_; //!< If true, the Timer ticks and calls the function if the time's up |
---|
| 126 | bool bKillAfterCall_; //!< If true the timer gets deleted after it called the function |
---|
[1505] | 127 | |
---|
[2087] | 128 | long long time_; //!< Internal variable, counting the time till the next function-call |
---|
[1505] | 129 | }; |
---|
| 130 | |
---|
| 131 | //! The Timer is a callback-object, calling a given function after a given time-interval. |
---|
| 132 | template <class T = BaseObject> |
---|
| 133 | class Timer : public TimerBase |
---|
| 134 | { |
---|
| 135 | public: |
---|
| 136 | Timer() {} |
---|
| 137 | |
---|
| 138 | /** |
---|
| 139 | @brief Constructor: Initializes the Timer with given values. |
---|
| 140 | @param interval The timer-interval in seconds |
---|
| 141 | @param bLoop If true, the function gets called every 'interval' seconds |
---|
| 142 | @param object The object owning the timer and the function |
---|
| 143 | @param exeuctor A executor of the function to call |
---|
| 144 | */ |
---|
[2087] | 145 | Timer(float interval, bool bLoop, T* object, ExecutorMember<T>* exeuctor, bool bKillAfterCall = false) |
---|
[1505] | 146 | { |
---|
[2087] | 147 | this->setTimer(interval, bLoop, object, exeuctor, bKillAfterCall); |
---|
[1505] | 148 | } |
---|
| 149 | |
---|
| 150 | /** |
---|
| 151 | @brief Initializes the Timer with given values. |
---|
| 152 | @param interval The timer-interval in seconds |
---|
| 153 | @param bLoop If true, the function gets called every 'interval' seconds |
---|
| 154 | @param object The object owning the timer and the function |
---|
| 155 | @param exeuctor A executor of the function to call |
---|
| 156 | */ |
---|
[2087] | 157 | void setTimer(float interval, bool bLoop, T* object, ExecutorMember<T>* executor, bool bKillAfterCall = false) |
---|
[1505] | 158 | { |
---|
[1552] | 159 | this->deleteExecutor(); |
---|
| 160 | |
---|
[1755] | 161 | this->setInterval(interval); |
---|
[1505] | 162 | this->bLoop_ = bLoop; |
---|
| 163 | executor->setObject(object); |
---|
[2662] | 164 | this->executor_ = static_cast<Executor*>(executor); |
---|
[1505] | 165 | this->bActive_ = true; |
---|
| 166 | |
---|
[1755] | 167 | this->time_ = this->interval_; |
---|
[2087] | 168 | this->bKillAfterCall_ = bKillAfterCall; |
---|
[1505] | 169 | } |
---|
| 170 | }; |
---|
| 171 | |
---|
| 172 | //! The StaticTimer is a callback-object, calling a static function after a given time-interval. |
---|
| 173 | class StaticTimer : public TimerBase |
---|
| 174 | { |
---|
| 175 | public: |
---|
| 176 | StaticTimer() {} |
---|
| 177 | |
---|
| 178 | /** |
---|
| 179 | @brief Constructor: Initializes the Timer with given values. |
---|
| 180 | @param interval The timer-interval in seconds |
---|
| 181 | @param bLoop If true, the function gets called every 'interval' seconds |
---|
| 182 | @param exeuctor A executor of the function to call |
---|
| 183 | */ |
---|
[2087] | 184 | StaticTimer(float interval, bool bLoop, ExecutorStatic* executor, bool bKillAfterCall = false) |
---|
[1505] | 185 | { |
---|
[2087] | 186 | this->setTimer(interval, bLoop, executor, bKillAfterCall); |
---|
[1505] | 187 | } |
---|
| 188 | |
---|
| 189 | /** |
---|
| 190 | @brief Initializes the Timer with given values. |
---|
| 191 | @param interval The timer-interval in seconds |
---|
| 192 | @param bLoop If true, the function gets called every 'interval' seconds |
---|
| 193 | @param object The object owning the timer and the function |
---|
| 194 | @param executor A executor of the function to call |
---|
| 195 | */ |
---|
[2087] | 196 | void setTimer(float interval, bool bLoop, ExecutorStatic* executor, bool bKillAfterCall = false) |
---|
[1505] | 197 | { |
---|
[1552] | 198 | this->deleteExecutor(); |
---|
| 199 | |
---|
[1755] | 200 | this->setInterval(interval); |
---|
[1505] | 201 | this->bLoop_ = bLoop; |
---|
[2662] | 202 | this->executor_ = executor; |
---|
[1505] | 203 | this->bActive_ = true; |
---|
| 204 | |
---|
[1755] | 205 | this->time_ = this->interval_; |
---|
[2087] | 206 | this->bKillAfterCall_ = bKillAfterCall; |
---|
[1505] | 207 | } |
---|
| 208 | }; |
---|
| 209 | |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | #endif /* _Timer_H__ */ |
---|