From a0d8da27762feed02c9714e42c196a8dc7b4fb12 Mon Sep 17 00:00:00 2001 From: tijani Date: Sun, 1 Sep 2024 14:28:07 -0500 Subject: [PATCH] Constant Delta Time set to 60 FPS --- src/ragar/ragar_main.c | 10 +++++++++- src/renderer/display.h | 3 +++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/ragar/ragar_main.c b/src/ragar/ragar_main.c index 59a6af7..3c8ef64 100644 --- a/src/ragar/ragar_main.c +++ b/src/ragar/ragar_main.c @@ -15,7 +15,8 @@ Vec2F32 g_projected_points[POINTS]; Vec3F32 camera_position = {.x = 0, .y = 0, .z = -5}; Vec3F32 cube_rotation = {.x = 0, .y = 0, .z = 0}; -int is_running; +s32 is_running; +s32 previous_frame_time = 0; // Perspective Projection (perspective divide) // Formula @@ -69,6 +70,13 @@ void process_input(void) { } void update(void) { + // while(!SDL_TICKS_PASSED(SDL_GetTicks(), previous_frame_time + FRAME_TARGET_TIME)); // NOTE(tijani): SLOWWWW!!!! + s32 time_to_wait = FRAME_TARGET_TIME - (SDL_GetTicks() - previous_frame_time); + if (time_to_wait > 0 && time_to_wait <= FRAME_TARGET_TIME) { + SDL_Delay(time_to_wait); + } + // previous_frame_time = SDL_GetTicks(); // NOTE(tijani): SLOWWW!!! because it locks the OS into this. + cube_rotation.x += 0.01; cube_rotation.y += 0.01; cube_rotation.z += 0.01; diff --git a/src/renderer/display.h b/src/renderer/display.h index bef3d8b..251df6d 100644 --- a/src/renderer/display.h +++ b/src/renderer/display.h @@ -3,6 +3,9 @@ #include +#define FPS 60 +#define FRAME_TARGET_TIME (1000 / FPS) + SDL_Window *window = NULL; SDL_Renderer *renderer = NULL; SDL_Texture *colour_buffer_texture = NULL;