Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/tools/Timer.h @ 12384

Last change on this file since 12384 was 12191, checked in by merholzl, 6 years ago

merged Wagnis

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