summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatric Stout <truebrain@openttd.org>2021-02-19 11:01:49 +0100
committerPatric Stout <github@truebrain.nl>2021-02-20 17:08:44 +0100
commit661eb39ecc3a128c24dbbc4f53d1c075fe89bc93 (patch)
treec579f44c8186f43169138cafce3ed02d38baf2ef
parent38b4ae1c0ea05944cda66a499454a5b08a7a4462 (diff)
downloadopenttd-661eb39ecc3a128c24dbbc4f53d1c075fe89bc93.tar.xz
Codechange: move all input-handling of video-drivers into InputLoop
-rw-r--r--src/video/allegro_v.cpp60
-rw-r--r--src/video/allegro_v.h3
-rw-r--r--src/video/cocoa/cocoa_v.h1
-rw-r--r--src/video/cocoa/cocoa_v.mm43
-rw-r--r--src/video/sdl2_v.cpp68
-rw-r--r--src/video/sdl2_v.h1
-rw-r--r--src/video/sdl_v.cpp98
-rw-r--r--src/video/sdl_v.h4
-rw-r--r--src/video/video_driver.hpp5
-rw-r--r--src/video/win32_v.cpp67
-rw-r--r--src/video/win32_v.h2
11 files changed, 193 insertions, 159 deletions
diff --git a/src/video/allegro_v.cpp b/src/video/allegro_v.cpp
index a18e16e69..5ddc8b808 100644
--- a/src/video/allegro_v.cpp
+++ b/src/video/allegro_v.cpp
@@ -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();
diff --git a/src/video/allegro_v.h b/src/video/allegro_v.h
index 641e3d6c2..1c3a05af7 100644
--- a/src/video/allegro_v.h
+++ b/src/video/allegro_v.h
@@ -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. */
diff --git a/src/video/cocoa/cocoa_v.h b/src/video/cocoa/cocoa_v.h
index 5dd3d1d1f..f42d57dfe 100644
--- a/src/video/cocoa/cocoa_v.h
+++ b/src/video/cocoa/cocoa_v.h
@@ -74,6 +74,7 @@ public:
protected:
Dimension GetScreenSize() const override;
float GetDPIScale() override;
+ void InputLoop() override;
private:
bool PollEvent();
diff --git a/src/video/cocoa/cocoa_v.mm b/src/video/cocoa/cocoa_v.mm
index cf2512b04..5e8c82b2a 100644
--- a/src/video/cocoa/cocoa_v.mm
+++ b/src/video/cocoa/cocoa_v.mm
@@ -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();
diff --git a/src/video/sdl2_v.cpp b/src/video/sdl2_v.cpp
index c8f239448..b02beacef 100644
--- a/src/video/sdl2_v.cpp
+++ b/src/video/sdl2_v.cpp
@@ -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();
diff --git a/src/video/sdl2_v.h b/src/video/sdl2_v.h
index b1afb6d96..254402905 100644
--- a/src/video/sdl2_v.h
+++ b/src/video/sdl2_v.h
@@ -43,6 +43,7 @@ public:
protected:
Dimension GetScreenSize() const override;
+ void InputLoop() override;
private:
int PollEvent();
diff --git a/src/video/sdl_v.cpp b/src/video/sdl_v.cpp
index 7c75ebfea..44391f184 100644
--- a/src/video/sdl_v.cpp
+++ b/src/video/sdl_v.cpp
@@ -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;
diff --git a/src/video/sdl_v.h b/src/video/sdl_v.h
index ffebd041e..1d3da9b6a 100644
--- a/src/video/sdl_v.h
+++ b/src/video/sdl_v.h
@@ -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);
diff --git a/src/video/video_driver.hpp b/src/video/video_driver.hpp
index 732050c76..8ba1d2466 100644
--- a/src/video/video_driver.hpp
+++ b/src/video/video_driver.hpp
@@ -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);
diff --git a/src/video/win32_v.cpp b/src/video/win32_v.cpp
index 3f230e621..f55a9c372 100644
--- a/src/video/win32_v.cpp
+++ b/src/video/win32_v.cpp
@@ -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();
diff --git a/src/video/win32_v.h b/src/video/win32_v.h
index 878132dac..a6219dfd0 100644
--- a/src/video/win32_v.h
+++ b/src/video/win32_v.h
@@ -43,8 +43,8 @@ public:
protected:
Dimension GetScreenSize() const override;
-
float GetDPIScale() override;
+ void InputLoop() override;
private:
void CheckPaletteAnim();