Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/buildsystem/src/orxonox/tools/Timer.h @ 2384

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

merged gui back to trunk.
update the media repository!

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