Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/formationupdate/src/libraries/tools/Timer.cc @ 9891

Last change on this file since 9891 was 8729, checked in by rgrieder, 13 years ago

Merged unity_build branch back to trunk.

Features:

  • Implemented fully automatic build units to speed up compilation if requested
  • Added DOUT macro for quick debug output
  • Activated text colouring in the POSIX IOConsole
  • DeclareToluaInterface is not necessary anymore

Improvements:

  • Output levels now change appropriately when switch back and forth from dev mode
  • Log level for the file output is now also correct during startup
  • Removed some header file dependencies in core and tools to speed up compilation

no more file for command line options

  • Improved util::tribool by adapting some concepts from boost::tribool

Regressions:

  • It is not possible anymore to specify command line arguments in an extra file because we've got config values for that purpose.
  • Property svn:eol-style set to native
File size: 8.5 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 <boost/bimap.hpp>
39
40#include "util/Clock.h"
41#include "core/CoreIncludes.h"
42#include "core/command/ConsoleCommand.h"
43#include "core/command/CommandExecutor.h"
44#include "core/command/Executor.h"
45#include "core/command/Functor.h"
46#include "tools/interfaces/TimeFactorListener.h"
47
48namespace orxonox
49{
50    SetConsoleCommand("delay", &delay).argumentCompleter(1, autocompletion::command());
51    SetConsoleCommand("delayreal", &delayreal).argumentCompleter(1, autocompletion::command());
52    SetConsoleCommand("killdelay", &killdelay);
53    SetConsoleCommand("killdelays", &killdelays);
54
55    static boost::bimap<unsigned int, Timer*> delaytimers;
56    static unsigned int delayHandleCounter = 0;
57
58    /**
59        @brief Console-command: Calls another console command after @a delay seconds (game time).
60        @param delay The delay in seconds
61        @param command The console command
62        @return The handle of the delayed command, can be used as argument for killdelay()
63    */
64    unsigned int delay(float delay, const std::string& command)
65    {
66        return addDelayedCommand(new Timer(), delay, command);
67    }
68
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
91        const ExecutorStaticPtr& delayexecutor = createExecutor(createFunctor(&executeDelayedCommand));
92        delayexecutor->setDefaultValues(timer, command);
93        timer->setTimer(delay, false, delayexecutor);
94
95        return delayHandleCounter;
96    }
97
98    /**
99        @brief Helper function for delay(), executes the command and destroys the timer.
100        @param timer The timer which called this function.
101        @param command The command to execute
102    */
103    void executeDelayedCommand(Timer* timer, const std::string& command)
104    {
105        CommandExecutor::execute(command);
106        timer->destroy();
107        delaytimers.right.erase(timer);
108    }
109
110    /**
111        @brief Console-command: Kills all scheduled commands that were delayed using delay().
112    */
113    void killdelays()
114    {
115        for (boost::bimap<unsigned int, Timer*>::left_map::iterator it = delaytimers.left.begin(); it != delaytimers.left.end(); ++it)
116            it->second->destroy();
117
118        delaytimers.clear();
119    }
120
121    /**
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        {
129            it->second->destroy();
130            delaytimers.left.erase(it);
131        }
132    }
133
134    /**
135        @brief Constructor: Sets the default-values.
136    */
137    Timer::Timer()
138    {
139        this->init();
140        RegisterRootObject(Timer);
141    }
142
143    /**
144        @brief Constructor: Initializes and starts the timer, which will call an executor after some time.
145        @param interval         The timer-interval in seconds
146        @param bLoop            If true, the executor gets called every @a interval seconds
147        @param executor         The executor that will be called
148        @param bKillAfterCall   If true, the timer will be deleted after the executor was called
149    */
150    Timer::Timer(float interval, bool bLoop, const ExecutorPtr& executor, bool bKillAfterCall)
151    {
152        this->init();
153        RegisterRootObject(Timer);
154
155        this->setTimer(interval, bLoop, executor, bKillAfterCall);
156    }
157
158    /**
159        @brief Initializes the Timer
160    */
161    void Timer::init()
162    {
163        this->executor_ = 0;
164        this->interval_ = 0;
165        this->bLoop_ = false;
166        this->bActive_ = false;
167        this->bKillAfterCall_ = false;
168
169        this->time_ = 0;
170    }
171
172    /**
173        @brief Returns the current time factor of the game.
174    */
175    float Timer::getTimeFactor()
176    {
177        return TimeFactorListener::getTimeFactor();
178    }
179
180    /**
181        @brief Initializes and starts the timer, which will call an executor after some time.
182        @param interval         The timer-interval in seconds
183        @param bLoop            If true, the executor gets called every @a interval seconds
184        @param executor         The executor that will be called
185        @param bKillAfterCall   If true, the timer will be deleted after the executor was called
186    */
187    void Timer::setTimer(float interval, bool bLoop, const ExecutorPtr& executor, bool bKillAfterCall)
188    {
189        this->setInterval(interval);
190        this->bLoop_ = bLoop;
191        this->executor_ = executor;
192        this->bActive_ = true;
193
194        this->time_ = this->interval_;
195        this->bKillAfterCall_ = bKillAfterCall;
196
197        executor->getFunctor()->setSafeMode(true);
198    }
199
200    /**
201        @brief Calls the executor and destroys the timer if requested.
202    */
203    void Timer::run()
204    {
205        bool temp = this->bKillAfterCall_; // to avoid errors with bKillAfterCall_=false and an exutors which destroy the timer
206
207        (*this->executor_)();
208
209        if (temp)
210            this->destroy();
211    }
212
213    /**
214        @brief Updates the timer before the frames are rendered.
215    */
216    void Timer::tick(const Clock& time)
217    {
218        if (this->bActive_)
219        {
220            // If active: Decrease the timer by the duration of the last frame
221            this->time_ -= static_cast<long long>(time.getDeltaTimeMicroseconds() * this->getTimeFactor());
222
223            if (this->time_ <= 0)
224            {
225                // It's time to call the function
226                if (this->bLoop_ && !this->bKillAfterCall_)
227                {
228                    this->time_ += this->interval_; // Q: Why '+=' and not '='? A: Think about it. It's more accurate like that. Seriously.
229                    while (this->time_ <= 0)
230                    {
231                        // The interval was shorter than one tick, so execute the function more than once
232                        this->run();
233                        this->time_ += this->interval_;
234                    }
235                }
236                else
237                    this->stopTimer(); // Stop the timer if we don't want to loop
238
239                this->run();
240            }
241        }
242    }
243
244    ///////////////
245    // RealTimer //
246    ///////////////
247    /// @copydoc Timer::Timer
248    RealTimer::RealTimer()
249    {
250        RegisterObject(RealTimer);
251    }
252
253    /// @copydoc Timer::Timer(float, bool, const ExecutorPtr&, bool)
254    RealTimer::RealTimer(float interval, bool bLoop, const ExecutorPtr& executor, bool bKillAfterCall) : Timer(interval, bLoop, executor, bKillAfterCall)
255    {
256        RegisterObject(RealTimer);
257    }
258
259    /// Returns always 1 because RealTimer doesn't depend on the game time.
260    float RealTimer::getTimeFactor()
261    {
262        return 1;
263    }
264}
Note: See TracBrowser for help on using the repository browser.