/// @file platform/window_win.cpp /// @brief Windows (Win32) implementation of platform/window.h. #define WIN32_LEAN_AND_MEAN #define NOMINMAX #include #include #include #include #include "core/types.h" #include "platform/keyboard.h" #include "platform/window.h" #include "render/frame_buffer.h" namespace kumo { struct Window { HWND handle = nullptr; bool should_close = false; Keyboard keyboard; }; 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(count)); return MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, title, -1, result.data(), count) != 0; } static Key key_from_virtual_key(WPARAM code) { using namespace ptn; return match(code) | on(val >> Key::Left, val >> Key::Right, val >> Key::Up, val >> Key::Down, val >> Key::Space, _ >> Key::Count); } static LRESULT CALLBACK window_proc(HWND handle, UINT message, WPARAM wparam, LPARAM lparam) { auto* window = reinterpret_cast(GetWindowLongPtrW(handle, GWLP_USERDATA)); // CreateWindowExW sends this before the window becomes visible. if (message == WM_NCCREATE) { const auto* create = reinterpret_cast(lparam); window = static_cast(create->lpCreateParams); window->handle = handle; SetWindowLongPtrW(handle, GWLP_USERDATA, reinterpret_cast(window)); } const auto default_proc = [&] { return DefWindowProcW(handle, message, wparam, lparam); }; if (window == nullptr) return default_proc(); using namespace ptn; return match(message) | on( val >> [&]() -> LRESULT { // Keep the window alive until the main loop destroys it. window->should_close = true; return 0; }, any(val, val, val) >> [&]() -> LRESULT { const bool down = message == WM_KEYDOWN; if (down && wparam == VK_ESCAPE) { window->should_close = true; return 0; } const Key key = key_from_virtual_key(wparam); if (key == Key::Count) return default_proc(); // Win32 sets bit 30 when the key was already held. constexpr LPARAM kWasDown = LPARAM{1} << 30; if (!down || (lparam & kWasDown) == 0) { window->keyboard.set_key(key, down); } // Forward system key messages after updating our state. return message == WM_SYSKEYUP ? default_proc() : 0; }, val >> [&]() -> LRESULT { window->keyboard.release_all(); return 0; }, val >> [&] { window->should_close = true; window->handle = nullptr; SetWindowLongPtrW(handle, GWLP_USERDATA, 0); return default_proc(); }, _ >> default_proc); } 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); } } Keyboard& window_keyboard(Window* window) { return window->keyboard; } 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)) { SetWindowTextW(window->handle, text.c_str()); } } } // namespace kumo