Changeset - r24833:e40e0f8b2230
[Not reviewed]
master
0 9 0
Patric Stout - 4 years ago 2021-02-17 14:04:46
truebrain@openttd.org
Add: draw the screen at a steady pace, also during fast-forward

During fast-forward, the game was drawing as fast as it could. This
means that the fast-forward was limited also by how fast we could
draw, something that people in general don't expect.

To give an extreme case, if you are fully zoomed out on a busy
map, fast-forward would be mostly limited because of the time it
takes to draw the screen.

By decoupling the draw-tick and game-tick, we can keep the pace
of the draw-tick the same while speeding up the game-tick. To use
the extreme case as example again, if you are fully zoomed out
now, the screen only redraws 33.33 times per second, fast-forwarding
or not. This means fast-forward is much more likely to go at the
same speed, no matter what you are looking at.
9 files changed with 114 insertions and 96 deletions:
0 comments (0 inline, 0 general)
src/openttd.cpp
Show inline comments
 
@@ -1465,7 +1465,6 @@ void GameLoop()
 
	if (_game_mode == GM_BOOTSTRAP) {
 
		/* Check for UDP stuff */
 
		if (_network_available) NetworkBackgroundLoop();
 
		InputLoop();
 
		return;
 
	}
 

	
 
@@ -1505,8 +1504,6 @@ void GameLoop()
 

	
 
	if (!_pause_mode && HasBit(_display_opt, DO_FULL_ANIMATION)) DoPaletteAnimations();
 

	
 
	InputLoop();
 

	
 
	SoundDriver::GetInstance()->MainLoop();
 
	MusicLoop();
 
}
src/video/allegro_v.cpp
Show inline comments
 
@@ -24,6 +24,7 @@
 
#include "../core/math_func.hpp"
 
#include "../framerate_type.h"
 
#include "../thread.h"
 
#include "../window_func.h"
 
#include "allegro_v.h"
 
#include <allegro.h>
 

	
 
@@ -449,7 +450,8 @@ void VideoDriver_Allegro::MainLoop()
 
{
 
	auto cur_ticks = std::chrono::steady_clock::now();
 
	auto last_realtime_tick = cur_ticks;
 
	auto next_tick = cur_ticks;
 
	auto next_game_tick = cur_ticks;
 
	auto next_draw_tick = cur_ticks;
 

	
 
	CheckPaletteAnim();
 

	
 
@@ -481,8 +483,14 @@ void VideoDriver_Allegro::MainLoop()
 
			last_realtime_tick += delta;
 
		}
 

	
 
		if (cur_ticks >= next_tick || (_fast_forward && !_pause_mode)) {
 
			next_tick = cur_ticks + std::chrono::milliseconds(MILLISECONDS_PER_TICK);
 
		if (cur_ticks >= next_game_tick || (_fast_forward && !_pause_mode)) {
 
			next_game_tick = cur_ticks + std::chrono::milliseconds(MILLISECONDS_PER_TICK);
 

	
 
			GameLoop();
 
		}
 

	
 
		if (cur_ticks >= next_draw_tick) {
 
			next_draw_tick = cur_ticks + std::chrono::milliseconds(MILLISECONDS_PER_TICK);
 

	
 
			bool old_ctrl_pressed = _ctrl_pressed;
 

	
 
@@ -498,16 +506,15 @@ void VideoDriver_Allegro::MainLoop()
 

	
 
			if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
 

	
 
			GameLoop();
 

	
 
			InputLoop();
 
			UpdateWindows();
 
			CheckPaletteAnim();
 

	
 
			DrawSurfaceToScreen();
 
		} else {
 
		}
 

	
 
		if (!_fast_forward || _pause_mode) {
 
			CSleep(1);
 
			NetworkDrawChatMessage();
 
			DrawMouseCursor();
 
			DrawSurfaceToScreen();
 
		}
 
	}
 
}
src/video/cocoa/cocoa_v.mm
Show inline comments
 
@@ -636,7 +636,8 @@ void VideoDriver_Cocoa::GameLoop()
 
