Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/archive/tutorialHS08/src/orxonox/tools/Timer.cc @ 10255

Last change on this file since 10255 was 1755, checked in by rgrieder, 16 years ago

merged gui back to trunk.
update the media repository!

  • Property svn:eol-style set to native
File size: 4.2 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
95        this->time_ = 0;
96
97        RegisterRootObject(TimerBase);
98    }
99
100    /**
101        @brief Deletes the executor.
102    */
103    TimerBase::~TimerBase()
104    {
105        this->deleteExecutor();
106    }
107
108    /**
109        @brief Executes the executor.
110    */
111    void TimerBase::run() const
112    {
113        (*this->executor_)();
114    }
115
116    /**
117        @brief Deletes the executor.
118    */
119    void TimerBase::deleteExecutor()
120    {
121      if (this->executor_)
122          delete this->executor_;
123    }
124
125    /**
126        @brief Updates the timer before the frames are rendered.
127    */
128    void TimerBase::tick(const Clock& time)
129    {
130        if (this->bActive_)
131        {
132            // If active: Decrease the timer by the duration of the last frame
133            this->time_ -= time.getDeltaTimeMicroseconds();
134
135            if (this->time_ <= 0)
136            {
137                // It's time to call the function
138                if (this->bLoop_)
139                {
140                    this->time_ += this->interval_; // Q: Why '+=' and not '='? A: Think about it. It's more accurate like that. Seriously.
141                    while (this->time_ <= 0)
142                    {
143                        // The interval was shorter than one tick, so execute the function more than once
144                        this->run();
145                        this->time_ += this->interval_;
146                    }
147                }
148                else
149                    this->stopTimer(); // Stop the timer if we don't want to loop
150
151                this->run();
152            }
153        }
154    }
155}
Note: See TracBrowser for help on using the repository browser.