aboutsummaryrefslogtreecommitdiff
path: root/simloop/include/simloop.h
blob: f267d404422fc270d95a8b533c280ef88718c191 (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
44
/* Simulation loop module.
 *
 * This implements a simulation loop but in a way that the client retains
 * control flow. The client steps the loop and then checks whether the
 * simulation must be updated and/or the result rendered.
 */
#pragma once

#include <timer.h>

#include <stdint.h>

typedef struct SimloopArgs {
  int    update_fps;     ///< Update frame rate. Must be >0.
  int    max_render_fps; ///< Render frame rate cap. 0 to disable.
  Timer* timer;          ///< Timer that drives the simulation.
} SimloopArgs;

typedef struct SimloopOut {
  uint64_t   frame;          ///< Frame counter.
  time_delta render_elapsed; ///< Amount of time elapsed in the rendering.
  time_delta update_elapsed; ///< Amount of time elapsed in the simulation.
  time_delta update_dt;      ///< Delta time for simulation updates.
  int  updates_pending; ///< Number of frames the simulation should produce.
  bool should_render;   ///< Whether the simulation should be rendered.
} SimloopOut;

typedef struct SimloopTimeline {
  time_delta ddt;       ///< Desired delta time.
  time_point last_step; ///< Time of the last simulation step.
} SimloopTimeline;

typedef struct Simloop {
  SimloopTimeline update; ///< Update timeline.
  SimloopTimeline render; ///< Render timeline.
  uint64_t        frame;  ///< Frame counter.
  Timer*          timer;
} Simloop;

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

/// Step the simulation loop.
void simloop_update(Simloop*, SimloopOut*);