From 9c78fe09c9d876e4737c76aa6d27424243139b39 Mon Sep 17 00:00:00 2001 From: Grayson Riffe Date: Sun, 2 Feb 2025 15:05:58 -0600 Subject: [PATCH] Add fullscreen --- NothinFancy/CMakeLists.txt | 2 +- NothinFancy/src/Engine.cpp | 7 +- NothinFancy/src/client/Client.cpp | 6 +- NothinFancy/src/client/Client.h | 6 +- NothinFancy/src/client/Window.cpp | 82 ++++++++++++++++------- NothinFancy/src/client/Window.h | 10 ++- NothinFancy/src/include/nf.h | 8 +-- NothinFancy/src/include/nf/EngineConfig.h | 14 ---- NothinFancy/src/include/nf/config.h | 32 +++++++++ TestGame/src/TestGame.cpp | 5 +- 10 files changed, 113 insertions(+), 59 deletions(-) delete mode 100644 NothinFancy/src/include/nf/EngineConfig.h create mode 100644 NothinFancy/src/include/nf/config.h diff --git a/NothinFancy/CMakeLists.txt b/NothinFancy/CMakeLists.txt index 30f4e03..684bd6a 100644 --- a/NothinFancy/CMakeLists.txt +++ b/NothinFancy/CMakeLists.txt @@ -1,5 +1,5 @@ # NF library CMakeLists.txt -add_library(NothinFancy STATIC "src/Engine.cpp" "src/include/nf.h" "src/pch.h" "src/util.h" "src/util/log.h" "src/util/log.cpp" "src/include/nf/EngineConfig.h" "src/util/util.cpp" "src/util/file.h" "src/util/file.cpp" "src/client/Client.h" "src/client/Client.cpp" "src/client/Window.h" "src/client/Window.cpp") +add_library(NothinFancy STATIC "src/Engine.cpp" "src/include/nf.h" "src/pch.h" "src/util.h" "src/util/log.h" "src/util/log.cpp" "src/include/nf/config.h" "src/util/util.cpp" "src/util/file.h" "src/util/file.cpp" "src/client/Client.h" "src/client/Client.cpp" "src/client/Window.h" "src/client/Window.cpp") # Use C++20 set_property(TARGET NothinFancy PROPERTY CXX_STANDARD 20) diff --git a/NothinFancy/src/Engine.cpp b/NothinFancy/src/Engine.cpp index 3f12430..8586a3b 100644 --- a/NothinFancy/src/Engine.cpp +++ b/NothinFancy/src/Engine.cpp @@ -1,13 +1,13 @@ // NF startup #include "pch.h" -#include "nf/EngineConfig.h" +#include "nf/config.h" #include "version.h" #include "util.h" #include "client/Client.h" namespace nf { - void runEngine(EngineConfig config) { + void runEngine(ClientConfig config) { std::string engineStr = std::format("Nothin' Fancy {}", NFVERSION); std::string gameStr = std::format("{} {}", config.appName, config.appVersion); NFLog(engineStr); @@ -18,9 +18,6 @@ namespace nf { SetConsoleTitle(std::format("{} Debug Console - {}", engineStr, gameStr).c_str()); #endif - // Disable automatic DPI upscaling - SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2); - // Start client { cli::Client client(config); diff --git a/NothinFancy/src/client/Client.cpp b/NothinFancy/src/client/Client.cpp index c5b1b10..bc23fc7 100644 --- a/NothinFancy/src/client/Client.cpp +++ b/NothinFancy/src/client/Client.cpp @@ -4,9 +4,9 @@ #include "Client.h" namespace nf::cli { - Client::Client(EngineConfig config) - : m_currentConfig(config) - , m_window(1280, 720) + Client::Client(ClientConfig config) + : m_config(config) + , m_window(m_config.display) { // Setup window and renderer } diff --git a/NothinFancy/src/client/Client.h b/NothinFancy/src/client/Client.h index 2fd3dac..87a772e 100644 --- a/NothinFancy/src/client/Client.h +++ b/NothinFancy/src/client/Client.h @@ -1,17 +1,17 @@ // NF Client class header #pragma once -#include "nf/EngineConfig.h" +#include "nf/config.h" #include "Window.h" namespace nf::cli { class Client { public: - Client(EngineConfig config); + Client(ClientConfig config); void run(); private: - EngineConfig m_currentConfig; + ClientConfig m_config; Window m_window; }; } diff --git a/NothinFancy/src/client/Window.cpp b/NothinFancy/src/client/Window.cpp index 67fe948..8eb0946 100644 --- a/NothinFancy/src/client/Window.cpp +++ b/NothinFancy/src/client/Window.cpp @@ -5,15 +5,18 @@ #include "util.h" namespace nf::cli { - Window::Window(unsigned int width, unsigned int height) + Window::Window(DisplayConfig config) : m_handle(nullptr) , m_wndClassName("NothinFancyWindow") , m_windowedStyle(WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX) - , m_width(width) - , m_height(height) + , m_fullscreenStyle(WS_POPUP) + , m_config() { NFLog("Creating window"); + // Disable automatic DPI upscaling + SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2); + // Register window class WNDCLASS wc = {}; wc.lpszClassName = m_wndClassName; @@ -21,8 +24,46 @@ namespace nf::cli { wc.lpfnWndProc = wndProc; RegisterClass(&wc); - m_handle = CreateWindow(m_wndClassName, "NF Window", m_windowedStyle, 0, 0, 0, 0, nullptr, nullptr, nullptr, this); - setSize(); + m_handle = CreateWindow(m_wndClassName, "NF Window", NULL, 0, 0, 0, 0, nullptr, nullptr, nullptr, this); + setDisplay(config); + } + + void Window::setDisplay(DisplayConfig config) { + m_config = config; + bool wasShown = IsWindowVisible(m_handle); + show(false); + + // Temporary monitor choice + POINT cursor = {}; + GetCursorPos(&cursor); + MONITORINFO mi = {}; + mi.cbSize = sizeof(mi); + GetMonitorInfo(MonitorFromPoint(cursor, MONITOR_DEFAULTTONEAREST), &mi); + int monitorX = mi.rcMonitor.left, monitorY = mi.rcMonitor.top; + int monitorWidth = mi.rcMonitor.right - monitorX, monitorHeight = mi.rcMonitor.bottom - monitorY; + int windowX = 0, windowY = 0; + unsigned int windowWidth = 0, windowHeight = 0; + + switch (config.mode) { + case DisplayMode::Windowed: { + SetWindowLongPtr(m_handle, GWL_STYLE, m_windowedStyle); + windowX = monitorX + (monitorWidth / 2) - (m_config.width / 2), windowY = monitorY + (monitorHeight / 2) - (m_config.height / 2); + SIZE windowSize = getWindowSize(); + windowWidth = windowSize.cx, windowHeight = windowSize.cy; + break; + } + + case DisplayMode::Fullscreen: { + SetWindowLongPtr(m_handle, GWL_STYLE, m_fullscreenStyle); + windowX = monitorX, windowY = monitorY; + windowWidth = monitorWidth, windowHeight = monitorHeight; + break; + } + } + + SetWindowPos(m_handle, nullptr, windowX, windowY, windowWidth, windowHeight, SWP_NOZORDER | SWP_FRAMECHANGED); + + show(wasShown); } void Window::runLoop() { @@ -42,24 +83,17 @@ namespace nf::cli { static Window* wnd = nullptr; switch (msg) { - case WM_CREATE: { + case WM_CREATE: wnd = reinterpret_cast(reinterpret_cast(lParam)->lpCreateParams); - - // Temporary monitor choice - POINT cursor = {}; - GetCursorPos(&cursor); - MONITORINFO mi = {}; - mi.cbSize = sizeof(mi); - GetMonitorInfo(MonitorFromPoint(cursor, MONITOR_DEFAULTTONEAREST), &mi); - int monWidth = mi.rcMonitor.right - mi.rcMonitor.left, monHeight = mi.rcMonitor.bottom - mi.rcMonitor.top; - int x = mi.rcMonitor.left + (monWidth / 2) - (wnd->m_width / 2), y = mi.rcMonitor.top + (monHeight / 2) - (wnd->m_height / 2); - SetWindowPos(hWnd, nullptr, x , y , 0, 0, SWP_NOZORDER | SWP_NOSIZE); return 0; - } case WM_DPICHANGED: { - // Prevents resize on DPI change - wnd->setSize(); + // Prevents automatic window resize on DPI change (don't apply to fullscreen) + if (GetWindowLongPtr(hWnd, GWL_STYLE) & WS_POPUP) + return 0; + + SIZE windowSize = wnd->getWindowSize(); + SetWindowPos(hWnd, nullptr, 0, 0, windowSize.cx, windowSize.cy, SWP_NOZORDER | SWP_NOMOVE); return 0; } @@ -75,12 +109,12 @@ namespace nf::cli { return DefWindowProc(hWnd, msg, wParam, lParam); } - void Window::setSize() { + SIZE Window::getWindowSize() { RECT cli = {}; - cli.right = m_width; - cli.bottom = m_height; + cli.right = m_config.width; + cli.bottom = m_config.height; AdjustWindowRectExForDpi(&cli, m_windowedStyle, FALSE, NULL, GetDpiForWindow(m_handle)); - unsigned int width = cli.right - cli.left, height = cli.bottom - cli.top; - SetWindowPos(m_handle, nullptr, 0, 0, width, height, SWP_NOZORDER | SWP_NOMOVE); + int width = cli.right - cli.left, height = cli.bottom - cli.top; + return { width, height }; } } diff --git a/NothinFancy/src/client/Window.h b/NothinFancy/src/client/Window.h index 8b9ea49..07bf33e 100644 --- a/NothinFancy/src/client/Window.h +++ b/NothinFancy/src/client/Window.h @@ -1,24 +1,28 @@ // Window class header #pragma once +#include "nf/config.h" + #include namespace nf::cli { class Window { public: - Window(unsigned int width, unsigned int height); + Window(DisplayConfig config); + void setDisplay(DisplayConfig config); void runLoop(); void show(bool show = true); private: static LRESULT CALLBACK wndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); - void setSize(); + SIZE getWindowSize(); HWND m_handle; const char* m_wndClassName; const DWORD m_windowedStyle; + const DWORD m_fullscreenStyle; - unsigned int m_width, m_height; + DisplayConfig m_config; }; } diff --git a/NothinFancy/src/include/nf.h b/NothinFancy/src/include/nf.h index 3b92b55..5756cf1 100644 --- a/NothinFancy/src/include/nf.h +++ b/NothinFancy/src/include/nf.h @@ -1,7 +1,7 @@ // NF main public header #pragma once -#include "nf/EngineConfig.h" +#include "nf/config.h" namespace nf { struct CommandLineArguments { @@ -9,16 +9,16 @@ namespace nf { char** argv; }; - EngineConfig configureEngine(CommandLineArguments cmdArgs); + ClientConfig configureEngine(CommandLineArguments cmdArgs); - void runEngine(EngineConfig config); + void runEngine(ClientConfig config); } // NF entry point #ifdef NFENTRY int main(int argc, char* argv[]) { - nf::EngineConfig config = nf::configureEngine({ argc, argv }); + nf::ClientConfig config = nf::configureEngine({ argc, argv }); nf::runEngine(config); diff --git a/NothinFancy/src/include/nf/EngineConfig.h b/NothinFancy/src/include/nf/EngineConfig.h deleted file mode 100644 index e8192ab..0000000 --- a/NothinFancy/src/include/nf/EngineConfig.h +++ /dev/null @@ -1,14 +0,0 @@ -// EngineConfig struct public header -#pragma once - -namespace nf { - struct EngineConfig { - const char* appName; - const char* appVersion; - - EngineConfig() - : appName("Nothin' Fancy Game") - , appVersion("v0.1.0") - {} - }; -} diff --git a/NothinFancy/src/include/nf/config.h b/NothinFancy/src/include/nf/config.h new file mode 100644 index 0000000..4e7e412 --- /dev/null +++ b/NothinFancy/src/include/nf/config.h @@ -0,0 +1,32 @@ +// EngineConfig struct public header +#pragma once + +namespace nf { + enum class DisplayMode { + Windowed, + Fullscreen + }; + + struct DisplayConfig { + DisplayMode mode; + unsigned int width, height; + + DisplayConfig() + : mode(DisplayMode::Windowed) + , width(1280) + , height(720) + {} + }; + + struct ClientConfig { + const char* appName; + const char* appVersion; + DisplayConfig display; + + ClientConfig() + : appName("Nothin' Fancy Game") + , appVersion("v0.1.0") + , display() + {} + }; +} diff --git a/TestGame/src/TestGame.cpp b/TestGame/src/TestGame.cpp index f8dbec4..bd90431 100644 --- a/TestGame/src/TestGame.cpp +++ b/TestGame/src/TestGame.cpp @@ -3,8 +3,9 @@ #include "nf.h" namespace nf { - EngineConfig configureEngine(CommandLineArguments cmdArgs) { - EngineConfig config; + ClientConfig configureEngine(CommandLineArguments cmdArgs) { + ClientConfig config; + //config.display.mode = DisplayMode::Fullscreen; return config; } }