Kumo

A hand-rolled game engine.

git clone https://git.sentomk.me/Kumo.git

clock.h (3388B) · raw


      1 /// @file core/clock.h
      2 /// @brief Time as integers, and the clocks that provide it.
      3 #pragma once
      4 
      5 #include "core/types.h"
      6 
      7 namespace kumo {
      8 
      9 /// @brief A span of time, or a point on a clock, in nanoseconds.
     10 ///
     11 /// A plain integer on purpose. Sums and differences of integers are exact and
     12 /// come out the same on every platform, which seconds held in a float stop
     13 /// being after many additions. 64 bits hold about 292 years.
     14 using Nanoseconds = int64_t;
     15 
     16 /// @brief Nanoseconds in a second.
     17 constexpr Nanoseconds kNanosecondsPerSecond = 1'000'000'000;
     18 
     19 /// @brief @p count microseconds as Nanoseconds.
     20 constexpr Nanoseconds microseconds(int64_t count) { return count * 1'000; }
     21 
     22 /// @brief @p count milliseconds as Nanoseconds.
     23 constexpr Nanoseconds milliseconds(int64_t count) { return count * 1'000'000; }
     24 
     25 /// @brief @p count seconds as Nanoseconds.
     26 constexpr Nanoseconds seconds(int64_t count) {
     27   return count * kNanosecondsPerSecond;
     28 }
     29 
     30 /// @brief The time between two steps of a loop that runs @p hz times a second,
     31 ///        rounded to the nearest nanosecond.
     32 ///
     33 /// 60 Hz is 16666667 ns, not exactly 1/60 s; what matters is that the step is
     34 /// the same number every time.
     35 constexpr Nanoseconds step_from_hz(int hz) {
     36   return (kNanosecondsPerSecond + hz / 2) / hz;
     37 }
     38 
     39 /// @brief Converts a time to seconds as a double. For showing and for rendering
     40 ///        only: do not feed the result back into the simulation.
     41 constexpr double to_seconds(Nanoseconds time) {
     42   return static_cast<double>(time) / static_cast<double>(kNanosecondsPerSecond);
     43 }
     44 
     45 /// @brief A source of time. Abstract, so that tests can supply their own.
     46 class Clock {
     47  public:
     48   virtual ~Clock() = default;
     49 
     50   /// @brief The current time. Only the difference between two calls means
     51   ///        something: the starting point is not defined.
     52   virtual Nanoseconds now() = 0;
     53 };
     54 
     55 /// @brief The clock of the machine: it never goes back, and keeps running at
     56 ///        the same rate whatever the user does to the date and time.
     57 class SystemClock final : public Clock {
     58  public:
     59   Nanoseconds now() override;
     60 };
     61 
     62 /// @brief A clock that moves only when told to, for tests.
     63 class ManualClock final : public Clock {
     64  public:
     65   /// @brief Creates a clock that shows @p start.
     66   explicit ManualClock(Nanoseconds start = 0) : time_(start) {}
     67 
     68   Nanoseconds now() override { return time_; }
     69 
     70   /// @brief Moves the clock forward by @p by.
     71   void advance(Nanoseconds by) { time_ += by; }
     72 
     73   /// @brief Sets the clock to @p time, which may be earlier than before.
     74   void set(Nanoseconds time) { time_ = time; }
     75 
     76  private:
     77   Nanoseconds time_;
     78 };
     79 
     80 /// @brief Tells how long each frame of a loop took.
     81 class FrameTimer {
     82  public:
     83   /// @brief Starts timing now.
     84   ///
     85   /// @param clock The clock to read; it must outlive the timer.
     86   explicit FrameTimer(Clock& clock) : clock_(clock), last_(clock.now()) {}
     87 
     88   /// @brief Returns the time since the previous call, or since the timer was
     89   ///        created or reset if there was none.
     90   ///
     91   /// Negative only if the clock went backwards, which SystemClock does not.
     92   Nanoseconds tick();
     93 
     94   /// @brief Forgets the time that has passed: the next tick measures from now.
     95   ///
     96   /// Useful after something slow that should not count, such as loading.
     97   void reset() { last_ = clock_.now(); }
     98 
     99  private:
    100   Clock& clock_;
    101   Nanoseconds last_;
    102 };
    103 
    104 }  // namespace kumo