Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/dockingsystem2/src/libraries/tools/Timer.cc @ 8953

Last change on this file since 8953 was 8079, checked in by landauf, 14 years ago

merged usability branch back to trunk

incomplete summary of the changes in this branch:

  • enhanced keyboard navigation in GUIs
  • implemented new graphics menu and changeable window size at runtime
  • added developer mode
  • HUD shows if game is paused, game pauses if ingame menu is opened
  • removed a few obsolete commands and hid some that are more for internal use
  • numpad works in console and gui
  • faster loading of level info
  • enhanced usage of compositors (Shader class)
  • improved camera handling, configurable FOV and aspect ratio
  • Property svn:eol-style set to native
File size: 7.6 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/Functor.h"
45#include "tools/interfaces/TimeFactorListener.h"
46
47namespace orxonox
48{
49    SetConsoleCommand("delay", &delay).argumentCompleter(1, autocompletion::command());
50    SetConsoleCommand("delayreal", &delayreal).argumentCompleter(1, autocompletion::command());
51    SetConsoleCommand("killdelay", &killdelay);
52    SetConsoleCommand("killdelays", &killdelays);
53
54    static boost::bimap<unsigned int, Timer*> delaytimers;
55    static unsigned int delayHandleCounter = 0;
56
57    /**
58        @brief Console-command: Calls another console command after @a delay seconds (game time).
59        @param delay The delay in seconds
60        @param command The console command
61        @return The handle of the delayed command, can be used as argument for killdelay()
62    */
63    unsigned int delay(float delay, const std::string& command)
64    {
65        return addDelayedCommand(new Timer(), delay, command);
66    }
67
68    /**
69        @brief Console-command: Calls another console command after @a delay seconds (real time)
70        @param delay The delay in seconds
71        @param command The console command
72        @return The handle of the delayed command, can be used as argument for killdelay()
73    */
74    unsigned int delayreal(float delay, const std::string& command)
75    {
76        return addDelayedCommand(new RealTimer(), delay, command);
77    }
78
79    /**
80        @brief Helper function, used by delay() and delayreal() to add a delayed command.
81        @param timer The timer which will execute the command
82        @param delay The delay in seconds
83        @param command The console command
84        @return The handle of the delayed command, can be used as argument for killdelay()
85    */
86    unsigned int addDelayedCommand(Timer* timer, float delay, const std::string& command)
87    {
88        delaytimers.insert(boost::bimap<unsigned int, Timer*>::value_type(++delayHandleCounter, timer));
89
90        const ExecutorStaticPtr& delayexecutor = createExecutor(createFunctor(&executeDelayedCommand));
91        delayexecutor->setDefaultValues(timer, command);
92        timer->setTimer(delay, false, delayexecutor);
93
94        return delayHandleCounter;
95    }
96
97    /**
98        @brief Helper function for delay(), executes the command and destroys the timer.
99        @param timer The timer which called this function.
100        @param command The command to execute
101    */
102    void executeDelayedCommand(Timer* timer, const std::string& command)
103    {
104        CommandExecutor::execute(command);
105        timer->destroy();
106        delaytimers.right.erase(timer);
107    }
108
109    /**
110        @brief Console-command: Kills all scheduled commands that were delayed using delay().
111    */
112    void killdelays()
113    {
114        for (boost::bimap<unsigned int, Timer*>::left_map::iterator it = delaytimers.left.begin(); it != delaytimers.left.end(); ++it)
115            it->second->destroy();
116
117        delaytimers.clear();
118    }
119
120    /**
121        @brief Console-command: Kills a delayed command with given handle.
122    */
123    void killdelay(unsigned int handle)
124    {
125        boost::bimap<unsigned int, Timer*>::left_map::iterator it = delaytimers.left.find(handle);
126        if (it != delaytimers.left.end())
127        {
128            it->second->destroy();
129            delaytimers.left.erase(it);
130        }
131    }
132
133    /**
134        @brief Constructor: Sets the default-values.
135    */
136    Timer::Timer()
137    {
138        this->init();
139        RegisterRootObject(Timer);
140    }
141
142    /**
143        @brief Constructor: Initializes and starts the timer, which will call an executor after some time.
144        @param interval         The timer-interval in seconds
145        @param bLoop            If true, the executor gets called every @a interval seconds
146        @param executor         The executor that will be called
147        @param bKillAfterCall   If true, the timer will be deleted after the executor was called
148    */
149    Timer::Timer(float interval, bool bLoop, const ExecutorPtr& executor, bool bKillAfterCall)
150    {
151        this->init();
152        RegisterRootObject(Timer);
153
154        this->setTimer(interval, bLoop, executor, bKillAfterCall);
155    }
156
157    /**
158        @brief Initializes the Timer
159    */
160    void Timer::init()
161    {
162        this->executor_ = 0;
163        this->interval_ = 0;
164        this->bLoop_ = false;
165        this->bActive_ = false;
166        this->bKillAfterCall_ = false;
167
168        this->time_ = 0;
169    }
170
171    /**
172        @brief Returns the current time factor of the game.
173    */
174    float Timer::getTimeFactor()
175    {
176        return TimeFactorListener::getTimeFactor();
177    }
178
179    /**
180        @brief Calls the executor and destroys the timer if requested.
181    */
182    void Timer::run()
183    {
184        bool temp = this->bKillAfterCall_; // to avoid errors with bKillAfterCall_=false and an exutors which destroy the timer
185
186        (*this->executor_)();
187
188        if (temp)
189            this->destroy();
190    }
191
192    /**
193        @brief Updates the timer before the frames are rendered.
194    */
195    void Timer::tick(const Clock& time)
196    {
197        if (this->bActive_)
198        {
199            // If active: Decrease the timer by the duration of the last frame
200            this->time_ -= static_cast<long long>(time.getDeltaTimeMicroseconds() * this->getTimeFactor());
201
202            if (this->time_ <= 0)
203            {
204                // It's time to call the function
205                if (this->bLoop_ && !this->bKillAfterCall_)
206                {
207                    this->time_ += this->interval_; // Q: Why '+=' and not '='? A: Think about it. It's more accurate like that. Seriously.
208                    while (this->time_ <= 0)
209                    {
210                        // The interval was shorter than one tick, so execute the function more than once
211                        this->run();
212                        this->time_ += this->interval_;
213                    }
214                }
215                else
216                    this->stopTimer(); // Stop the timer if we don't want to loop
217
218                this->run();
219            }
220        }
221    }
222
223    ///////////////
224    // RealTimer //
225    ///////////////
226    /// @copydoc Timer::Timer
227    RealTimer::RealTimer()
228    {
229        RegisterObject(RealTimer);
230    }
231
232    /// @copydoc Timer::Timer(float, bool, const ExecutorPtr&, bool)
233    RealTimer::RealTimer(float interval, bool bLoop, const ExecutorPtr& executor, bool bKillAfterCall) : Timer(interval, bLoop, executor, bKillAfterCall)
234    {
235        RegisterObject(RealTimer);
236    }
237
238    /// Returns always 1 because RealTimer doesn't depend on the game time.
239    float RealTimer::getTimeFactor()
240    {
241        return 1;
242    }
243}
Note: See TracBrowser for help on using the repository browser.