Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/tools/Timer.cc @ 2131

Last change on this file since 2131 was 2087, checked in by landauf, 16 years ago

merged objecthierarchy branch back to trunk

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