Kumo

A hand-rolled game engine.

git clone https://git.sentomk.me/Kumo.git
commit 29525b91887d0cdef5b2b85c97b941e11bdd72bc
parent a756ffc358e3d76b534d5b58ca5a673c137597d2
Author: sentomk <sentomk@proton.me>
Date:   Thu, 19 Nov 2026 18:47:37 +0800

feat: add headless runtime loop and wire demo game

Diffstat:
M.clang-format | 1+
Msrc/core/fixed_step.h | 4++++
Msrc/main.cpp | 87++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------
Msrc/runtime/game.h | 24++++++++++++++++++------
Asrc/runtime/loop.cpp | 28++++++++++++++++++++++++++++
Asrc/runtime/loop.h | 59+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atests/unit/loop_test.cpp | 140+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7 files changed, 312 insertions(+), 31 deletions(-)

diff --git a/.clang-format b/.clang-format @@ -1,3 +1,4 @@ BasedOnStyle: Google DerivePointerAlignment: false PointerAlignment: Left +AlignTrailingComments: true diff --git a/src/core/fixed_step.h b/src/core/fixed_step.h @@ -22,6 +22,7 @@ #pragma once #include "core/clock.h" +#include "core/fixed.h" #include "core/types.h" namespace kumo { @@ -59,6 +60,9 @@ class FixedStep { return static_cast<double>(left_over_) / static_cast<double>(step_); } + /// @brief Same as alpha(), as a Fixed in [0, 1), ready for lerp(). + Fixed alpha_fixed() const { return Fixed::ratio(left_over_, step_); } + /// @brief The time that was added but is not yet part of a step. Nanoseconds left_over() const { return left_over_; } diff --git a/src/main.cpp b/src/main.cpp @@ -2,28 +2,68 @@ #include <cstdlib> #include "core/clock.h" -#include "core/fixed_step.h" +#include "core/fixed.h" #include "platform/window.h" #include "render/frame_buffer.h" #include "render/raster.h" +#include "runtime/game.h" +#include "runtime/loop.h" -/// @brief Draws a test picture: a gradient, a triangle and a frame around it. -static void draw_scene(kumo::FrameBuffer& frame) { - for (int y = 0; y < frame.height; ++y) { - for (int x = 0; x < frame.width; ++x) { - frame.at(x, y) = kumo::make_color( - static_cast<uint8_t>(x * 255 / (frame.width - 1)), - static_cast<uint8_t>(y * 255 / (frame.height - 1)), 96); +namespace { + +constexpr int kScreenWidth = 320; +constexpr int kScreenHeight = 180; +constexpr int kBoxSize = 20; + +/// @brief Minimal bouncing-box demo exercising fixed updates and interpolation. +class DemoGame final : public kumo::Game { + public: + void fixed_update(const kumo::Tick& /*tick*/) override { + 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_; } } - kumo::fill_triangle(frame, kumo::pixel_point(160, 30), - kumo::pixel_point(240, 150), kumo::pixel_point(80, 150), - kumo::make_color(250, 220, 60)); - kumo::draw_line(frame, 0, 0, frame.width - 1, frame.height - 1, - kumo::make_color(255, 255, 255)); - kumo::draw_line(frame, 0, frame.height - 1, frame.width - 1, 0, - kumo::make_color(0, 0, 0)); -} + + void render(kumo::FrameBuffer& frame, kumo::Fixed alpha) const override { + kumo::clear(frame, kumo::make_color(24, 28, 40)); + + const int draw_x = kumo::lerp(prev_x_, x_, alpha).round(); + const int draw_y = kumo::lerp(prev_y_, y_, alpha).round(); + kumo::fill_rect(frame, draw_x, draw_y, kBoxSize, kBoxSize, + kumo::make_color(250, 210, 70)); + } + + private: + kumo::Fixed x_ = kumo::Fixed::from_int(40); + kumo::Fixed y_ = kumo::Fixed::from_int(40); + kumo::Fixed prev_x_ = x_; + kumo::Fixed prev_y_ = y_; + kumo::Fixed vx_ = kumo::Fixed::ratio(5, 2); // 2.5 px/tick + kumo::Fixed vy_ = kumo::Fixed::ratio(7, 4); // 1.75 px/tick +}; + +} // namespace int main() { kumo::Window* window = kumo::window_create({"Kumo", 1280, 720}); @@ -32,21 +72,18 @@ int main() { return EXIT_FAILURE; } - kumo::FrameBuffer frame(320, 180); - draw_scene(frame); - + kumo::FrameBuffer frame(kScreenWidth, kScreenHeight); + DemoGame game; kumo::SystemClock clock; - kumo::FrameTimer timer(clock); - kumo::FixedStep step(kumo::step_from_hz(60), kumo::milliseconds(250)); + kumo::Loop loop(clock); 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; + loop.step_frame(game, frame); + + const int64_t seconds = loop.simulated_time() / kumo::kNanosecondsPerSecond; if (seconds != shown_seconds) { shown_seconds = seconds; char title[64]; diff --git a/src/runtime/game.h b/src/runtime/game.h @@ -1,29 +1,41 @@ +/// @file runtime/game.h +/// @brief The Tick value and the Game interface driven by the runtime loop. #pragma once #include "core/clock.h" +#include "core/fixed.h" #include "core/types.h" namespace kumo { struct FrameBuffer; -/// Information about one fixed simulation update. +/// @brief Information about one fixed simulation update. struct Tick { uint64_t index; ///< Zero-based index of the update being executed. Nanoseconds dt; ///< Fixed duration of this update. + + /// @brief Duration of this update in seconds, as a deterministic Fixed. + constexpr Fixed dt_seconds() const { + return Fixed::ratio(dt, kNanosecondsPerSecond); + } }; -/// Game logic driven by the engine's main loop. +/// @brief Game logic driven by the engine's main loop. +/// /// The caller owns the game and keeps it alive while running. class Game { public: virtual ~Game() = default; - /// Advances the simulation by exactly one fixed step. + /// @brief Advances the simulation by exactly one fixed step. virtual void fixed_update(const Tick& tick) = 0; - /// Draws the game without advancing the simulation. - /// alpha is the interpolation fraction in [0, 1). - virtual void render(FrameBuffer& frame, double alpha) const = 0; + /// @brief Draws the game without advancing the simulation. + /// + /// @param frame Pixel buffer to draw into. + /// @param alpha Interpolation fraction in [0, 1), between the last two ticks. + virtual void render(FrameBuffer& frame, Fixed alpha) const = 0; }; + } // namespace kumo diff --git a/src/runtime/loop.cpp b/src/runtime/loop.cpp @@ -0,0 +1,28 @@ +#include "runtime/loop.h" + +namespace kumo { + +Loop::Loop(Clock& clock, LoopConfig config) + : timer_(clock), stepper_(config.step, config.max_frame) {} + +FrameReport Loop::step_frame(Game& game, FrameBuffer& frame) { + const int updates = stepper_.advance(timer_.tick()); + for (int i = 0; i < updates; ++i) { + const Tick tick = {next_tick_index_, stepper_.step()}; + game.fixed_update(tick); + ++next_tick_index_; + } + const Fixed alpha = stepper_.alpha_fixed(); + game.render(frame, alpha); + return {updates, next_tick_index_, alpha}; +} + +void Loop::step_ticks(Game& game, uint64_t count) { + for (uint64_t i = 0; i < count; ++i) { + const Tick tick = {next_tick_index_, stepper_.step()}; + game.fixed_update(tick); + ++next_tick_index_; + } +} + +} // namespace kumo diff --git a/src/runtime/loop.h b/src/runtime/loop.h @@ -0,0 +1,59 @@ +/// @file runtime/loop.h +/// @brief Drives a Game on a fixed simulation step, without owning a window. +#pragma once + +#include "core/clock.h" +#include "core/fixed.h" +#include "core/fixed_step.h" +#include "core/types.h" +#include "runtime/game.h" + +namespace kumo { + +/// @brief Settings for Loop. +struct LoopConfig { + Nanoseconds step = step_from_hz(60); ///< Fixed update duration. + Nanoseconds max_frame = milliseconds(250); ///< Longest frame accepted. +}; + +/// @brief Outcome of one rendered frame. +struct FrameReport { + int ticks_run = 0; ///< Fixed updates executed during this frame. + uint64_t total_ticks = 0; ///< Total fixed updates executed since start. + Fixed alpha; ///< Interpolation fraction passed to render(). +}; + +/// @brief Connects a Clock and a FixedStep to a Game. +/// +/// Does not own a Window, so the same code drives both the windowed app +/// (with SystemClock) and headless tests or replays (with ManualClock). +class Loop { + public: + /// @brief Starts a loop reading frame durations from @p clock. + /// + /// @param clock Time source; must outlive the loop. + /// @param config Step length and frame clamp. + explicit Loop(Clock& clock, LoopConfig config = {}); + + /// @brief Measures elapsed time from the clock, runs 0 or more fixed updates, + /// and renders one frame into @p frame. + FrameReport step_frame(Game& game, FrameBuffer& frame); + + /// @brief Runs exactly @p count fixed updates without consulting the clock + /// or rendering. Useful for deterministic tests and replays. + void step_ticks(Game& game, uint64_t count); + /// @brief Forgets elapsed time since the last frame (e.g. after loading). + void reset_timer() { timer_.reset(); } + uint64_t total_ticks() const { return next_tick_index_; } + Nanoseconds step() const { return stepper_.step(); } + Nanoseconds simulated_time() const { + return static_cast<Nanoseconds>(next_tick_index_) * stepper_.step(); + } + + private: + FrameTimer timer_; + FixedStep stepper_; + uint64_t next_tick_index_ = 0; +}; + +} // namespace kumo diff --git a/tests/unit/loop_test.cpp b/tests/unit/loop_test.cpp @@ -0,0 +1,140 @@ +#include "runtime/loop.h" + +#include <vector> + +#include "check.h" +#include "core/clock.h" +#include "core/fixed.h" +#include "core/types.h" +#include "render/frame_buffer.h" +#include "runtime/game.h" + +using kumo::Fixed; +using kumo::FrameBuffer; +using kumo::FrameReport; +using kumo::Game; +using kumo::Loop; +using kumo::LoopConfig; +using kumo::ManualClock; +using kumo::milliseconds; +using kumo::Nanoseconds; +using kumo::seconds; +using kumo::Tick; + +namespace { + +class RecordingGame final : public Game { + public: + void fixed_update(const Tick& tick) override { ticks.push_back(tick); } + + void render(FrameBuffer& /*frame*/, Fixed alpha) const override { + ++render_count; + last_alpha = alpha; + } + + std::vector<Tick> ticks; + mutable int render_count = 0; + mutable Fixed last_alpha; +}; + +} // namespace + +TEST(step_frame_runs_zero_one_and_many_ticks_with_sequential_indices) { + ManualClock clock(0); + Loop loop(clock, {milliseconds(10), milliseconds(100)}); + RecordingGame game; + FrameBuffer frame(8, 8); + + // Shorter than one step: 0 updates, still renders once. + clock.advance(milliseconds(4)); + const FrameReport r0 = loop.step_frame(game, frame); + CHECK_EQ(r0.ticks_run, 0); + CHECK_EQ(r0.total_ticks, 0u); + CHECK_EQ(game.ticks.size(), 0u); + CHECK_EQ(game.render_count, 1); + + // Crosses one step boundary (4 + 6 = 10 ms): tick 0. + clock.advance(milliseconds(6)); + const FrameReport r1 = loop.step_frame(game, frame); + CHECK_EQ(r1.ticks_run, 1); + CHECK_EQ(r1.total_ticks, 1u); + CHECK_EQ(game.ticks.size(), 1u); + CHECK_EQ(game.ticks[0].index, 0u); + CHECK_EQ(game.ticks[0].dt, milliseconds(10)); + + // Long frame (25 ms): ticks 1 and 2, with 5 ms left over. + clock.advance(milliseconds(25)); + const FrameReport r2 = loop.step_frame(game, frame); + CHECK_EQ(r2.ticks_run, 2); + CHECK_EQ(r2.total_ticks, 3u); + CHECK_EQ(game.ticks.size(), 3u); + CHECK_EQ(game.ticks[1].index, 1u); + CHECK_EQ(game.ticks[2].index, 2u); + CHECK_EQ(loop.simulated_time(), milliseconds(30)); +} + +TEST(step_frame_passes_fixed_alpha_to_render) { + ManualClock clock(0); + Loop loop(clock, {milliseconds(10), milliseconds(100)}); + RecordingGame game; + FrameBuffer frame(8, 8); + + clock.advance(milliseconds(15)); + const FrameReport report = loop.step_frame(game, frame); + CHECK_EQ(report.alpha, Fixed::ratio(1, 2)); + CHECK_EQ(game.last_alpha, Fixed::ratio(1, 2)); + + clock.advance(milliseconds(5)); + const FrameReport report2 = loop.step_frame(game, frame); + CHECK_EQ(report2.alpha, Fixed::from_int(0)); + CHECK_EQ(game.last_alpha, Fixed::from_int(0)); +} + +TEST(step_frame_clamps_long_frames_to_max_frame) { + ManualClock clock(0); + Loop loop(clock, {milliseconds(10), milliseconds(50)}); + RecordingGame game; + FrameBuffer frame(8, 8); + + clock.advance(seconds(3)); + const FrameReport report = loop.step_frame(game, frame); + CHECK_EQ(report.ticks_run, 5); + CHECK_EQ(game.ticks.size(), 5u); + for (size_t i = 0; i < game.ticks.size(); ++i) { + CHECK_EQ(game.ticks[i].index, static_cast<uint64_t>(i)); + } +} + +TEST(step_ticks_advances_without_using_the_clock_or_rendering) { + ManualClock clock(0); + Loop loop(clock, {milliseconds(10), milliseconds(100)}); + RecordingGame game; + + loop.step_ticks(game, 4); + CHECK_EQ(game.ticks.size(), 4u); + CHECK_EQ(game.render_count, 0); + CHECK_EQ(loop.total_ticks(), 4u); + CHECK_EQ(loop.simulated_time(), milliseconds(40)); + CHECK_EQ(game.ticks[0].index, 0u); + CHECK_EQ(game.ticks[3].index, 3u); +} + +TEST(reset_timer_discards_elapsed_time) { + ManualClock clock(0); + Loop loop(clock, {milliseconds(10), milliseconds(100)}); + RecordingGame game; + FrameBuffer frame(8, 8); + + clock.advance(seconds(2)); // e.g. slow loading pause + loop.reset_timer(); + clock.advance(milliseconds(10)); + + const FrameReport report = loop.step_frame(game, frame); + CHECK_EQ(report.ticks_run, 1); + CHECK_EQ(game.ticks.size(), 1u); +} + +TEST(tick_dt_seconds_matches_fixed_ratio) { + const Tick tick = {0, milliseconds(250)}; + CHECK_EQ(tick.dt_seconds(), Fixed::ratio(1, 4)); +}