Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7535 was 7401, checked in by landauf, 14 years ago

merged doc branch back to trunk

  • Property svn:eol-style set to native
File size: 5.1 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
[5929]38#include "util/Clock.h"
[1505]39#include "core/CoreIncludes.h"
[7284]40#include "core/command/ConsoleCommand.h"
41#include "core/command/CommandExecutor.h"
42#include "core/command/Functor.h"
[1505]43
44namespace orxonox
45{
[7284]46    SetConsoleCommand("delay", &delay).argumentCompleter(1, autocompletion::command());
47    SetConsoleCommand("killdelays", &killdelays);
[1505]48
[5929]49    static std::set<Timer*> delaytimerset;
[1505]50
51    /**
[7401]52        @brief Console-command: Calls another console command after @a delay seconds.
[1505]53        @param delay The delay in seconds
54        @param command The console command
55    */
56    void delay(float delay, const std::string& command)
57    {
[5929]58        Timer* delaytimer = new Timer();
[1505]59        delaytimerset.insert(delaytimer);
60
[7284]61        const ExecutorStaticPtr& delayexecutor = createExecutor(createFunctor(&executeDelayedCommand));
[1505]62        delayexecutor->setDefaultValues(delaytimer, command);
63        delaytimer->setTimer(delay, false, delayexecutor);
64    }
65
66    /**
[7401]67        @brief Helper function for delay(), executes the command and destroys the timer.
68        @param timer The timer which called this function.
[1505]69        @param command The command to execute
70    */
[5929]71    void executeDelayedCommand(Timer* timer, const std::string& command)
[1505]72    {
73        CommandExecutor::execute(command);
[5929]74        timer->destroy();
[1505]75        delaytimerset.erase(timer);
76    }
77
78    /**
[7401]79        @brief Console-command: Kills all scheduled commands that were delayed using delay().
[1505]80    */
81    void killdelays()
82    {
[5929]83        for (std::set<Timer*>::iterator it = delaytimerset.begin(); it != delaytimerset.end(); ++it)
84            (*it)->destroy();
[1505]85
86        delaytimerset.clear();
87    }
88
89    /**
90        @brief Constructor: Sets the default-values.
91    */
[5929]92    Timer::Timer()
[1505]93    {
[5929]94        this->init();
95        RegisterObject(Timer);
96    }
[1505]97
[5929]98    /**
[7401]99        @brief Constructor: Initializes and starts the timer, which will call an executor after some time.
100        @param interval         The timer-interval in seconds
101        @param bLoop            If true, the executor gets called every @a interval seconds
102        @param executor         The executor that will be called
103        @param bKillAfterCall   If true, the timer will be deleted after the executor was called
[5929]104    */
[7284]105    Timer::Timer(float interval, bool bLoop, const ExecutorPtr& executor, bool bKillAfterCall)
[5929]106    {
107        this->init();
108        RegisterObject(Timer);
[1505]109
[5929]110        this->setTimer(interval, bLoop, executor, bKillAfterCall);
[1505]111    }
112
113    /**
[5929]114        @brief Initializes the Timer
115    */
116    void Timer::init()
117    {
118        this->executor_ = 0;
119        this->interval_ = 0;
120        this->bLoop_ = false;
121        this->bActive_ = false;
122        this->bKillAfterCall_ = false;
[1505]123
[5929]124        this->time_ = 0;
125    }
126
[1505]127    /**
[7401]128        @brief Calls the executor and destroys the timer if requested.
[1505]129    */
[5929]130    void Timer::run()
[1505]131    {
[2087]132        bool temp = this->bKillAfterCall_; // to avoid errors with bKillAfterCall_=false and an exutors which destroy the timer
133
[1505]134        (*this->executor_)();
[2087]135
136        if (temp)
[5929]137            this->destroy();
[1505]138    }
139
140    /**
141        @brief Updates the timer before the frames are rendered.
142    */
[5929]143    void Timer::tick(const Clock& time)
[1505]144    {
145        if (this->bActive_)
146        {
147            // If active: Decrease the timer by the duration of the last frame
[3301]148            this->time_ -= static_cast<long long>(time.getDeltaTimeMicroseconds() * this->getTimeFactor());
[1505]149
150            if (this->time_ <= 0)
151            {
152                // It's time to call the function
[2087]153                if (this->bLoop_ && !this->bKillAfterCall_)
[1505]154                {
155                    this->time_ += this->interval_; // Q: Why '+=' and not '='? A: Think about it. It's more accurate like that. Seriously.
156                    while (this->time_ <= 0)
157                    {
158                        // The interval was shorter than one tick, so execute the function more than once
159                        this->run();
160                        this->time_ += this->interval_;
161                    }
162                }
163                else
164                    this->stopTimer(); // Stop the timer if we don't want to loop
165
166                this->run();
167            }
168        }
169    }
170}
Note: See TracBrowser for help on using the repository browser.