Changeset - r24849:01243a72f255
[Not reviewed]
master
0 11 0
Patric Stout - 3 years ago 2021-02-19 10:01:49
truebrain@openttd.org
Codechange: move all input-handling of video-drivers into InputLoop
11 files changed with 193 insertions and 159 deletions:
0 comments (0 inline, 0 general)
src/video/allegro_v.cpp
Show inline comments
 
@@ -447,6 +447,36 @@ void VideoDriver_Allegro::Stop()
 
	if (--_allegro_instance_count == 0) allegro_exit();
 
}
 

	
 
void VideoDriver_Allegro::InputLoop()
 
{
 
	bool old_ctrl_pressed = _ctrl_pressed;
 

	
 
	_ctrl_pressed  = !!(key_shifts & KB_CTRL_FLAG);
 
	_shift_pressed = !!(key_shifts & KB_SHIFT_FLAG);
 

	
 
#if defined(_DEBUG)
 
	if (_shift_pressed)
 
#else
 
	/* Speedup when pressing tab, except when using ALT+TAB
 
	 * to switch to another application. */
 
	if (key[KEY_TAB] && (key_shifts & KB_ALT_FLAG) == 0)
 
#endif
 
	{
 
		if (!_networking && _game_mode != GM_MENU) _fast_forward |= 2;
 
	} else if (_fast_forward & 2) {
 
		_fast_forward = 0;
 
	}
 

	
 
	/* Determine which directional keys are down. */
 
	_dirkeys =
 
		(key[KEY_LEFT]  ? 1 : 0) |
 
		(key[KEY_UP]    ? 2 : 0) |
 
		(key[KEY_RIGHT] ? 4 : 0) |
 
		(key[KEY_DOWN]  ? 8 : 0);
 

	
 
	if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
 
}
 

	
 
void VideoDriver_Allegro::MainLoop()
 
{
 
	auto cur_ticks = std::chrono::steady_clock::now();
 
@@ -462,19 +492,6 @@ void VideoDriver_Allegro::MainLoop()
 
		PollEvent();
 
		if (_exit_game) return;
 

	
 
#if defined(_DEBUG)
 
		if (_shift_pressed)
 
#else
 
		/* Speedup when pressing tab, except when using ALT+TAB
 
		 * to switch to another application */
 
		if (key[KEY_TAB] && (key_shifts & KB_ALT_FLAG) == 0)
 
#endif
 
		{
 
			if (!_networking && _game_mode != GM_MENU) _fast_forward |= 2;
 
		} else if (_fast_forward & 2) {
 
			_fast_forward = 0;
 
		}
 

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

	
 
		/* If more than a millisecond has passed, increase the _realtime_tick. */
 
@@ -502,21 +519,8 @@ void VideoDriver_Allegro::MainLoop()
 
			/* Avoid next_draw_tick getting behind more and more if it cannot keep up. */
 
			if (next_draw_tick < cur_ticks - ALLOWED_DRIFT * this->GetDrawInterval()) next_draw_tick = cur_ticks;
 

	
 
			bool old_ctrl_pressed = _ctrl_pressed;
 

	
 
			_ctrl_pressed  = !!(key_shifts & KB_CTRL_FLAG);
 
			_shift_pressed = !!(key_shifts & KB_SHIFT_FLAG);
 

	
 
			/* determine which directional keys are down */
 
			_dirkeys =
 
				(key[KEY_LEFT]  ? 1 : 0) |
 
				(key[KEY_UP]    ? 2 : 0) |
 
				(key[KEY_RIGHT] ? 4 : 0) |
 
				(key[KEY_DOWN]  ? 8 : 0);
 

	
 
			if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
 

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

	
src/video/allegro_v.h
Show inline comments
 
@@ -32,6 +32,9 @@ public:
 
	bool ClaimMousePointer() override;
 

	
 
	const char *GetName() const override { return "allegro"; }
 

	
 
protected:
 
	void InputLoop() override;
 
};
 

	
 
/** Factory for the allegro video driver. */
src/video/cocoa/cocoa_v.h
Show inline comments
 
@@ -74,6 +74,7 @@ public:
 
protected:
 
	Dimension GetScreenSize() const override;
 
	float GetDPIScale() override;
 
	void InputLoop() override;
 

	
 
private:
 
	bool PollEvent();
src/video/cocoa/cocoa_v.mm
Show inline comments
 
