Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1055 was 1052, checked in by landauf, 16 years ago

merged core2 back to trunk
there might be some errors, wasn't able to test it yet due to some strange g++ and linker behaviour.

File size: 7.4 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *
4 *
5 *   License notice:
6 *
7 *   This program is free software; you can redistribute it and/or
8 *   modify it under the terms of the GNU General Public License
9 *   as published by the Free Software Foundation; either version 2
10 *   of the License, or (at your option) any later version.
11 *
12 *   This program is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with this program; if not, write to the Free Software
19 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 *
21 *   Author:
22 *      Fabian 'x3n' Landau
23 *   Co-authors:
24 *      ...
25 *
26 */
27
28/*!
29    @file Timer.h
30    @brief Definition and Implementation of the Timer class.
31
32    The Timer is a callback-object, calling a given function after a given time-interval.
33
34    Usage:
35    header.h:
36        class ClassName
37        {
38            public:
39                ClassName();
40                void functionName();
41                Timer<ClassName> myTimer;
42        };
43
44    source.cc:
45        include "core/Executor.h"
46
47        ClassName::ClassName()
48        {
49            myTimer.setTimer(interval_in_seconds, bLoop, this, createExecutor(createFunctor(&ClassName::functionName)));
50        }
51
52        void ClassName::functionName()
53        {
54            whateveryouwant();
55            something(else);
56        }
57*/
58
59#ifndef _Timer_H__
60#define _Timer_H__
61
62#include "OrxonoxPrereqs.h"
63#include "core/CorePrereqs.h"
64#include "core/Tickable.h"
65
66namespace orxonox
67{
68    class StaticTimer;
69    void delay(float delay, const std::string& command);
70    void executeDelayedCommand(StaticTimer* timer, const std::string& command);
71
72    //! TimerBase is the parent of the Timer class.
73    class _OrxonoxExport TimerBase : public Tickable
74    {
75        public:
76            ~TimerBase();
77
78            void run() const;
79
80            /** @brief Starts the Timer: Function-call after 'interval' seconds. */
81            inline void startTimer()
82                { this->bActive_ = true; this->time_ = this->interval_; }
83            /** @brief Stops the Timer. */
84            inline void stopTimer()
85                { this->bActive_ = false; this->time_ = this->interval_; }
86            /** @brief Pauses the Timer - it will continue with the actual state if you unpause it. */
87            inline void pauseTimer()
88                { this->bActive_ = false; }
89            /** @brief Unpauses the Timer - continues with the given state. */
90            inline void unpauseTimer()
91                { this->bActive_ = true; }
92            /** @brief Returns true if the Timer is active (= not stoped, not paused). @return True = Time is active */
93            inline bool isActive() const
94                { return this->bActive_; }
95            /** @brief Gives the Timer some extra time. @param time The amount of extra time in seconds */
96            inline void addTime(float time)
97                { this->time_ += time; }
98            /** @brief Decreases the remaining time of the Timer. @param time The amount of time to remove */
99            inline void removeTime(float time)
100                { this->time_ -= time; }
101            /** @brief Sets the interval of the Timer. @param interval The interval */
102            inline void setInterval(float interval)
103                { this->interval_ = interval; }
104            /** @brief Sets bLoop to a given value. @param bLoop True = loop */
105            inline void setLoop(bool bLoop)
106                { this->bLoop_ = bLoop; }
107
108            void tick(float dt);
109
110        protected:
111            TimerBase();
112
113            Executor* executor_; //!< The executor of the function that should be called when the time expires
114
115            float interval_;     //!< The time-interval in seconds
116            bool bLoop_;         //!< If true, the function gets called every 'interval' seconds
117            bool bActive_;       //!< If true, the Timer ticks and calls the function if the time's up
118
119            float time_;         //!< Internal variable, counting the time till the next function-call
120    };
121
122    //! The Timer is a callback-object, calling a given function after a given time-interval.
123    template <class T = BaseObject>
124    class Timer : public TimerBase
125    {
126        public:
127            Timer() {}
128
129            /**
130                @brief Constructor: Initializes the Timer with given values.
131                @param interval The timer-interval in seconds
132                @param bLoop If true, the function gets called every 'interval' seconds
133                @param object The object owning the timer and the function
134                @param exeuctor A executor of the function to call
135            */
136            Timer(float interval, bool bLoop, T* object, ExecutorMember<T>* exeuctor)
137            {
138                this->setTimer(interval, bLoop, object, exeuctor);
139            }
140
141            /**
142                @brief Initializes the Timer with given values.
143                @param interval The timer-interval in seconds
144                @param bLoop If true, the function gets called every 'interval' seconds
145                @param object The object owning the timer and the function
146                @param exeuctor A executor of the function to call
147            */
148            void setTimer(float interval, bool bLoop, T* object, ExecutorMember<T>* executor)
149            {
150                this->interval_ = interval;
151                this->bLoop_ = bLoop;
152                executor->setObject(object);
153                this->executor_ = (Executor*)executor;
154                this->bActive_ = true;
155
156                this->time_ = interval;
157            }
158    };
159
160    //! The StaticTimer is a callback-object, calling a static function after a given time-interval.
161    class StaticTimer : public TimerBase
162    {
163        public:
164            StaticTimer() {}
165
166            /**
167                @brief Constructor: Initializes the Timer with given values.
168                @param interval The timer-interval in seconds
169                @param bLoop If true, the function gets called every 'interval' seconds
170                @param exeuctor A executor of the function to call
171            */
172            StaticTimer(float interval, bool bLoop, ExecutorStatic* executor)
173            {
174                this->setTimer(interval, bLoop, executor);
175            }
176
177            /**
178                @brief 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 object The object owning the timer and the function
182                @param executor A executor of the function to call
183            */
184            void setTimer(float interval, bool bLoop, ExecutorStatic* executor)
185            {
186                this->interval_ = interval;
187                this->bLoop_ = bLoop;
188                this->executor_ = (Executor*)executor;
189                this->bActive_ = true;
190
191                this->time_ = interval;
192            }
193    };
194
195}
196
197#endif /* _Timer_H__ */
Note: See TracBrowser for help on using the repository browser.