Kumo
A hand-rolled game engine.
git clone https://git.sentomk.me/Kumo.git
commit 2fcd5087ece9a6d2bccc7a0f3841906cfb71fc6d parent ef21df73e807181ef3cfeaa6f83ca50fa10e6e2e Author: sentomk <mk20040924@outlook.com> Date: Sat, 21 Nov 2026 02:11:25 +0800 feat: use Patternia for window input and demo movement Diffstat:
| M | kumo.toml | | | 8 | ++++---- |
| M | src/main.cpp | | | 43 | ++++++++++++++++++++++--------------------- |
| M | src/platform/window.h | | | 6 | ++++++ |
| M | src/platform/window_mac.mm | | | 64 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ |
| M | src/platform/window_win.cpp | | | 67 | ++++++++++++++++++++++++++++++++++++++++++++++++++----------------- |
5 files changed, 140 insertions(+), 48 deletions(-)
diff --git a/kumo.toml b/kumo.toml @@ -53,7 +53,7 @@ link = ["-fsanitize=address,undefined"] # for reproducible checkouts if an upstream tag might move. # Include paths are relative to the dependency checkout. # -# [deps.patternia] -# git = "https://github.com/SentoMK/patternia.git" -# rev = "v0.9.6" -# include = ["include"] +[deps.patternia] +git = "https://github.com/sentomk/patternia.git" +rev = "v0.9.7" +include = ["include"] diff --git a/src/main.cpp b/src/main.cpp @@ -1,6 +1,8 @@ #include <cstdio> #include <cstdlib> +#include <ptn/patternia.hpp> + #include "core/clock.h" #include "core/fixed.h" #include "platform/window.h" @@ -15,6 +17,24 @@ constexpr int kScreenWidth = 320; constexpr int kScreenHeight = 180; constexpr int kBoxSize = 20; +/// @brief Advances one axis and reflects any overshoot at either boundary. +void advance_axis(kumo::Fixed& position, kumo::Fixed& velocity, + kumo::Fixed limit) { + using namespace ptn; + + const auto reflect = [&velocity](kumo::Fixed next) { + velocity = -velocity; + return next; + }; + position = match(position + velocity) | + on($[_ < kumo::Fixed{}] >> + [&](kumo::Fixed next) { return reflect(-next); }, + $[_ > limit] >> [&](kumo::Fixed next) { + return reflect(limit - (next - limit)); + }, + $ >> [](kumo::Fixed next) { return next; }); +} + /// @brief Minimal bouncing-box demo exercising fixed updates and interpolation. class DemoGame final : public kumo::Game { public: @@ -22,27 +42,8 @@ class DemoGame final : public kumo::Game { prev_x_ = x_; prev_y_ = y_; - x_ += vx_; - y_ += vy_; - - const kumo::Fixed max_x = kumo::Fixed::from_int(kScreenWidth - kBoxSize); - const kumo::Fixed max_y = kumo::Fixed::from_int(kScreenHeight - kBoxSize); - - if (x_ < kumo::Fixed::from_int(0)) { - x_ = -x_; - vx_ = -vx_; - } else if (x_ > max_x) { - x_ = max_x - (x_ - max_x); - vx_ = -vx_; - } - - if (y_ < kumo::Fixed::from_int(0)) { - y_ = -y_; - vy_ = -vy_; - } else if (y_ > max_y) { - y_ = max_y - (y_ - max_y); - vy_ = -vy_; - } + advance_axis(x_, vx_, kumo::Fixed::from_int(kScreenWidth - kBoxSize)); + advance_axis(y_, vy_, kumo::Fixed::from_int(kScreenHeight - kBoxSize)); } void render(kumo::FrameBuffer& frame, kumo::Fixed alpha) const override { diff --git a/src/platform/window.h b/src/platform/window.h @@ -11,6 +11,7 @@ namespace kumo { struct FrameBuffer; +class Keyboard; /// @brief Opaque window handle; the definition lives in the platform file. struct Window; @@ -39,6 +40,11 @@ void window_destroy(Window* window); /// Call once per frame. void window_poll_events(Window* window); +/// @brief Returns the keyboard state owned by the window. +/// +/// Use on the main thread. The reference is valid until window_destroy(). +Keyboard& window_keyboard(Window* window); + /// @brief Tells whether the user asked to close the window. /// /// True after the close button is pressed or Escape is hit. The window stays diff --git a/src/platform/window_mac.mm b/src/platform/window_mac.mm @@ -7,10 +7,16 @@ #import <Metal/Metal.h> #import <QuartzCore/CAMetalLayer.h> +#include <ptn/patternia.hpp> + +#include "platform/keyboard.h" #include "render/frame_buffer.h" -/// @brief Window delegate that records close requests instead of closing. -@interface KumoWindowDelegate : NSObject <NSWindowDelegate> +/// @brief Handles close requests and keyboard focus changes. +@interface KumoWindowDelegate : NSObject <NSWindowDelegate> { + @public + kumo::Keyboard keyboard; +} @property(nonatomic) BOOL should_close; @end @@ -21,6 +27,11 @@ self.should_close = YES; return NO; } + +- (void)windowDidResignKey:(NSNotification*)notification { + (void)notification; + keyboard.release_all(); +} @end /// @brief A view whose backing layer is a CAMetalLayer, so that Metal can draw @@ -120,6 +131,45 @@ void window_destroy(Window* window) { delete window; } +static Key key_from_event(NSEvent* event) { + NSString* characters = event.charactersIgnoringModifiers; + if (characters.length != 1) return Key::Count; + + using namespace ptn; + return match([characters characterAtIndex:0]) | on( + val<NSLeftArrowFunctionKey> >> Key::Left, + val<NSRightArrowFunctionKey> >> Key::Right, + val<NSUpArrowFunctionKey> >> Key::Up, + val<NSDownArrowFunctionKey> >> Key::Down, + val<' '> >> Key::Space, + _ >> Key::Count); +} + +static bool handle_key_event(Window* window, NSEvent* event) { + const bool down = event.type == NSEventTypeKeyDown; + if ((!down && event.type != NSEventTypeKeyUp) || + event.window != window->ns_window || + ![window->ns_window isKeyWindow]) { + return false; + } + if (down && (event.modifierFlags & NSEventModifierFlagCommand) != 0) { + return false; + } + + constexpr unsigned short kEscapeKeyCode = 53; + if (down && event.keyCode == kEscapeKeyCode) { + window->delegate.should_close = YES; + return true; + } + + const Key key = key_from_event(event); + if (key == Key::Count) return false; + if (!down || ![event isARepeat]) { + window->delegate->keyboard.set_key(key, down); + } + return true; +} + void window_poll_events(Window* window) { // Keeps the autoreleased event objects from piling up across frames. @autoreleasepool { @@ -131,15 +181,17 @@ void window_poll_events(Window* window) { dequeue:YES]; if (event == nil) break; - constexpr unsigned short kEscapeKeyCode = 53; - if (event.type == NSEventTypeKeyDown && event.keyCode == kEscapeKeyCode) { - window->delegate.should_close = YES; + if (!handle_key_event(window, event)) { + [NSApp sendEvent:event]; } - [NSApp sendEvent:event]; } } } +Keyboard& window_keyboard(Window* window) { + return window->delegate->keyboard; +} + bool window_should_close(const Window* window) { return window->delegate.should_close; } void window_present(Window* window, const FrameBuffer& framebuffer) { diff --git a/src/platform/window_win.cpp b/src/platform/window_win.cpp @@ -8,7 +8,10 @@ #include <string> +#include <ptn/patternia.hpp> + #include "core/types.h" +#include "platform/keyboard.h" #include "platform/window.h" #include "render/frame_buffer.h" @@ -17,6 +20,7 @@ namespace kumo { struct Window { HWND handle = nullptr; bool should_close = false; + Keyboard keyboard; }; static constexpr wchar_t kWindowClass[] = L"KumoWindow"; @@ -34,6 +38,14 @@ static bool wide_title(const char* title, std::wstring& result) { result.data(), count) != 0; } +static Key key_from_virtual_key(WPARAM code) { + using namespace ptn; + return match(code) | on(val<VK_LEFT> >> Key::Left, + val<VK_RIGHT> >> Key::Right, val<VK_UP> >> Key::Up, + val<VK_DOWN> >> Key::Down, + val<VK_SPACE> >> Key::Space, _ >> Key::Count); +} + static LRESULT CALLBACK window_proc(HWND handle, UINT message, WPARAM wparam, LPARAM lparam) { auto* window = @@ -49,29 +61,48 @@ static LRESULT CALLBACK window_proc(HWND handle, UINT message, WPARAM wparam, reinterpret_cast<LONG_PTR>(window)); } - if (window != nullptr) { - switch (message) { - case WM_CLOSE: + const auto default_proc = [&] { + return DefWindowProcW(handle, message, wparam, lparam); + }; + if (window == nullptr) return default_proc(); + + using namespace ptn; + return match(message) | on( + val<WM_CLOSE> >> [&]() -> LRESULT { // 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: + }, + any(val<WM_KEYDOWN>, val<WM_KEYUP>, val<WM_SYSKEYUP>) >> + [&]() -> 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<WM_KILLFOCUS> >> [&]() -> LRESULT { + window->keyboard.release_all(); + return 0; + }, + val<WM_NCDESTROY> >> [&] { window->should_close = true; window->handle = nullptr; SetWindowLongPtrW(handle, GWLP_USERDATA, 0); - break; - } - } - - return DefWindowProcW(handle, message, wparam, lparam); + return default_proc(); + }, + _ >> default_proc); } Window* window_create(const WindowDesc& desc) { @@ -144,6 +175,8 @@ void window_poll_events(Window* window) { } } +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) {
