test/snake/snake.c

changeset 244
ceab8a9f0366
parent 242
6eeb987f1681
--- a/test/snake/snake.c	Sat Aug 02 15:45:43 2025 +0200
+++ b/test/snake/snake.c	Sat Aug 02 21:43:39 2025 +0200
@@ -96,11 +96,20 @@
 
 static void backdrop_scale(AscBehavior *behavior) {
     // scale the backdrop to the size of the window
-    if (asc_active_window->resized) {
-        asc_ptr_cast(AscSprite, sprite, behavior->node);
-        asc_vec2u window_size = asc_active_window->dimensions;
-        asc_sprite_set_size(sprite, window_size);
-    }
+    if (!asc_active_window->resized) return;
+    asc_ptr_cast(AscSprite, sprite, behavior->node);
+    asc_vec2u window_size = asc_active_window->dimensions;
+    asc_sprite_set_size(sprite, window_size);
+}
+
+static void main_scene_frame_scale(AscBehavior *behavior) {
+    // TODO: we cannot skip this behavior when window is resized,
+    //       because the viewport is updated after executing all behaviors
+    //       and then the resized flag is cleared already.
+    //       A possible solution is to add something like a post-rendering behavior.
+    //       Another solution would be a viewport-changed-event (once we implement events)
+    asc_ptr_cast(AscRectangle, frame, behavior->node);
+    asc_rectangle_set_bounds(frame, MAIN_SCENE->camera.viewport);
 }
 
 static void backdrop_create(void) {
@@ -112,6 +121,12 @@
     );
     asc_behavior_add(node, .func = backdrop_scale);
     asc_scene_add_node(BACKDROP_SCENE, node);
+
+    // also add a frame for the main scene
+    // add this to the UI layer so that the border size does not scale
+    AscSceneNode *frame = asc_rectangle(.thickness = 2, .color = ASC_RGB(66, 142, 161));
+    asc_behavior_add(frame, .func = main_scene_frame_scale);
+    asc_ui_add_node(frame);
 }
 
 static void fps_counter_update(AscBehavior *behavior) {

mercurial