Kumo
A hand-rolled game engine.
git clone https://git.sentomk.me/Kumo.git
commit 1f1824efb8756c0bce4a5f69a2f83ae43115b56f parent 4197ddd6d4ffd48dbd1e807bcd7c798d57f497c8 Author: sentomk <sentomk@proton.me> Date: Wed, 14 Oct 2026 21:04:32 +0800 feat: add a fixed time step for the simulation FixedStep turns the time of each frame into a number of steps of one fixed length, so that the simulation is the same sequence of equal steps whatever the frame rate is and only the grouping into frames differs. The time that does not make a whole step is kept for the next frame, and alpha() says how far into the next step it is, for drawing between two states. A negative frame counts as no time, and one longer than the maximum counts as the maximum, so that a stall of the process does not start an avalanche of updates that makes the next frame slower still. The tests include two properties that matter for replays and for the pixel for pixel goal: the same total time gives the same steps and the same left over time however it is split into frames, and a simulation whose state depends on every step comes out the same at 24 to 240 frames a second. Diffstat:
| A | src/core/fixed_step.cpp | | | 25 | +++++++++++++++++++++++++ |
| A | src/core/fixed_step.h | | | 81 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | tests/unit/fixed_step_test.cpp | | | 161 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
3 files changed, 267 insertions(+), 0 deletions(-)
diff --git a/src/core/fixed_step.cpp b/src/core/fixed_step.cpp @@ -0,0 +1,25 @@ +#include "core/fixed_step.h" + +#include <algorithm> +#include <cassert> + +namespace kumo { + +FixedStep::FixedStep(Nanoseconds step, Nanoseconds max_frame) + : step_(step), max_frame_(max_frame) { + assert(step > 0); + assert(max_frame >= step); +} + +int FixedStep::advance(Nanoseconds frame_time) { + frame_time = std::clamp(frame_time, Nanoseconds{0}, max_frame_); + left_over_ += frame_time; + + // Whole steps are taken out in one division; no loop is needed. + const Nanoseconds steps = left_over_ / step_; + left_over_ -= steps * step_; + total_steps_ += static_cast<uint64_t>(steps); + return static_cast<int>(steps); +} + +} // namespace kumo diff --git a/src/core/fixed_step.h b/src/core/fixed_step.h @@ -0,0 +1,81 @@ +/// @file core/fixed_step.h +/// @brief Runs the simulation in steps of one fixed length, whatever the frame +/// rate is. +/// +/// A frame can take 4 ms or 40 ms. If the simulation advanced by the time of +/// the frame, the same game would play out differently on a fast and on a slow +/// machine, and replays and tests would not repeat. Instead every update of the +/// simulation takes the same time step, and the loop runs as many of them per +/// frame as the time that passed calls for: +/// @code +/// kumo::SystemClock clock; +/// kumo::FrameTimer timer(clock); +/// kumo::FixedStep step(kumo::step_from_hz(60), kumo::milliseconds(250)); +/// for (;;) { +/// const int updates = step.advance(timer.tick()); +/// for (int i = 0; i < updates; ++i) update(step.step()); +/// render(step.alpha()); +/// } +/// @endcode +/// The simulation is then the same sequence of equal steps on every machine; +/// only how they are grouped into frames differs. +#pragma once + +#include "core/clock.h" +#include "core/types.h" + +namespace kumo { + +/// @brief Turns the time of each frame into a number of fixed steps. +class FixedStep { + public: + /// @brief Creates a stepper. + /// + /// @param step Length of one step; must be positive. + /// @param max_frame The longest frame that is believed. A longer one, such + /// as after the window was dragged or the process was + /// stopped, counts as this long, so that one slow frame does + /// not cause so many updates that the next is slower still. + /// At least @p step. + FixedStep(Nanoseconds step, Nanoseconds max_frame); + + /// @brief Adds the time of one frame, and says how many steps to run. + /// + /// A negative time counts as none, and one beyond the maximum counts as the + /// maximum. The part of the time that does not make a whole step is kept for + /// the next call. + /// + /// @param frame_time Time since the previous frame. + /// @return The number of steps to run now, possibly 0. + int advance(Nanoseconds frame_time); + + /// @brief The length of one step. + Nanoseconds step() const { return step_; } + + /// @brief How far the time is into the next step, from 0 up to but not + /// including 1. Rendering can use it to draw between the last two + /// states of the simulation. + double alpha() const { + return static_cast<double>(left_over_) / static_cast<double>(step_); + } + + /// @brief The time that was added but is not yet part of a step. + Nanoseconds left_over() const { return left_over_; } + + /// @brief How many steps have been returned in total. + uint64_t total_steps() const { return total_steps_; } + + /// @brief The time the simulation has run for: the steps so far, times the + /// length of a step. + Nanoseconds simulated_time() const { + return static_cast<Nanoseconds>(total_steps_) * step_; + } + + private: + Nanoseconds step_; + Nanoseconds max_frame_; + Nanoseconds left_over_ = 0; + uint64_t total_steps_ = 0; +}; + +} // namespace kumo diff --git a/tests/unit/fixed_step_test.cpp b/tests/unit/fixed_step_test.cpp @@ -0,0 +1,161 @@ +#include "core/fixed_step.h" + +#include <algorithm> + +#include "check.h" +#include "core/clock.h" +#include "core/types.h" + +using kumo::FixedStep; +using kumo::ManualClock; +using kumo::milliseconds; +using kumo::Nanoseconds; +using kumo::seconds; + +namespace { + +// A small deterministic random number generator, as in raster_test.cpp: the +// standard distributions give different numbers in different libraries. +struct Random { + uint64_t state; + uint32_t next() { + state = state * 6364136223846793005ull + 1442695040888963407ull; + return static_cast<uint32_t>(state >> 33); + } + // A value in [low, high]. + Nanoseconds between(Nanoseconds low, Nanoseconds high) { + return low + static_cast<Nanoseconds>( + next() % static_cast<uint64_t>(high - low + 1)); + } +}; + +// One update of a made up simulation. Any state that depends on every step +// in order would do; this one cannot be right by accident. +uint64_t update(uint64_t state) { + return state * 6364136223846793005ull + 1442695040888963407ull; +} + +// Runs a simulation for `total` of time, fed to the stepper in frames of +// `frame` (the last one is whatever is left), and returns its final state. +uint64_t simulate(Nanoseconds frame, Nanoseconds total) { + FixedStep stepper(kumo::step_from_hz(60), milliseconds(250)); + uint64_t state = 1; + Nanoseconds remaining = total; + while (remaining > 0) { + const Nanoseconds this_frame = std::min(frame, remaining); + remaining -= this_frame; + for (int i = stepper.advance(this_frame); i > 0; --i) state = update(state); + } + return state; +} + +} // namespace + +TEST(a_frame_of_exactly_one_step_runs_one_step) { + FixedStep stepper(milliseconds(10), milliseconds(100)); + CHECK_EQ(stepper.advance(milliseconds(10)), 1); + CHECK_EQ(stepper.left_over(), 0); +} + +TEST(a_longer_frame_runs_several_steps_and_keeps_the_rest) { + FixedStep stepper(milliseconds(10), milliseconds(100)); + CHECK_EQ(stepper.advance(milliseconds(25)), 2); + CHECK_EQ(stepper.left_over(), milliseconds(5)); + CHECK_EQ(stepper.advance(milliseconds(5)), 1); // the 5 ms kept plus these + CHECK_EQ(stepper.left_over(), 0); +} + +TEST(short_frames_run_nothing_until_they_add_up) { + FixedStep stepper(milliseconds(10), milliseconds(100)); + CHECK_EQ(stepper.advance(milliseconds(4)), 0); + CHECK_EQ(stepper.advance(milliseconds(4)), 0); + CHECK_EQ(stepper.advance(milliseconds(4)), 1); + CHECK_EQ(stepper.left_over(), milliseconds(2)); +} + +TEST(alpha_is_how_far_into_the_next_step_the_time_is) { + FixedStep stepper(milliseconds(10), milliseconds(100)); + CHECK(stepper.alpha() == 0.0); + stepper.advance(milliseconds(15)); + CHECK(stepper.alpha() == 0.5); + stepper.advance(milliseconds(5)); + CHECK(stepper.alpha() == 0.0); +} + +TEST(a_negative_frame_counts_as_no_time) { + FixedStep stepper(milliseconds(10), milliseconds(100)); + CHECK_EQ(stepper.advance(milliseconds(-5)), 0); + CHECK_EQ(stepper.left_over(), 0); + CHECK_EQ(stepper.advance(milliseconds(10)), 1); +} + +TEST(a_very_long_frame_counts_as_the_longest_that_is_believed) { + FixedStep stepper(milliseconds(10), milliseconds(100)); + CHECK_EQ(stepper.advance(seconds(10)), 10); // 100 ms of steps, not 10 s + CHECK_EQ(stepper.left_over(), 0); +} + +TEST(a_stall_does_not_start_an_avalanche) { + FixedStep stepper(milliseconds(10), milliseconds(100)); + stepper.advance(seconds(5)); // the process was stopped for a while + // The next frames are ordinary: one step each, not a long catching up. + CHECK_EQ(stepper.advance(milliseconds(10)), 1); + CHECK_EQ(stepper.advance(milliseconds(10)), 1); +} + +TEST(the_totals_count_every_step_that_was_run) { + FixedStep stepper(milliseconds(10), milliseconds(100)); + stepper.advance(milliseconds(25)); + stepper.advance(milliseconds(50)); + CHECK_EQ(stepper.total_steps(), 7u); + CHECK_EQ(stepper.simulated_time(), milliseconds(70)); +} + +TEST(the_same_total_time_gives_the_same_steps_however_it_is_split) { + const Nanoseconds total = seconds(10); + const Nanoseconds step = kumo::step_from_hz(60); + const uint64_t expected = static_cast<uint64_t>(total / step); + const Nanoseconds expected_left_over = total - total / step * step; + + Random random = {2024}; + int wrong = 0; + for (int round = 0; round < 200; ++round) { + FixedStep stepper(step, milliseconds(250)); + Nanoseconds remaining = total; + while (remaining > 0) { + // Frames of 1 microsecond to 200 ms, none longer than the maximum. + const Nanoseconds frame = std::min( + random.between(kumo::microseconds(1), milliseconds(200)), remaining); + stepper.advance(frame); + remaining -= frame; + } + if (stepper.total_steps() != expected) ++wrong; + if (stepper.left_over() != expected_left_over) ++wrong; + } + CHECK_EQ(wrong, 0); +} + +TEST(a_simulation_comes_out_the_same_at_any_frame_rate) { + const Nanoseconds total = seconds(5); + const uint64_t at_60 = simulate(kumo::step_from_hz(60), total); + CHECK(at_60 != 1); // it did run + CHECK(simulate(kumo::step_from_hz(24), total) == at_60); + CHECK(simulate(kumo::step_from_hz(30), total) == at_60); + CHECK(simulate(kumo::step_from_hz(144), total) == at_60); + CHECK(simulate(kumo::step_from_hz(240), total) == at_60); + CHECK(simulate(milliseconds(7), total) == at_60); + CHECK(simulate(kumo::microseconds(137), total) == at_60); +} + +TEST(a_manual_clock_drives_the_loop_exactly) { + ManualClock clock; + kumo::FrameTimer timer(clock); + FixedStep stepper(milliseconds(10), milliseconds(100)); + int steps = 0; + for (int frame = 0; frame < 100; ++frame) { + clock.advance(milliseconds(7)); // 700 ms in all + steps += stepper.advance(timer.tick()); + } + CHECK_EQ(steps, 70); + CHECK_EQ(stepper.left_over(), 0); +}
