1.1 --- a/src/context.c Thu Mar 28 23:30:21 2024 +0100 1.2 +++ b/src/context.c Fri Mar 29 00:03:25 2024 +0100 1.3 @@ -33,8 +33,16 @@ 1.4 #include <SDL2/SDL.h> 1.5 #include <SDL2/SDL_ttf.h> 1.6 1.7 +#include <time.h> 1.8 + 1.9 AscContext asc_context; 1.10 1.11 +static uint64_t asc_nanos(void) { 1.12 + struct timespec ts; 1.13 + clock_gettime(CLOCK_MONOTONIC, &ts); 1.14 + return 1000000000ull*(uint64_t)ts.tv_sec + (uint64_t)ts.tv_nsec; 1.15 +} 1.16 + 1.17 void asc_context_initialize(void) { 1.18 if (asc_test_flag(asc_context.flags, ASC_FLAG_INITILIZED)) 1.19 return; 1.20 @@ -58,6 +66,7 @@ 1.21 } 1.22 } 1.23 SDL_ClearError(); 1.24 + asc_context.total_nanos = asc_nanos(); 1.25 asc_set_flag(&asc_context.flags, ASC_FLAG_INITILIZED); 1.26 asc_dprintf("Ascension context initialized."); 1.27 } 1.28 @@ -128,11 +137,13 @@ 1.29 } 1.30 1.31 // compute frame time 1.32 - static Uint32 ticks; 1.33 - Uint32 ticks_elapsed = SDL_GetTicks() - ticks; 1.34 - ticks = SDL_GetTicks(); 1.35 - asc_context.elapsed_millis = ticks_elapsed; 1.36 - asc_context.elapsed = (float) ticks_elapsed / 1000.0f; 1.37 + uint64_t frame_nanos, ns; 1.38 + do { 1.39 + ns = asc_nanos(); 1.40 + frame_nanos = ns - asc_context.total_nanos; 1.41 + } while (frame_nanos == 0); 1.42 + asc_context.frame_nanos = frame_nanos; 1.43 + asc_context.total_nanos = ns; 1.44 1.45 return !asc_test_flag(asc_context.flags, ASC_FLAG_QUIT); 1.46 }