Changeset - r24832:f2376ca1bbb7
[Not reviewed]
master
0 7 0
Patric Stout - 4 years ago 2021-02-17 13:49:45
truebrain@openttd.org
Codechange: track _realtime_tick more accurate

_realtime_tick was reset every time the diff was calculated. This
means if it would trigger, say, every N.9 milliseconds, it would
after two iterations already drift a millisecond. This adds up
pretty quick.
7 files changed with 55 insertions and 18 deletions:
0 comments (0 inline, 0 general)
src/video/allegro_v.cpp
Show inline comments
 
@@ -448,7 +448,7 @@ void VideoDriver_Allegro::Stop()
 
void VideoDriver_Allegro::MainLoop()
 
{
 
	auto cur_ticks = std::chrono::steady_clock::now();
 
	auto last_cur_ticks = cur_ticks;
 
	auto last_realtime_tick = cur_ticks;
 
	auto next_tick = cur_ticks;
 

	
 
	CheckPaletteAnim();
 
@@ -473,9 +473,15 @@ void VideoDriver_Allegro::MainLoop()
 
		}
 

	
 
		cur_ticks = std::chrono::steady_clock::now();
 

	
 
		/* If more than a millisecond has passed, increase the _realtime_tick. */
 
		if (cur_ticks - last_realtime_tick > std::chrono::milliseconds(1)) {
 
			auto delta = std::chrono::duration_cast<std::chrono::milliseconds>(cur_ticks - last_realtime_tick);
 
			_realtime_tick += delta.count();
 
			last_realtime_tick += delta;
 
		}
 

	
 
		if (cur_ticks >= next_tick || (_fast_forward && !_pause_mode)) {
 
			_realtime_tick += std::chrono::duration_cast<std::chrono::milliseconds>(cur_ticks - last_cur_ticks).count();
 
			last_cur_ticks = cur_ticks;
 
			next_tick = cur_ticks + std::chrono::milliseconds(MILLISECONDS_PER_TICK);
 

	
 
			bool old_ctrl_pressed = _ctrl_pressed;
src/video/cocoa/cocoa_v.mm
Show inline comments
 
@@ -635,7 +635,7 @@ bool VideoDriver_Cocoa::PollEvent()
 
void VideoDriver_Cocoa::GameLoop()
 
{
 
	auto cur_ticks = std::chrono::steady_clock::now();
 
	auto last_cur_ticks = cur_ticks;
 
	auto last_realtime_tick = cur_ticks;
 
	auto next_tick = cur_ticks;
 

	
 
	for (;;) {
 
@@ -664,9 +664,15 @@ void VideoDriver_Cocoa::GameLoop()
 
			}
 

	
 
			cur_ticks = std::chrono::steady_clock::now();
 

	
 
			/* If more than a millisecond has passed, increase the _realtime_tick. */
 
			if (cur_ticks - last_realtime_tick > std::chrono::milliseconds(1)) {
 
				auto delta = std::chrono::duration_cast<std::chrono::milliseconds>(cur_ticks - last_realtime_tick);
 
				_realtime_tick += delta.count();
 
				last_realtime_tick += delta;
 
			}
 

	
 
			if (cur_ticks >= next_tick || (_fast_forward && !_pause_mode)) {
 
				_realtime_tick += std::chrono::duration_cast<std::chrono::milliseconds>(cur_ticks - last_cur_ticks).count();
 
				last_cur_ticks = cur_ticks;
 
				next_tick = cur_ticks + std::chrono::milliseconds(MILLISECONDS_PER_TICK);
 

	
 
				bool old_ctrl_pressed = _ctrl_pressed;
src/video/dedicated_v.cpp
Show inline comments
 
@@ -236,7 +236,7 @@ static void DedicatedHandleKeyInput()
 
void VideoDriver_Dedicated::MainLoop()
 
{
 
	auto cur_ticks = std::chrono::steady_clock::now();
 
	auto last_cur_ticks = cur_ticks;
 
	auto last_realtime_tick = cur_ticks;
 
	auto next_tick = cur_ticks;
 

	
 
	/* Signal handlers */
 
@@ -283,7 +283,14 @@ void VideoDriver_Dedicated::MainLoop()
 
		if (!_dedicated_forks) DedicatedHandleKeyInput();
 

	
 
		cur_ticks = std::chrono::steady_clock::now();
 
		_realtime_tick += std::chrono::duration_cast<std::chrono::milliseconds>(cur_ticks - last_cur_ticks).count();
 

	
 
		/* If more than a millisecond has passed, increase the _realtime_tick. */
 
		if (cur_ticks - last_realtime_tick > std::chrono::milliseconds(1)) {
 
			auto delta = std::chrono::duration_cast<std::chrono::milliseconds>(cur_ticks - last_realtime_tick);
 
			_realtime_tick += delta.count();
 
			last_realtime_tick += delta;
 
		}
 

	
 
		if (cur_ticks >= next_tick || _ddc_fastforward) {
 
			next_tick = cur_ticks + std::chrono::milliseconds(MILLISECONDS_PER_TICK);
 

	
src/video/sdl2_v.cpp
Show inline comments
 
@@ -768,9 +768,15 @@ void VideoDriver_SDL::LoopOnce()
 
	}
 

	
 
	cur_ticks = std::chrono::steady_clock::now();
 

	
 
	/* If more than a millisecond has passed, increase the _realtime_tick. */
 
	if (cur_ticks - last_realtime_tick > std::chrono::milliseconds(1)) {
 
		auto delta = std::chrono::duration_cast<std::chrono::milliseconds>(cur_ticks - last_realtime_tick);
 
		_realtime_tick += delta.count();
 
		last_realtime_tick += delta;
 
	}
 

	
 
	if (cur_ticks >= next_tick || (_fast_forward && !_pause_mode)) {
 
		_realtime_tick += std::chrono::duration_cast<std::chrono::milliseconds>(cur_ticks - last_cur_ticks).count();
 
		last_cur_ticks = cur_ticks;
 
		next_tick = cur_ticks + std::chrono::milliseconds(MILLISECONDS_PER_TICK);
 

	
 
		bool old_ctrl_pressed = _ctrl_pressed;
 
@@ -826,7 +832,7 @@ void VideoDriver_SDL::LoopOnce()
 
void VideoDriver_SDL::MainLoop()
 
{
 
	cur_ticks = std::chrono::steady_clock::now();
 
	last_cur_ticks = cur_ticks;
 
	last_realtime_tick = cur_ticks;
 
	next_tick = cur_ticks;
 

	
 
	this->CheckPaletteAnim();
src/video/sdl2_v.h
Show inline comments
 
@@ -63,7 +63,7 @@ private:
 
	bool edit_box_focused;
 

	
 
	std::chrono::steady_clock::time_point cur_ticks;
 
	std::chrono::steady_clock::time_point last_cur_ticks;
 
	std::chrono::steady_clock::time_point last_realtime_tick;
 
	std::chrono::steady_clock::time_point next_tick;
 

	
 
	int startup_display;
src/video/sdl_v.cpp
Show inline comments
 
@@ -649,7 +649,7 @@ void VideoDriver_SDL::Stop()
 
void VideoDriver_SDL::MainLoop()
 
{
 
	auto cur_ticks = std::chrono::steady_clock::now();
 
	auto last_cur_ticks = cur_ticks;
 
	auto last_realtime_tick = cur_ticks;
 
	auto next_tick = cur_ticks;
 
	uint32 mod;
 
	int numkeys;
 
@@ -719,9 +719,15 @@ void VideoDriver_SDL::MainLoop()
 
		}
 

	
 
		cur_ticks = std::chrono::steady_clock::now();
 

	
 
		/* If more than a millisecond has passed, increase the _realtime_tick. */
 
		if (cur_ticks - last_realtime_tick > std::chrono::milliseconds(1)) {
 
			auto delta = std::chrono::duration_cast<std::chrono::milliseconds>(cur_ticks - last_realtime_tick);
 
			_realtime_tick += delta.count();
 
			last_realtime_tick += delta;
 
		}
 

	
 
		if (cur_ticks >= next_tick || (_fast_forward && !_pause_mode)) {
 
			_realtime_tick += std::chrono::duration_cast<std::chrono::milliseconds>(cur_ticks - last_cur_ticks).count();
 
			last_cur_ticks = cur_ticks;
 
			next_tick = cur_ticks + std::chrono::milliseconds(MILLISECONDS_PER_TICK);
 

	
 
			bool old_ctrl_pressed = _ctrl_pressed;
src/video/win32_v.cpp
Show inline comments
 
@@ -1134,7 +1134,7 @@ void VideoDriver_Win32::MainLoop()
 
{
 
	MSG mesg;
 
	auto cur_ticks = std::chrono::steady_clock::now();
 
	auto last_cur_ticks = cur_ticks;
 
	auto last_realtime_tick = cur_ticks;
 
	auto next_tick = cur_ticks;
 

	
 
	std::thread draw_thread;
 
@@ -1197,9 +1197,15 @@ void VideoDriver_Win32::MainLoop()
 
		}
 

	
 
		cur_ticks = std::chrono::steady_clock::now();
 

	
 
		/* If more than a millisecond has passed, increase the _realtime_tick. */
 
		if (cur_ticks - last_realtime_tick > std::chrono::milliseconds(1)) {
 
			auto delta = std::chrono::duration_cast<std::chrono::milliseconds>(cur_ticks - last_realtime_tick);
 
			_realtime_tick += delta.count();
 
			last_realtime_tick += delta;
 
		}
 

	
 
		if (cur_ticks >= next_tick || (_fast_forward && !_pause_mode)) {
 
			_realtime_tick += std::chrono::duration_cast<std::chrono::milliseconds>(cur_ticks - last_cur_ticks).count();
 
			last_cur_ticks = cur_ticks;
 
			next_tick = cur_ticks + std::chrono::milliseconds(MILLISECONDS_PER_TICK);
 

	
 
			bool old_ctrl_pressed = _ctrl_pressed;
0 comments (0 inline, 0 general)