Changeset - r24830:d8e510c24281
[Not reviewed]
master
0 2 0
Patric Stout - 3 years ago 2021-02-17 13:36:11
truebrain@openttd.org
Codechange: use std::chrono to track time in modal windows

Adding to _realtime_ticks in a random place is a bit of a hack,
and by using modern C++, we can avoid this hack.
2 files changed with 5 insertions and 6 deletions:
0 comments (0 inline, 0 general)
src/genworld_gui.cpp
Show inline comments
 
@@ -1185,13 +1185,13 @@ static WindowDesc _generate_progress_des
 

	
 
struct GenWorldStatus {
 
	uint percent;
 
	StringID cls;
 
	uint current;
 
	uint total;
 
	int timer;
 
	std::chrono::steady_clock::time_point timer;
 
};
 

	
 
static GenWorldStatus _gws;
 

	
 
static const StringID _generation_class_table[]  = {
 
	STR_GENERATION_WORLD_GENERATION,
 
@@ -1291,13 +1291,13 @@ struct GenerateProgressWindow : public W
 
void PrepareGenerateWorldProgress()
 
{
 
	_gws.cls     = STR_GENERATION_WORLD_GENERATION;
 
	_gws.current = 0;
 
	_gws.total   = 0;
 
	_gws.percent = 0;
 
	_gws.timer   = 0; // Forces to paint the progress window immediately
 
	_gws.timer   = std::chrono::steady_clock::now() - std::chrono::milliseconds(MODAL_PROGRESS_REDRAW_TIMEOUT * 2); // Ensure we draw on first update
 
}
 

	
 
/**
 
 * Show the window where a user can follow the process of the map generation.
 
 */
 
void ShowGenerateWorldProgress()
 
@@ -1326,13 +1326,13 @@ static void _SetGeneratingWorldProgress(
 
		_gws.current = progress;
 
		_gws.total   = total;
 
		_gws.percent = percent_table[cls];
 
	}
 

	
 
	/* Don't update the screen too often. So update it once in every once in a while... */
 
	if (!_network_dedicated && _gws.timer != 0 && _realtime_tick - _gws.timer < MODAL_PROGRESS_REDRAW_TIMEOUT) return;
 
	if (!_network_dedicated && std::chrono::steady_clock::now() - _gws.timer < std::chrono::milliseconds(MODAL_PROGRESS_REDRAW_TIMEOUT)) return;
 

	
 
	/* Percentage is about the number of completed tasks, so 'current - 1' */
 
	_gws.percent = percent_table[cls] + (percent_table[cls + 1] - percent_table[cls]) * (_gws.current == 0 ? 0 : _gws.current - 1) / _gws.total;
 

	
 
	if (_network_dedicated) {
 
		static uint last_percent = 0;
 
@@ -1362,13 +1362,13 @@ static void _SetGeneratingWorldProgress(
 
	 * before we reacquire the mapgen rights */
 
	_modal_progress_work_mutex.unlock();
 
	_modal_progress_paint_mutex.lock();
 
	_modal_progress_work_mutex.lock();
 
	_modal_progress_paint_mutex.unlock();
 

	
 
	_gws.timer = _realtime_tick;
 
	_gws.timer = std::chrono::steady_clock::now();
 
}
 

	
 
/**
 
 * Set the total of a stage of the world generation.
 
 * @param cls the current class we are in.
 
 * @param total Set the total expected items for this class.
src/gfx.cpp
Show inline comments
 
@@ -1467,15 +1467,14 @@ void DrawDirtyBlocks()
 
	if (HasModalProgress()) {
 
		/* We are generating the world, so release our rights to the map and
 
		 * painting while we are waiting a bit. */
 
		_modal_progress_paint_mutex.unlock();
 
		_modal_progress_work_mutex.unlock();
 

	
 
		/* Wait a while and update _realtime_tick so we are given the rights */
 
		/* Wait a while and hope the modal gives us a bit of time to draw the GUI. */
 
		if (!IsFirstModalProgressLoop()) CSleep(MODAL_PROGRESS_REDRAW_TIMEOUT);
 
		_realtime_tick += MODAL_PROGRESS_REDRAW_TIMEOUT;
 

	
 
		/* Modal progress thread may need blitter access while we are waiting for it. */
 
		VideoDriver::GetInstance()->ReleaseBlitterLock();
 
		_modal_progress_paint_mutex.lock();
 
		VideoDriver::GetInstance()->AcquireBlitterLock();
 
		_modal_progress_work_mutex.lock();
0 comments (0 inline, 0 general)