test/snake/snake.c

changeset 148
9f030f402699
parent 144
43636d6a6e25
child 149
560772519ff9
--- a/test/snake/snake.c	Fri Jun 13 17:45:19 2025 +0200
+++ b/test/snake/snake.c	Fri Jun 13 18:09:49 2025 +0200
@@ -57,22 +57,19 @@
 
 static void update_fps_counter(AscBehavior *behavior) {
     AscSceneNode *node = behavior->node;
+    // update text
     static uint64_t last_fps = 0;
-    static uint64_t debounce = ASC_NANOS_SECOND - 1;
-    debounce += asc_context.frame_nanos;
-    // only update text every second
-    if (debounce >= ASC_NANOS_SECOND) {
-        debounce = 0;
-        uint64_t fps = ASC_NANOS_SECOND;
-        fps /= asc_context.frame_nanos;
-        if (fps != last_fps) {
-            last_fps = fps;
-            asc_text_printf(node, "%"PRIu64" FPS", fps);
-        }
+    uint64_t fps = asc_seconds(1) / asc_context.frame_nanos;
+    if (fps != last_fps) {
+        last_fps = fps;
+        asc_text_printf(node, "%"PRIu64" FPS", fps);
     }
-    // tie to bottom right of the screen
-    if (asc_test_flag(node->flags, ASC_SCENE_NODE_GRAPHICS_UPDATED)
-        || asc_active_window->resized) {
+}
+
+static void tie_fps_counter_to_corner(AscBehavior *behavior) {
+    // TODO: this should be replaced with some sort of UI layout manager
+    AscSceneNode *node = behavior->node;
+    if (asc_test_flag(node->flags, ASC_SCENE_NODE_GRAPHICS_UPDATED) || asc_active_window->resized) {
         asc_vec2u bottom_right = asc_active_window->dimensions;
         asc_vec2u text_size = ((AscText*)node)->dimension;
         asc_set_position2d(
@@ -105,7 +102,8 @@
     asc_font(ASC_FONT_REGULAR, 12);
     asc_ink_rgb(255, 255, 255);
     AscSceneNode *node = asc_text(.name = "FPS Counter");
-    asc_behavior_add(node, .func = update_fps_counter);
+    asc_behavior_add(node, .func = update_fps_counter, .interval = asc_seconds(1));
+    asc_behavior_add(node, .func = tie_fps_counter_to_corner);
     asc_add_ui_node(node);
 }
 

mercurial