Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7869 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
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 Implementation of the Timer class.
32*/
33
34#include "Timer.h"
35
36#include <set>
37
38#include "util/Clock.h"
39#include "core/CoreIncludes.h"
40#include "core/command/ConsoleCommand.h"
41#include "core/command/CommandExecutor.h"
42#include "core/command/Functor.h"
43
44namespace orxonox
45{
46    SetConsoleCommand("delay", &delay).argumentCompleter(1, autocompletion::command());
47    SetConsoleCommand("killdelays", &killdelays);
48
49    static std::set<Timer*> delaytimerset;
50
51    /**
52        @brief Console-command: Calls another console command after @a delay seconds.
53        @param delay The delay in seconds
54        @param command The console command
55    */
56    void delay(float delay, const std::string& command)
57    {
58        Timer* delaytimer = new Timer();
59        delaytimerset.insert(delaytimer);
60
61        const ExecutorStaticPtr& delayexecutor = createExecutor(createFunctor(&executeDelayedCommand));
62        delayexecutor->setDefaultValues(delaytimer, command);
63        delaytimer->setTimer(delay, false, delayexecutor);
64    }
65
66    /**
67        @brief Helper function for delay(), executes the command and destroys the timer.
68        @param timer The timer which called this function.
69        @param command The command to execute
70    */
71    void executeDelayedCommand(Timer* timer, const std::string& command)
72    {
73        CommandExecutor::execute(command);
74        timer->destroy();
75        delaytimerset.erase(timer);
76    }
77
78    /**
79        @brief Console-command: Kills all scheduled commands that were delayed using delay().
80    */
81    void killdelays()
82    {
83        for (std::set<Timer*>::iterator it = delaytimerset.begin(); it != delaytimerset.end(); ++it)
84            (*it)->destroy();
85
86        delaytimerset.clear();
87    }
88
89    /**
90        @brief Constructor: Sets the default-values.
91    */
92    Timer::Timer()
93    {
94        this->init();
95        RegisterObject(Timer);
96    }
97
98    /**
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
104    */
105    Timer::Timer(float interval, bool bLoop, const ExecutorPtr& executor, bool bKillAfterCall)
106    {
107        this->init();
108        RegisterObject(Timer);
109
110        this->setTimer(interval, bLoop, executor, bKillAfterCall);
111    }
112
113    /**
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;
123
124        this->time_ = 0;
125    }
126
127    /**
128        @brief Calls the executor and destroys the timer if requested.
129    */
130    void Timer::run()
131    {
132        bool temp = this->bKillAfterCall_; // to avoid errors with bKillAfterCall_=false and an exutors which destroy the timer
133
134        (*this->executor_)();
135
136        if (temp)
137            this->destroy();
138    }
139
140    /**
141        @brief Updates the timer before the frames are rendered.
142    */
143    void Timer::tick(const Clock& time)
144    {
145        if (this->bActive_)
146        {
147            // If active: Decrease the timer by the duration of the last frame
148            this->time_ -= static_cast<long long>(time.getDeltaTimeMicroseconds() * this->getTimeFactor());
149
150            if (this->time_ <= 0)
151            {
152                // It's time to call the function
153                if (this->bLoop_ && !this->bKillAfterCall_)
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.