aboutsummaryrefslogtreecommitdiff
path: root/simloop/include/simloop.h
blob: 6a83b23357639f7a272134bf1688042e7b783686 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#pragma once

#include <stdint.h>

typedef uint64_t simloop_time_t; ///< Time delta in nanoseconds.

typedef struct SimloopArgs {
  int update_fps;     ///< Update frame rate. Must be >0.
  int max_render_fps; ///< Render frame rate cap. 0 to disable.
} SimloopArgs;

typedef struct SimloopOut {
  uint64_t       frame;          ///< Frame counter.
  simloop_time_t update_elapsed; ///< Amount of time elapsed in the simulation.
  simloop_time_t update_dt;      ///< Delta time for simulation updates.
  simloop_time_t throttle;      ///< Render throttle if max render fps is given.
  double         percent_frame; ///< Percent progress between this frame and
                                ///< the next. Used for smooth animation.
  bool should_update;           ///< Whether the simulation should update.
} SimloopOut;

typedef struct SimloopTimeline {
  simloop_time_t ddt;  ///< Desired delta time.
  simloop_time_t time; ///< Time point of the last simulation step.
} SimloopTimeline;

typedef struct Simloop {
  simloop_time_t  clock;      ///< Tracks simulation time.
  uint64_t        frame;      ///< Frame counter, number of updates done.
  SimloopTimeline update;     ///< Update timeline.
  simloop_time_t  render_ddt; ///< Desired render delta time.
} Simloop;

/// Create a simulation loop.
Simloop simloop_make(const SimloopArgs*);

/// Step the simulation loop.
///
/// The simulation always triggers a render of the initial state of simulation.
void simloop_update(Simloop*, simloop_time_t dt, SimloopOut*);