{
 
	auto cur_ticks = std::chrono::steady_clock::now();
 
	auto last_realtime_tick = cur_ticks;
 
	auto next_tick = cur_ticks;
 
	auto next_game_tick = cur_ticks;
 
	auto next_draw_tick = cur_ticks;
 

	
 
	for (;;) {
 
		@autoreleasepool {
 
@@ -672,8 +673,14 @@ void VideoDriver_Cocoa::GameLoop()
 
				last_realtime_tick += delta;
 
			}
 

	
 
			if (cur_ticks >= next_tick || (_fast_forward && !_pause_mode)) {
 
				next_tick = cur_ticks + std::chrono::milliseconds(MILLISECONDS_PER_TICK);
 
			if (cur_ticks >= next_game_tick || (_fast_forward && !_pause_mode)) {
 
				next_game_tick = cur_ticks + std::chrono::milliseconds(MILLISECONDS_PER_TICK);
 

	
 
				::GameLoop();
 
			}
 

	
 
			if (cur_ticks >= next_draw_tick) {
 
				next_draw_tick = cur_ticks + std::chrono::milliseconds(MILLISECONDS_PER_TICK);
 

	
 
				bool old_ctrl_pressed = _ctrl_pressed;
 

	
 
@@ -682,16 +689,15 @@ void VideoDriver_Cocoa::GameLoop()
 

	
 
				if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
 

	
 
				::GameLoop();
 

	
 
				InputLoop();
 
				UpdateWindows();
 
				this->CheckPaletteAnim();
 

	
 
				this->Draw();
 
			} else {
 
			}
 

	
 
			if (!_fast_forward || _pause_mode) {
 
				CSleep(1);
 
				NetworkDrawChatMessage();
 
				DrawMouseCursor();
 
				this->Draw();
 
			}
 
		}
 
	}
src/video/dedicated_v.cpp
Show inline comments
 
@@ -21,6 +21,7 @@
 
#include "../core/random_func.hpp"
 
#include "../saveload/saveload.h"
 
#include "../thread.h"
 
#include "../window_func.h"
 
#include "dedicated_v.h"
 

	
 
#ifdef __OS2__
 
@@ -295,6 +296,7 @@ void VideoDriver_Dedicated::MainLoop()
 
			next_tick = cur_ticks + std::chrono::milliseconds(MILLISECONDS_PER_TICK);
 

	
 
			GameLoop();
 
			InputLoop();
 
			UpdateWindows();
 
		}
 

	
src/video/null_v.cpp
Show inline comments
 
@@ -10,6 +10,7 @@
 
#include "../stdafx.h"
 
#include "../gfx_func.h"
 
#include "../blitter/factory.hpp"
 
#include "../window_func.h"
 
#include "null_v.h"
 

	
 
#include "../safeguards.h"
 
@@ -48,6 +49,7 @@ void VideoDriver_Null::MainLoop()
 

	
 
	for (i = 0; i < this->ticks; i++) {
 
		GameLoop();
 
		InputLoop();
 
		UpdateWindows();
 
	}
 
}
src/video/sdl2_v.cpp
Show inline comments
 
@@ -776,8 +776,18 @@ void VideoDriver_SDL::LoopOnce()
 
		last_realtime_tick += delta;
 
	}
 

	
 
	if (cur_ticks >= next_tick || (_fast_forward && !_pause_mode)) {
 
		next_tick = cur_ticks + std::chrono::milliseconds(MILLISECONDS_PER_TICK);
 
	if (cur_ticks >= next_game_tick || (_fast_forward && !_pause_mode)) {
 
		next_game_tick = cur_ticks + std::chrono::milliseconds(MILLISECONDS_PER_TICK);
 

	
 
		/* The gameloop is the part that can run asynchronously. The rest
 
		 * except sleeping can't. */
 
		if (_draw_mutex != nullptr) draw_lock.unlock();
 
		GameLoop();
 
		if (_draw_mutex != nullptr) draw_lock.lock();
 
	}
 

	
 
	if (cur_ticks >= next_draw_tick) {
 
		next_draw_tick = cur_ticks + std::chrono::milliseconds(MILLISECONDS_PER_TICK);
 

	
 
		bool old_ctrl_pressed = _ctrl_pressed;
 

	
 
@@ -792,48 +802,33 @@ void VideoDriver_SDL::LoopOnce()
 
			(keys[SDL_SCANCODE_DOWN]  ? 8 : 0);
 
		if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
 

	
 
		/* The gameloop is the part that can run asynchronously. The rest
 
		 * except sleeping can't. */
 
		if (_draw_mutex != nullptr) draw_lock.unlock();
 

	
 
		GameLoop();
 

	
 
		if (_draw_mutex != nullptr) draw_lock.lock();
 

	
 
		InputLoop();
 
		UpdateWindows();
 
		this->CheckPaletteAnim();
 
	} else {
 
		/* Release the thread while sleeping */
 
		if (_draw_mutex != nullptr) {
 
			draw_lock.unlock();
 
			CSleep(1);
 
			draw_lock.lock();
 

	
 
		if (_draw_mutex != nullptr && !HasModalProgress()) {
 
			_draw_signal->notify_one();
 
		} else {
 
			Paint();
 
		}
 
	}
 

	
 
/* Emscripten is running an event-based mainloop; there is already some
 
 * downtime between each iteration, so no need to sleep. */
 
#ifndef __EMSCRIPTEN__
 
			CSleep(1);
 
#endif
 
		}
 

	
 
		NetworkDrawChatMessage();
 
		DrawMouseCursor();
 
	if (!_fast_forward || _pause_mode) {
 
		if (_draw_mutex != nullptr) draw_lock.unlock();
 
		CSleep(1);
 
		if (_draw_mutex != nullptr) draw_lock.lock();
 
	}
 

	
 
	/* End of the critical part. */
 
	if (_draw_mutex != nullptr && !HasModalProgress()) {
 
		_draw_signal->notify_one();
 
	} else {
 
		/* Oh, we didn't have threads, then just draw unthreaded */
 
		Paint();
 
	}
 
#endif
 
}
 

	
 
