Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/tools/Timer.cc @ 12138

Last change on this file since 12138 was 12028, checked in by merholzl, 6 years ago

added scriptable controller

  • Property svn:eol-style set to native
File size: 9.3 KB
RevLine 
[1505]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
[7401]29/**
30    @file
31    @brief Implementation of the Timer class.
32*/
33
[3196]34#include "Timer.h"
35
[1505]36#include <set>
37
[8079]38#include <boost/bimap.hpp>
39
[5929]40#include "util/Clock.h"
[1505]41#include "core/CoreIncludes.h"
[10624]42#include "core/command/ConsoleCommandIncludes.h"
[7284]43#include "core/command/CommandExecutor.h"
[8729]44#include "core/command/Executor.h"
[7284]45#include "core/command/Functor.h"
[8079]46#include "tools/interfaces/TimeFactorListener.h"
[1505]47
48namespace orxonox
49{
[7284]50    SetConsoleCommand("delay", &delay).argumentCompleter(1, autocompletion::command());
[8079]51    SetConsoleCommand("delayreal", &delayreal).argumentCompleter(1, autocompletion::command());
52    SetConsoleCommand("killdelay", &killdelay);
[7284]53    SetConsoleCommand("killdelays", &killdelays);
[1505]54
[8079]55    static boost::bimap<unsigned int, Timer*> delaytimers;
56    static unsigned int delayHandleCounter = 0;
[1505]57
58    /**
[8079]59        @brief Console-command: Calls another console command after @a delay seconds (game time).
[1505]60        @param delay The delay in seconds
61        @param command The console command
[8079]62        @return The handle of the delayed command, can be used as argument for killdelay()
[1505]63    */
[8079]64    unsigned int delay(float delay, const std::string& command)
[1505]65    {
[8079]66        return addDelayedCommand(new Timer(), delay, command);
67    }
[1505]68
[8079]69    /**
70        @brief Console-command: Calls another console command after @a delay seconds (real time)
71        @param delay The delay in seconds
72        @param command The console command
73        @return The handle of the delayed command, can be used as argument for killdelay()
74    */
75    unsigned int delayreal(float delay, const std::string& command)
76    {
77        return addDelayedCommand(new RealTimer(), delay, command);
78    }
79
80    /**
81        @brief Helper function, used by delay() and delayreal() to add a delayed command.
82        @param timer The timer which will execute the command
83        @param delay The delay in seconds
84        @param command The console command
85        @return The handle of the delayed command, can be used as argument for killdelay()
86    */
87    unsigned int addDelayedCommand(Timer* timer, float delay, const std::string& command)
88    {
89        delaytimers.insert(boost::bimap<unsigned int, Timer*>::value_type(++delayHandleCounter, timer));
90
[7284]91        const ExecutorStaticPtr& delayexecutor = createExecutor(createFunctor(&executeDelayedCommand));
[8079]92        delayexecutor->setDefaultValues(timer, command);
93        timer->setTimer(delay, false, delayexecutor);
94
95        return delayHandleCounter;
[1505]96    }
97
98    /**
[7401]99        @brief Helper function for delay(), executes the command and destroys the timer.
100        @param timer The timer which called this function.
[1505]101        @param command The command to execute
102    */
[5929]103    void executeDelayedCommand(Timer* timer, const std::string& command)
[1505]104    {
105        CommandExecutor::execute(command);
[11018]106        delete timer;
[8079]107        delaytimers.right.erase(timer);
[1505]108    }
109
110    /**
[7401]111        @brief Console-command: Kills all scheduled commands that were delayed using delay().
[1505]112    */
113    void killdelays()
114    {
[8079]115        for (boost::bimap<unsigned int, Timer*>::left_map::iterator it = delaytimers.left.begin(); it != delaytimers.left.end(); ++it)
[11018]116            delete it->second;
[1505]117
[8079]118        delaytimers.clear();
[1505]119    }
120
121    /**
[8079]122        @brief Console-command: Kills a delayed command with given handle.
123    */
124    void killdelay(unsigned int handle)
125    {
126        boost::bimap<unsigned int, Timer*>::left_map::iterator it = delaytimers.left.find(handle);
127        if (it != delaytimers.left.end())
128        {
[11018]129            delete it->second;
[8079]130            delaytimers.left.erase(it);
131        }
132    }
133
[9667]134    RegisterClassNoArgs(Timer);
135
[8079]136    /**
[1505]137        @brief Constructor: Sets the default-values.
138    */
[5929]139    Timer::Timer()
[1505]140    {
[5929]141        this->init();
[9667]142        RegisterObject(Timer);
[5929]143    }
[1505]144
[5929]145    /**
[7401]146        @brief Constructor: Initializes and starts the timer, which will call an executor after some time.
147        @param interval         The timer-interval in seconds
148        @param bLoop            If true, the executor gets called every @a interval seconds
149        @param executor         The executor that will be called
150        @param bKillAfterCall   If true, the timer will be deleted after the executor was called
[5929]151    */
[7284]152    Timer::Timer(float interval, bool bLoop, const ExecutorPtr& executor, bool bKillAfterCall)
[5929]153    {
154        this->init();
[9667]155        RegisterObject(Timer);
[1505]156
[5929]157        this->setTimer(interval, bLoop, executor, bKillAfterCall);
[1505]158    }
159
[12028]160    Timer::Timer(float interval, bool bLoop, std::function<void ()> func, bool bKillAfterCall)
161    {
162        this->init();
163        RegisterObject(Timer);
164
165        this->setTimer(interval, bLoop, func, bKillAfterCall);
166    }
167
[1505]168    /**
[5929]169        @brief Initializes the Timer
170    */
171    void Timer::init()
172    {
[11071]173        this->executor_ = nullptr;
[5929]174        this->interval_ = 0;
175        this->bLoop_ = false;
176        this->bActive_ = false;
177        this->bKillAfterCall_ = false;
[1505]178
[5929]179        this->time_ = 0;
180    }
181
[1505]182    /**
[8079]183        @brief Returns the current time factor of the game.
184    */
185    float Timer::getTimeFactor()
186    {
187        return TimeFactorListener::getTimeFactor();
188    }
189
190    /**
[8729]191        @brief Initializes and starts the timer, which will call an executor after some time.
192        @param interval         The timer-interval in seconds
193        @param bLoop            If true, the executor gets called every @a interval seconds
194        @param executor         The executor that will be called
195        @param bKillAfterCall   If true, the timer will be deleted after the executor was called
196    */
197    void Timer::setTimer(float interval, bool bLoop, const ExecutorPtr& executor, bool bKillAfterCall)
198    {
199        this->setInterval(interval);
200        this->bLoop_ = bLoop;
201        this->executor_ = executor;
202        this->bActive_ = true;
[12028]203        this->isStdFunction_ = false;
[8729]204
205        this->time_ = this->interval_;
206        this->bKillAfterCall_ = bKillAfterCall;
207
[12028]208        if(executor != nullptr)
209            executor->getFunctor()->setSafeMode(true);
[8729]210    }
211
[12028]212    void Timer::setTimer(float interval, bool bLoop, std::function<void ()> func, bool bKillAfterCall)
213    {
214        // Without the cast, the call would be ambiguous, because nullptr is castable to
215        // both, ExecutorPtr and std::function.
216        this->setTimer(interval, bLoop, static_cast<ExecutorPtr>(nullptr), bKillAfterCall);
217        this->function_ = func;
218        this->isStdFunction_ = true;
219    }
220
[8729]221    /**
[7401]222        @brief Calls the executor and destroys the timer if requested.
[1505]223    */
[5929]224    void Timer::run()
[1505]225    {
[2087]226        bool temp = this->bKillAfterCall_; // to avoid errors with bKillAfterCall_=false and an exutors which destroy the timer
227
[12028]228        if(this->isStdFunction_)
229            this->function_();
230        else
231            (*this->executor_)();
[2087]232
233        if (temp)
[11018]234            delete this;
[1505]235    }
236
237    /**
238        @brief Updates the timer before the frames are rendered.
239    */
[5929]240    void Timer::tick(const Clock& time)
[1505]241    {
242        if (this->bActive_)
243        {
244            // If active: Decrease the timer by the duration of the last frame
[3301]245            this->time_ -= static_cast<long long>(time.getDeltaTimeMicroseconds() * this->getTimeFactor());
[1505]246
247            if (this->time_ <= 0)
248            {
249                // It's time to call the function
[2087]250                if (this->bLoop_ && !this->bKillAfterCall_)
[1505]251                {
252                    this->time_ += this->interval_; // Q: Why '+=' and not '='? A: Think about it. It's more accurate like that. Seriously.
253                    while (this->time_ <= 0)
254                    {
255                        // The interval was shorter than one tick, so execute the function more than once
256                        this->run();
257                        this->time_ += this->interval_;
258                    }
259                }
260                else
261                    this->stopTimer(); // Stop the timer if we don't want to loop
262
263                this->run();
264            }
265        }
266    }
[8079]267
268    ///////////////
269    // RealTimer //
270    ///////////////
[10624]271
272    RegisterClassNoArgs(RealTimer);
273
[8079]274    /// @copydoc Timer::Timer
275    RealTimer::RealTimer()
276    {
277        RegisterObject(RealTimer);
278    }
279
280    /// @copydoc Timer::Timer(float, bool, const ExecutorPtr&, bool)
281    RealTimer::RealTimer(float interval, bool bLoop, const ExecutorPtr& executor, bool bKillAfterCall) : Timer(interval, bLoop, executor, bKillAfterCall)
282    {
283        RegisterObject(RealTimer);
284    }
285
286    /// Returns always 1 because RealTimer doesn't depend on the game time.
287    float RealTimer::getTimeFactor()
288    {
289        return 1;
290    }
[1505]291}
Note: See TracBrowser for help on using the repository browser.