Kumo
A hand-rolled game engine.
git clone https://git.sentomk.me/Kumo.git
commit 4e39d1d4f4132248458f0485e000efffe9e808ea parent 1f1824efb8756c0bce4a5f69a2f83ae43115b56f Author: sentomk <sentomk@proton.me> Date: Thu, 15 Oct 2026 20:20:20 +0800 feat: open a macOS window and run the main loop on the fixed step Add a platform window interface with a Cocoa implementation: manual event pumping, close button and Escape set a flag, and a minimal menu gives Cmd+Q. The main loop now reads the system clock and advances the fixed step, showing the simulated seconds in the title. Diffstat:
| M | src/main.cpp | | | 43 | +++++++++++++++++++++++++++++++++++++++++-- |
| A | src/platform/window.h | | | 51 | +++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | src/platform/window_mac.mm | | | 113 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
3 files changed, 205 insertions(+), 2 deletions(-)
diff --git a/src/main.cpp b/src/main.cpp @@ -1,3 +1,42 @@ -#include <iostream> +#include <chrono> +#include <cstdio> +#include <thread> -int main() { std::cout << "Hello from Kumo..." << '\n'; } +#include "core/clock.h" +#include "core/fixed_step.h" +#include "platform/window.h" + +int main() { + kumo::Window* window = kumo::window_create({"Kumo", 1280, 720}); + if (window == nullptr) { + std::fprintf(stderr, "could not create the window\n);"); + return EXIT_FAILURE; + } + + kumo::SystemClock clock; + kumo::FrameTimer timer(clock); + kumo::FixedStep step(kumo::step_from_hz(60), kumo::milliseconds(250)); + + int64_t shown_seconds = -1; + while (!kumo::window_should_close(window)) { + kumo::window_poll_events(window); + + // Nothing to simulate yet; the steps are only counted, and the title shows + // the simulated time once a second so that the loop can be seen running. + step.advance(timer.tick()); + const int64_t seconds = step.simulated_time() / kumo::kNanosecondsPerSecond; + if (seconds != shown_seconds) { + shown_seconds = seconds; + char title[64]; + std::snprintf(title, sizeof(title), "Kumo - %lld s", + static_cast<long long>(seconds)); + kumo::window_set_title(window, title); + } + + // Until presenting a frame paces the loop, do not spin a whole core. + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + + kumo::window_destroy(window); + return 0; +} diff --git a/src/platform/window.h b/src/platform/window.h @@ -0,0 +1,51 @@ +/// @file platform/window.h +/// @brief Platform-independent window interface. +/// +/// The implementation is chosen at build time by the name of the file: +/// `window_mac.mm` for macOS. A window is created and driven from the main +/// thread only. +#pragma once + +#include "core/types.h" + +namespace kumo { + +/// @brief Opaque window handle; the definition lives in the platform file. +struct Window; + +/// @brief Parameters for window_create(). +struct WindowDesc { + const char* title; ///< UTF-8 window title. + int width; ///< Content width in points. + int height; ///< Content height in points. +}; + +/// @brief Creates a window, shows it and brings it to the front. +/// +/// The first call also sets up the application: its menu (with Cmd+Q) and the +/// rest of what a program needs to be a normal windowed application. +/// +/// @param desc Window parameters. +/// @return The new window, or `nullptr` on failure. +Window* window_create(const WindowDesc& desc); + +/// @brief Closes the window and frees it. Passing `nullptr` is allowed. +void window_destroy(Window* window); + +/// @brief Processes every pending event of the window system without blocking. +/// +/// Call once per frame. +void window_poll_events(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 +/// open until window_destroy(), so the caller decides when to shut down. +bool window_should_close(const Window* window); + +/// @brief Changes the title of the window. +/// +/// @param title UTF-8 text. +void window_set_title(Window* window, const char* title); + +} // namespace kumo diff --git a/src/platform/window_mac.mm b/src/platform/window_mac.mm @@ -0,0 +1,113 @@ +/// @file platform/window_mac.mm +/// @brief macOS (Cocoa) implementation of platform/window.h + +#include "platform/window.h" + +#import <Cocoa/Cocoa.h> + +/// @brief Window delegate that records close requests instead of closing. +@interface KumoWindowDelegate : NSObject <NSWindowDelegate> +@property(nonatomic) BOOL should_close; +@end + +@implementation KumoWindowDelegate +/// Called when the close button is pressed. Returning NO keeps the window +/// open; the main loop sees the flag and shuts down on its own terms. +- (BOOL)windowShouldClose:(NSWindow*)sender { + self.should_close = YES; + return NO; +} +@end + +namespace kumo { + +/// @brief Cocoa state behind the opaque Window handle. +struct Window { + NSWindow* ns_window; + KumoWindowDelegate* delegate; +}; + +/// @brief Creates the application object and a minimal menu with Cmd+Q. +/// +/// A bare executable (not an .app bundle) has no menu and is not a regular +/// foreground application by default; without this it could neither receive +/// keyboard focus nor be quit from the keyboard. +static void init_application() { + [NSApplication sharedApplication]; + [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular]; + + NSMenu* menu_bar = [[NSMenu alloc] init]; + NSMenuItem* app_item = [[NSMenuItem alloc] init]; + [menu_bar addItem:app_item]; + NSMenu* app_menu = [[NSMenu alloc] init]; + [app_menu addItemWithTitle:@"Quit Kumo" action:@selector(terminate:) keyEquivalent:@"q"]; + [app_item setSubmenu:app_menu]; + [NSApp setMainMenu:menu_bar]; + + // Finish launching by hand: events are pumped by window_poll_events() + // instead of [NSApp run], which would take over the main loop. + [NSApp finishLaunching]; +} + +Window* window_create(const WindowDesc& desc) { + static bool application_ready = false; + if (!application_ready) { + init_application(); + application_ready = true; + } + + NSWindowStyleMask style = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | + NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable; + NSRect frame = NSMakeRect(0, 0, desc.width, desc.height); + NSWindow* ns_window = [[NSWindow alloc] initWithContentRect:frame + styleMask:style + backing:NSBackingStoreBuffered + defer:NO]; + if (ns_window == nil) return nullptr; + + KumoWindowDelegate* delegate = [[KumoWindowDelegate alloc] init]; + // We own the reference through Window; AppKit must not release it on close. + [ns_window setReleasedWhenClosed:NO]; + [ns_window setDelegate:delegate]; + [ns_window setTitle:[NSString stringWithUTF8String:desc.title]]; + [ns_window center]; + [ns_window makeKeyAndOrderFront:nil]; + [NSApp activate]; + + return new Window{ns_window, delegate}; +} + +void window_destroy(Window* window) { + if (window == nullptr) return; + [window->ns_window setDelegate:nil]; + [window->ns_window close]; + delete window; +} + +void window_poll_events(Window* window) { + // Keeps the autoreleased event objects from pilling up cross frames. + @autoreleasepool { + for (;;) { + // distantPast: return at once if no event is queued. + NSEvent* event = [NSApp nextEventMatchingMask:NSEventMaskAny + untilDate:[NSDate distantPast] + inMode:NSDefaultRunLoopMode + dequeue:YES]; + if (event == nil) break; + + constexpr unsigned short kEscapeKeyCode = 53; + if (event.type == NSEventTypeKeyDown && event.keyCode == kEscapeKeyCode) { + window->delegate.should_close = YES; + } + [NSApp sendEvent:event]; + } + } +} + +bool window_should_close(const Window* window) { return window->delegate.should_close; } + +void window_set_title(Window* window, const char* title) { + [window->ns_window setTitle:[NSString stringWithUTF8String:title]]; +} + +} // namespace kumo
