Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7387 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
Line 
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
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 myTimer;
43        };
44
45    source.cc:
46        #include "core/command/Executor.h"
47
48        ClassName::ClassName()
49        {
50            myTimer.setTimer(interval_in_seconds, bLoop, createExecutor(createFunctor(&ClassName::functionName, this)));
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 "tools/ToolsPrereqs.h"
64
65#include "core/OrxonoxClass.h"
66#include "core/command/Executor.h"
67#include "tools/interfaces/TimeFactorListener.h"
68
69namespace orxonox
70{
71    void delay(float delay, const std::string& command);
72    void killdelays();
73    void executeDelayedCommand(Timer* timer, const std::string& command);
74
75    //! The Timer is a callback-object, calling a given function after a given time-interval.
76    class _ToolsExport Timer : public TimeFactorListener
77    {
78        public:
79            Timer();
80
81            Timer(float interval, bool bLoop, const ExecutorPtr& executor, bool bKillAfterCall = false);
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            */
90            void setTimer(float interval, bool bLoop, const ExecutorPtr& executor, bool bKillAfterCall = false)
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();
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_; }
118            /** @brief Returns the remaining time until the Timer calls the function. @return The remaining time */
119            inline float getRemainingTime() const
120                { return static_cast<float>(this->time_ / 1000000.0f); }
121            /** @brief Gives the Timer some extra time. @param time The amount of extra time in seconds */
122            inline void addTime(float time)
123                { if (time > 0.0f) this->time_ += static_cast<long long>(time * 1000000.0f); }
124            /** @brief Decreases the remaining time of the Timer. @param time The amount of time to remove */
125            inline void removeTime(float time)
126                { if (time > 0.0f) this->time_ -= static_cast<long long>(time * 1000000.0f); }
127            /** @brief Sets the interval of the Timer. @param interval The interval */
128            inline void setInterval(float interval)
129                { this->interval_ = static_cast<long long>(interval * 1000000.0f); }
130            /** @brief Sets bLoop to a given value. @param bLoop True = loop */
131            inline void setLoop(bool bLoop)
132                { this->bLoop_ = bLoop; }
133
134            void tick(const Clock& time);
135
136        private:
137            void init();
138
139            ExecutorPtr executor_;  //!< The executor of the function that should be called when the time expires
140
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
145
146            long long time_;        //!< Internal variable, counting the time till the next function-call
147    };
148}
149
150#endif /* _Timer_H__ */
Note: See TracBrowser for help on using the repository browser.