Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/tools/Timer.h @ 2828

Last change on this file since 2828 was 2662, checked in by rgrieder, 16 years ago

Merged presentation branch back to trunk.

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