Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1129 was 1063, checked in by landauf, 17 years ago

fixed an interesting bug in ObjectList/MetaObjectList/Iterator: it's now possible to delete multiple objects in one function-call while iterating through the objects. (and it would have crashed with std::list as well, so no stupid comments please :D)

added a feature in Timer.cc, allowing timers to tick faster than orxonox. this is from core2-branch but got lost while merging.

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