@@ -632,6 +632,28 @@ bool VideoDriver_Cocoa::PollEvent()
 
	return true;
 
}
 

	
 
void VideoDriver_Cocoa::InputLoop()
 
{
 
	NSUInteger cur_mods = [ NSEvent modifierFlags ];
 

	
 
	bool old_ctrl_pressed = _ctrl_pressed;
 

	
 
	_ctrl_pressed = (cur_mods & ( _settings_client.gui.right_mouse_btn_emulation != RMBE_CONTROL ? NSControlKeyMask : NSCommandKeyMask)) != 0;
 
	_shift_pressed = (cur_mods & NSShiftKeyMask) != 0;
 

	
 
#if defined(_DEBUG)
 
	if (_shift_pressed) {
 
#else
 
	if (_tab_is_down) {
 
#endif
 
		if (!_networking && _game_mode != GM_MENU) _fast_forward |= 2;
 
	} else if (_fast_forward & 2) {
 
		_fast_forward = 0;
 
	}
 

	
 
	if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
 
}
 

	
 
/** Main game loop. */
 
void VideoDriver_Cocoa::GameLoop()
 
{
 
@@ -653,17 +675,6 @@ void VideoDriver_Cocoa::GameLoop()
 
				break;
 
			}
 

	
 
			NSUInteger cur_mods = [ NSEvent modifierFlags ];
 

	
 
#if defined(_DEBUG)
 
			if (cur_mods & NSShiftKeyMask) {
 
#else
 
			if (_tab_is_down) {
 
#endif
 
				if (!_networking && _game_mode != GM_MENU) _fast_forward |= 2;
 
			} else if (_fast_forward & 2) {
 
				_fast_forward = 0;
 
			}
 

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

	
 
@@ -692,14 +703,8 @@ void VideoDriver_Cocoa::GameLoop()
 
				/* Avoid next_draw_tick getting behind more and more if it cannot keep up. */
 
				if (next_draw_tick < cur_ticks - ALLOWED_DRIFT * this->GetDrawInterval()) next_draw_tick = cur_ticks;
 

	
 
				bool old_ctrl_pressed = _ctrl_pressed;
 

	
 
				_ctrl_pressed = (cur_mods & ( _settings_client.gui.right_mouse_btn_emulation != RMBE_CONTROL ? NSControlKeyMask : NSCommandKeyMask)) != 0;
 
				_shift_pressed = (cur_mods & NSShiftKeyMask) != 0;
 

	
 
				if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
 

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

	
src/video/sdl2_v.cpp
Show inline comments
 
@@ -729,11 +729,41 @@ void VideoDriver_SDL::Stop()
 
	}
 
}
 

	
 
void VideoDriver_SDL::InputLoop()
 
{
 
	uint32 mod = SDL_GetModState();
 
	const Uint8 *keys = SDL_GetKeyboardState(NULL);
 

	
 
	bool old_ctrl_pressed = _ctrl_pressed;
 

	
 
	_ctrl_pressed  = !!(mod & KMOD_CTRL);
 
	_shift_pressed = !!(mod & KMOD_SHIFT);
 

	
 
#if defined(_DEBUG)
 
	if (_shift_pressed)
 
#else
 
	/* Speedup when pressing tab, except when using ALT+TAB
 
	 * to switch to another application. */
 
	if (keys[SDL_SCANCODE_TAB] && (mod & KMOD_ALT) == 0)
 
#endif /* defined(_DEBUG) */
 
	{
 
		if (!_networking && _game_mode != GM_MENU) _fast_forward |= 2;
 
	} else if (_fast_forward & 2) {
 
		_fast_forward = 0;
 
	}
 

	
 
	/* Determine which directional keys are down. */
 
	_dirkeys =
 
		(keys[SDL_SCANCODE_LEFT]  ? 1 : 0) |
 
		(keys[SDL_SCANCODE_UP]    ? 2 : 0) |
 
		(keys[SDL_SCANCODE_RIGHT] ? 4 : 0) |
 
		(keys[SDL_SCANCODE_DOWN]  ? 8 : 0);
 

	
 
	if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
 
}
 

	
 
void VideoDriver_SDL::LoopOnce()
 
