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 | /*! |
---|
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(); |
---|
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" |
---|
64 | #include "util/Integers.h" |
---|
65 | #include "core/OrxonoxClass.h" |
---|
66 | |
---|
67 | namespace orxonox |
---|
68 | { |
---|
69 | class StaticTimer; |
---|
70 | void delay(float delay, const std::string& command); |
---|
71 | void killdelays(); |
---|
72 | void executeDelayedCommand(StaticTimer* timer, const std::string& command); |
---|
73 | |
---|
74 | //! TimerBase is the parent of the Timer class. |
---|
75 | class _OrxonoxExport TimerBase : public OrxonoxClass |
---|
76 | { |
---|
77 | public: |
---|
78 | ~TimerBase(); |
---|
79 | |
---|
80 | void run() const; |
---|
81 | void deleteExecutor(); |
---|
82 | |
---|
83 | /** @brief Starts the Timer: Function-call after 'interval' seconds. */ |
---|
84 | inline void startTimer() |
---|
85 | { this->bActive_ = true; this->time_ = this->interval_; } |
---|
86 | /** @brief Stops the Timer. */ |
---|
87 | inline void stopTimer() |
---|
88 | { 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() |
---|
91 | { this->bActive_ = false; } |
---|
92 | /** @brief Unpauses the Timer - continues with the given state. */ |
---|
93 | inline void unpauseTimer() |
---|
94 | { this->bActive_ = true; } |
---|
95 | /** @brief Returns true if the Timer is active (= not stoped, not paused). @return True = Time is active */ |
---|
96 | inline bool isActive() const |
---|
97 | { return this->bActive_; } |
---|
98 | /** @brief Returns the remaining time until the Timer calls the function. @return The remaining time */ |
---|
99 | inline float getRemainingTime() const |
---|
100 | { return (float)this->time_ / 1000000.0f; } |
---|
101 | /** @brief Gives the Timer some extra time. @param time The amount of extra time in seconds */ |
---|
102 | inline void addTime(float time) |
---|
103 | { if (time > 0.0f) this->time_ += (int64_t)(time * 1000000.0f); } |
---|
104 | /** @brief Decreases the remaining time of the Timer. @param time The amount of time to remove */ |
---|
105 | inline void removeTime(float time) |
---|
106 | { if (time > 0.0f) this->time_ -= (int64_t)(time * 1000000.0f); } |
---|
107 | /** @brief Sets the interval of the Timer. @param interval The interval */ |
---|
108 | inline void setInterval(float interval) |
---|
109 | { this->interval_ = (int64_t)(interval * 1000000.0f); } |
---|
110 | /** @brief Sets bLoop to a given value. @param bLoop True = loop */ |
---|
111 | inline void setLoop(bool bLoop) |
---|
112 | { this->bLoop_ = bLoop; } |
---|
113 | |
---|
114 | void tick(const Clock& time); |
---|
115 | |
---|
116 | protected: |
---|
117 | TimerBase(); |
---|
118 | |
---|
119 | Executor* executor_; //!< The executor of the function that should be called when the time expires |
---|
120 | |
---|
121 | int64_t interval_; //!< The time-interval in micro seconds |
---|
122 | bool bLoop_; //!< If true, the function gets called every 'interval' seconds |
---|
123 | bool bActive_; //!< If true, the Timer ticks and calls the function if the time's up |
---|
124 | |
---|
125 | int64_t time_; //!< Internal variable, counting the time till the next function-call |
---|
126 | }; |
---|
127 | |
---|
128 | //! The Timer is a callback-object, calling a given function after a given time-interval. |
---|
129 | template <class T = BaseObject> |
---|
130 | class Timer : public TimerBase |
---|
131 | { |
---|
132 | public: |
---|
133 | Timer() {} |
---|
134 | |
---|
135 | /** |
---|
136 | @brief Constructor: Initializes the Timer with given values. |
---|
137 | @param interval The timer-interval in seconds |
---|
138 | @param bLoop If true, the function gets called every 'interval' seconds |
---|
139 | @param object The object owning the timer and the function |
---|
140 | @param exeuctor A executor of the function to call |
---|
141 | */ |
---|
142 | Timer(float interval, bool bLoop, T* object, ExecutorMember<T>* exeuctor) |
---|
143 | { |
---|
144 | this->setTimer(interval, bLoop, object, exeuctor); |
---|
145 | } |
---|
146 | |
---|
147 | /** |
---|
148 | @brief Initializes the Timer with given values. |
---|
149 | @param interval The timer-interval in seconds |
---|
150 | @param bLoop If true, the function gets called every 'interval' seconds |
---|
151 | @param object The object owning the timer and the function |
---|
152 | @param exeuctor A executor of the function to call |
---|
153 | */ |
---|
154 | void setTimer(float interval, bool bLoop, T* object, ExecutorMember<T>* executor) |
---|
155 | { |
---|
156 | this->deleteExecutor(); |
---|
157 | |
---|
158 | this->setInterval(interval); |
---|
159 | this->bLoop_ = bLoop; |
---|
160 | executor->setObject(object); |
---|
161 | this->executor_ = (Executor*)executor; |
---|
162 | this->bActive_ = true; |
---|
163 | |
---|
164 | this->time_ = this->interval_; |
---|
165 | } |
---|
166 | }; |
---|
167 | |
---|
168 | //! The StaticTimer is a callback-object, calling a static function after a given time-interval. |
---|
169 | class StaticTimer : public TimerBase |
---|
170 | { |
---|
171 | public: |
---|
172 | StaticTimer() {} |
---|
173 | |
---|
174 | /** |
---|
175 | @brief Constructor: Initializes the Timer with given values. |
---|
176 | @param interval The timer-interval in seconds |
---|
177 | @param bLoop If true, the function gets called every 'interval' seconds |
---|
178 | @param exeuctor A executor of the function to call |
---|
179 | */ |
---|
180 | StaticTimer(float interval, bool bLoop, ExecutorStatic* executor) |
---|
181 | { |
---|
182 | this->setTimer(interval, bLoop, executor); |
---|
183 | } |
---|
184 | |
---|
185 | /** |
---|
186 | @brief Initializes the Timer with given values. |
---|
187 | @param interval The timer-interval in seconds |
---|
188 | @param bLoop If true, the function gets called every 'interval' seconds |
---|
189 | @param object The object owning the timer and the function |
---|
190 | @param executor A executor of the function to call |
---|
191 | */ |
---|
192 | void setTimer(float interval, bool bLoop, ExecutorStatic* executor) |
---|
193 | { |
---|
194 | this->deleteExecutor(); |
---|
195 | |
---|
196 | this->setInterval(interval); |
---|
197 | this->bLoop_ = bLoop; |
---|
198 | this->executor_ = (Executor*)executor; |
---|
199 | this->bActive_ = true; |
---|
200 | |
---|
201 | this->time_ = this->interval_; |
---|
202 | } |
---|
203 | }; |
---|
204 | |
---|
205 | } |
---|
206 | |
---|
207 | #endif /* _Timer_H__ */ |
---|