[1505] | 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 | |
---|
[3196] | 29 | #include "Timer.h" |
---|
| 30 | |
---|
[1505] | 31 | #include <set> |
---|
| 32 | |
---|
[5929] | 33 | #include "util/Clock.h" |
---|
[1505] | 34 | #include "core/CoreIncludes.h" |
---|
[7284] | 35 | #include "core/command/ConsoleCommand.h" |
---|
| 36 | #include "core/command/CommandExecutor.h" |
---|
| 37 | #include "core/command/Functor.h" |
---|
[1505] | 38 | |
---|
| 39 | namespace orxonox |
---|
| 40 | { |
---|
[7284] | 41 | SetConsoleCommand("delay", &delay).argumentCompleter(1, autocompletion::command()); |
---|
| 42 | SetConsoleCommand("killdelays", &killdelays); |
---|
[1505] | 43 | |
---|
[5929] | 44 | static std::set<Timer*> delaytimerset; |
---|
[1505] | 45 | |
---|
| 46 | /** |
---|
| 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 | { |
---|
[5929] | 53 | Timer* delaytimer = new Timer(); |
---|
[1505] | 54 | delaytimerset.insert(delaytimer); |
---|
| 55 | |
---|
[7284] | 56 | const ExecutorStaticPtr& delayexecutor = createExecutor(createFunctor(&executeDelayedCommand)); |
---|
[1505] | 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 | */ |
---|
[5929] | 66 | void executeDelayedCommand(Timer* timer, const std::string& command) |
---|
[1505] | 67 | { |
---|
| 68 | CommandExecutor::execute(command); |
---|
[5929] | 69 | timer->destroy(); |
---|
[1505] | 70 | delaytimerset.erase(timer); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | /** |
---|
| 74 | @brief Kills all delayed commands. |
---|
| 75 | */ |
---|
| 76 | void killdelays() |
---|
| 77 | { |
---|
[5929] | 78 | for (std::set<Timer*>::iterator it = delaytimerset.begin(); it != delaytimerset.end(); ++it) |
---|
| 79 | (*it)->destroy(); |
---|
[1505] | 80 | |
---|
| 81 | delaytimerset.clear(); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | /** |
---|
| 85 | @brief Constructor: Sets the default-values. |
---|
| 86 | */ |
---|
[5929] | 87 | Timer::Timer() |
---|
[1505] | 88 | { |
---|
[5929] | 89 | this->init(); |
---|
| 90 | RegisterObject(Timer); |
---|
| 91 | } |
---|
[1505] | 92 | |
---|
[5929] | 93 | /** |
---|
| 94 | @brief Constructor: Initializes the Timer with given values. |
---|
| 95 | @param interval The timer-interval in seconds |
---|
| 96 | @param bLoop If true, the function gets called every 'interval' seconds |
---|
[7297] | 97 | @param executor A executor of the function to call |
---|
| 98 | @param bKillAfterCall If true, the timer will be deleted after the function was executed |
---|
[5929] | 99 | */ |
---|
[7284] | 100 | Timer::Timer(float interval, bool bLoop, const ExecutorPtr& executor, bool bKillAfterCall) |
---|
[5929] | 101 | { |
---|
| 102 | this->init(); |
---|
| 103 | RegisterObject(Timer); |
---|
[1505] | 104 | |
---|
[5929] | 105 | this->setTimer(interval, bLoop, executor, bKillAfterCall); |
---|
[1505] | 106 | } |
---|
| 107 | |
---|
| 108 | /** |
---|
[5929] | 109 | @brief Initializes the Timer |
---|
| 110 | */ |
---|
| 111 | void Timer::init() |
---|
| 112 | { |
---|
| 113 | this->executor_ = 0; |
---|
| 114 | this->interval_ = 0; |
---|
| 115 | this->bLoop_ = false; |
---|
| 116 | this->bActive_ = false; |
---|
| 117 | this->bKillAfterCall_ = false; |
---|
[1505] | 118 | |
---|
[5929] | 119 | this->time_ = 0; |
---|
| 120 | } |
---|
| 121 | |
---|
[1505] | 122 | /** |
---|
| 123 | @brief Executes the executor. |
---|
| 124 | */ |
---|
[5929] | 125 | void Timer::run() |
---|
[1505] | 126 | { |
---|
[2087] | 127 | bool temp = this->bKillAfterCall_; // to avoid errors with bKillAfterCall_=false and an exutors which destroy the timer |
---|
| 128 | |
---|
[1505] | 129 | (*this->executor_)(); |
---|
[2087] | 130 | |
---|
| 131 | if (temp) |
---|
[5929] | 132 | this->destroy(); |
---|
[1505] | 133 | } |
---|
| 134 | |
---|
| 135 | /** |
---|
| 136 | @brief Updates the timer before the frames are rendered. |
---|
| 137 | */ |
---|
[5929] | 138 | void Timer::tick(const Clock& time) |
---|
[1505] | 139 | { |
---|
| 140 | if (this->bActive_) |
---|
| 141 | { |
---|
| 142 | // If active: Decrease the timer by the duration of the last frame |
---|
[3301] | 143 | this->time_ -= static_cast<long long>(time.getDeltaTimeMicroseconds() * this->getTimeFactor()); |
---|
[1505] | 144 | |
---|
| 145 | if (this->time_ <= 0) |
---|
| 146 | { |
---|
| 147 | // It's time to call the function |
---|
[2087] | 148 | if (this->bLoop_ && !this->bKillAfterCall_) |
---|
[1505] | 149 | { |
---|
| 150 | this->time_ += this->interval_; // Q: Why '+=' and not '='? A: Think about it. It's more accurate like that. Seriously. |
---|
| 151 | while (this->time_ <= 0) |
---|
| 152 | { |
---|
| 153 | // The interval was shorter than one tick, so execute the function more than once |
---|
| 154 | this->run(); |
---|
| 155 | this->time_ += this->interval_; |
---|
| 156 | } |
---|
| 157 | } |
---|
| 158 | else |
---|
| 159 | this->stopTimer(); // Stop the timer if we don't want to loop |
---|
| 160 | |
---|
| 161 | this->run(); |
---|
| 162 | } |
---|
| 163 | } |
---|
| 164 | } |
---|
| 165 | } |
---|