Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy2/src/orxonox/tools/Timer.h @ 2687

Last change on this file since 2687 was 2406, checked in by landauf, 16 years ago

Added TimeFactorListener to properly handle changes of the global time factor (makes the game faster or slower). Currently supported in Backlight, ParticleInterface and Timer, which were all critical classes I could think of (all other classes are already covered by the adjustment of dt in tick(dt)). It seems to work well, both with small values (0.1, 0.01) and great values (10, 100). Even pausing the game (0) works fine.

  • Property svn:eol-style set to native
File size: 8.1 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<ClassName> myTimer;
43        };
44
45    source.cc:
46        include "core/Executor.h"
47
48        ClassName::ClassName()
49        {
50            myTimer.setTimer(interval_in_seconds, bLoop, this, createExecutor(createFunctor(&ClassName::functionName)));
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 "OrxonoxPrereqs.h"
64#include "core/OrxonoxClass.h"
65#include "gamestates/GSRoot.h"
66
67namespace orxonox
68{
69    class StaticTimer;
70    void delay(float delay, const std::string& command);
71    void killdelays();
72    void executeDelayedCommand(StaticTimer* timer, const std::string& command);
73
74    //! TimerBase is the parent of the Timer class.
75    class _OrxonoxExport TimerBase : public TimeFactorListener
76    {
77        public:
78            ~TimerBase();
79
80            void run() const;
81            void deleteExecutor();
82
83            /** @brief Starts the Timer: Function-call after 'interval' seconds. */
84            inline void startTimer()
85                { this->bActive_ = true; this->time_ = this->interval_; }
86            /** @brief Stops the Timer. */
87            inline void stopTimer()
88                { this->bActive_ = false; this->time_ = this->interval_; }
89            /** @brief Pauses the Timer - it will continue with the actual state if you unpause it. */
90            inline void pauseTimer()
91                { this->bActive_ = false; }
92            /** @brief Unpauses the Timer - continues with the given state. */
93            inline void unpauseTimer()
94                { this->bActive_ = true; }
95            /** @brief Returns true if the Timer is active (= not stoped, not paused). @return True = Time is active */
96            inline bool isActive() const
97                { return this->bActive_; }
98            /** @brief Returns the remaining time until the Timer calls the function. @return The remaining time */
99            inline float getRemainingTime() const
100                { return (float)this->time_ / 1000000.0f; }
101            /** @brief Gives the Timer some extra time. @param time The amount of extra time in seconds */
102            inline void addTime(float time)
103                { if (time > 0.0f) this->time_ += (long long)(time * 1000000.0f); }
104            /** @brief Decreases the remaining time of the Timer. @param time The amount of time to remove */
105            inline void removeTime(float time)
106                { if (time > 0.0f) this->time_ -= (long long)(time * 1000000.0f); }
107            /** @brief Sets the interval of the Timer. @param interval The interval */
108            inline void setInterval(float interval)
109                { this->interval_ = (long long)(interval * 1000000.0f); }
110            /** @brief Sets bLoop to a given value. @param bLoop True = loop */
111            inline void setLoop(bool bLoop)
112                { this->bLoop_ = bLoop; }
113
114            void tick(const Clock& time);
115
116        protected:
117            TimerBase();
118
119            Executor* executor_;  //!< The executor of the function that should be called when the time expires
120
121            long long interval_;  //!< The time-interval in micro seconds
122            bool bLoop_;          //!< If true, the function gets called every 'interval' seconds
123            bool bActive_;        //!< If true, the Timer ticks and calls the function if the time's up
124            bool bKillAfterCall_; //!< If true the timer gets deleted after it called the function
125
126            long long time_;      //!< Internal variable, counting the time till the next function-call
127    };
128
129    //! The Timer is a callback-object, calling a given function after a given time-interval.
130    template <class T = BaseObject>
131    class Timer : public TimerBase
132    {
133        public:
134            Timer() {}
135
136            /**
137                @brief Constructor: Initializes the Timer with given values.
138                @param interval The timer-interval in seconds
139                @param bLoop If true, the function gets called every 'interval' seconds
140                @param object The object owning the timer and the function
141                @param exeuctor A executor of the function to call
142            */
143            Timer(float interval, bool bLoop, T* object, ExecutorMember<T>* exeuctor, bool bKillAfterCall = false)
144            {
145                this->setTimer(interval, bLoop, object, exeuctor, bKillAfterCall);
146            }
147
148            /**
149                @brief Initializes the Timer with given values.
150                @param interval The timer-interval in seconds
151                @param bLoop If true, the function gets called every 'interval' seconds
152                @param object The object owning the timer and the function
153                @param exeuctor A executor of the function to call
154            */
155            void setTimer(float interval, bool bLoop, T* object, ExecutorMember<T>* executor, bool bKillAfterCall = false)
156            {
157                this->deleteExecutor();
158
159                this->setInterval(interval);
160                this->bLoop_ = bLoop;
161                executor->setObject(object);
162                this->executor_ = (Executor*)executor;
163                this->bActive_ = true;
164
165                this->time_ = this->interval_;
166                this->bKillAfterCall_ = bKillAfterCall;
167            }
168    };
169
170    //! The StaticTimer is a callback-object, calling a static function after a given time-interval.
171    class StaticTimer : public TimerBase
172    {
173        public:
174            StaticTimer() {}
175
176            /**
177                @brief Constructor: Initializes the Timer with given values.
178                @param interval The timer-interval in seconds
179                @param bLoop If true, the function gets called every 'interval' seconds
180                @param exeuctor A executor of the function to call
181            */
182            StaticTimer(float interval, bool bLoop, ExecutorStatic* executor, bool bKillAfterCall = false)
183            {
184                this->setTimer(interval, bLoop, executor, bKillAfterCall);
185            }
186
187            /**
188                @brief Initializes the Timer with given values.
189                @param interval The timer-interval in seconds
190                @param bLoop If true, the function gets called every 'interval' seconds
191                @param object The object owning the timer and the function
192                @param executor A executor of the function to call
193            */
194            void setTimer(float interval, bool bLoop, ExecutorStatic* executor, bool bKillAfterCall = false)
195            {
196                this->deleteExecutor();
197
198                this->setInterval(interval);
199                this->bLoop_ = bLoop;
200                this->executor_ = (Executor*)executor;
201                this->bActive_ = true;
202
203                this->time_ = this->interval_;
204                this->bKillAfterCall_ = bKillAfterCall;
205            }
206    };
207
208}
209
210#endif /* _Timer_H__ */
Note: See TracBrowser for help on using the repository browser.