Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2/src/libraries/tools/Timer.cc @ 6435

Last change on this file since 6435 was 6387, checked in by rgrieder, 15 years ago

Found some non empty new lines.

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