Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy/src/orxonox/objects/Timer.h @ 452

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

More documentation and comments (it's so much fun!)

File size: 5.8 KB
RevLine 
[452]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
[375]30#ifndef _Timer_H__
31#define _Timer_H__
32
[443]33#include "../core/CoreIncludes.h"
[434]34#include "OgreFrameListener.h"
[375]35
36namespace orxonox
37{
[452]38    //! TimerBase is the parent of the Timer class.
[375]39    class TimerBase : public OrxonoxClass
40    {
41        friend class TimerFrameListener;
42
43        public:
[452]44            /** @brief Constructor: Sets the default-values. */
[375]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
[452]58            /** @brief Starts the Timer: Function-call after 'interval' seconds. */
[375]59            inline void startTimer() { this->bActive_ = true; this->time_ = this->interval_; }
[452]60            /** @brief Stops the Timer. */
[375]61            inline void stopTimer() { this->bActive_ = false; this->time_ = this->interval_; }
[452]62            /** @brief Pauses the Timer - it will continue with the actual state if you unpause it. */
[375]63            inline void pauseTimer() { this->bActive_ = false; }
[452]64            /** @brief Unpauses the Timer - continues with the given state. */
[375]65            inline void unpauseTimer() { this->bActive_ = true; }
[452]66            /** @returns true if the Timer is active (= not stoped, not paused). */
[375]67            inline bool isActive() const { return this->bActive_; }
68
69        protected:
[452]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
[375]73
[452]74            float time_;        //!< Internal variable, counting the time till the next function-call
[375]75    };
76
[452]77    //! The Timer is a callback-object, calling a given function after a given time-interval.
[375]78    template <class T = BaseObject>
79    class Timer : public TimerBase
80    {
81        public:
[452]82            /** @brief Constructor: Sets the default-values. */
[375]83            Timer()
84            {
85                this->timerFunction_ = 0;
86                this->object_ = 0;
87            }
88
[452]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            */
[375]96            Timer(float interval, bool bLoop, T* object, void (T::*timerFunction)())
97            {
98                this->setTimer(interval, bLoop, timerFunction, object);
99            }
100
[452]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            */
[375]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
[452]119            /** @brief Calls the given function of the given object. */
[375]120            void run() const
121            {
122                ((*this->object_).*timerFunction_)();
123            }
124
125        private:
126            void (T::*timerFunction_)();
127            T* object_;
128    };
129
[452]130    //! The TimerFrameListener manages all Timers in the game.
[375]131    class TimerFrameListener : public Ogre::FrameListener
132    {
133        private:
[452]134            /** @brief Gets called before a frame gets rendered. */
[375]135            bool frameStarted(const Ogre::FrameEvent &evt)
136            {
[452]137                // Iterate through all Timers
[375]138                for (Iterator<TimerBase> it = ObjectList<TimerBase>::start(); it; ++it)
139                {
140                    if (it->isActive())
141                    {
[452]142                        // If active: Decrease the timer by the duration of the last frame
[375]143                        it->time_ -= evt.timeSinceLastFrame;
144
145                        if (it->time_ <= 0)
146                        {
[452]147                            // It's time to call the function
[375]148                            if (it->bLoop_)
[452]149                                it->time_ += it->interval_; // Q: Why '+=' and not '='? A: Think about it. It's more accurate like that. Seriously.
[375]150                            else
[452]151                                it->stopTimer(); // Stop the timer if we don't want to loop
[375]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.