{
 
	uint32 mod;
 
	int numkeys;
 
	const Uint8 *keys;
 
	InteractiveRandom(); // randomness
 

	
 
	while (PollEvent() == -1) {}
 
@@ -751,22 +781,6 @@ void VideoDriver_SDL::LoopOnce()
 
		return;
 
	}
 

	
 
	mod = SDL_GetModState();
 
	keys = SDL_GetKeyboardState(&numkeys);
 

	
 
#if defined(_DEBUG)
 
	if (_shift_pressed)
 
#else
 
	/* Speedup when pressing tab, except when using ALT+TAB
 
	 * to switch to another application */
 
	if (keys[SDL_SCANCODE_TAB] && (mod & KMOD_ALT) == 0)
 
#endif /* defined(_DEBUG) */
 
	{
 
		if (!_networking && _game_mode != GM_MENU) _fast_forward |= 2;
 
	} else if (_fast_forward & 2) {
 
		_fast_forward = 0;
 
	}
 

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

	
 
	/* If more than a millisecond has passed, increase the _realtime_tick. */
 
@@ -798,20 +812,8 @@ void VideoDriver_SDL::LoopOnce()
 
		/* Avoid next_draw_tick getting behind more and more if it cannot keep up. */
 
		if (next_draw_tick < cur_ticks - ALLOWED_DRIFT * this->GetDrawInterval()) next_draw_tick = cur_ticks;
 

	
 
		bool old_ctrl_pressed = _ctrl_pressed;
 

	
 
		_ctrl_pressed  = !!(mod & KMOD_CTRL);
 
		_shift_pressed = !!(mod & KMOD_SHIFT);
 

	
 
		/* determine which directional keys are down */
 
		_dirkeys =
 
			(keys[SDL_SCANCODE_LEFT]  ? 1 : 0) |
 
			(keys[SDL_SCANCODE_UP]    ? 2 : 0) |
 
			(keys[SDL_SCANCODE_RIGHT] ? 4 : 0) |
 
			(keys[SDL_SCANCODE_DOWN]  ? 8 : 0);
 
		if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
 

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

	
src/video/sdl2_v.h
Show inline comments
 
@@ -43,6 +43,7 @@ public:
 

	
 
protected:
 
	Dimension GetScreenSize() const override;
 
	void InputLoop() override;
 

	
 
private:
 
	int PollEvent();
src/video/sdl_v.cpp
Show inline comments
 
@@ -647,15 +647,61 @@ void VideoDriver_SDL::Stop()
 
	}
 
}
 

	
 
void VideoDriver_SDL::InputLoop()
 
{
 
	uint32 mod = SDL_GetModState();
 
#if SDL_VERSION_ATLEAST(1, 3, 0)
 
	Uint8 *keys = SDL_GetKeyboardState(&numkeys);
 
#else
 
	int numkeys;
 
	Uint8 *keys = SDL_GetKeyState(&numkeys);
 
#endif
 

	
 
	bool old_ctrl_pressed = _ctrl_pressed;
 

	
 
	_ctrl_pressed  = !!(mod & KMOD_CTRL);
 
	_shift_pressed = !!(mod & KMOD_SHIFT);
 

	
 
#if defined(_DEBUG)
 
	if (_shift_pressed)
 
#else
 
	/* Speedup when pressing tab, except when using ALT+TAB
 
	 * to switch to another application. */
 
#if SDL_VERSION_ATLEAST(1, 3, 0)
 
	if (keys[SDL_SCANCODE_TAB] && (mod & KMOD_ALT) == 0)
 
#else
 
	if (keys[SDLK_TAB] && (mod & KMOD_ALT) == 0)
 
#endif /* SDL_VERSION_ATLEAST(1, 3, 0) */
 
#endif /* defined(_DEBUG) */
 
	{
 
		if (!_networking && _game_mode != GM_MENU) _fast_forward |= 2;
 
	} else if (_fast_forward & 2) {
 
		_fast_forward = 0;
 
	}
 

	
 
	/* Determine which directional keys are down. */
 
	_dirkeys =
 
#if SDL_VERSION_ATLEAST(1, 3, 0)
 
		(keys[SDL_SCANCODE_LEFT]  ? 1 : 0) |
 
		(keys[SDL_SCANCODE_UP]    ? 2 : 0) |
 
		(keys[SDL_SCANCODE_RIGHT] ? 4 : 0) |
 
		(keys[SDL_SCANCODE_DOWN]  ? 8 : 0);
 
#else
 
		(keys[SDLK_LEFT]  ? 1 : 0) |
 
		(keys[SDLK_UP]    ? 2 : 0) |
 
		(keys[SDLK_RIGHT] ? 4 : 0) |
 
		(keys[SDLK_DOWN]  ? 8 : 0);
 
#endif
 

	
 
	if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
 
}
 

	
 
