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 | @defgroup Timer Timer |
---|
31 | @ingroup Tools |
---|
32 | */ |
---|
33 | |
---|
34 | /** |
---|
35 | @file |
---|
36 | @ingroup Timer |
---|
37 | @brief Declaration of the Timer class, used to call functions after a given time-interval. |
---|
38 | |
---|
39 | @anchor TimerExample |
---|
40 | |
---|
41 | Timer is a helper class that executes a function after a given amount of time. |
---|
42 | |
---|
43 | Usage: <br> |
---|
44 | header.h: |
---|
45 | @code |
---|
46 | class MyClass |
---|
47 | { |
---|
48 | public: |
---|
49 | MyClass(); |
---|
50 | void functionName(); |
---|
51 | |
---|
52 | private: |
---|
53 | Timer myTimer; |
---|
54 | }; |
---|
55 | @endcode |
---|
56 | |
---|
57 | source.cc: |
---|
58 | @code |
---|
59 | #include "core/command/Executor.h" |
---|
60 | |
---|
61 | MyClass::MyClass() |
---|
62 | { |
---|
63 | myTimer.setTimer(3, false, createExecutor(createFunctor(&ClassName::myFunction, this))); |
---|
64 | } |
---|
65 | |
---|
66 | void MyClass::myFunction() |
---|
67 | { |
---|
68 | orxout() << "Hello World" << endl; |
---|
69 | } |
---|
70 | @endcode |
---|
71 | |
---|
72 | The code in this example prints "Hello World" to the console, 3 seconds after creating |
---|
73 | an instance of MyClass. |
---|
74 | */ |
---|
75 | |
---|
76 | #ifndef _Timer_H__ |
---|
77 | #define _Timer_H__ |
---|
78 | |
---|
79 | #include "tools/ToolsPrereqs.h" |
---|
80 | |
---|
81 | #include "core/object/Listable.h" |
---|
82 | #include "core/command/ExecutorPtr.h" |
---|
83 | |
---|
84 | namespace orxonox |
---|
85 | { |
---|
86 | unsigned int delay(float delay, const std::string& command); |
---|
87 | unsigned int delayreal(float delay, const std::string& command); |
---|
88 | |
---|
89 | unsigned int addDelayedCommand(Timer* timer, float delay, const std::string& command); |
---|
90 | void executeDelayedCommand(Timer* timer, const std::string& command); |
---|
91 | |
---|
92 | void killdelay(unsigned int handle); |
---|
93 | void killdelays(); |
---|
94 | |
---|
95 | /** |
---|
96 | @brief Timer is a helper class that executes a function after a given amount of seconds in game-time. |
---|
97 | |
---|
98 | @see See @ref TimerExample "Timer.h" for an example. |
---|
99 | |
---|
100 | The time interval of Timer depends on the game time, hence it stops if the game is paused or runs |
---|
101 | slower/faster if the game-speed is modified. See RealTimer for a timer class which doesn't depend |
---|
102 | on the game time. |
---|
103 | */ |
---|
104 | class _ToolsExport Timer : public Listable |
---|
105 | { |
---|
106 | public: |
---|
107 | Timer(); |
---|
108 | |
---|
109 | Timer(float interval, bool bLoop, const ExecutorPtr& executor, bool bKillAfterCall = false); |
---|
110 | |
---|
111 | void setTimer(float interval, bool bLoop, const ExecutorPtr& executor, bool bKillAfterCall = false); |
---|
112 | |
---|
113 | void run(); |
---|
114 | |
---|
115 | |
---|
116 | /// Re-starts the timer: The executor will be called after @a interval seconds. |
---|
117 | inline void startTimer() |
---|
118 | { this->bActive_ = true; this->time_ = this->interval_; } |
---|
119 | /// Stops the timer. |
---|
120 | inline void stopTimer() |
---|
121 | { this->bActive_ = false; this->time_ = this->interval_; } |
---|
122 | /// Pauses the timer - it will continue with the actual state if you call unpauseTimer(). |
---|
123 | inline void pauseTimer() |
---|
124 | { this->bActive_ = false; } |
---|
125 | /// Unpauses the timer - continues with the given state. |
---|
126 | inline void unpauseTimer() |
---|
127 | { this->bActive_ = true; } |
---|
128 | /// Returns true if the timer is active (neither stopped nor paused). |
---|
129 | inline bool isActive() const |
---|
130 | { return this->bActive_; } |
---|
131 | /// Returns the remaining time until the timer calls the executor. |
---|
132 | inline float getRemainingTime() const |
---|
133 | { return static_cast<float>(this->time_ / 1000000.0f); } |
---|
134 | /// Increases the remaining time of the timer by the given amount of time (in seconds). |
---|
135 | inline void addTime(float time) |
---|
136 | { if (time > 0.0f) this->time_ += static_cast<long long>(time * 1000000.0f); } |
---|
137 | /// Decreases the remaining time of the timer by the given amount of time (in seconds) |
---|
138 | inline void removeTime(float time) |
---|
139 | { if (time > 0.0f) this->time_ -= static_cast<long long>(time * 1000000.0f); } |
---|
140 | /// Changes the calling interval. |
---|
141 | inline void setInterval(float interval) |
---|
142 | { this->interval_ = static_cast<long long>(interval * 1000000.0f); } |
---|
143 | /// Defines if the timer call the executor every @a interval seconds or only once. |
---|
144 | inline void setLoop(bool bLoop) |
---|
145 | { this->bLoop_ = bLoop; } |
---|
146 | |
---|
147 | void tick(const Clock& time); |
---|
148 | |
---|
149 | protected: |
---|
150 | virtual float getTimeFactor(); |
---|
151 | |
---|
152 | private: |
---|
153 | void init(); |
---|
154 | |
---|
155 | ExecutorPtr executor_; //!< The executor of the function that will be called when the time expires |
---|
156 | |
---|
157 | long long interval_; //!< The time-interval in micro seconds |
---|
158 | bool bLoop_; //!< If true, the executor gets called every @a interval seconds |
---|
159 | bool bActive_; //!< If true, the timer ticks and calls the executor if the time's up |
---|
160 | bool bKillAfterCall_; //!< If true the timer gets deleted after it expired and called the executor |
---|
161 | |
---|
162 | long long time_; //!< Internal variable, counting the time untill the next executor-call |
---|
163 | }; |
---|
164 | |
---|
165 | /** |
---|
166 | @brief RealTimer is a helper class that executes a function after a given amount of seconds in real-time. |
---|
167 | |
---|
168 | The time interval of RealTimer doesn't depend on the game time, it will also call the function |
---|
169 | if the game is paused. See Timer for a timer class that depends on the game time. |
---|
170 | */ |
---|
171 | class _ToolsExport RealTimer : public Timer |
---|
172 | { |
---|
173 | public: |
---|
174 | RealTimer(); |
---|
175 | RealTimer(float interval, bool bLoop, const ExecutorPtr& executor, bool bKillAfterCall = false); |
---|
176 | |
---|
177 | protected: |
---|
178 | virtual float getTimeFactor() override; |
---|
179 | }; |
---|
180 | } |
---|
181 | |
---|
182 | #endif /* _Timer_H__ */ |
---|