2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
9 * @file timer_game_tick.cpp
10 * This file implements the timer logic for the tick-based game-timer.
13 #include "../stdafx.h"
15 #include "timer_game_tick.h"
17 #include "../safeguards.h"
19 TimerGameTick::TickCounter
TimerGameTick::counter
= 0;
22 void IntervalTimer
<TimerGameTick
>::Elapsed(TimerGameTick::TElapsed delta
)
24 if (this->period
.value
== 0) return;
26 this->storage
.elapsed
+= delta
;
29 while (this->storage
.elapsed
>= this->period
.value
) {
30 this->storage
.elapsed
-= this->period
.value
;
35 this->callback(count
);
40 void TimeoutTimer
<TimerGameTick
>::Elapsed(TimerGameTick::TElapsed delta
)
42 if (this->fired
) return;
43 if (this->period
.value
== 0) return;
45 this->storage
.elapsed
+= delta
;
47 if (this->storage
.elapsed
>= this->period
.value
) {
54 bool TimerManager
<TimerGameTick
>::Elapsed(TimerGameTick::TElapsed delta
)
56 TimerGameTick::counter
++;
58 for (auto timer
: TimerManager
<TimerGameTick
>::GetTimers()) {
59 timer
->Elapsed(delta
);
67 void TimerManager
<TimerGameTick
>::Validate(TimerGameTick::TPeriod period
)
69 if (period
.priority
== TimerGameTick::Priority::NONE
) return;
71 /* Validate we didn't make a developer error and scheduled more than one
72 * entry on the same priority. There can only be one timer on
73 * a specific priority, to ensure we are deterministic, and to avoid
74 * container sort order invariant issues with timer period saveload. */
75 for (const auto &timer
: TimerManager
<TimerGameTick
>::GetTimers()) {
76 assert(timer
->period
.priority
!= period
.priority
);
79 #endif /* WITH_ASSERT */