Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7287 was 7284, checked in by landauf, 14 years ago

merged consolecommands3 branch back to trunk.

note: the console command interface has changed completely, but the documentation is not yet up to date. just copy an existing command and change it to your needs, it's pretty self-explanatory. also the include files related to console commands are now located in core/command/. in the game it should work exactly like before, except for some changes in the auto-completion.

  • Property svn:eol-style set to native
File size: 5.8 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
29/*!
[2171]30    @file
[1505]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();
[5929]42                Timer myTimer;
[1505]43        };
44
45    source.cc:
[7284]46        #include "core/command/Executor.h"
[1505]47
48        ClassName::ClassName()
49        {
[5929]50            myTimer.setTimer(interval_in_seconds, bLoop, createExecutor(createFunctor(&ClassName::functionName, this)));
[1505]51        }
52
53        void ClassName::functionName()
54        {
55            whateveryouwant();
56            something(else);
57        }
58*/
59
60#ifndef _Timer_H__
61#define _Timer_H__
62
[5693]63#include "tools/ToolsPrereqs.h"
[3196]64
[1755]65#include "core/OrxonoxClass.h"
[7284]66#include "core/command/Executor.h"
[5693]67#include "tools/interfaces/TimeFactorListener.h"
[1505]68
69namespace orxonox
70{
71    void delay(float delay, const std::string& command);
72    void killdelays();
[5929]73    void executeDelayedCommand(Timer* timer, const std::string& command);
[1505]74
[5929]75    //! The Timer is a callback-object, calling a given function after a given time-interval.
76    class _ToolsExport Timer : public TimeFactorListener
[1505]77    {
78        public:
[5929]79            Timer();
[1505]80
[7284]81            Timer(float interval, bool bLoop, const ExecutorPtr& executor, bool bKillAfterCall = false);
[5929]82
83            /**
84                @brief Initializes the Timer with given values.
85                @param interval The timer-interval in seconds
86                @param bLoop If true, the function gets called every 'interval' seconds
87                @param object The object owning the timer and the function
88                @param executor A executor of the function to call
89            */
[7284]90            void setTimer(float interval, bool bLoop, const ExecutorPtr& executor, bool bKillAfterCall = false)
[5929]91            {
92                this->setInterval(interval);
93                this->bLoop_ = bLoop;
94                this->executor_ = executor;
95                this->bActive_ = true;
96
97                this->time_ = this->interval_;
98                this->bKillAfterCall_ = bKillAfterCall;
99            }
100
101            void run();
[1505]102
103            /** @brief Starts the Timer: Function-call after 'interval' seconds. */
104            inline void startTimer()
105                { this->bActive_ = true; this->time_ = this->interval_; }
106            /** @brief Stops the Timer. */
107            inline void stopTimer()
108                { this->bActive_ = false; this->time_ = this->interval_; }
109            /** @brief Pauses the Timer - it will continue with the actual state if you unpause it. */
110            inline void pauseTimer()
111                { this->bActive_ = false; }
112            /** @brief Unpauses the Timer - continues with the given state. */
113            inline void unpauseTimer()
114                { this->bActive_ = true; }
115            /** @brief Returns true if the Timer is active (= not stoped, not paused). @return True = Time is active */
116            inline bool isActive() const
117                { return this->bActive_; }
[1608]118            /** @brief Returns the remaining time until the Timer calls the function. @return The remaining time */
119            inline float getRemainingTime() const
[3300]120                { return static_cast<float>(this->time_ / 1000000.0f); }
[1505]121            /** @brief Gives the Timer some extra time. @param time The amount of extra time in seconds */
122            inline void addTime(float time)
[3301]123                { if (time > 0.0f) this->time_ += static_cast<long long>(time * 1000000.0f); }
[1505]124            /** @brief Decreases the remaining time of the Timer. @param time The amount of time to remove */
125            inline void removeTime(float time)
[3301]126                { if (time > 0.0f) this->time_ -= static_cast<long long>(time * 1000000.0f); }
[1505]127            /** @brief Sets the interval of the Timer. @param interval The interval */
128            inline void setInterval(float interval)
[3301]129                { this->interval_ = static_cast<long long>(interval * 1000000.0f); }
[1505]130            /** @brief Sets bLoop to a given value. @param bLoop True = loop */
131            inline void setLoop(bool bLoop)
132                { this->bLoop_ = bLoop; }
133
[1755]134            void tick(const Clock& time);
[1505]135
[5929]136        private:
137            void init();
[6417]138
[7284]139            ExecutorPtr executor_;  //!< The executor of the function that should be called when the time expires
[1505]140
[7284]141            long long interval_;    //!< The time-interval in micro seconds
142            bool bLoop_;            //!< If true, the function gets called every 'interval' seconds
143            bool bActive_;          //!< If true, the Timer ticks and calls the function if the time's up
144            bool bKillAfterCall_;   //!< If true the timer gets deleted after it called the function
[1505]145
[7284]146            long long time_;        //!< Internal variable, counting the time till the next function-call
[1505]147    };
148}
149
150#endif /* _Timer_H__ */
Note: See TracBrowser for help on using the repository browser.