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