Mon, 23 Jun 2025 21:07:31 +0200
add frame_rate and frame_factor to context
fixes #685
src/ascension/context.h | file | annotate | diff | comparison | revisions | |
src/context.c | file | annotate | diff | comparison | revisions | |
test/snake/snake.c | file | annotate | diff | comparison | revisions |
--- a/src/ascension/context.h Mon Jun 23 20:38:21 2025 +0200 +++ b/src/ascension/context.h Mon Jun 23 21:07:31 2025 +0200 @@ -61,6 +61,8 @@ AscFont active_font; unsigned char active_window; asc_col4i ink; + float frame_rate; + float frame_factor; uint64_t frame_nanos; uint64_t total_nanos; } AscContext;
--- a/src/context.c Mon Jun 23 20:38:21 2025 +0200 +++ b/src/context.c Mon Jun 23 21:07:31 2025 +0200 @@ -198,12 +198,22 @@ } // compute frame time + // TODO: think about whether frame rate is actually a per-window thing + // the answer is hopefully NO, because that would mean behaviors are also a per-window thing uint64_t frame_nanos, ns; do { ns = asc_nanos(); frame_nanos = ns - asc_context.total_nanos; } while (frame_nanos == 0); asc_context.frame_nanos = frame_nanos; + unsigned long long fps_1k = asc_seconds(1000) / frame_nanos; + asc_context.frame_rate = (float)fps_1k / 1000.f; + if (asc_context.frame_rate < 5) { + // effectively stop the world when the frame rate drops too low + asc_context.frame_factor = 0; + } else { + asc_context.frame_factor = 1.f / asc_context.frame_rate; + } asc_context.total_nanos = ns; // sync the windows
--- a/test/snake/snake.c Mon Jun 23 20:38:21 2025 +0200 +++ b/test/snake/snake.c Mon Jun 23 21:07:31 2025 +0200 @@ -57,12 +57,10 @@ static void update_fps_counter(AscBehavior *behavior) { AscSceneNode *node = behavior->node; - // update text - static uint64_t last_fps = 0; - uint64_t fps = asc_seconds(1) / asc_context.frame_nanos; - if (fps != last_fps) { - last_fps = fps; - asc_text_printf(node, "%"PRIu64" FPS", fps); + static float last_fps = 0.f; + if (fabsf(asc_context.frame_rate - last_fps) > 1) { + last_fps = asc_context.frame_rate; + asc_text_printf(node, "%.2f FPS", asc_context.frame_rate); } }