summaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
Diffstat (limited to 'src/core')
-rw-r--r--src/core/src/loop.c45
1 files changed, 28 insertions, 17 deletions
diff --git a/src/core/src/loop.c b/src/core/src/loop.c
index fa95723..13a2856 100644
--- a/src/core/src/loop.c
+++ b/src/core/src/loop.c
@@ -51,18 +51,29 @@ i32 nproc(void) {
return get_nprocs();
}
-void delay( uint64_t ns )
+void delay( uint64_t us )
{
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
Sleep( ms );
#else
struct timespec ts = {
- .tv_sec = ns / 1000000000,
- .tv_nsec = ns
+ .tv_sec = us / 1000000,
+ .tv_nsec = (us * 1000) % 1000000000
};
struct timespec rem = {0,0};
- while(nanosleep(&ts, &rem)) {
- WARN("nanosleep didn't sleep full duration!");
+ int err = 0;
+ while((err = clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, &rem))) {
+ switch (err) {
+ case EINTR:
+ WARN("clock_nanosleep didn't sleep full duration: interrupted.");
+ break;
+ case EINVAL:
+ WARN("clock_nanosleep didn't sleep full duration: invalid argument.");
+ break;
+ case ENOTSUP:
+ WARN("clock_nanosleep didn't sleep full duration: unsupported clock.");
+ break;
+ }
ts = rem;
rem = (struct timespec){0,0};
}
@@ -310,15 +321,15 @@ i32 engine_run(Platform* p, StateType initial_state, void* state_arg) {
u64 last_fps_measurement = frame_end;
u64 last_fps_ticks = 0;
- /* The target frametime measured in milliseconds */
- const u32 fps_cap = p->fps_target > 0 ? 1000000000 / p->fps_target : 0;
+ /* The target frametime measured in μs */
+ const u32 fps_cap = p->fps_target > 0 ? 1000000 / p->fps_target : 0;
/* Main loop */
do {
- /* frame_start is milliseconds since engine was initialized */
+ /* frame_start is μs since engine was initialized */
const u64 frame_start = get_time();
- /* dt measured in milliseconds */
+ /* dt measured in μs */
const u64 dt = frame_start - frame_end;
/* Delta is relative to FPS cap */
@@ -366,9 +377,9 @@ i32 engine_run(Platform* p, StateType initial_state, void* state_arg) {
/* Regulate FPS */
u64 frame_end = get_time();
- const u64 fps_diff = fps_cap - frame_end - frame_start;
+ const u64 fps_diff = fps_cap - (frame_end - frame_start);
- if (fps_cap >= (frame_end - frame_start)) {
+ if (fps_diff > 0) {
delay(fps_diff);
}
@@ -376,17 +387,17 @@ i32 engine_run(Platform* p, StateType initial_state, void* state_arg) {
const u64 dt_measurement = frame_end - last_fps_measurement;
/* only make measurements once a second */
- if (dt_measurement > 1000000000) {
+ if (dt_measurement > 1000000) {
printf(" FPS: %.1f"
"\tticks: %lu"
- "\tframetime: %.1fμs"
- "\trendertime: %.1fμs"
+ "\tframetime: %luμs"
+ "\trendertime: %luμs"
"\n"
- , ((f64)(ticks - last_fps_ticks)) / (dt_measurement / 1000000000.0)
+ , ((f64)(ticks - last_fps_ticks)) / (dt_measurement / 1000000.0)
, ticks
- , dt / 1000.0
- , rendertime_dt / 1000.0
+ , dt
+ , rendertime_dt
);
last_fps_measurement = frame_end;