Timer: Add upstream TimerWindow

This commit is contained in:
Jonathan G Rennison 2024-08-21 21:34:21 +01:00
parent a56efeb609
commit c7904e23f0
4 changed files with 106 additions and 1 deletions

View File

@ -4,5 +4,7 @@ add_files(
timer_game_tick.cpp
timer_game_tick.h
timer_manager.h
timer_window.cpp
timer_window.h
timer.h
)

View File

@ -0,0 +1,64 @@
/*
* This file is part of OpenTTD.
* 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.
* 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.
* 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/>.
*/
/**
* @file timer_window.cpp
* This file implements the timer logic for the Window system.
*/
#include "../stdafx.h"
#include "timer.h"
#include "timer_window.h"
#include "../safeguards.h"
template<>
void IntervalTimer<TimerWindow>::Elapsed(TimerWindow::TElapsed delta)
{
if (this->period == std::chrono::milliseconds::zero()) return;
this->storage.elapsed += delta;
uint count = 0;
while (this->storage.elapsed >= this->period) {
this->storage.elapsed -= this->period;
count++;
}
if (count > 0) {
this->callback(count);
}
}
template<>
void TimeoutTimer<TimerWindow>::Elapsed(TimerWindow::TElapsed delta)
{
if (this->fired) return;
if (this->period == std::chrono::milliseconds::zero()) return;
this->storage.elapsed += delta;
if (this->storage.elapsed >= this->period) {
this->callback();
this->fired = true;
}
}
template<>
void TimerManager<TimerWindow>::Elapsed(TimerWindow::TElapsed delta)
{
for (auto timer : TimerManager<TimerWindow>::GetTimerVector()) {
timer->Elapsed(delta);
}
}
#ifdef WITH_ASSERT
template<>
void TimerManager<TimerWindow>::Validate(TimerWindow::TPeriod)
{
}
#endif /* WITH_ASSERT */

35
src/timer/timer_window.h Normal file
View File

@ -0,0 +1,35 @@
/*
* This file is part of OpenTTD.
* 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.
* 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.
* 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/>.
*/
/** @file timer_window.h Definition of the Window system */
#ifndef TIMER_WINDOW_H
#define TIMER_WINDOW_H
#include <chrono>
/**
* Timer that represents the real time, usable for the Window system.
*
* This can be used to create intervals based on milliseconds, seconds, etc.
* Mostly used for animation, scrolling, etc.
*
* Please be mindful that the order in which timers are called is not guaranteed.
*
* @note The lowest possible interval is 1ms.
* @note These timers can only be used in the Window system.
*/
class TimerWindow {
public:
using TPeriod = std::chrono::milliseconds;
using TElapsed = std::chrono::milliseconds;
struct TStorage {
std::chrono::milliseconds elapsed;
};
};
#endif /* TIMER_WINDOW_H */

View File

@ -40,6 +40,8 @@
#include "guitimer_func.h"
#include "news_func.h"
#include "core/backup_type.hpp"
#include "timer/timer.h"
#include "timer/timer_window.h"
#include <bitset>
@ -3253,7 +3255,8 @@ void CallWindowRealtimeTickEvent(uint delta_ms)
void UpdateWindows()
{
static std::chrono::steady_clock::time_point last_time = std::chrono::steady_clock::now();
uint delta_ms = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - last_time).count();
auto delta_ms_duration = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - last_time);
uint delta_ms = delta_ms_duration.count();
if (delta_ms == 0) return;
@ -3264,6 +3267,7 @@ void UpdateWindows()
ProcessPendingPerformanceMeasurements();
TimerManager<TimerWindow>::Elapsed(delta_ms_duration);
CallWindowRealtimeTickEvent(delta_ms);
static GUITimer network_message_timer = GUITimer(1);