Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

added a "Tickable"-interface
added a "Timer"-class

File size: 2.9 KB
Line 
1#ifndef _Timer_H__
2#define _Timer_H__
3
4#include "../core/IdentifierIncludes.h"
5#include "Ogre.h"
6
7namespace orxonox
8{
9    class TimerBase : public OrxonoxClass
10    {
11        friend class TimerFrameListener;
12
13        public:
14            TimerBase()
15            {
16                RegisterRootObject(TimerBase);
17
18                this->interval_ = 0;
19                this->bLoop_ = false;
20                this->bActive_ = false;
21
22                this->time_ = 0;
23            }
24
25            virtual void run() const = 0;
26
27            inline void startTimer() { this->bActive_ = true; this->time_ = this->interval_; }
28            inline void stopTimer() { this->bActive_ = false; this->time_ = this->interval_; }
29            inline void pauseTimer() { this->bActive_ = false; }
30            inline void unpauseTimer() { this->bActive_ = true; }
31            inline bool isActive() const { return this->bActive_; }
32
33        protected:
34            float interval_;
35            bool bLoop_;
36            bool bActive_;
37
38            float time_;
39    };
40
41    template <class T = BaseObject>
42    class Timer : public TimerBase
43    {
44        public:
45            Timer()
46            {
47                this->timerFunction_ = 0;
48                this->object_ = 0;
49            }
50
51            Timer(float interval, bool bLoop, T* object, void (T::*timerFunction)())
52            {
53                this->setTimer(interval, bLoop, timerFunction, object);
54            }
55
56            void setTimer(float interval, bool bLoop, T* object, void (T::*timerFunction)())
57            {
58                this->interval_ = interval;
59                this->bLoop_ = bLoop;
60                this->timerFunction_ = timerFunction;
61                this->object_ = object;
62                this->bActive_ = true;
63
64                this->time_ = interval;
65            }
66
67            void run() const
68            {
69                ((*this->object_).*timerFunction_)();
70            }
71
72        private:
73            void (T::*timerFunction_)();
74            T* object_;
75    };
76
77    class TimerFrameListener : public Ogre::FrameListener
78    {
79        private:
80            bool frameStarted(const Ogre::FrameEvent &evt)
81            {
82                for (Iterator<TimerBase> it = ObjectList<TimerBase>::start(); it; ++it)
83                {
84                    if (it->isActive())
85                    {
86                        it->time_ -= evt.timeSinceLastFrame;
87
88                        if (it->time_ <= 0)
89                        {
90                            if (it->bLoop_)
91                                it->time_ += it->interval_;
92                            else
93                                it->stopTimer();
94
95                            it->run();
96                        }
97                    }
98                }
99
100                return FrameListener::frameStarted(evt);
101            }
102    };
103}
104
105#endif
Note: See TracBrowser for help on using the repository browser.