Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Once again, set all the svn:eol-style property to native. I really hope this doesn't pose more problems than it solves..

  • Property eol-style set to native
File size: 7.4 KB
RevLine 
[670]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
[1056]3 *                    > www.orxonox.net <
[670]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
[496]29/*!
30    @file Timer.h
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();
[640]42                Timer<ClassName> myTimer;
[496]43        };
44
45    source.cc:
[1052]46        include "core/Executor.h"
47
[496]48        ClassName::ClassName()
49        {
[1052]50            myTimer.setTimer(interval_in_seconds, bLoop, this, createExecutor(createFunctor(&ClassName::functionName)));
[496]51        }
52
53        void ClassName::functionName()
54        {
55            whateveryouwant();
56            something(else);
57        }
58*/
59
[384]60#ifndef _Timer_H__
61#define _Timer_H__
62
[1039]63#include "OrxonoxPrereqs.h"
[1052]64#include "core/CorePrereqs.h"
[1021]65#include "core/Tickable.h"
[708]66
[384]67namespace orxonox
68{
[1052]69    class StaticTimer;
70    void delay(float delay, const std::string& command);
[1502]71    void killdelays();
[1052]72    void executeDelayedCommand(StaticTimer* timer, const std::string& command);
73
[496]74    //! TimerBase is the parent of the Timer class.
[1021]75    class _OrxonoxExport TimerBase : public Tickable
[384]76    {
77        public:
[1052]78            ~TimerBase();
[384]79
[1052]80            void run() const;
[384]81
[496]82            /** @brief Starts the Timer: Function-call after 'interval' seconds. */
[1052]83            inline void startTimer()
84                { this->bActive_ = true; this->time_ = this->interval_; }
[496]85            /** @brief Stops the Timer. */
[1052]86            inline void stopTimer()
87                { this->bActive_ = false; this->time_ = this->interval_; }
[496]88            /** @brief Pauses the Timer - it will continue with the actual state if you unpause it. */
[1052]89            inline void pauseTimer()
90                { this->bActive_ = false; }
[496]91            /** @brief Unpauses the Timer - continues with the given state. */
[1052]92            inline void unpauseTimer()
93                { this->bActive_ = true; }
[871]94            /** @brief Returns true if the Timer is active (= not stoped, not paused). @return True = Time is active */
[1052]95            inline bool isActive() const
96                { return this->bActive_; }
97            /** @brief Gives the Timer some extra time. @param time The amount of extra time in seconds */
98            inline void addTime(float time)
99                { this->time_ += time; }
100            /** @brief Decreases the remaining time of the Timer. @param time The amount of time to remove */
101            inline void removeTime(float time)
102                { this->time_ -= time; }
103            /** @brief Sets the interval of the Timer. @param interval The interval */
104            inline void setInterval(float interval)
105                { this->interval_ = interval; }
106            /** @brief Sets bLoop to a given value. @param bLoop True = loop */
107            inline void setLoop(bool bLoop)
108                { this->bLoop_ = bLoop; }
[384]109
[1021]110            void tick(float dt);
111
[384]112        protected:
[1052]113            TimerBase();
[384]114
[1052]115            Executor* executor_; //!< The executor of the function that should be called when the time expires
116
117            float interval_;     //!< The time-interval in seconds
118            bool bLoop_;         //!< If true, the function gets called every 'interval' seconds
119            bool bActive_;       //!< If true, the Timer ticks and calls the function if the time's up
120
121            float time_;         //!< Internal variable, counting the time till the next function-call
[384]122    };
123
[496]124    //! The Timer is a callback-object, calling a given function after a given time-interval.
[384]125    template <class T = BaseObject>
126    class Timer : public TimerBase
127    {
128        public:
[1052]129            Timer() {}
[384]130
[496]131            /**
132                @brief Constructor: Initializes the Timer with given values.
133                @param interval The timer-interval in seconds
134                @param bLoop If true, the function gets called every 'interval' seconds
135                @param object The object owning the timer and the function
[1052]136                @param exeuctor A executor of the function to call
[496]137            */
[1052]138            Timer(float interval, bool bLoop, T* object, ExecutorMember<T>* exeuctor)
[384]139            {
[1052]140                this->setTimer(interval, bLoop, object, exeuctor);
[384]141            }
142
[496]143            /**
144                @brief Initializes the Timer with given values.
145                @param interval The timer-interval in seconds
146                @param bLoop If true, the function gets called every 'interval' seconds
147                @param object The object owning the timer and the function
[1052]148                @param exeuctor A executor of the function to call
[496]149            */
[1052]150            void setTimer(float interval, bool bLoop, T* object, ExecutorMember<T>* executor)
[384]151            {
152                this->interval_ = interval;
153                this->bLoop_ = bLoop;
[1052]154                executor->setObject(object);
155                this->executor_ = (Executor*)executor;
[384]156                this->bActive_ = true;
157
158                this->time_ = interval;
159            }
[1052]160    };
[384]161
[1052]162    //! The StaticTimer is a callback-object, calling a static function after a given time-interval.
163    class StaticTimer : public TimerBase
164    {
165        public:
166            StaticTimer() {}
167
168            /**
169                @brief Constructor: Initializes the Timer with given values.
170                @param interval The timer-interval in seconds
171                @param bLoop If true, the function gets called every 'interval' seconds
172                @param exeuctor A executor of the function to call
173            */
174            StaticTimer(float interval, bool bLoop, ExecutorStatic* executor)
[384]175            {
[1052]176                this->setTimer(interval, bLoop, executor);
[384]177            }
178
[1052]179            /**
180                @brief Initializes the Timer with given values.
181                @param interval The timer-interval in seconds
182                @param bLoop If true, the function gets called every 'interval' seconds
183                @param object The object owning the timer and the function
184                @param executor A executor of the function to call
185            */
186            void setTimer(float interval, bool bLoop, ExecutorStatic* executor)
187            {
188                this->interval_ = interval;
189                this->bLoop_ = bLoop;
190                this->executor_ = (Executor*)executor;
191                this->bActive_ = true;
192
193                this->time_ = interval;
194            }
[384]195    };
196
197}
198
[673]199#endif /* _Timer_H__ */
Note: See TracBrowser for help on using the repository browser.