Kumo

A hand-rolled game engine.

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

fixed_step.h (3203B) · raw


      1 /// @file core/fixed_step.h
      2 /// @brief Runs the simulation in steps of one fixed length, whatever the frame
      3 ///        rate is.
      4 ///
      5 /// A frame can take 4 ms or 40 ms. If the simulation advanced by the time of
      6 /// the frame, the same game would play out differently on a fast and on a slow
      7 /// machine, and replays and tests would not repeat. Instead every update of the
      8 /// simulation takes the same time step, and the loop runs as many of them per
      9 /// frame as the time that passed calls for:
     10 /// @code
     11 ///   kumo::SystemClock clock;
     12 ///   kumo::FrameTimer timer(clock);
     13 ///   kumo::FixedStep step(kumo::step_from_hz(60), kumo::milliseconds(250));
     14 ///   for (;;) {
     15 ///     const int updates = step.advance(timer.tick());
     16 ///     for (int i = 0; i < updates; ++i) update(step.step());
     17 ///     render(step.alpha());
     18 ///   }
     19 /// @endcode
     20 /// The simulation is then the same sequence of equal steps on every machine;
     21 /// only how they are grouped into frames differs.
     22 #pragma once
     23 
     24 #include "core/clock.h"
     25 #include "core/fixed.h"
     26 #include "core/types.h"
     27 
     28 namespace kumo {
     29 
     30 /// @brief Turns the time of each frame into a number of fixed steps.
     31 class FixedStep {
     32  public:
     33   /// @brief Creates a stepper.
     34   ///
     35   /// @param step      Length of one step; must be positive.
     36   /// @param max_frame The longest frame that is believed. A longer one, such
     37   ///                  as after the window was dragged or the process was
     38   ///                  stopped, counts as this long, so that one slow frame does
     39   ///                  not cause so many updates that the next is slower still.
     40   ///                  At least @p step.
     41   FixedStep(Nanoseconds step, Nanoseconds max_frame);
     42 
     43   /// @brief Adds the time of one frame, and says how many steps to run.
     44   ///
     45   /// A negative time counts as none, and one beyond the maximum counts as the
     46   /// maximum. The part of the time that does not make a whole step is kept for
     47   /// the next call.
     48   ///
     49   /// @param frame_time Time since the previous frame.
     50   /// @return The number of steps to run now, possibly 0.
     51   int advance(Nanoseconds frame_time);
     52 
     53   /// @brief The length of one step.
     54   Nanoseconds step() const { return step_; }
     55 
     56   /// @brief How far the time is into the next step, from 0 up to but not
     57   ///        including 1. Rendering can use it to draw between the last two
     58   ///        states of the simulation.
     59   double alpha() const {
     60     return static_cast<double>(left_over_) / static_cast<double>(step_);
     61   }
     62 
     63   /// @brief Same as alpha(), as a Fixed in [0, 1), ready for lerp().
     64   Fixed alpha_fixed() const { return Fixed::ratio(left_over_, step_); }
     65 
     66   /// @brief The time that was added but is not yet part of a step.
     67   Nanoseconds left_over() const { return left_over_; }
     68 
     69   /// @brief How many steps have been returned in total.
     70   uint64_t total_steps() const { return total_steps_; }
     71 
     72   /// @brief The time the simulation has run for: the steps so far, times the
     73   ///        length of a step.
     74   Nanoseconds simulated_time() const {
     75     return static_cast<Nanoseconds>(total_steps_) * step_;
     76   }
     77 
     78  private:
     79   Nanoseconds step_;
     80   Nanoseconds max_frame_;
     81   Nanoseconds left_over_ = 0;
     82   uint64_t total_steps_ = 0;
     83 };
     84 
     85 }  // namespace kumo