Kumo

A hand-rolled game engine.

git clone https://git.sentomk.me/Kumo.git
commit cb41c818d211675d2c46257ced7a2216d556df9f
parent 4e39d1d4f4132248458f0485e000efffe9e808ea
Author: sentomk <mk20040924@outlook.com>
Date:   Thu, 22 Oct 2026 00:24:51 +0800

feat: add a Win32 window backend

Diffstat:
M.clangd | 3+++
Mkumo.toml | 3++-
Msrc/main.cpp | 3++-
Asrc/platform/window_win.cpp | 154+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 161 insertions(+), 2 deletions(-)

diff --git a/.clangd b/.clangd @@ -1,2 +1,4 @@ CompileFlags: Add: [-std=c++20] +Completion: + HeaderInsertion: Never +\ No newline at end of file diff --git a/kumo.toml b/kumo.toml @@ -34,4 +34,5 @@ link = ["-framework", "Cocoa", "-framework", "Metal", "-framework", "QuartzCore" [windows] # The MSVC standard library must be told explicitly that exceptions are off, # otherwise its headers still contain `try` blocks. -flags = ["-D_HAS_EXCEPTIONS=0"] +flags = ["-D_HAS_EXCEPTIONS=0", "-D_CRT_SECURE_NO_WARNINGS"] +link = ["-luser32"] diff --git a/src/main.cpp b/src/main.cpp @@ -1,5 +1,6 @@ #include <chrono> #include <cstdio> +#include <cstdlib> #include <thread> #include "core/clock.h" @@ -9,7 +10,7 @@ int main() { kumo::Window* window = kumo::window_create({"Kumo", 1280, 720}); if (window == nullptr) { - std::fprintf(stderr, "could not create the window\n);"); + std::fprintf(stderr, "could not create the window\n"); return EXIT_FAILURE; } diff --git a/src/platform/window_win.cpp b/src/platform/window_win.cpp @@ -0,0 +1,154 @@ +/// @file platform/window_win.cpp +/// @brief Windows implementation of platform/window.h + +#define WIN32_LEAN_AND_MEAN +#define NOMINMAX +#include <windows.h> + +#include <string> + +#include "core/types.h" +#include "platform/window.h" + +namespace kumo { + +struct Window { + HWND handle = nullptr; + bool should_close = false; +}; + +static constexpr wchar_t kWindowClass[] = L"KumoWindow"; + +// Public titles use UTF-8; Win32's W functions use UTF-16. +static bool wide_title(const char* title, std::wstring& result) { + if (title == nullptr) return false; + + const int count = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, title, + -1, nullptr, 0); + if (count == 0) return false; + + result.resize(static_cast<size_t>(count)); + return MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, title, -1, + result.data(), count) != 0; +} + +static LRESULT CALLBACK window_proc(HWND handle, UINT message, WPARAM wparam, + LPARAM lparam) { + auto* window = + reinterpret_cast<Window*>(GetWindowLongPtrW(handle, GWLP_USERDATA)); + + // CreateWindowExW sends this before the window becomes visible. + if (message == WM_NCCREATE) { + const auto* create = reinterpret_cast<const CREATESTRUCTW*>(lparam); + window = static_cast<Window*>(create->lpCreateParams); + window->handle = handle; + + SetWindowLongPtrW(handle, GWLP_USERDATA, + reinterpret_cast<LONG_PTR>(window)); + } + + if (window != nullptr) { + switch (message) { + case WM_CLOSE: + // Keep the window alive until the main loop destroys it. + window->should_close = true; + return 0; + + case WM_KEYDOWN: + if (wparam == VK_ESCAPE) { + window->should_close = true; + return 0; + } + break; + + case WM_NCDESTROY: + window->should_close = true; + window->handle = nullptr; + SetWindowLongPtrW(handle, GWLP_USERDATA, 0); + break; + } + } + + return DefWindowProcW(handle, message, wparam, lparam); +} + +Window* window_create(const WindowDesc& desc) { + if (desc.width <= 0 || desc.height <= 0) return nullptr; + + std::wstring title; + if (!wide_title(desc.title, title)) return nullptr; + + const HINSTANCE instance = GetModuleHandleW(nullptr); + + // Register once; later windows reuse the same class. + static ATOM window_class = 0; + if (window_class == 0) { + WNDCLASSEXW info{}; + info.cbSize = sizeof(info); + info.lpfnWndProc = window_proc; + info.hInstance = instance; + info.hCursor = LoadCursorW(nullptr, MAKEINTRESOURCEW(32512)); // Arrow. + info.hbrBackground = GetSysColorBrush(COLOR_WINDOW); + info.lpszClassName = kWindowClass; + + window_class = RegisterClassExW(&info); + if (window_class == 0) return nullptr; + } + + constexpr DWORD style = WS_OVERLAPPEDWINDOW; + + // Convert the requested client size to the outer window size. + RECT bounds{0, 0, desc.width, desc.height}; + if (!AdjustWindowRectEx(&bounds, style, FALSE, 0)) return nullptr; + + auto* window = new Window; + + const HWND handle = CreateWindowExW( + 0, kWindowClass, title.c_str(), style, CW_USEDEFAULT, CW_USEDEFAULT, + bounds.right - bounds.left, bounds.bottom - bounds.top, nullptr, nullptr, + instance, window); + + if (handle == nullptr) { + delete window; + return nullptr; + } + + ShowWindow(handle, SW_SHOW); + UpdateWindow(handle); + return window; +} + +void window_destroy(Window* window) { + if (window == nullptr) return; + + if (window->handle != nullptr) { + DestroyWindow(window->handle); + } + delete window; +} + +void window_poll_events(Window* window) { + MSG message{}; + + // Return immediately when no messages remain. + while (PeekMessageW(&message, nullptr, 0, 0, PM_REMOVE)) { + if (message.message == WM_QUIT) { + window->should_close = true; + break; + } + + TranslateMessage(&message); + DispatchMessageW(&message); + } +} + +bool window_should_close(const Window* window) { return window->should_close; } + +void window_set_title(Window* window, const char* title) { + std::wstring text; + if (wide_title(title, text)) { + SetWindowTextW(window->handle, text.c_str()); + } +} + +} // namespace kumo