From a094eadb72d3d059b765f0d5d46c01c3a4211c87 Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 22 Mar 2022 23:54:20 +0100 Subject: Moved WndProc implementation to own file along low-level keyboard funcs --- Stars45/CMakeLists.txt | 1 + Stars45/Game.cpp | 255 +----------------------------------------------- Stars45/Game.h | 11 +-- Stars45/WndProc.cpp | 259 +++++++++++++++++++++++++++++++++++++++++++++++++ Stars45/WndProc.h | 21 ++++ 5 files changed, 283 insertions(+), 264 deletions(-) create mode 100644 Stars45/WndProc.cpp create mode 100644 Stars45/WndProc.h (limited to 'Stars45') diff --git a/Stars45/CMakeLists.txt b/Stars45/CMakeLists.txt index 43836f3..4ff9b4a 100644 --- a/Stars45/CMakeLists.txt +++ b/Stars45/CMakeLists.txt @@ -279,6 +279,7 @@ add_executable( WebBrowser.cpp WepView.cpp Window.cpp + WndProc.cpp ) target_include_directories( Stars45 diff --git a/Stars45/Game.cpp b/Stars45/Game.cpp index 7d88bf8..b70911b 100644 --- a/Stars45/Game.cpp +++ b/Stars45/Game.cpp @@ -16,7 +16,6 @@ #include "EventDispatch.h" #include "Color.h" #include "DataLoader.h" -#include "Keyboard.h" #include "Panic.h" #include "Pcx.h" #include "Bitmap.h" @@ -26,6 +25,7 @@ #include "VideoSettings.h" #include "ContentBundle.h" #include "Clock.h" +#include "WndProc.h" // +--------------------------------------------------------------------+ @@ -614,259 +614,6 @@ Game::ShowStats() } // +====================================================================+ -// WndProc -// +====================================================================+ - -#ifndef WM_MOUSEWHEEL -#define WM_MOUSEWHEEL 0x20A -#endif - -LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM uParam, LPARAM lParam) -{ - switch (message) { - case WM_SYSKEYDOWN: - if (uParam == VK_TAB || uParam == VK_F4) - return DefWindowProc(hwnd, message, uParam, lParam); - - return 0; - - case WM_MENUCHAR: - return MNC_CLOSE << 16; - - case WM_ACTIVATEAPP: - // Keep track of whether or not the app is in the foreground - if (game) - game->Activate(uParam?true:false); - break; - - case WM_PAINT: - if (!game || !game->OnPaint()) - return DefWindowProc(hwnd, message, uParam, lParam); - break; - - case WM_SETCURSOR: - if (game && game->ShowMouse()) { - return DefWindowProc(hwnd, message, uParam, lParam); - } - else { - // hide the windows mouse cursor - SetCursor(NULL); - return 1; - } - break; - - case WM_ENTERSIZEMOVE: - // Halt frame movement while the app is sizing or moving - if (game) - game->Pause(true); - break; - - case WM_SIZE: - // Pick up possible changes to window style due to maximize, etc. - if (game && game->hwnd != NULL ) { - game->window_style = GetWindowLong(game->hwnd, GWL_STYLE ); - - if (uParam == SIZE_MINIMIZED) { - game->Pause(true); // Pause while we're minimized - game->is_minimized = true; - game->is_maximized = false; - } - - else if (uParam == SIZE_MAXIMIZED) { - if (game->is_minimized) - game->Pause(false); // Unpause since we're no longer minimized - - game->is_minimized = false; - game->is_maximized = true; - game->ResizeVideo(); - } - - else if (uParam == SIZE_RESTORED) { - if (game->is_maximized) { - game->is_maximized = false; - game->ResizeVideo(); - } - - else if (game->is_minimized) { - game->Pause(false); // Unpause since we're no longer minimized - game->is_minimized = false; - game->ResizeVideo(); - } - else { - // If we're neither maximized nor minimized, the window size - // is changing by the user dragging the window edges. In this - // case, we don't reset the device yet -- we wait until the - // user stops dragging, and a WM_EXITSIZEMOVE message comes. - } - } - } - break; - - case WM_EXITSIZEMOVE: - if (game) { - game->Pause(false); - game->ResizeVideo(); - } - break; - - - case WM_ENTERMENULOOP: - if (game) - game->Pause(true); - break; - - case WM_EXITMENULOOP: - if (game) - game->Pause(false); - break; - - /* -case WM_HELP: - if (game) - return game->OnHelp(); - break; -*/ - - case WM_KEYDOWN: - BufferKey(uParam); - return 0; - - case WM_DESTROY: - PostQuitMessage(0); - break; - - case WM_MOUSEMOVE: - Mouse::x = LOWORD(lParam); - Mouse::y = HIWORD(lParam); - break; - - case WM_LBUTTONDOWN: - Mouse::l = 1; - break; - - case WM_LBUTTONDBLCLK: - Mouse::l = 2; - break; - - case WM_LBUTTONUP: - Mouse::l = 0; - break; - - case WM_MBUTTONDOWN: - Mouse::m = 1; - break; - - case WM_MBUTTONDBLCLK: - Mouse::m = 2; - break; - - case WM_MBUTTONUP: - Mouse::m = 0; - break; - - case WM_RBUTTONDOWN: - Mouse::r = 1; - break; - - case WM_RBUTTONDBLCLK: - Mouse::r = 2; - break; - - case WM_RBUTTONUP: - Mouse::r = 0; - break; - - case WM_MOUSEWHEEL: - { - int w = (int) (uParam >> 16); - if (w > 32000) w -= 65536; - Mouse::w += w; - } - break; - - case WM_CLOSE: - if (game) // && game->Server()) - game->Exit(); - break; - - default: - return DefWindowProc(hwnd, message, uParam, lParam); - } - - return 0; -} - -// +====================================================================+ - -const int MAX_KEY_BUF = 512; -static int vkbuf[MAX_KEY_BUF]; -static int vkshiftbuf[MAX_KEY_BUF]; -static int vkins = 0; -static int vkext = 0; - -void -FlushKeys() -{ - Keyboard::FlushKeys(); - vkins = vkext = 0; -} - -void -BufferKey(int vkey) -{ - if (vkey < 1) return; - - int shift = 0; - - if (GetAsyncKeyState(VK_SHIFT)) - shift |= 1; - - if (GetAsyncKeyState(VK_CONTROL)) - shift |= 2; - - if (GetAsyncKeyState(VK_MENU)) - shift |= 4; - - vkbuf[vkins] = vkey; - vkshiftbuf[vkins++] = shift; - - if (vkins >= MAX_KEY_BUF) - vkins = 0; - - if (vkins == vkext) { - vkext++; - if (vkext >= MAX_KEY_BUF) - vkext = 0; - } -} - -int -GetKey() -{ - if (vkins == vkext) return 0; - - int result = vkbuf[vkext++]; - if (vkext >= MAX_KEY_BUF) - vkext = 0; - - return result; -} - -int -GetKeyPlus(int& key, int& shift) -{ - if (vkins == vkext) return 0; - - key = vkbuf[vkext]; - shift = vkshiftbuf[vkext++]; - - if (vkext >= MAX_KEY_BUF) - vkext = 0; - - return key; -} - -// +====================================================================+ Clock* Game::GetClock() diff --git a/Stars45/Game.h b/Stars45/Game.h index f0bf968..3783906 100644 --- a/Stars45/Game.h +++ b/Stars45/Game.h @@ -15,16 +15,7 @@ #include "Screen.h" #include "Video.h" #include "VideoSettings.h" - -// +--------------------------------------------------------------------+ - -LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); - -void FlushKeys(); -void BufferKey(int vkey); -int GetKey(); -int GetKeyPlus(int& key, int& shift); -void ProcessKeyMessage(); +#include "WndProc.h" // +--------------------------------------------------------------------+ diff --git a/Stars45/WndProc.cpp b/Stars45/WndProc.cpp new file mode 100644 index 0000000..6d80ef3 --- /dev/null +++ b/Stars45/WndProc.cpp @@ -0,0 +1,259 @@ +/* Starshatter: The Open Source Project + Copyright (c) 2021-2022, Starshatter: The Open Source Project Contributors + Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors + Copyright (c) 1997-2006, Destroyer Studios LLC. +*/ + +#include "WndProc.h" + +#include "Game.h" +#include "Keyboard.h" +#include "Mouse.h" +#include "Types.h" + + +#ifndef WM_MOUSEWHEEL +#define WM_MOUSEWHEEL 0x20A +#endif + + +LRESULT CALLBACK +WndProc(HWND hwnd, UINT message, WPARAM uParam, LPARAM lParam) +{ + auto game = Game::GetInstance(); + switch (message) { + case WM_SYSKEYDOWN: + if (uParam == VK_TAB || uParam == VK_F4) + return DefWindowProc(hwnd, message, uParam, lParam); + return 0; + + case WM_MENUCHAR: + return MNC_CLOSE << 16; + + case WM_ACTIVATEAPP: + // Keep track of whether or not the app is in the foreground + if (game) + game->Activate(uParam?true:false); + break; + + case WM_PAINT: + if (!game || !game->OnPaint()) + return DefWindowProc(hwnd, message, uParam, lParam); + break; + + case WM_SETCURSOR: + if (game && game->ShowMouse()) { + return DefWindowProc(hwnd, message, uParam, lParam); + } + else { + // hide the windows mouse cursor + SetCursor(NULL); + return 1; + } + break; + + case WM_ENTERSIZEMOVE: + // Halt frame movement while the app is sizing or moving + if (game) + game->Pause(true); + break; + + case WM_SIZE: + // Pick up possible changes to window style due to maximize, etc. + if (game && game->hwnd != NULL ) { + game->window_style = GetWindowLong(game->hwnd, GWL_STYLE ); + + if (uParam == SIZE_MINIMIZED) { + game->Pause(true); // Pause while we're minimized + game->is_minimized = true; + game->is_maximized = false; + } + + else if (uParam == SIZE_MAXIMIZED) { + if (game->is_minimized) + game->Pause(false); // Unpause since we're no longer minimized + + game->is_minimized = false; + game->is_maximized = true; + game->ResizeVideo(); + } + + else if (uParam == SIZE_RESTORED) { + if (game->is_maximized) { + game->is_maximized = false; + game->ResizeVideo(); + } + + else if (game->is_minimized) { + game->Pause(false); // Unpause since we're no longer minimized + game->is_minimized = false; + game->ResizeVideo(); + } + else { + // If we're neither maximized nor minimized, the window size + // is changing by the user dragging the window edges. In this + // case, we don't reset the device yet -- we wait until the + // user stops dragging, and a WM_EXITSIZEMOVE message comes. + } + } + } + break; + + case WM_EXITSIZEMOVE: + if (game) { + game->Pause(false); + game->ResizeVideo(); + } + break; + + case WM_ENTERMENULOOP: + if (game) + game->Pause(true); + break; + + case WM_EXITMENULOOP: + if (game) + game->Pause(false); + break; + + case WM_KEYDOWN: + BufferKey(uParam); + return 0; + + case WM_DESTROY: + PostQuitMessage(0); + break; + + case WM_MOUSEMOVE: + Mouse::x = LOWORD(lParam); + Mouse::y = HIWORD(lParam); + break; + + case WM_LBUTTONDOWN: + Mouse::l = 1; + break; + + case WM_LBUTTONDBLCLK: + Mouse::l = 2; + break; + + case WM_LBUTTONUP: + Mouse::l = 0; + break; + + case WM_MBUTTONDOWN: + Mouse::m = 1; + break; + + case WM_MBUTTONDBLCLK: + Mouse::m = 2; + break; + + case WM_MBUTTONUP: + Mouse::m = 0; + break; + + case WM_RBUTTONDOWN: + Mouse::r = 1; + break; + + case WM_RBUTTONDBLCLK: + Mouse::r = 2; + break; + + case WM_RBUTTONUP: + Mouse::r = 0; + break; + + case WM_MOUSEWHEEL: + { + int w = (int) (uParam >> 16); + if (w > 32000) w -= 65536; + Mouse::w += w; + } + break; + + case WM_CLOSE: + if (game) // && game->Server()) + game->Exit(); + break; + + default: + return DefWindowProc(hwnd, message, uParam, lParam); + } + + return 0; +} + + +const int MAX_KEY_BUF = 512; +static int vkbuf[MAX_KEY_BUF]; +static int vkshiftbuf[MAX_KEY_BUF]; +static int vkins = 0; +static int vkext = 0; + + +void +FlushKeys() +{ + Keyboard::FlushKeys(); + vkins = vkext = 0; +} + + +void +BufferKey(int vkey) +{ + if (vkey < 1) return; + + int shift = 0; + + if (GetAsyncKeyState(VK_SHIFT)) + shift |= 1; + + if (GetAsyncKeyState(VK_CONTROL)) + shift |= 2; + + if (GetAsyncKeyState(VK_MENU)) + shift |= 4; + + vkbuf[vkins] = vkey; + vkshiftbuf[vkins++] = shift; + + if (vkins >= MAX_KEY_BUF) + vkins = 0; + + if (vkins == vkext) { + vkext++; + if (vkext >= MAX_KEY_BUF) + vkext = 0; + } +} + + +int +GetKey() +{ + if (vkins == vkext) return 0; + + int result = vkbuf[vkext++]; + if (vkext >= MAX_KEY_BUF) + vkext = 0; + + return result; +} + + +int +GetKeyPlus(int& key, int& shift) +{ + if (vkins == vkext) return 0; + + key = vkbuf[vkext]; + shift = vkshiftbuf[vkext++]; + + if (vkext >= MAX_KEY_BUF) + vkext = 0; + + return key; +} diff --git a/Stars45/WndProc.h b/Stars45/WndProc.h new file mode 100644 index 0000000..715ebef --- /dev/null +++ b/Stars45/WndProc.h @@ -0,0 +1,21 @@ +/* Starshatter: The Open Source Project + Copyright (c) 2021-2022, Starshatter: The Open Source Project Contributors + Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors + Copyright (c) 1997-2006, Destroyer Studios LLC. +*/ + +#ifndef WndProc_h +#define WndProc_h + +#include "Types.h" + + +LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); +void FlushKeys(); +void BufferKey(int vkey); +int GetKey(); +int GetKeyPlus(int& key, int& shift); +void ProcessKeyMessage(); + + +#endif // WndProc_h -- cgit v1.1