Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/consolecommands/src/libraries/tools/Timer.h @ 6514

Last change on this file since 6514 was 5929, checked in by rgrieder, 15 years ago

Merged core5 branch back to the trunk.
Key features include clean level unloading and an extended XML event system.

Two important notes:
Delete your keybindings.ini files! * or you will still get parser errors when loading the key bindings.
Delete build_dir/lib/modules/libgamestates.module! * or orxonox won't start.
Best thing to do is to delete the build folder ;)

  • Property svn:eol-style set to native
File size: 5.8 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/*!
30    @file
31    @brief Definition and Implementation of the Timer class.
32
33    The Timer is a callback-object, calling a given function after a given time-interval.
34
35    Usage:
36    header.h:
37        class ClassName
38        {
39            public:
40                ClassName();
41                void functionName();
42                Timer myTimer;
43        };
44
45    source.cc:
46        include "core/Executor.h"
47
48        ClassName::ClassName()
49        {
50            myTimer.setTimer(interval_in_seconds, bLoop, createExecutor(createFunctor(&ClassName::functionName, this)));
51        }
52
53        void ClassName::functionName()
54        {
55            whateveryouwant();
56            something(else);
57        }
58*/
59
60#ifndef _Timer_H__
61#define _Timer_H__
62
63#include "tools/ToolsPrereqs.h"
64
65#include "core/Executor.h"
66#include "core/OrxonoxClass.h"
67#include "tools/interfaces/TimeFactorListener.h"
68
69namespace orxonox
70{
71    void delay(float delay, const std::string& command);
72    void killdelays();
73    void executeDelayedCommand(Timer* timer, const std::string& command);
74
75    //! The Timer is a callback-object, calling a given function after a given time-interval.
76    class _ToolsExport Timer : public TimeFactorListener
77    {
78        public:
79            Timer();
80            ~Timer();
81
82            Timer(float interval, bool bLoop, Executor* executor, bool bKillAfterCall = false);
83
84            /**
85                @brief Initializes the Timer with given values.
86                @param interval The timer-interval in seconds
87                @param bLoop If true, the function gets called every 'interval' seconds
88                @param object The object owning the timer and the function
89                @param executor A executor of the function to call
90            */
91            void setTimer(float interval, bool bLoop, Executor* executor, bool bKillAfterCall = false)
92            {
93                this->deleteExecutor();
94
95                this->setInterval(interval);
96                this->bLoop_ = bLoop;
97                this->executor_ = executor;
98                this->bActive_ = true;
99
100                this->time_ = this->interval_;
101                this->bKillAfterCall_ = bKillAfterCall;
102            }
103
104            void run();
105            void deleteExecutor();
106
107            /** @brief Starts the Timer: Function-call after 'interval' seconds. */
108            inline void startTimer()
109                { this->bActive_ = true; this->time_ = this->interval_; }
110            /** @brief Stops the Timer. */
111            inline void stopTimer()
112                { this->bActive_ = false; this->time_ = this->interval_; }
113            /** @brief Pauses the Timer - it will continue with the actual state if you unpause it. */
114            inline void pauseTimer()
115                { this->bActive_ = false; }
116            /** @brief Unpauses the Timer - continues with the given state. */
117            inline void unpauseTimer()
118                { this->bActive_ = true; }
119            /** @brief Returns true if the Timer is active (= not stoped, not paused). @return True = Time is active */
120            inline bool isActive() const
121                { return this->bActive_; }
122            /** @brief Returns the remaining time until the Timer calls the function. @return The remaining time */
123            inline float getRemainingTime() const
124                { return static_cast<float>(this->time_ / 1000000.0f); }
125            /** @brief Gives the Timer some extra time. @param time The amount of extra time in seconds */
126            inline void addTime(float time)
127                { if (time > 0.0f) this->time_ += static_cast<long long>(time * 1000000.0f); }
128            /** @brief Decreases the remaining time of the Timer. @param time The amount of time to remove */
129            inline void removeTime(float time)
130                { if (time > 0.0f) this->time_ -= static_cast<long long>(time * 1000000.0f); }
131            /** @brief Sets the interval of the Timer. @param interval The interval */
132            inline void setInterval(float interval)
133                { this->interval_ = static_cast<long long>(interval * 1000000.0f); }
134            /** @brief Sets bLoop to a given value. @param bLoop True = loop */
135            inline void setLoop(bool bLoop)
136                { this->bLoop_ = bLoop; }
137
138            void tick(const Clock& time);
139
140        private:
141            void init();
142       
143            Executor* executor_;  //!< The executor of the function that should be called when the time expires
144
145            long long interval_;  //!< The time-interval in micro seconds
146            bool bLoop_;          //!< If true, the function gets called every 'interval' seconds
147            bool bActive_;        //!< If true, the Timer ticks and calls the function if the time's up
148            bool bKillAfterCall_; //!< If true the timer gets deleted after it called the function
149
150            long long time_;      //!< Internal variable, counting the time till the next function-call
151    };
152}
153
154#endif /* _Timer_H__ */
Note: See TracBrowser for help on using the repository browser.