aboutsummaryrefslogtreecommitdiff
path: root/simloop/src/simloop.c
blob: aa2b6b78123e9c439435605e53cb6bd5cc4634bb (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
#include <simloop.h>

#include <assert.h>

static uint64_t ddt_from_fps(int fps) {
  static constexpr double NANOSECONDS = 1e9;
  return (fps == 0) ? 0 : (uint64_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),
                                      .last_step = args->timer->start_time},
      .render     = (SimloopTimeline){  .ddt = ddt_from_fps(args->max_render_fps),
                                      .last_step = args->timer->start_time},
      .timer      = args->timer,
      .first_iter = true,
  };
}

static time_delta time_elapsed(const Simloop* sim, time_point t) {
  assert(sim);
  return time_diff(sim->timer->start_time, t);
}

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

  const time_delta dt = time_diff(timeline->last_step, sim->timer->last_tick);
  const bool       should_step = dt >= timeline->ddt;
  timeline->last_step =
      should_step ? time_add(timeline->last_step, dt) : timeline->last_step;
  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 {
    timeline->last_step = sim->timer->last_tick;
    render              = true;
  }
  return render;
}

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

  out->should_update = step_update(sim, &sim->update);
  out->should_render =
      step_render(sim, &sim->render) ||
      (sim->first_iter); // Trigger an initial render on the first frame.
  sim->frame += (out->should_update ? 1 : 0);
  sim->first_iter     = false;
  out->frame          = sim->frame;
  out->render_elapsed = time_elapsed(sim, sim->render.last_step);
  out->update_elapsed = time_elapsed(sim, sim->update.last_step);
  out->update_dt      = sim->update.ddt;
}