void VideoDriver_SDL::MainLoop()
 
{
 
	auto cur_ticks = std::chrono::steady_clock::now();
 
	auto last_realtime_tick = cur_ticks;
 
	auto next_game_tick = cur_ticks;
 
	auto next_draw_tick = cur_ticks;
 
	uint32 mod;
 
	int numkeys;
 
	Uint8 *keys;
 

	
 
	CheckPaletteAnim();
 

	
 
@@ -697,29 +743,6 @@ void VideoDriver_SDL::MainLoop()
 
		while (PollEvent() == -1) {}
 
		if (_exit_game) break;
 

	
 
		mod = SDL_GetModState();
 
#if SDL_VERSION_ATLEAST(1, 3, 0)
 
		keys = SDL_GetKeyboardState(&numkeys);
 
#else
 
		keys = SDL_GetKeyState(&numkeys);
 
#endif
 
#if defined(_DEBUG)
 
		if (_shift_pressed)
 
#else
 
		/* Speedup when pressing tab, except when using ALT+TAB
 
		 * to switch to another application */
 
#if SDL_VERSION_ATLEAST(1, 3, 0)
 
		if (keys[SDL_SCANCODE_TAB] && (mod & KMOD_ALT) == 0)
 
#else
 
		if (keys[SDLK_TAB] && (mod & KMOD_ALT) == 0)
 
#endif /* SDL_VERSION_ATLEAST(1, 3, 0) */
 
#endif /* defined(_DEBUG) */
 
		{
 
			if (!_networking && _game_mode != GM_MENU) _fast_forward |= 2;
 
		} else if (_fast_forward & 2) {
 
			_fast_forward = 0;
 
		}
 

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

	
 
		/* If more than a millisecond has passed, increase the _realtime_tick. */
 
@@ -751,27 +774,8 @@ void VideoDriver_SDL::MainLoop()
 
			/* Avoid next_draw_tick getting behind more and more if it cannot keep up. */
 
			if (next_draw_tick < cur_ticks - ALLOWED_DRIFT * this->GetDrawInterval()) next_draw_tick = cur_ticks;
 

	
 
			bool old_ctrl_pressed = _ctrl_pressed;
 

	
 
			_ctrl_pressed  = !!(mod & KMOD_CTRL);
 
			_shift_pressed = !!(mod & KMOD_SHIFT);
 

	
 
			/* determine which directional keys are down */
 
			_dirkeys =
 
#if SDL_VERSION_ATLEAST(1, 3, 0)
 
				(keys[SDL_SCANCODE_LEFT]  ? 1 : 0) |
 
				(keys[SDL_SCANCODE_UP]    ? 2 : 0) |
 
				(keys[SDL_SCANCODE_RIGHT] ? 4 : 0) |
 
				(keys[SDL_SCANCODE_DOWN]  ? 8 : 0);
 
#else
 
				(keys[SDLK_LEFT]  ? 1 : 0) |
 
				(keys[SDLK_UP]    ? 2 : 0) |
 
				(keys[SDLK_RIGHT] ? 4 : 0) |
 
				(keys[SDLK_DOWN]  ? 8 : 0);
 
#endif
 
			if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
 

	
 
			InputLoop();
 
			this->InputLoop();
 
			::InputLoop();
 
			UpdateWindows();
 
			_local_palette = _cur_palette;
 

	
src/video/sdl_v.h
Show inline comments
 
@@ -36,6 +36,10 @@ public:
 
	bool ClaimMousePointer() override;
 

	
 
	const char *GetName() const override { return "sdl"; }
 

	
 
protected:
 
	void InputLoop() override;
 

	
 
private:
 
	int PollEvent();
 
	bool CreateMainSurface(uint w, uint h);
src/video/video_driver.hpp
Show inline comments
 
@@ -156,6 +156,11 @@ protected:
 
		}
 
	}
 

	
 
	/**
 
	 * Handle input logic, is CTRL pressed, should we fast-forward, etc.
 
	 */
 
	virtual void InputLoop() {}
 

	
 
	std::chrono::steady_clock::duration GetGameInterval()
 
	{
 
		return std::chrono::milliseconds(MILLISECONDS_PER_TICK);
src/video/win32_v.cpp
Show inline comments
 
@@ -1118,6 +1118,40 @@ void VideoDriver_Win32::CheckPaletteAnim
 
	this->MakeDirty(0, 0, _screen.width, _screen.height);
 
}
 

	
 
