Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

don't panic, no codechanges!
added a link to www.orxonox.net

File size: 3.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 "OrxonoxStableHeaders.h"
30#include "core/Executor.h"
31#include "core/CoreIncludes.h"
32#include "core/ConsoleCommand.h"
33#include "core/CommandExecutor.h"
34#include "Timer.h"
35
36namespace orxonox
37{
38    ConsoleCommandShortcutExtern(delay, AccessLevel::None);
39
40    /**
41        @brief Calls a console command after 'delay' seconds.
42        @param delay The delay in seconds
43        @param command The console command
44    */
45    void delay(float delay, const std::string& command)
46    {
47        StaticTimer *delaytimer = new StaticTimer();
48        ExecutorStatic* delayexecutor = createExecutor(createFunctor(&executeDelayedCommand));
49        delayexecutor->setDefaultValues(delaytimer, command);
50        delaytimer->setTimer(delay, false, delayexecutor);
51    }
52
53    /**
54        @brief Executes the command.
55        @param timer The timer to destroy after the command-execution
56        @param command The command to execute
57    */
58    void executeDelayedCommand(StaticTimer* timer, const std::string& command)
59    {
60        CommandExecutor::execute(command);
61        delete timer;
62    }
63
64    /**
65        @brief Constructor: Sets the default-values.
66    */
67    TimerBase::TimerBase()
68    {
69        RegisterRootObject(TimerBase);
70
71        this->executor_ = 0;
72        this->interval_ = 0;
73        this->bLoop_ = false;
74        this->bActive_ = false;
75
76        this->time_ = 0;
77    }
78
79    /**
80        @brief Deletes the executor.
81    */
82    TimerBase::~TimerBase()
83    {
84        delete this->executor_;
85    }
86
87    /**
88        @brief Executes the executor.
89    */
90    void TimerBase::run() const
91    {
92        (*this->executor_)();
93    }
94
95    /**
96        @brief Updates the timer before the frames are rendered.
97    */
98    void TimerBase::tick(float dt)
99    {
100        if (this->bActive_)
101        {
102            // If active: Decrease the timer by the duration of the last frame
103            this->time_ -= dt;
104
105            if (this->time_ <= 0)
106            {
107                // It's time to call the function
108                if (this->bLoop_)
109                    // Q: Why '+=' and not '='? A: Think about it. It's more accurate like that. Seriously.
110                    this->time_ += this->interval_;
111                else
112                    this->stopTimer(); // Stop the timer if we don't want to loop
113
114                this->run();
115            }
116        }
117    }
118
119}
Note: See TracBrowser for help on using the repository browser.