| 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 |  | 
|---|
| 37 | namespace 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 | this->executor_ = 0; | 
|---|
| 71 | this->interval_ = 0; | 
|---|
| 72 | this->bLoop_ = false; | 
|---|
| 73 | this->bActive_ = false; | 
|---|
| 74 |  | 
|---|
| 75 | this->time_ = 0; | 
|---|
| 76 |  | 
|---|
| 77 | RegisterRootObject(TimerBase); | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 80 | /** | 
|---|
| 81 | @brief Deletes the executor. | 
|---|
| 82 | */ | 
|---|
| 83 | TimerBase::~TimerBase() | 
|---|
| 84 | { | 
|---|
| 85 | if (this->executor_) | 
|---|
| 86 | delete this->executor_; | 
|---|
| 87 | } | 
|---|
| 88 |  | 
|---|
| 89 | /** | 
|---|
| 90 | @brief Executes the executor. | 
|---|
| 91 | */ | 
|---|
| 92 | void TimerBase::run() const | 
|---|
| 93 | { | 
|---|
| 94 | (*this->executor_)(); | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | /** | 
|---|
| 98 | @brief Updates the timer before the frames are rendered. | 
|---|
| 99 | */ | 
|---|
| 100 | void TimerBase::tick(float dt) | 
|---|
| 101 | { | 
|---|
| 102 | if (this->bActive_) | 
|---|
| 103 | { | 
|---|
| 104 | // If active: Decrease the timer by the duration of the last frame | 
|---|
| 105 | this->time_ -= dt; | 
|---|
| 106 |  | 
|---|
| 107 | if (this->time_ <= 0) | 
|---|
| 108 | { | 
|---|
| 109 | // It's time to call the function | 
|---|
| 110 | if (this->bLoop_) | 
|---|
| 111 | { | 
|---|
| 112 | this->time_ += this->interval_; // Q: Why '+=' and not '='? A: Think about it. It's more accurate like that. Seriously. | 
|---|
| 113 | while (this->time_ <= 0) | 
|---|
| 114 | { | 
|---|
| 115 | // The interval was shorter than one tick, so execute the function more than once | 
|---|
| 116 | this->run(); | 
|---|
| 117 | this->time_ += this->interval_; | 
|---|
| 118 | } | 
|---|
| 119 | } | 
|---|
| 120 | else | 
|---|
| 121 | this->stopTimer(); // Stop the timer if we don't want to loop | 
|---|
| 122 |  | 
|---|
| 123 | this->run(); | 
|---|
| 124 | } | 
|---|
| 125 | } | 
|---|
| 126 | } | 
|---|
| 127 | } | 
|---|