forked from ulfalizer/nesalizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtiming.cpp
More file actions
34 lines (27 loc) · 1.12 KB
/
timing.cpp
File metadata and controls
34 lines (27 loc) · 1.12 KB
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
#include "common.h"
#include "timing.h"
#include <time.h>
// SDL's timing functions only have millisecond precision, which doesn't seem
// good enough (60 FPS is approx. 16 ms per frame). Roll our own.
// Used for main loop synchronization
static timespec clock_previous;
static void add_to_timespec(timespec &ts, long nano_secs) {
assert(nano_secs <= 1000000000l);
long const new_nanos = ts.tv_nsec + nano_secs;
ts.tv_sec += (new_nanos >= 1000000000l);
ts.tv_nsec = new_nanos%1000000000l;
}
void init_timing() {
errno_fail_if(clock_gettime(CLOCK_MONOTONIC, &clock_previous) < 0,
"Failed to fetch initial synchronization timestamp from clock_gettime()");
}
void sleep_till_end_of_frame() {
add_to_timespec(clock_previous, nanoseconds_per_frame);
again:
int const res =
clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &clock_previous, 0);
if (res == EINTR) goto again;
errno_val_fail_if(res != 0, res, "failed to sleep with clock_nanosleep()");
errno_fail_if(clock_gettime(CLOCK_MONOTONIC, &clock_previous) < 0,
"failed to fetch synchronization timestamp from clock_gettime()");
}