Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 669 was 646, checked in by landauf, 17 years ago
  • added very bad collision detection (presentation hack :D)
  • added explosions
  • fixed bug in ParticleInterface (it tried to delete SceneManager)

AND:

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