Kumo

A hand-rolled game engine.

git clone https://git.sentomk.me/Kumo.git
commit 96b935295319d940febb35ba3df30a71af085c72
parent 5cf1af290e82060b75b2d2c92c3856a047fea9bc
Author: sentomk <sentomk@proton.me>
Date:   Sat, 14 Nov 2026 16:23:00 +0800

feat: present the software framebuffer on Windows

The frame is stretched to the client area with StretchDIBits, nearest
neighbor, and DwmFlush() waits for the compositor so that the loop runs
at the refresh rate, as the Metal backend does. Without this the Windows
build did not link, because main.cpp calls window_present().

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

Diffstat:
Mkumo.toml | 4++--
Msrc/platform/window_win.cpp | 40++++++++++++++++++++++++++++++++++++++++
2 files changed, 42 insertions(+), 2 deletions(-)

diff --git a/kumo.toml b/kumo.toml @@ -26,7 +26,7 @@ link = [] # Per-platform additions: `flags` and `link` here are appended to the lists # above, on that platform only. Linux will need "-lX11" once the window code -# for it exists, and Windows "-lgdi32" once its window presents a frame. +# for it exists. [mac] link = ["-framework", "Cocoa", "-framework", "Metal", "-framework", "QuartzCore"] @@ -35,4 +35,4 @@ link = ["-framework", "Cocoa", "-framework", "Metal", "-framework", "QuartzCore" # The MSVC standard library must be told explicitly that exceptions are off, # otherwise its headers still contain `try` blocks. flags = ["-D_HAS_EXCEPTIONS=0", "-D_CRT_SECURE_NO_WARNINGS"] -link = ["-luser32"] +link = ["-luser32", "-lgdi32", "-ldwmapi"] diff --git a/src/platform/window_win.cpp b/src/platform/window_win.cpp @@ -3,12 +3,14 @@ #define WIN32_LEAN_AND_MEAN #define NOMINMAX +#include <dwmapi.h> #include <windows.h> #include <string> #include "core/types.h" #include "platform/window.h" +#include "render/frame_buffer.h" namespace kumo { @@ -144,6 +146,44 @@ void window_poll_events(Window* window) { bool window_should_close(const Window* window) { return window->should_close; } +void window_present(Window* window, const FrameBuffer& framebuffer) { + if (window->handle == nullptr) return; + + RECT client{}; + if (!GetClientRect(window->handle, &client)) return; + + // Color is 0xAARRGGBB in a 32-bit integer, which is the bytes B, G, R, A on + // a little-endian machine: exactly a 32-bit DIB, so no conversion is needed. + // GDI ignores the fourth byte. A negative height says that the first row is + // the top one. + BITMAPINFO info{}; + info.bmiHeader.biSize = sizeof(info.bmiHeader); + info.bmiHeader.biWidth = framebuffer.width; + info.bmiHeader.biHeight = -framebuffer.height; + info.bmiHeader.biPlanes = 1; + info.bmiHeader.biBitCount = 32; + info.bmiHeader.biCompression = BI_RGB; + + // A minimized window has an empty client area, and there is nothing to draw. + if (client.right > 0 && client.bottom > 0) { + const HDC context = GetDC(window->handle); + if (context != nullptr) { + // The frame is stretched to the client area. COLORONCOLOR drops or + // repeats pixels, which is nearest-neighbor: the pixels stay sharp. + SetStretchBltMode(context, COLORONCOLOR); + StretchDIBits(context, 0, 0, client.right, client.bottom, 0, 0, + framebuffer.width, framebuffer.height, + framebuffer.pixels.data(), &info, DIB_RGB_COLORS, SRCCOPY); + ReleaseDC(window->handle, context); + } + } + + // GDI does not wait for the display. DwmFlush() returns when the compositor + // has shown its next frame, which is the vsync wait; if it cannot, sleep + // briefly so that the loop does not spin a whole core. + if (FAILED(DwmFlush())) Sleep(1); +} + void window_set_title(Window* window, const char* title) { std::wstring text; if (wide_title(title, text)) {