aboutsummaryrefslogtreecommitdiff
path: root/simloop/src/simloop.c
blob: b8547fd2f8883968795ddd47b2b6dc7b1ff541dc (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <simloop.h>

#include <assert.h>

static double min(double a, double b) { return a <= b ? a : b; }

static simloop_time_t ddt_from_fps(int fps) {
  static constexpr double NANOSECONDS = 1e9;
  return (fps == 0) ? 0 : (simloop_time_t)(NANOSECONDS / (double)fps);
}

Simloop simloop_make(const SimloopArgs* args) {
  assert(args);
  assert(args->update_fps > 0);

  return (Simloop){
      .frame = 0,
      .update =
          (SimloopTimeline){
                            .ddt  = ddt_from_fps(args->update_fps),
                            .time = 0,
                            },
      .render =
          (SimloopTimeline){
                            .ddt  = ddt_from_fps(args->max_render_fps),
                            .time = 0,
                            },
      .percent_frame = 0.,
      .first_iter    = true,
  };
}

static bool step_update(const Simloop* sim, SimloopTimeline* timeline) {
  assert(sim);
  assert(timeline);
  assert(timeline->ddt > 0);

  // If the update falls behind the clock, we advance by a single ddt increment
  // per loop iteration here and give it a chance to catch up over subsequent
  // iterations.
  // This has the implication that percent_frame can fall out of range (>1) if
  // we are not careful with how it is defined. See the general update function
  // below.
  const simloop_time_t dt          = sim->clock - timeline->time;
  const bool           should_step = dt >= timeline->ddt;
  timeline->time += should_step ? timeline->ddt : 0;
  return should_step;
}

static bool step_render(const Simloop* sim, SimloopTimeline* timeline) {
  assert(sim);
  assert(timeline);

  bool render = false;
  if (timeline->ddt > 0) {
    render = step_update(sim, timeline);
  } else {
    render         = timeline->time < sim->clock;
    timeline->time = sim->clock;
  }
  return render;
}

void simloop_update(Simloop* sim, simloop_time_t dt, SimloopOut* out) {
  assert(sim);
  assert(out);

  sim->clock += dt;

  // Simulation update.
  const bool update_this_tick = step_update(sim, &sim->update);

  // Simulation render.
  const bool render_this_tick =
      step_render(sim, &sim->render) ||
      sim->first_iter; // Trigger an initial render on the first frame.

  // Interpolator for smooth animation.
  // If rendering is not frame-rate capped, then its timeline should always be
  // at least as recent as the update's. Otherwise, it is possible for the
  // rendering timeline to be behind.
  // If the update falls behind the clock, then percent_frame can fall out of
  // range (>1) if we are not careful. We impose that it is strictly never >1
  // to account for this case.
  assert(sim->update.ddt > 0);
  assert(
      (sim->render.ddt == 0) ? (sim->update.time <= sim->render.time) : true);
  sim->percent_frame =
      (sim->render.time >= sim->update.time)
          ? min(1., ((double)(sim->render.time - sim->update.time) /
                     (double)sim->update.ddt))
          : sim->percent_frame;
  assert((0. <= sim->percent_frame) && (sim->percent_frame <= 1.));

  // Loop state update.
  sim->frame += (update_this_tick ? 1 : 0);
  sim->first_iter = false;

  out->frame          = sim->frame;
  out->render_elapsed = sim->render.time;
  out->update_elapsed = sim->update.time;
  out->update_dt      = sim->update.ddt;
  out->percent_frame  = sim->percent_frame;
  out->should_update  = update_this_tick;
  out->should_render  = render_this_tick;
}