handle the edge-case when a viewport vanishes

Thu, 31 Jul 2025 20:40:48 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 31 Jul 2025 20:40:48 +0200
changeset 233
bda74199223f
parent 232
4c43b9de3294
child 234
a1d70b8018c1

handle the edge-case when a viewport vanishes

src/scene.c file | annotate | diff | comparison | revisions
test/snake/snake.c file | annotate | diff | comparison | revisions
--- a/src/scene.c	Wed Jul 30 00:12:13 2025 +0200
+++ b/src/scene.c	Thu Jul 31 20:40:48 2025 +0200
@@ -134,6 +134,11 @@
         }
     }
 
+    // when the viewport is zero, exit immediately
+    if (scene->camera.viewport.size.width == 0 || scene->camera.viewport.size.height == 0) {
+        return;
+    }
+
     // reset render groups
     CxList **render_group = scene->internal.render_groups;
     for (unsigned i = 0 ; i < ASC_RENDER_GROUP_COUNT ; i++) {
--- a/test/snake/snake.c	Wed Jul 30 00:12:13 2025 +0200
+++ b/test/snake/snake.c	Thu Jul 31 20:40:48 2025 +0200
@@ -231,16 +231,22 @@
 
 static asc_rect update_viewport_for_window_resize(asc_vec2u window_size) {
 
-    // TODO: special case: window is SO small that nothing would be rendered
-
-    // remove margins
+    // margins
     const unsigned margin = 10;
-    window_size.width -= 2*margin;
-    window_size.height -= 2*margin;
+
+    // space for score, power-ups, etc.
+    const unsigned left_area = (unsigned) (asc_active_window->ui_scale*200);
 
-    // remove space for score, power-ups, etc.
-    const unsigned left_area = (unsigned) (asc_active_window->ui_scale*200);
-    window_size.width -= left_area;
+    // calculate how many pixels need to be removed from width and height
+    const unsigned rw = 2*margin + left_area;
+    const unsigned rh = 2*margin;
+
+    // check if there is still a viewport left and chicken out when not
+    if (window_size.width < rw || window_size.height < rh) {
+        return ASC_RECT(0, 0, 0, 0);
+    }
+    window_size.width -= rw;
+    window_size.height -= rh;
 
     // Compute scaling and offsets
     unsigned viewport_size, offset_x = 0, offset_y = 0;

mercurial