[890] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
[1056] | 3 | * > www.orxonox.net < |
---|
[890] | 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 | |
---|
[1502] | 29 | #include <set> |
---|
| 30 | |
---|
[890] | 31 | #include "OrxonoxStableHeaders.h" |
---|
[1062] | 32 | #include "Timer.h" |
---|
| 33 | |
---|
[1052] | 34 | #include "core/Executor.h" |
---|
| 35 | #include "core/CoreIncludes.h" |
---|
| 36 | #include "core/ConsoleCommand.h" |
---|
| 37 | #include "core/CommandExecutor.h" |
---|
[890] | 38 | |
---|
[852] | 39 | namespace orxonox |
---|
| 40 | { |
---|
[1502] | 41 | SetConsoleCommandShortcutExtern(delay); |
---|
| 42 | SetConsoleCommandShortcutExtern(killdelays); |
---|
[1052] | 43 | |
---|
[1502] | 44 | static std::set<StaticTimer*> delaytimerset; |
---|
| 45 | |
---|
[852] | 46 | /** |
---|
[1052] | 47 | @brief Calls a console command after 'delay' seconds. |
---|
| 48 | @param delay The delay in seconds |
---|
| 49 | @param command The console command |
---|
| 50 | */ |
---|
| 51 | void delay(float delay, const std::string& command) |
---|
| 52 | { |
---|
| 53 | StaticTimer *delaytimer = new StaticTimer(); |
---|
[1502] | 54 | delaytimerset.insert(delaytimer); |
---|
| 55 | |
---|
[1052] | 56 | ExecutorStatic* delayexecutor = createExecutor(createFunctor(&executeDelayedCommand)); |
---|
| 57 | delayexecutor->setDefaultValues(delaytimer, command); |
---|
| 58 | delaytimer->setTimer(delay, false, delayexecutor); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | /** |
---|
| 62 | @brief Executes the command. |
---|
| 63 | @param timer The timer to destroy after the command-execution |
---|
| 64 | @param command The command to execute |
---|
| 65 | */ |
---|
| 66 | void executeDelayedCommand(StaticTimer* timer, const std::string& command) |
---|
| 67 | { |
---|
| 68 | CommandExecutor::execute(command); |
---|
| 69 | delete timer; |
---|
[1502] | 70 | delaytimerset.erase(timer); |
---|
[1052] | 71 | } |
---|
| 72 | |
---|
| 73 | /** |
---|
[1502] | 74 | @brief Kills all delayed commands. |
---|
| 75 | */ |
---|
| 76 | void killdelays() |
---|
| 77 | { |
---|
| 78 | for (std::set<StaticTimer*>::iterator it = delaytimerset.begin(); it != delaytimerset.end(); ++it) |
---|
| 79 | delete (*it); |
---|
| 80 | |
---|
| 81 | delaytimerset.clear(); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | /** |
---|
[852] | 85 | @brief Constructor: Sets the default-values. |
---|
| 86 | */ |
---|
| 87 | TimerBase::TimerBase() |
---|
| 88 | { |
---|
[1052] | 89 | this->executor_ = 0; |
---|
[852] | 90 | this->interval_ = 0; |
---|
| 91 | this->bLoop_ = false; |
---|
| 92 | this->bActive_ = false; |
---|
| 93 | |
---|
| 94 | this->time_ = 0; |
---|
[1293] | 95 | |
---|
| 96 | RegisterRootObject(TimerBase); |
---|
[852] | 97 | } |
---|
[1021] | 98 | |
---|
| 99 | /** |
---|
[1052] | 100 | @brief Deletes the executor. |
---|
| 101 | */ |
---|
| 102 | TimerBase::~TimerBase() |
---|
| 103 | { |
---|
[1293] | 104 | if (this->executor_) |
---|
| 105 | delete this->executor_; |
---|
[1052] | 106 | } |
---|
| 107 | |
---|
| 108 | /** |
---|
| 109 | @brief Executes the executor. |
---|
| 110 | */ |
---|
| 111 | void TimerBase::run() const |
---|
| 112 | { |
---|
| 113 | (*this->executor_)(); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | /** |
---|
[1021] | 117 | @brief Updates the timer before the frames are rendered. |
---|
| 118 | */ |
---|
| 119 | void TimerBase::tick(float dt) |
---|
| 120 | { |
---|
| 121 | if (this->bActive_) |
---|
| 122 | { |
---|
| 123 | // If active: Decrease the timer by the duration of the last frame |
---|
| 124 | this->time_ -= dt; |
---|
| 125 | |
---|
| 126 | if (this->time_ <= 0) |
---|
| 127 | { |
---|
| 128 | // It's time to call the function |
---|
| 129 | if (this->bLoop_) |
---|
[1063] | 130 | { |
---|
| 131 | this->time_ += this->interval_; // Q: Why '+=' and not '='? A: Think about it. It's more accurate like that. Seriously. |
---|
| 132 | while (this->time_ <= 0) |
---|
| 133 | { |
---|
| 134 | // The interval was shorter than one tick, so execute the function more than once |
---|
| 135 | this->run(); |
---|
| 136 | this->time_ += this->interval_; |
---|
| 137 | } |
---|
| 138 | } |
---|
[1021] | 139 | else |
---|
| 140 | this->stopTimer(); // Stop the timer if we don't want to loop |
---|
| 141 | |
---|
| 142 | this->run(); |
---|
| 143 | } |
---|
| 144 | } |
---|
| 145 | } |
---|
[852] | 146 | } |
---|