Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/FICN/src/orxonox/objects/Timer.h @ 619

Last change on this file since 619 was 496, checked in by landauf, 16 years ago

added files from objecthierarchy, changed includes

File size: 5.8 KB
RevLine 
[496]1/*!
2    @file Timer.h
3    @brief Definition and Implementation of the Timer class.
4
5    The Timer is a callback-object, calling a given function after a given time-interval.
6
7    Usage:
8    header.h:
9        class ClassName
10        {
11            public:
12                ClassName();
13                void functionName();
14                Timer myTimer;
15        };
16
17    source.cc:
18        ClassName::ClassName()
19        {
20            myTimer.setTimer(interval_in_seconds, bLoop, this, &ClassName::functionName);
21        }
22
23        void ClassName::functionName()
24        {
25            whateveryouwant();
26            something(else);
27        }
28*/
29
[384]30#ifndef _Timer_H__
31#define _Timer_H__
32
[496]33#include "../core/CoreIncludes.h"
34#include "OgreFrameListener.h"
[384]35
36namespace orxonox
37{
[496]38    //! TimerBase is the parent of the Timer class.
[384]39    class TimerBase : public OrxonoxClass
40    {
41        friend class TimerFrameListener;
42
43        public:
[496]44            /** @brief Constructor: Sets the default-values. */
[384]45            TimerBase()
46            {
47                RegisterRootObject(TimerBase);
48
49                this->interval_ = 0;
50                this->bLoop_ = false;
51                this->bActive_ = false;
52
53                this->time_ = 0;
54            }
55
56            virtual void run() const = 0;
57
[496]58            /** @brief Starts the Timer: Function-call after 'interval' seconds. */
[384]59            inline void startTimer() { this->bActive_ = true; this->time_ = this->interval_; }
[496]60            /** @brief Stops the Timer. */
[384]61            inline void stopTimer() { this->bActive_ = false; this->time_ = this->interval_; }
[496]62            /** @brief Pauses the Timer - it will continue with the actual state if you unpause it. */
[384]63            inline void pauseTimer() { this->bActive_ = false; }
[496]64            /** @brief Unpauses the Timer - continues with the given state. */
[384]65            inline void unpauseTimer() { this->bActive_ = true; }
[496]66            /** @returns true if the Timer is active (= not stoped, not paused). */
[384]67            inline bool isActive() const { return this->bActive_; }
68
69        protected:
[496]70            float interval_;    //!< The time-interval in seconds
71            bool bLoop_;        //!< If true, the function gets called every 'interval' seconds
72            bool bActive_;      //!< If true, the Timer ticks and calls the function if the time's up
[384]73
[496]74            float time_;        //!< Internal variable, counting the time till the next function-call
[384]75    };
76
[496]77    //! The Timer is a callback-object, calling a given function after a given time-interval.
[384]78    template <class T = BaseObject>
79    class Timer : public TimerBase
80    {
81        public:
[496]82            /** @brief Constructor: Sets the default-values. */
[384]83            Timer()
84            {
85                this->timerFunction_ = 0;
86                this->object_ = 0;
87            }
88
[496]89            /**
90                @brief Constructor: Initializes the Timer with given values.
91                @param interval The timer-interval in seconds
92                @param bLoop If true, the function gets called every 'interval' seconds
93                @param object The object owning the timer and the function
94                @param timerFunction A function pointer to the function to call
95            */
[384]96            Timer(float interval, bool bLoop, T* object, void (T::*timerFunction)())
97            {
98                this->setTimer(interval, bLoop, timerFunction, object);
99            }
100
[496]101            /**
102                @brief Initializes the Timer with given values.
103                @param interval The timer-interval in seconds
104                @param bLoop If true, the function gets called every 'interval' seconds
105                @param object The object owning the timer and the function
106                @param timerFunction A function pointer to the function to call
107            */
[384]108            void setTimer(float interval, bool bLoop, T* object, void (T::*timerFunction)())
109            {
110                this->interval_ = interval;
111                this->bLoop_ = bLoop;
112                this->timerFunction_ = timerFunction;
113                this->object_ = object;
114                this->bActive_ = true;
115
116                this->time_ = interval;
117            }
118
[496]119            /** @brief Calls the given function of the given object. */
[384]120            void run() const
121            {
122                ((*this->object_).*timerFunction_)();
123            }
124
125        private:
126            void (T::*timerFunction_)();
127            T* object_;
128    };
129
[496]130    //! The TimerFrameListener manages all Timers in the game.
[384]131    class TimerFrameListener : public Ogre::FrameListener
132    {
133        private:
[496]134            /** @brief Gets called before a frame gets rendered. */
[384]135            bool frameStarted(const Ogre::FrameEvent &evt)
136            {
[496]137                // Iterate through all Timers
[384]138                for (Iterator<TimerBase> it = ObjectList<TimerBase>::start(); it; ++it)
139                {
140                    if (it->isActive())
141                    {
[496]142                        // If active: Decrease the timer by the duration of the last frame
[384]143                        it->time_ -= evt.timeSinceLastFrame;
144
145                        if (it->time_ <= 0)
146                        {
[496]147                            // It's time to call the function
[384]148                            if (it->bLoop_)
[496]149                                it->time_ += it->interval_; // Q: Why '+=' and not '='? A: Think about it. It's more accurate like that. Seriously.
[384]150                            else
[496]151                                it->stopTimer(); // Stop the timer if we don't want to loop
[384]152
153                            it->run();
154                        }
155                    }
156                }
157
158                return FrameListener::frameStarted(evt);
159            }
160    };
161}
162
163#endif
Note: See TracBrowser for help on using the repository browser.