[670] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
[1056] | 3 | * > www.orxonox.net < |
---|
[670] | 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 | |
---|
[496] | 29 | /*! |
---|
| 30 | @file Timer.h |
---|
| 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(); |
---|
[640] | 42 | Timer<ClassName> myTimer; |
---|
[496] | 43 | }; |
---|
| 44 | |
---|
| 45 | source.cc: |
---|
[1052] | 46 | include "core/Executor.h" |
---|
| 47 | |
---|
[496] | 48 | ClassName::ClassName() |
---|
| 49 | { |
---|
[1052] | 50 | myTimer.setTimer(interval_in_seconds, bLoop, this, createExecutor(createFunctor(&ClassName::functionName))); |
---|
[496] | 51 | } |
---|
| 52 | |
---|
| 53 | void ClassName::functionName() |
---|
| 54 | { |
---|
| 55 | whateveryouwant(); |
---|
| 56 | something(else); |
---|
| 57 | } |
---|
| 58 | */ |
---|
| 59 | |
---|
[384] | 60 | #ifndef _Timer_H__ |
---|
| 61 | #define _Timer_H__ |
---|
| 62 | |
---|
[1039] | 63 | #include "OrxonoxPrereqs.h" |
---|
[1052] | 64 | #include "core/CorePrereqs.h" |
---|
[1021] | 65 | #include "core/Tickable.h" |
---|
[708] | 66 | |
---|
[384] | 67 | namespace orxonox |
---|
| 68 | { |
---|
[1052] | 69 | class StaticTimer; |
---|
| 70 | void delay(float delay, const std::string& command); |
---|
| 71 | void executeDelayedCommand(StaticTimer* timer, const std::string& command); |
---|
| 72 | |
---|
[496] | 73 | //! TimerBase is the parent of the Timer class. |
---|
[1021] | 74 | class _OrxonoxExport TimerBase : public Tickable |
---|
[384] | 75 | { |
---|
| 76 | public: |
---|
[1052] | 77 | ~TimerBase(); |
---|
[384] | 78 | |
---|
[1052] | 79 | void run() const; |
---|
[384] | 80 | |
---|
[496] | 81 | /** @brief Starts the Timer: Function-call after 'interval' seconds. */ |
---|
[1052] | 82 | inline void startTimer() |
---|
| 83 | { this->bActive_ = true; this->time_ = this->interval_; } |
---|
[496] | 84 | /** @brief Stops the Timer. */ |
---|
[1052] | 85 | inline void stopTimer() |
---|
| 86 | { this->bActive_ = false; this->time_ = this->interval_; } |
---|
[496] | 87 | /** @brief Pauses the Timer - it will continue with the actual state if you unpause it. */ |
---|
[1052] | 88 | inline void pauseTimer() |
---|
| 89 | { this->bActive_ = false; } |
---|
[496] | 90 | /** @brief Unpauses the Timer - continues with the given state. */ |
---|
[1052] | 91 | inline void unpauseTimer() |
---|
| 92 | { this->bActive_ = true; } |
---|
[871] | 93 | /** @brief Returns true if the Timer is active (= not stoped, not paused). @return True = Time is active */ |
---|
[1052] | 94 | inline bool isActive() const |
---|
| 95 | { return this->bActive_; } |
---|
| 96 | /** @brief Gives the Timer some extra time. @param time The amount of extra time in seconds */ |
---|
| 97 | inline void addTime(float time) |
---|
| 98 | { this->time_ += time; } |
---|
| 99 | /** @brief Decreases the remaining time of the Timer. @param time The amount of time to remove */ |
---|
| 100 | inline void removeTime(float time) |
---|
| 101 | { this->time_ -= time; } |
---|
| 102 | /** @brief Sets the interval of the Timer. @param interval The interval */ |
---|
| 103 | inline void setInterval(float interval) |
---|
| 104 | { this->interval_ = interval; } |
---|
| 105 | /** @brief Sets bLoop to a given value. @param bLoop True = loop */ |
---|
| 106 | inline void setLoop(bool bLoop) |
---|
| 107 | { this->bLoop_ = bLoop; } |
---|
[384] | 108 | |
---|
[1021] | 109 | void tick(float dt); |
---|
| 110 | |
---|
[384] | 111 | protected: |
---|
[1052] | 112 | TimerBase(); |
---|
[384] | 113 | |
---|
[1052] | 114 | Executor* executor_; //!< The executor of the function that should be called when the time expires |
---|
| 115 | |
---|
| 116 | float interval_; //!< The time-interval in seconds |
---|
| 117 | bool bLoop_; //!< If true, the function gets called every 'interval' seconds |
---|
| 118 | bool bActive_; //!< If true, the Timer ticks and calls the function if the time's up |
---|
| 119 | |
---|
| 120 | float time_; //!< Internal variable, counting the time till the next function-call |
---|
[384] | 121 | }; |
---|
| 122 | |
---|
[496] | 123 | //! The Timer is a callback-object, calling a given function after a given time-interval. |
---|
[384] | 124 | template <class T = BaseObject> |
---|
| 125 | class Timer : public TimerBase |
---|
| 126 | { |
---|
| 127 | public: |
---|
[1052] | 128 | Timer() {} |
---|
[384] | 129 | |
---|
[496] | 130 | /** |
---|
| 131 | @brief Constructor: Initializes the Timer with given values. |
---|
| 132 | @param interval The timer-interval in seconds |
---|
| 133 | @param bLoop If true, the function gets called every 'interval' seconds |
---|
| 134 | @param object The object owning the timer and the function |
---|
[1052] | 135 | @param exeuctor A executor of the function to call |
---|
[496] | 136 | */ |
---|
[1052] | 137 | Timer(float interval, bool bLoop, T* object, ExecutorMember<T>* exeuctor) |
---|
[384] | 138 | { |
---|
[1052] | 139 | this->setTimer(interval, bLoop, object, exeuctor); |
---|
[384] | 140 | } |
---|
| 141 | |
---|
[496] | 142 | /** |
---|
| 143 | @brief Initializes the Timer with given values. |
---|
| 144 | @param interval The timer-interval in seconds |
---|
| 145 | @param bLoop If true, the function gets called every 'interval' seconds |
---|
| 146 | @param object The object owning the timer and the function |
---|
[1052] | 147 | @param exeuctor A executor of the function to call |
---|
[496] | 148 | */ |
---|
[1052] | 149 | void setTimer(float interval, bool bLoop, T* object, ExecutorMember<T>* executor) |
---|
[384] | 150 | { |
---|
| 151 | this->interval_ = interval; |
---|
| 152 | this->bLoop_ = bLoop; |
---|
[1052] | 153 | executor->setObject(object); |
---|
| 154 | this->executor_ = (Executor*)executor; |
---|
[384] | 155 | this->bActive_ = true; |
---|
| 156 | |
---|
| 157 | this->time_ = interval; |
---|
| 158 | } |
---|
[1052] | 159 | }; |
---|
[384] | 160 | |
---|
[1052] | 161 | //! The StaticTimer is a callback-object, calling a static function after a given time-interval. |
---|
| 162 | class StaticTimer : public TimerBase |
---|
| 163 | { |
---|
| 164 | public: |
---|
| 165 | StaticTimer() {} |
---|
| 166 | |
---|
| 167 | /** |
---|
| 168 | @brief Constructor: Initializes the Timer with given values. |
---|
| 169 | @param interval The timer-interval in seconds |
---|
| 170 | @param bLoop If true, the function gets called every 'interval' seconds |
---|
| 171 | @param exeuctor A executor of the function to call |
---|
| 172 | */ |
---|
| 173 | StaticTimer(float interval, bool bLoop, ExecutorStatic* executor) |
---|
[384] | 174 | { |
---|
[1052] | 175 | this->setTimer(interval, bLoop, executor); |
---|
[384] | 176 | } |
---|
| 177 | |
---|
[1052] | 178 | /** |
---|
| 179 | @brief 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 object The object owning the timer and the function |
---|
| 183 | @param executor A executor of the function to call |
---|
| 184 | */ |
---|
| 185 | void setTimer(float interval, bool bLoop, ExecutorStatic* executor) |
---|
| 186 | { |
---|
| 187 | this->interval_ = interval; |
---|
| 188 | this->bLoop_ = bLoop; |
---|
| 189 | this->executor_ = (Executor*)executor; |
---|
| 190 | this->bActive_ = true; |
---|
| 191 | |
---|
| 192 | this->time_ = interval; |
---|
| 193 | } |
---|
[384] | 194 | }; |
---|
| 195 | |
---|
| 196 | } |
---|
| 197 | |
---|
[673] | 198 | #endif /* _Timer_H__ */ |
---|