Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/usability/src/libraries/tools/Timer.cc @ 8020

Last change on this file since 8020 was 8020, checked in by landauf, 13 years ago

delayed commands now return a handle which allows to kill them selectively

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