OpenTTD Source: src/timer/timer_game_tick.cpp Source File

OpenTTD Source 15.0-RC2

timer_game_tick.cpp
Go to the documentation of this file.
1/*
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
13#include "../stdafx.h"
14#include "timer.h"
15#include "timer_game_tick.h"
16
17#include "../safeguards.h"
18
19 TimerGameTick::TickCounter TimerGameTick::counter = 0;
20
21template <>
22void IntervalTimer<TimerGameTick>::Elapsed(TimerGameTick::TElapsed delta)
23{
24 if (this->period.value == 0) return;
25
26 this->storage.elapsed += delta;
27
28 uint count = 0;
29 while (this->storage.elapsed >= this->period.value) {
30 this->storage.elapsed -= this->period.value;
31 count++;
32 }
33
34 if (count > 0) {
35 this->callback(count);
36 }
37}
38
39template <>
40void TimeoutTimer<TimerGameTick>::Elapsed(TimerGameTick::TElapsed delta)
41{
42 if (this->fired) return;
43 if (this->period.value == 0) return;
44
45 this->storage.elapsed += delta;
46
47 if (this->storage.elapsed >= this->period.value) {
48 this->callback();
49 this->fired = true;
50 }
51}
52
53template <>
54bool TimerManager<TimerGameTick>::Elapsed(TimerGameTick::TElapsed delta)
55{
56 TimerGameTick::counter++;
57
58 for (auto timer : TimerManager<TimerGameTick>::GetTimers()) {
59 timer->Elapsed(delta);
60 }
61
62 return true;
63}
64
65#ifdef WITH_ASSERT
66template <>
67void TimerManager<TimerGameTick>::Validate(TimerGameTick::TPeriod period)
68{
69 if (period.priority == TimerGameTick::Priority::NONE) return;
70
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);
77 }
78}
79#endif /* WITH_ASSERT */
IntervalTimer::Elapsed
void Elapsed(TElapsed count) override
Called by the timer manager to notify the timer that the given amount of time has elapsed.
TimeoutTimer::Elapsed
void Elapsed(TElapsed count) override
Called by the timer manager to notify the timer that the given amount of time has elapsed.
TimerGameTick
Timer that represents the game-ticks.
Definition timer_game_tick.h:22
TimerGameTick::TickCounter
uint64_t TickCounter
The type that the tick counter is stored in.
Definition timer_game_tick.h:25
TimerGameTick::counter
static TickCounter counter
Monotonic counter, in ticks, since start of game.
Definition timer_game_tick.h:60
TimerGameTick::NONE
@ NONE
These timers can be executed in any order; the order is not relevant.
Definition timer_game_tick.h:28
TimerManager
The TimerManager manages a single Timer-type.
Definition timer_manager.h:25
TimerManager::Elapsed
static bool Elapsed(TElapsed value)
Called when time for this timer elapsed.
TimerGameTick::TPeriod
Definition timer_game_tick.h:35
timer.h
Definition of Interval and OneShot timers.
timer_game_tick.h
Definition of the tick-based game-timer.