Add fullscreen

This commit is contained in:
Grayson Riffe 2025-02-02 15:05:58 -06:00
parent bd8eda13aa
commit 9c78fe09c9
10 changed files with 113 additions and 59 deletions

View File

@ -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)

View File

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

View File

@ -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
}

View File

@ -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;
};
}

View File

@ -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<Window*>(reinterpret_cast<CREATESTRUCT*>(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 };
}
}

View File

@ -1,24 +1,28 @@
// Window class header
#pragma once
#include "nf/config.h"
#include <Windows.h>
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;
};
}

View File

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

View File

@ -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")
{}
};
}

View File

@ -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()
{}
};
}

View File

@ -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;
}
}