/// @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(time) / static_cast(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