Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 885 was 871, checked in by landauf, 17 years ago

merged core branch to trunk

File size: 6.6 KB
RevLine 
[670]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
[496]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();
[640]41                Timer<ClassName> myTimer;
[496]42        };
43
44    source.cc:
45        ClassName::ClassName()
46        {
47            myTimer.setTimer(interval_in_seconds, bLoop, this, &ClassName::functionName);
48        }
49
50        void ClassName::functionName()
51        {
52            whateveryouwant();
53            something(else);
54        }
55*/
56
[384]57#ifndef _Timer_H__
58#define _Timer_H__
59
[708]60#include <OgreFrameListener.h>
61#include "../OrxonoxPrereqs.h"
62
[384]63namespace orxonox
64{
[496]65    //! TimerBase is the parent of the Timer class.
[729]66    class _OrxonoxExport TimerBase : public OrxonoxClass
[384]67    {
68        friend class TimerFrameListener;
69
70        public:
[871]71            TimerBase();
[384]72
73            virtual void run() const = 0;
74
[496]75            /** @brief Starts the Timer: Function-call after 'interval' seconds. */
[384]76            inline void startTimer() { this->bActive_ = true; this->time_ = this->interval_; }
[496]77            /** @brief Stops the Timer. */
[384]78            inline void stopTimer() { this->bActive_ = false; this->time_ = this->interval_; }
[496]79            /** @brief Pauses the Timer - it will continue with the actual state if you unpause it. */
[384]80            inline void pauseTimer() { this->bActive_ = false; }
[496]81            /** @brief Unpauses the Timer - continues with the given state. */
[384]82            inline void unpauseTimer() { this->bActive_ = true; }
[871]83            /** @brief Returns true if the Timer is active (= not stoped, not paused). @return True = Time is active */
[384]84            inline bool isActive() const { return this->bActive_; }
85
86        protected:
[496]87            float interval_;    //!< The time-interval in seconds
88            bool bLoop_;        //!< If true, the function gets called every 'interval' seconds
89            bool bActive_;      //!< If true, the Timer ticks and calls the function if the time's up
[384]90
[496]91            float time_;        //!< Internal variable, counting the time till the next function-call
[384]92    };
93
[496]94    //! The Timer is a callback-object, calling a given function after a given time-interval.
[384]95    template <class T = BaseObject>
96    class Timer : public TimerBase
97    {
98        public:
[496]99            /** @brief Constructor: Sets the default-values. */
[384]100            Timer()
101            {
102                this->timerFunction_ = 0;
103                this->object_ = 0;
104            }
105
[496]106            /**
107                @brief Constructor: Initializes the Timer with given values.
108                @param interval The timer-interval in seconds
109                @param bLoop If true, the function gets called every 'interval' seconds
110                @param object The object owning the timer and the function
111                @param timerFunction A function pointer to the function to call
112            */
[384]113            Timer(float interval, bool bLoop, T* object, void (T::*timerFunction)())
114            {
115                this->setTimer(interval, bLoop, timerFunction, object);
116            }
117
[496]118            /**
119                @brief Initializes the Timer with given values.
120                @param interval The timer-interval in seconds
121                @param bLoop If true, the function gets called every 'interval' seconds
122                @param object The object owning the timer and the function
123                @param timerFunction A function pointer to the function to call
124            */
[384]125            void setTimer(float interval, bool bLoop, T* object, void (T::*timerFunction)())
126            {
127                this->interval_ = interval;
128                this->bLoop_ = bLoop;
129                this->timerFunction_ = timerFunction;
130                this->object_ = object;
131                this->bActive_ = true;
132
133                this->time_ = interval;
134            }
135
[496]136            /** @brief Calls the given function of the given object. */
[384]137            void run() const
138            {
139                ((*this->object_).*timerFunction_)();
140            }
141
142        private:
143            void (T::*timerFunction_)();
144            T* object_;
145    };
146
[496]147    //! The TimerFrameListener manages all Timers in the game.
[384]148    class TimerFrameListener : public Ogre::FrameListener
149    {
150        private:
[496]151            /** @brief Gets called before a frame gets rendered. */
[384]152            bool frameStarted(const Ogre::FrameEvent &evt)
153            {
[496]154                // Iterate through all Timers
[646]155                for (Iterator<TimerBase> it = ObjectList<TimerBase>::start(); it; )
[384]156                {
157                    if (it->isActive())
158                    {
[496]159                        // If active: Decrease the timer by the duration of the last frame
[384]160                        it->time_ -= evt.timeSinceLastFrame;
161
162                        if (it->time_ <= 0)
163                        {
[496]164                            // It's time to call the function
[384]165                            if (it->bLoop_)
[496]166                                it->time_ += it->interval_; // Q: Why '+=' and not '='? A: Think about it. It's more accurate like that. Seriously.
[384]167                            else
[496]168                                it->stopTimer(); // Stop the timer if we don't want to loop
[384]169
[646]170                            (it++)->run();
[384]171                        }
[646]172                        else
173                            ++it;
[384]174                    }
[646]175                    else
176                        ++it;
[384]177                }
178
179                return FrameListener::frameStarted(evt);
180            }
181    };
182}
183
[673]184#endif /* _Timer_H__ */
Note: See TracBrowser for help on using the repository browser.