aboutsummaryrefslogtreecommitdiff
path: root/simloop/include/simloop.h
blob: 4e3ed20185c2b673a25675a35e1bf51f703b481a (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
41
42
43
#pragma once

#include <stdint.h>

typedef uint64_t simloop_time_t;

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 render_elapsed; ///< Amount of time elapsed in the rendering.
  simloop_time_t update_elapsed; ///< Amount of time elapsed in the simulation.
  simloop_time_t update_dt;      ///< Delta time for simulation updates.
  double         percent_frame;  ///< Percent progress between this frame and
                                 ///< the next. Used for smooth animation.
  bool should_update;            ///< Whether the simulation should update.
  bool should_render;            ///< Whether the simulation should be rendered.
} 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.
  SimloopTimeline render; ///< Render timeline.
  double          percent_frame;
  bool            first_iter;
} 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*);