Orxonox  0.0.5 Codename: Arcturus
Timer.h
Go to the documentation of this file.
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 
76 #ifndef _Timer_H__
77 #define _Timer_H__
78 
79 #include "tools/ToolsPrereqs.h"
80 #include <functional>
81 
82 #include "core/object/Listable.h"
84 
85 namespace orxonox
86 {
87  unsigned int delay(float delay, const std::string& command);
88  unsigned int delayreal(float delay, const std::string& command);
89 
90  unsigned int addDelayedCommand(Timer* timer, float delay, const std::string& command);
91  void executeDelayedCommand(Timer* timer, const std::string& command);
92 
93  void killdelay(unsigned int handle);
94  void killdelays();
95 
105  class _ToolsExport Timer : public Listable
106  {
107  public:
108  Timer();
109 
110  Timer(float interval, bool bLoop, const ExecutorPtr& executor, bool bKillAfterCall = false);
111  Timer(float interval, bool bLoop, std::function<void (void)> func, bool bKillAfterCall = false);
112 
113  void setTimer(float interval, bool bLoop, const ExecutorPtr& executor, bool bKillAfterCall = false);
114  void setTimer(float interval, bool bLoop, std::function<void (void)> func, bool bKillAfterCall = false);
115 
116  void run();
117 
118 
120  inline void startTimer()
121  { this->bActive_ = true; this->time_ = this->interval_; }
123  inline void stopTimer()
124  { this->bActive_ = false; this->time_ = this->interval_; }
126  inline void pauseTimer()
127  { this->bActive_ = false; }
129  inline void unpauseTimer()
130  { this->bActive_ = true; }
132  inline bool isActive() const
133  { return this->bActive_; }
135  inline float getRemainingTime() const
136  { return static_cast<float>(this->time_ / 1000000.0f); }
138  inline void addTime(float time)
139  { if (time > 0.0f) this->time_ += static_cast<long long>(time * 1000000.0f); }
141  inline void removeTime(float time)
142  { if (time > 0.0f) this->time_ -= static_cast<long long>(time * 1000000.0f); }
144  inline void setInterval(float interval)
145  { this->interval_ = static_cast<long long>(interval * 1000000.0f); }
147  inline void setLoop(bool bLoop)
148  { this->bLoop_ = bLoop; }
149 
150  void tick(const Clock& time);
151 
152  protected:
153  virtual float getTimeFactor();
154 
155  private:
156  void init();
157 
159  std::function<void (void)> function_;
160 
162  long long interval_;
163  bool bLoop_;
164  bool bActive_;
166 
167  long long time_;
168  };
169 
176  class _ToolsExport RealTimer : public Timer
177  {
178  public:
179  RealTimer();
180  RealTimer(float interval, bool bLoop, const ExecutorPtr& executor, bool bKillAfterCall = false);
181 
182  protected:
183  virtual float getTimeFactor() override;
184  };
185 }
186 
187 #endif /* _Timer_H__ */
long long interval_
The time-interval in micro seconds.
Definition: Timer.h:162
void setLoop(bool bLoop)
Defines if the timer call the executor every interval seconds or only once.
Definition: Timer.h:147
Listable stores the entries of all object lists pointing to this instance.
Definition: Listable.h:50
::std::string string
Definition: gtest-port.h:756
void removeTime(float time)
Decreases the remaining time of the timer by the given amount of time (in seconds) ...
Definition: Timer.h:141
void startTimer()
Re-starts the timer: The executor will be called after interval seconds.
Definition: Timer.h:120
Shared library macros, enums, constants and forward declarations for the tools module ...
bool bKillAfterCall_
If true the timer gets deleted after it expired and called the executor.
Definition: Timer.h:165
#define _ToolsExport
Definition: ToolsPrereqs.h:59
void killdelay(unsigned int handle)
Console-command: Kills a delayed command with given handle.
Definition: Timer.cc:125
void pauseTimer()
Pauses the timer - it will continue with the actual state if you call unpauseTimer().
Definition: Timer.h:126
void killdelays()
Console-command: Kills all scheduled commands that were delayed using delay().
Definition: Timer.cc:114
bool bActive_
If true, the timer ticks and calls the executor if the time&#39;s up.
Definition: Timer.h:164
void addTime(float time)
Increases the remaining time of the timer by the given amount of time (in seconds).
Definition: Timer.h:138
long long time_
Internal variable, counting the time untill the next executor-call.
Definition: Timer.h:167
bool isActive() const
Returns true if the timer is active (neither stopped nor paused).
Definition: Timer.h:132
Die Wagnis Klasse hat die folgenden Aufgaben:
Definition: ApplicationPaths.cc:66
ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION_MULTI() command(const std::string &fragment)
Returns a list of commands and groups and also supports auto-completion of the arguments of these com...
Definition: ArgumentCompletionFunctions.cc:178
unsigned int delayreal(float delay, const std::string &command)
Console-command: Calls another console command after delay seconds (real time)
Definition: Timer.cc:76
std::function< void(void)> function_
Definition: Timer.h:159
Typedefs and definitions of ExecutorPtr, ExecutorStaticPtr, and ExecutorMemberPtr.
void stopTimer()
Stops the timer.
Definition: Timer.h:123
unsigned int delay(float delay, const std::string &command)
Console-command: Calls another console command after delay seconds (game time).
Definition: Timer.cc:65
void executeDelayedCommand(Timer *timer, const std::string &command)
Helper function for delay(), executes the command and destroys the timer.
Definition: Timer.cc:104
Declaration of Listable, the base of all classes whose instances can be stored in object lists...
void setInterval(float interval)
Changes the calling interval.
Definition: Timer.h:144
bool bLoop_
If true, the executor gets called every interval seconds.
Definition: Timer.h:163
bool isStdFunction_
Definition: Timer.h:161
Simple real time clock based on Ogre::Timer.
Definition: Clock.h:57
float getRemainingTime() const
Returns the remaining time until the timer calls the executor.
Definition: Timer.h:135
Timer is a helper class that executes a function after a given amount of seconds in game-time...
Definition: Timer.h:105
unsigned int addDelayedCommand(Timer *timer, float delay, const std::string &command)
Helper function, used by delay() and delayreal() to add a delayed command.
Definition: Timer.cc:88
std::shared_ptr< Executor > ExecutorPtr
Definition: ExecutorPtr.h:55
RealTimer is a helper class that executes a function after a given amount of seconds in real-time...
Definition: Timer.h:176
void unpauseTimer()
Unpauses the timer - continues with the given state.
Definition: Timer.h:129
ExecutorPtr executor_
The executor of the function that will be called when the time expires.
Definition: Timer.h:158