Kumo
A hand-rolled game engine.
git clone https://git.sentomk.me/Kumo.git
commit 4197ddd6d4ffd48dbd1e807bcd7c798d57f497c8 parent d127e64c75020dd498723de9ee4e93ea2a1bbeca Author: sentomk <sentomk@proton.me> Date: Wed, 14 Oct 2026 20:54:33 +0800 feat: add integer time, clocks and a frame timer Time is kept as an int64 count of nanoseconds, Nanoseconds, so that sums and differences are exact and the same on every platform, which seconds in a float would not be after many additions. milliseconds(), seconds() and step_from_hz() build it; to_seconds() goes the other way, for showing and rendering only. Clock is an abstract source of time. SystemClock wraps the steady clock of the standard library, which never goes back; ManualClock moves only when told to, so that tests do not have to wait. FrameTimer tells how long each frame of a loop took, and can be reset after something slow that should not count. The tests cover the units and the rounding of a step from a rate, both clocks, and the frame timer including a clock that goes backwards. Diffstat:
| A | src/core/clock.cpp | | | 20 | ++++++++++++++++++++ |
| A | src/core/clock.h | | | 104 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | tests/unit/clock_test.cpp | | | 92 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
3 files changed, 216 insertions(+), 0 deletions(-)
diff --git a/src/core/clock.cpp b/src/core/clock.cpp @@ -0,0 +1,20 @@ +#include "core/clock.h" + +#include <chrono> + +namespace kumo { + +Nanoseconds SystemClock::now() { + const auto since_start = std::chrono::steady_clock::now().time_since_epoch(); + return std::chrono::duration_cast<std::chrono::nanoseconds>(since_start) + .count(); +} + +Nanoseconds FrameTimer::tick() { + const Nanoseconds now = clock_.now(); + const Nanoseconds elapsed = now - last_; + last_ = now; + return elapsed; +} + +} // namespace kumo diff --git a/src/core/clock.h b/src/core/clock.h @@ -0,0 +1,104 @@ +/// @file core/clock.h +/// @brief Time as integers, and the clocks that provide it. +#pragma once + +#include "core/types.h" + +namespace kumo { + +/// @brief A span of time, or a point on a clock, in nanoseconds. +/// +/// A plain integer on purpose. Sums and differences of integers are exact and +/// come out the same on every platform, which seconds held in a float stop +/// being after many additions. 64 bits hold about 292 years. +using Nanoseconds = int64_t; + +/// @brief Nanoseconds in a second. +constexpr Nanoseconds kNanosecondsPerSecond = 1'000'000'000; + +/// @brief @p count microseconds as Nanoseconds. +constexpr Nanoseconds microseconds(int64_t count) { return count * 1'000; } + +/// @brief @p count milliseconds as Nanoseconds. +constexpr Nanoseconds milliseconds(int64_t count) { return count * 1'000'000; } + +/// @brief @p count seconds as Nanoseconds. +constexpr Nanoseconds seconds(int64_t count) { + return count * kNanosecondsPerSecond; +} + +/// @brief The time between two steps of a loop that runs @p hz times a second, +/// rounded to the nearest nanosecond. +/// +/// 60 Hz is 16666667 ns, not exactly 1/60 s; what matters is that the step is +/// the same number every time. +constexpr Nanoseconds step_from_hz(int hz) { + return (kNanosecondsPerSecond + hz / 2) / hz; +} + +/// @brief Converts a time to seconds as a double. For showing and for rendering +/// only: do not feed the result back into the simulation. +constexpr double to_seconds(Nanoseconds time) { + return static_cast<double>(time) / static_cast<double>(kNanosecondsPerSecond); +} + +/// @brief A source of time. Abstract, so that tests can supply their own. +class Clock { + public: + virtual ~Clock() = default; + + /// @brief The current time. Only the difference between two calls means + /// something: the starting point is not defined. + virtual Nanoseconds now() = 0; +}; + +/// @brief The clock of the machine: it never goes back, and keeps running at +/// the same rate whatever the user does to the date and time. +class SystemClock final : public Clock { + public: + Nanoseconds now() override; +}; + +/// @brief A clock that moves only when told to, for tests. +class ManualClock final : public Clock { + public: + /// @brief Creates a clock that shows @p start. + explicit ManualClock(Nanoseconds start = 0) : time_(start) {} + + Nanoseconds now() override { return time_; } + + /// @brief Moves the clock forward by @p by. + void advance(Nanoseconds by) { time_ += by; } + + /// @brief Sets the clock to @p time, which may be earlier than before. + void set(Nanoseconds time) { time_ = time; } + + private: + Nanoseconds time_; +}; + +/// @brief Tells how long each frame of a loop took. +class FrameTimer { + public: + /// @brief Starts timing now. + /// + /// @param clock The clock to read; it must outlive the timer. + explicit FrameTimer(Clock& clock) : clock_(clock), last_(clock.now()) {} + + /// @brief Returns the time since the previous call, or since the timer was + /// created or reset if there was none. + /// + /// Negative only if the clock went backwards, which SystemClock does not. + Nanoseconds tick(); + + /// @brief Forgets the time that has passed: the next tick measures from now. + /// + /// Useful after something slow that should not count, such as loading. + void reset() { last_ = clock_.now(); } + + private: + Clock& clock_; + Nanoseconds last_; +}; + +} // namespace kumo diff --git a/tests/unit/clock_test.cpp b/tests/unit/clock_test.cpp @@ -0,0 +1,92 @@ +#include "core/clock.h" + +#include <chrono> +#include <thread> + +#include "check.h" + +using kumo::Clock; +using kumo::FrameTimer; +using kumo::ManualClock; +using kumo::milliseconds; +using kumo::Nanoseconds; +using kumo::SystemClock; + +TEST(time_units_convert_exactly) { + CHECK_EQ(kumo::microseconds(3), 3'000); + CHECK_EQ(milliseconds(16), 16'000'000); + CHECK_EQ(kumo::seconds(2), 2'000'000'000); + CHECK(kumo::to_seconds(milliseconds(1500)) == 1.5); +} + +TEST(a_step_from_a_rate_is_rounded_to_the_nearest_nanosecond) { + CHECK_EQ(kumo::step_from_hz(60), 16'666'667); + CHECK_EQ(kumo::step_from_hz(120), 8'333'333); + CHECK_EQ(kumo::step_from_hz(100), 10'000'000); + CHECK_EQ(kumo::step_from_hz(1), 1'000'000'000); +} + +TEST(a_manual_clock_moves_only_when_told) { + ManualClock clock(100); + CHECK_EQ(clock.now(), 100); + CHECK_EQ(clock.now(), 100); + clock.advance(5); + CHECK_EQ(clock.now(), 105); + clock.set(7); + CHECK_EQ(clock.now(), 7); +} + +TEST(a_frame_timer_returns_the_time_between_calls) { + ManualClock clock(1000); + FrameTimer timer(clock); + clock.advance(milliseconds(16)); + CHECK_EQ(timer.tick(), milliseconds(16)); + clock.advance(milliseconds(5)); + CHECK_EQ(timer.tick(), milliseconds(5)); + CHECK_EQ(timer.tick(), 0); // nothing passed since the last call +} + +TEST(a_frame_timer_measures_from_the_moment_it_was_made) { + ManualClock clock(500); + clock.advance(milliseconds(3)); + FrameTimer timer(clock); + clock.advance(2); + CHECK_EQ(timer.tick(), 2); +} + +TEST(resetting_a_frame_timer_forgets_the_time_that_passed) { + ManualClock clock; + FrameTimer timer(clock); + clock.advance(milliseconds(900)); // for example, loading + timer.reset(); + clock.advance(milliseconds(4)); + CHECK_EQ(timer.tick(), milliseconds(4)); +} + +TEST(a_frame_timer_shows_a_clock_that_went_backwards) { + ManualClock clock(100); + FrameTimer timer(clock); + clock.set(40); + CHECK_EQ(timer.tick(), -60); +} + +TEST(the_system_clock_never_goes_back) { + SystemClock system; + Clock& clock = system; // through the interface, as the loop will use it + Nanoseconds previous = clock.now(); + int went_back = 0; + for (int i = 0; i < 100000; ++i) { + const Nanoseconds now = clock.now(); + if (now < previous) ++went_back; + previous = now; + } + CHECK_EQ(went_back, 0); +} + +TEST(the_system_clock_follows_real_time) { + SystemClock clock; + const Nanoseconds before = clock.now(); + std::this_thread::sleep_for(std::chrono::milliseconds(20)); + // Generous: only that it moved by about as much as was slept. + CHECK(clock.now() - before >= milliseconds(15)); +}
