test/snake.c

changeset 37
8a8cc6725b48
parent 36
e26b4ac1661c
child 44
b3da4096c607
--- a/test/snake.c	Wed Mar 06 23:38:17 2024 +0100
+++ b/test/snake.c	Fri Mar 15 00:06:59 2024 +0100
@@ -30,7 +30,7 @@
 
 static void update_fps_counter(AscSceneNode *node) {
     // addition and multiplication is more efficient testing for zero
-    // at an unnoticible cost of imprecision
+    // at an unnoticeable cost of imprecision
     static unsigned last_fps = 0u;
     static unsigned debounce = 999u;
     unsigned fps = 1000u;
@@ -51,7 +51,27 @@
     asc_ink_rgb(255, 0, 0);
     AscSceneNode* node = asc_text(10, 10, "XXXXX FPS");
     asc_scene_add_behavior(node, update_fps_counter);
-    asc_scene_add(asc_window_active_ui, node);
+    asc_scene_add(&asc_window_active->ui, node);
+}
+
+static void update_score_counter(AscSceneNode *node) {
+    AscText *text = asc_text_data(node);
+
+    // tie to bottom left of the screen
+    if (asc_window_active->resized) {
+        asc_vec2i bottom_left = asc_window_active->dimensions;
+        text->position.x = bottom_left.x - text->dimension.width - 10;
+        text->position.y = bottom_left.y - text->dimension.height - 10;
+        asc_node_update_transform(text);
+    }
+}
+
+static void create_score_counter(void) {
+    asc_set_font(asc_font(ASC_FONT_BOLD, 14));
+    asc_ink_rgb(0, 255, 0);
+    AscSceneNode* node = asc_text(0, 0, "Score: 0");
+    asc_scene_add_behavior(node, update_score_counter);
+    asc_scene_add(&asc_window_active->ui, node);
 }
 
 int main(int argc, char** argv) {
@@ -69,8 +89,10 @@
     AscWindow *window = asc_window_initialize(0, &settings);
     asc_shader_initialize_predefined();
 
-    // create fps counter
+    // create UI elements
     create_fps_counter();
+    create_score_counter();
+
 
     // Main Loop
     do {

mercurial