Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 708 was 708, checked in by rgrieder, 16 years ago
  • added Vector2, Vector3, Matrix3, ColourValue, Quaternion and String to the misc folder as header files (each of them contains #include <string> … typedef std::string String , etc.)
  • please use String from now on by including <misc/String.h"
  • removed #include <OgreVector3.h", etc. from "CoreIncludes.h" (adjusted all source files)
  • adjusted all the source files (except network, that keeps <string> for the moment) (what a mess..)
  • moved usleep hack to misc/Sleep.h
  • relative include paths for files from other root directories (like misc, network, etc.) (but it stills writes "../Orxonox.h" when in folder orxonox/objects)
  • "OgreSceneManager.h" —> <OgreSceneManager.h>
  • included OrxonoxPrereqs in every file in folder orxonox
  • moved HUD and ParticleInterface to namespace orxonox
  • removed some using namespace Ogre/std when appropriate
  • I hope I haven't forgotten important points..
File size: 6.9 KB
Line 
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
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();
41                Timer<ClassName> myTimer;
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
57#ifndef _Timer_H__
58#define _Timer_H__
59
60#include <OgreFrameListener.h>
61
62#include "../OrxonoxPrereqs.h"
63
64#include "../core/CoreIncludes.h"
65
66namespace orxonox
67{
68    //! TimerBase is the parent of the Timer class.
69    class TimerBase : public OrxonoxClass
70    {
71        friend class TimerFrameListener;
72
73        public:
74            /** @brief Constructor: Sets the default-values. */
75            TimerBase()
76            {
77                RegisterRootObject(TimerBase);
78
79                this->interval_ = 0;
80                this->bLoop_ = false;
81                this->bActive_ = false;
82
83                this->time_ = 0;
84            }
85
86            virtual void run() const = 0;
87
88            /** @brief Starts the Timer: Function-call after 'interval' seconds. */
89            inline void startTimer() { this->bActive_ = true; this->time_ = this->interval_; }
90            /** @brief Stops the Timer. */
91            inline void stopTimer() { this->bActive_ = false; this->time_ = this->interval_; }
92            /** @brief Pauses the Timer - it will continue with the actual state if you unpause it. */
93            inline void pauseTimer() { this->bActive_ = false; }
94            /** @brief Unpauses the Timer - continues with the given state. */
95            inline void unpauseTimer() { this->bActive_ = true; }
96            /** @returns true if the Timer is active (= not stoped, not paused). */
97            inline bool isActive() const { return this->bActive_; }
98
99        protected:
100            float interval_;    //!< The time-interval in seconds
101            bool bLoop_;        //!< If true, the function gets called every 'interval' seconds
102            bool bActive_;      //!< If true, the Timer ticks and calls the function if the time's up
103
104            float time_;        //!< Internal variable, counting the time till the next function-call
105    };
106
107    //! The Timer is a callback-object, calling a given function after a given time-interval.
108    template <class T = BaseObject>
109    class Timer : public TimerBase
110    {
111        public:
112            /** @brief Constructor: Sets the default-values. */
113            Timer()
114            {
115                this->timerFunction_ = 0;
116                this->object_ = 0;
117            }
118
119            /**
120                @brief Constructor: Initializes the Timer with given values.
121                @param interval The timer-interval in seconds
122                @param bLoop If true, the function gets called every 'interval' seconds
123                @param object The object owning the timer and the function
124                @param timerFunction A function pointer to the function to call
125            */
126            Timer(float interval, bool bLoop, T* object, void (T::*timerFunction)())
127            {
128                this->setTimer(interval, bLoop, timerFunction, object);
129            }
130
131            /**
132                @brief Initializes the Timer with given values.
133                @param interval The timer-interval in seconds
134                @param bLoop If true, the function gets called every 'interval' seconds
135                @param object The object owning the timer and the function
136                @param timerFunction A function pointer to the function to call
137            */
138            void setTimer(float interval, bool bLoop, T* object, void (T::*timerFunction)())
139            {
140                this->interval_ = interval;
141                this->bLoop_ = bLoop;
142                this->timerFunction_ = timerFunction;
143                this->object_ = object;
144                this->bActive_ = true;
145
146                this->time_ = interval;
147            }
148
149            /** @brief Calls the given function of the given object. */
150            void run() const
151            {
152                ((*this->object_).*timerFunction_)();
153            }
154
155        private:
156            void (T::*timerFunction_)();
157            T* object_;
158    };
159
160    //! The TimerFrameListener manages all Timers in the game.
161    class TimerFrameListener : public Ogre::FrameListener
162    {
163        private:
164            /** @brief Gets called before a frame gets rendered. */
165            bool frameStarted(const Ogre::FrameEvent &evt)
166            {
167                // Iterate through all Timers
168                for (Iterator<TimerBase> it = ObjectList<TimerBase>::start(); it; )
169                {
170                    if (it->isActive())
171                    {
172                        // If active: Decrease the timer by the duration of the last frame
173                        it->time_ -= evt.timeSinceLastFrame;
174
175                        if (it->time_ <= 0)
176                        {
177                            // It's time to call the function
178                            if (it->bLoop_)
179                                it->time_ += it->interval_; // Q: Why '+=' and not '='? A: Think about it. It's more accurate like that. Seriously.
180                            else
181                                it->stopTimer(); // Stop the timer if we don't want to loop
182
183                            (it++)->run();
184                        }
185                        else
186                            ++it;
187                    }
188                    else
189                        ++it;
190                }
191
192                return FrameListener::frameStarted(evt);
193            }
194    };
195}
196
197#endif /* _Timer_H__ */
Note: See TracBrowser for help on using the repository browser.