void VideoDriver_SDL::MainLoop()
 
{
 
	cur_ticks = std::chrono::steady_clock::now();
 
	last_realtime_tick = cur_ticks;
 
	next_tick = cur_ticks;
 
	next_game_tick = cur_ticks;
 

	
 
	this->CheckPaletteAnim();
 

	
src/video/sdl2_v.h
Show inline comments
 
@@ -64,7 +64,8 @@ private:
 

	
 
	std::chrono::steady_clock::time_point cur_ticks;
 
	std::chrono::steady_clock::time_point last_realtime_tick;
 
	std::chrono::steady_clock::time_point next_tick;
 
	std::chrono::steady_clock::time_point next_game_tick;
 
	std::chrono::steady_clock::time_point next_draw_tick;
 

	
 
	int startup_display;
 
	std::thread draw_thread;
src/video/sdl_v.cpp
Show inline comments
 
@@ -21,6 +21,7 @@
 
#include "../core/math_func.hpp"
 
#include "../fileio_func.h"
 
#include "../framerate_type.h"
 
#include "../window_func.h"
 
#include "sdl_v.h"
 
#include <SDL.h>
 
#include <mutex>
 
@@ -650,7 +651,8 @@ void VideoDriver_SDL::MainLoop()
 
{
 
	auto cur_ticks = std::chrono::steady_clock::now();
 
	auto last_realtime_tick = cur_ticks;
 
	auto next_tick = cur_ticks;
 
	auto next_game_tick = cur_ticks;
 
	auto next_draw_tick = cur_ticks;
 
	uint32 mod;
 
	int numkeys;
 
	Uint8 *keys;
 
@@ -727,8 +729,18 @@ void VideoDriver_SDL::MainLoop()
 
			last_realtime_tick += delta;
 
		}
 

	
 
		if (cur_ticks >= next_tick || (_fast_forward && !_pause_mode)) {
 
			next_tick = cur_ticks + std::chrono::milliseconds(MILLISECONDS_PER_TICK);
 
		if (cur_ticks >= next_game_tick || (_fast_forward && !_pause_mode)) {
 
			next_game_tick = cur_ticks + std::chrono::milliseconds(MILLISECONDS_PER_TICK);
 

	
 
			/* The gameloop is the part that can run asynchronously. The rest
 
			 * except sleeping can't. */
 
			if (_draw_mutex != nullptr) draw_lock.unlock();
 
			GameLoop();
 
			if (_draw_mutex != nullptr) draw_lock.lock();
 
		}
 

	
 
		if (cur_ticks >= next_draw_tick) {
 
			next_draw_tick = cur_ticks + std::chrono::milliseconds(MILLISECONDS_PER_TICK);
 

	
 
			bool old_ctrl_pressed = _ctrl_pressed;
 

	
 
@@ -750,33 +762,22 @@ void VideoDriver_SDL::MainLoop()
 
#endif
 
			if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
 

	
 
			/* The gameloop is the part that can run asynchronously. The rest
 
			 * except sleeping can't. */
 
			if (_draw_mutex != nullptr) draw_lock.unlock();
 

	
 
			GameLoop();
 

	
 
			if (_draw_mutex != nullptr) draw_lock.lock();
 

	
 
			InputLoop();
 
			UpdateWindows();
 
			_local_palette = _cur_palette;
 
		} else {
 
			/* Release the thread while sleeping */
 

	
 
			if (_draw_mutex != nullptr && !HasModalProgress()) {
 
				_draw_signal->notify_one();
 
			} else {
 
				CheckPaletteAnim();
 
				DrawSurfaceToScreen();
 
			}
 
		}
 

	
 
		if (!_fast_forward || _pause_mode) {
 
			if (_draw_mutex != nullptr) draw_lock.unlock();
 
			CSleep(1);
 
			if (_draw_mutex != nullptr) draw_lock.lock();
 

	
 
			NetworkDrawChatMessage();
 
			DrawMouseCursor();
 
		}
 

	
 
		/* End of the critical part. */
 
		if (_draw_mutex != nullptr && !HasModalProgress()) {
 
			_draw_signal->notify_one();
 
		} else {
 
			/* Oh, we didn't have threads, then just draw unthreaded */
 
			CheckPaletteAnim();
 
			DrawSurfaceToScreen();
 
		}
 
	}
 

	
src/video/win32_v.cpp
Show inline comments
 
@@ -1135,7 +1135,8 @@ void VideoDriver_Win32::MainLoop()
 
	MSG mesg;
 
	auto cur_ticks = std::chrono::steady_clock::now();
 
	auto last_realtime_tick = cur_ticks;
 
	auto next_tick = cur_ticks;
 
	auto next_game_tick = cur_ticks;
 
	auto next_draw_tick = cur_ticks;
 

	
 
	std::thread draw_thread;
 
	std::unique_lock<std::recursive_mutex> draw_lock;
 
@@ -1205,8 +1206,21 @@ void VideoDriver_Win32::MainLoop()
 
			last_realtime_tick += delta;
 
		}
 

	
 
		if (cur_ticks >= next_tick || (_fast_forward && !_pause_mode)) {
 
			next_tick = cur_ticks + std::chrono::milliseconds(MILLISECONDS_PER_TICK);
 
		if (cur_ticks >= next_game_tick || (_fast_forward && !_pause_mode)) {
 
			next_game_tick = cur_ticks + std::chrono::milliseconds(MILLISECONDS_PER_TICK);
 

	
 
			/* Flush GDI buffer to ensure we don't conflict with the drawing thread. */
 
			GdiFlush();
 

	
 
			/* The game loop is the part that can run asynchronously.
 
			 * The rest except sleeping can't. */
 
			if (_draw_threaded) draw_lock.unlock();
 
			GameLoop();
 
			if (_draw_threaded) draw_lock.lock();
 
		}
 

	
 
		if (cur_ticks >= next_draw_tick) {
 
			next_draw_tick = cur_ticks + std::chrono::milliseconds(MILLISECONDS_PER_TICK);
 

	
 
			bool old_ctrl_pressed = _ctrl_pressed;
 

	
 
@@ -1226,30 +1240,23 @@ void VideoDriver_Win32::MainLoop()
 

	
 
			if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
 

	
 
			if (_force_full_redraw) MarkWholeScreenDirty();
 

	
 
			/* Flush GDI buffer to ensure we don't conflict with the drawing thread. */
 
			GdiFlush();
 

	
 
			/* The game loop is the part that can run asynchronously.
 
			 * The rest except sleeping can't. */
 
			if (_draw_threaded) draw_lock.unlock();
 
			GameLoop();
 
			if (_draw_threaded) draw_lock.lock();
 

	
 
			if (_force_full_redraw) MarkWholeScreenDirty();
 

	
 
			InputLoop();
 
			UpdateWindows();
 
			CheckPaletteAnim();
 
		} else {
 
		}
 

	
 
		if (!_fast_forward || _pause_mode) {
 
			/* Flush GDI buffer to ensure we don't conflict with the drawing thread. */
 
			GdiFlush();
 

	
 
			/* Release the thread while sleeping */
 
			if (_draw_threaded) draw_lock.unlock();
 
			if (_draw_mutex != nullptr) draw_lock.unlock();
 
			CSleep(1);
 
			if (_draw_threaded) draw_lock.lock();
 

	
 
			NetworkDrawChatMessage();
 
			DrawMouseCursor();
 
			if (_draw_mutex != nullptr) draw_lock.lock();
 
		}
 
	}
 

	
0 comments (0 inline, 0 general)