void VideoDriver_Win32::InputLoop()
 
{
 
	bool old_ctrl_pressed = _ctrl_pressed;
 

	
 
	_ctrl_pressed = _wnd.has_focus && GetAsyncKeyState(VK_CONTROL) < 0;
 
	_shift_pressed = _wnd.has_focus && GetAsyncKeyState(VK_SHIFT) < 0;
 

	
 
#if defined(_DEBUG)
 
	if (_shift_pressed)
 
#else
 
	/* Speedup when pressing tab, except when using ALT+TAB
 
	 * to switch to another application. */
 
	if (_wnd.has_focus && GetAsyncKeyState(VK_TAB) < 0 && GetAsyncKeyState(VK_MENU) >= 0)
 
#endif
 
	{
 
		if (!_networking && _game_mode != GM_MENU) _fast_forward |= 2;
 
	} else if (_fast_forward & 2) {
 
		_fast_forward = 0;
 
	}
 

	
 
	/* Determine which directional keys are down. */
 
	if (_wnd.has_focus) {
 
		_dirkeys =
 
			(GetAsyncKeyState(VK_LEFT) < 0 ? 1 : 0) +
 
			(GetAsyncKeyState(VK_UP) < 0 ? 2 : 0) +
 
			(GetAsyncKeyState(VK_RIGHT) < 0 ? 4 : 0) +
 
			(GetAsyncKeyState(VK_DOWN) < 0 ? 8 : 0);
 
	} else {
 
		_dirkeys = 0;
 
	}
 

	
 
	if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
 
}
 

	
 
void VideoDriver_Win32::MainLoop()
 
{
 
	MSG mesg;
 
@@ -1174,18 +1208,6 @@ void VideoDriver_Win32::MainLoop()
 
		}
 
		if (_exit_game) break;
 

	
 
#if defined(_DEBUG)
 
		if (_wnd.has_focus && GetAsyncKeyState(VK_SHIFT) < 0)
 
#else
 
		/* Speed up using TAB, but disable for ALT+TAB of course */
 
		if (_wnd.has_focus && GetAsyncKeyState(VK_TAB) < 0 && GetAsyncKeyState(VK_MENU) >= 0)
 
#endif
 
		{
 
			if (!_networking && _game_mode != GM_MENU) _fast_forward |= 2;
 
		} else if (_fast_forward & 2) {
 
			_fast_forward = 0;
 
		}
 

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

	
 
		/* If more than a millisecond has passed, increase the _realtime_tick. */
 
@@ -1220,30 +1242,13 @@ void VideoDriver_Win32::MainLoop()
 
			/* Avoid next_draw_tick getting behind more and more if it cannot keep up. */
 
			if (next_draw_tick < cur_ticks - ALLOWED_DRIFT * this->GetDrawInterval()) next_draw_tick = cur_ticks;
 

	
 
			bool old_ctrl_pressed = _ctrl_pressed;
 

	
 
			_ctrl_pressed = _wnd.has_focus && GetAsyncKeyState(VK_CONTROL)<0;
 
			_shift_pressed = _wnd.has_focus && GetAsyncKeyState(VK_SHIFT)<0;
 

	
 
			/* determine which directional keys are down */
 
			if (_wnd.has_focus) {
 
				_dirkeys =
 
					(GetAsyncKeyState(VK_LEFT) < 0 ? 1 : 0) +
 
					(GetAsyncKeyState(VK_UP) < 0 ? 2 : 0) +
 
					(GetAsyncKeyState(VK_RIGHT) < 0 ? 4 : 0) +
 
					(GetAsyncKeyState(VK_DOWN) < 0 ? 8 : 0);
 
			} else {
 
				_dirkeys = 0;
 
			}
 

	
 
			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();
 

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

	
src/video/win32_v.h
Show inline comments
 
@@ -43,8 +43,8 @@ public:
 

	
 
protected:
 
	Dimension GetScreenSize() const override;
 

	
 
	float GetDPIScale() override;
 
	void InputLoop() override;
 

	
 
private:
 
	void CheckPaletteAnim();
0 comments (0 inline, 0 general)