diff options
| -rw-r--r-- | src/core/src/loop.c | 63 | ||||
| -rw-r--r-- | src/ctrl/include/engine/ctrl/input.h | 2 | ||||
| -rw-r--r-- | src/ctrl/src/input.c | 5 | ||||
| -rw-r--r-- | src/rendering/include/engine/rendering/window.h | 2 | ||||
| -rw-r--r-- | src/rendering/src/rendering.c | 2 | ||||
| -rw-r--r-- | src/rendering/src/window.c | 14 |
6 files changed, 48 insertions, 40 deletions
diff --git a/src/core/src/loop.c b/src/core/src/loop.c index db2033d..fab79fa 100644 --- a/src/core/src/loop.c +++ b/src/core/src/loop.c @@ -51,14 +51,14 @@ i32 nproc(void) { return get_nprocs(); } -void delay( uint64_t ms ) +void delay( uint64_t ns ) { #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) Sleep( ms ); #else struct timespec ts = { - .tv_sec = ms / 1000, - .tv_nsec = ms * 1000000 + .tv_sec = ns / 1000000000, + .tv_nsec = ns }; struct timespec rem = {0,0}; while(nanosleep(&ts, &rem)) { @@ -66,7 +66,6 @@ void delay( uint64_t ms ) ts = rem; rem = (struct timespec){0,0}; } - //usleep( ms * 1000 ); #endif } @@ -291,34 +290,34 @@ i32 engine_run(Platform* p, StateType initial_state, void* state_arg) { StateType state = initial_state; { - f64 state_init_time = get_time(); + u64 state_init_time = get_time(); State_init(state, mem, state_arg); - INFO("Initializing state \"%s\" took %.1fms", StateTypeStr[state], - (get_time() - state_init_time) * 1000.0); + INFO("Initializing state \"%s\" took %.1fns", StateTypeStr[state], + (get_time() - state_init_time)); } - f64 time = get_time(); + u64 time = get_time(); // Update ticks u64 ticks = 0; StateType (*update_func)(f64, void*) = State_updateFunc(state); - f64 last_fps_measurement = time; + u64 last_fps_measurement = time; u64 last_fps_ticks = 0; /* The target frametime measured in milliseconds */ - const f64 fps_cap = p->fps_target > 0 ? 1000.0 / (f64)p->fps_target : 0; + const u32 fps_cap = p->fps_target > 0 ? 1000000000 / p->fps_target : 0; /* Main loop */ do { /* frame_start is milliseconds since engine was initialized */ - const f64 now = get_time(); + const u64 now = get_time(); /* dt measured in milliseconds */ - const f64 dt = now - time; + const u64 dt = now - time; - const f64 delta = dt / fps_cap; + const f64 delta = (f64)dt / (f64)fps_cap; time = now; @@ -369,38 +368,39 @@ i32 engine_run(Platform* p, StateType initial_state, void* state_arg) { state = next_state; update_func = State_updateFunc(state); { - f64 state_init_time = get_time(); + u64 state_init_time = get_time(); State_init(state, mem, retval); - INFO("Initializing state \"%s\" took %.1fms", StateTypeStr[state], - (get_time() - state_init_time) * 1000.0); + INFO("Initializing state \"%s\" took %.1fns", StateTypeStr[state], + (get_time() - state_init_time)); } } else { - // Render - const f64 rendertime_begin = get_time(); + /* Render */ + const u64 rendertime_begin = get_time(); render_begin(p->window); render_present(p->window); - const f64 rendertime_dt = get_time() - rendertime_begin; + const u64 rendertime_dt = get_time() - rendertime_begin; /* Regulate FPS */ - f64 frame_end = get_time(); - const f64 fps_diff = fps_cap - (frame_end - now); + u64 frame_end = get_time(); + const u64 fps_diff = fps_cap - (frame_end - now); if (fps_cap >= (frame_end - now)) { delay(fps_diff); } /* Print stats */ - const f64 dt_measurement = frame_end - last_fps_measurement; + const u64 dt_measurement = frame_end - last_fps_measurement; + /* only make measurements once a second */ - if (dt_measurement > 1000.0) { + if (dt_measurement > 1000000000) { printf(" FPS: %.1f" "\tticks: %lu" - "\tframetime: %.1fms" - "\trendertime: %.2fms" + "\tframetime: %luns" + "\trendertime: %luns" "\n" - , ((f64)(ticks - last_fps_ticks)) / (dt_measurement / 1000.0) + , ((f64)(ticks - last_fps_ticks)) / (dt_measurement / 1000000000.0) , ticks , dt , rendertime_dt @@ -411,13 +411,11 @@ i32 engine_run(Platform* p, StateType initial_state, void* state_arg) { } } - - - ticks++; } while( !glfwWindowShouldClose(p->window->window) - && state != STATE_quit); + && state != STATE_quit + ); return 0; } @@ -444,7 +442,10 @@ void engine_stop(Platform* p) { } /* Set the maximum framerate */ -void engine_fps_max(Platform* p, u16 cap) { LOG("Setting max fps to %llu", cap); p->fps_target = cap; } +void engine_fps_max(Platform* p, u16 cap) { + LOG("Setting max fps to %llu", cap); + p->fps_target = cap; +} isize f_get_sz(FILE* f) { if (f == NULL) { diff --git a/src/ctrl/include/engine/ctrl/input.h b/src/ctrl/include/engine/ctrl/input.h index 73a265b..960428c 100644 --- a/src/ctrl/include/engine/ctrl/input.h +++ b/src/ctrl/include/engine/ctrl/input.h @@ -55,7 +55,7 @@ void i_ctx_t_free(i_ctx* c); /* Executes all callbacks that has been pushed onto the callstack and resets the * callstack */ void i_flush_bindings(f64 dt, usize numcalls, input_callback_t* c[], void* state_mem); -action_t i_get_action(const i_ctx* restrict ctx, u32 time, scancode_t scancode); +action_t i_get_action(const i_ctx* restrict ctx, u64 time, scancode_t scancode); void key_callback(void* window, int key, int scancode, int action, int mods); diff --git a/src/ctrl/src/input.c b/src/ctrl/src/input.c index 4b878d1..4d29770 100644 --- a/src/ctrl/src/input.c +++ b/src/ctrl/src/input.c @@ -42,7 +42,7 @@ void key_callback(void* window, int key, int scancode, int action, int mods) { const i_ctx* bindings = *GLOBAL_PLATFORM->bindings; const usize bindings_len = GLOBAL_PLATFORM->bindings_len; - const f64 now = get_time(); + const u64 now = get_time(); for (usize b = 0; b < bindings_len; b++) { const action_t a = i_get_action(&bindings[b], now, scancode); @@ -190,6 +190,7 @@ bool i_update_unique_binding(i_ctx* ctx, binding_t* binding) { return false; } +/* Call binding callbacks of respective bindings */ void i_flush_bindings(f64 dt, usize numcalls, input_callback_t* c[], void* state_mem) { for (usize i = 0; i < numcalls; i++) { (c[i])(state_mem); @@ -198,7 +199,7 @@ void i_flush_bindings(f64 dt, usize numcalls, input_callback_t* c[], void* state callbacks_len = 0; } -action_t i_get_action(const i_ctx* restrict ctx, u32 time, +action_t i_get_action(const i_ctx* restrict ctx, u64 time, scancode_t scancode) { isize idx = 0; diff --git a/src/rendering/include/engine/rendering/window.h b/src/rendering/include/engine/rendering/window.h index 9d44855..fff7bc6 100644 --- a/src/rendering/include/engine/rendering/window.h +++ b/src/rendering/include/engine/rendering/window.h @@ -10,7 +10,7 @@ extern "C" { #ifndef ENGINE_RENDERING_WINDOW_H_EXCLUDE_EXTERNS extern void* window_poll_events; -extern f64 (*get_time)(void); +extern u64 (*get_time)(void); #endif typedef enum { diff --git a/src/rendering/src/rendering.c b/src/rendering/src/rendering.c index 8a49e92..1eb5438 100644 --- a/src/rendering/src/rendering.c +++ b/src/rendering/src/rendering.c @@ -242,7 +242,7 @@ void render_present(Window* w) { // bind index buffer - f64 t = get_time(); + u64 t = get_time(); RenderObject* o = dc.data.model.model; vec3 pos; glm_vec3_copy(dc.data.model.pos, pos); diff --git a/src/rendering/src/window.c b/src/rendering/src/window.c index abcaf1e..8b964ce 100644 --- a/src/rendering/src/window.c +++ b/src/rendering/src/window.c @@ -22,7 +22,7 @@ extern Platform* GLOBAL_PLATFORM; void *window_poll_events = NULL; /* wrapper to get time in ms */ -f64 (*get_time)(void) = NULL; +u64 (*get_time)(void) = NULL; /* GLFW And vulkan spaghetti boiler */ void glfw_err_callback(int code, const char* description) { @@ -59,8 +59,14 @@ GladGLContext* create_context(GLFWwindow *window) { return context; } -static inline f64 glfw_gettime_msec() { - return glfwGetTime() / 1000.0; +/* Should honestly just write my own */ +static inline u64 glfw_gettime_msec() { + return glfwGetTime() * 1000; +} + +/* Should honestly just write my own */ +static inline u64 glfw_gettime_nsec() { + return (f64)glfwGetTime() * 1000000000; } Window* init_window_glfw( @@ -128,7 +134,7 @@ Window* init_window_glfw( ret->context = NULL; window_poll_events = &glfwPollEvents; - get_time = &glfw_gettime_msec; + get_time = &glfw_gettime_nsec; return ret; } |
