aboutsummaryrefslogtreecommitdiff
path: root/src/system/clock.c
blob: 52869b47841b610e68e7c2f39d67d85b9ebc78ef (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
#include "../../include/mgl/system/clock.h"
#include <time.h>

/* TODO: Implement for macOS */

static double clock_get_monotonic_seconds() {
    struct timespec ts;
    ts.tv_sec = 0;
    ts.tv_nsec = 0;
    clock_gettime(CLOCK_MONOTONIC, &ts);
    return (double)ts.tv_sec + (double)ts.tv_nsec * 0.000000001;
}

void mgl_clock_init(mgl_clock *self) {
    self->captured_seconds = clock_get_monotonic_seconds();
}

double mgl_clock_restart(mgl_clock *self) {
    const double new_time_seconds = clock_get_monotonic_seconds();
    const double elapsed_time_seconds = new_time_seconds - self->captured_seconds;
    self->captured_seconds = new_time_seconds;
    return elapsed_time_seconds;
}

double mgl_clock_get_elapsed_time_seconds(mgl_clock *self) {
    return clock_get_monotonic_seconds() - self->captured_seconds;
}