test/snake/snake.c

changeset 99
ac143db979dc
parent 98
83bec26453a8
--- a/test/snake/snake.c	Sun Apr 27 13:27:27 2025 +0200
+++ b/test/snake/snake.c	Sun Apr 27 15:17:12 2025 +0200
@@ -108,6 +108,28 @@
     // TODO: return something
 }
 
+static asc_recti update_viewport_for_window_resize(asc_vec2i window_size) {
+    // Compute scaling and offsets
+    int viewport_size, offset_x = 0, offset_y = 0;
+    if (window_size.width > window_size.height) {
+        // Wider window: letterbox (black bars on top/bottom)
+        offset_x = (window_size.width - window_size.height) / 2;
+        viewport_size = window_size.height;
+    } else {
+        // Taller window: pillarbox (black bars on sides)
+        offset_y = (window_size.height - window_size.width) / 2;
+        viewport_size = window_size.width;
+    }
+
+    // Set the viewport to the scaled and centered region
+    return (asc_recti){
+        offset_x,
+        offset_y,
+        viewport_size,
+        viewport_size
+    };
+}
+
 int main(int argc, char** argv) {
     asc_context_initialize();
     if (asc_has_error()) {
@@ -129,8 +151,8 @@
 
     // initialize the main scene (a 500x500 game field)
     asc_scene_init(MAIN_SCENE);
-    // TODO: this is intentionally bullshit at the moment to force me to care about aspect ratio
     asc_camera_ortho(&MAIN_SCENE->camera, (asc_recti){0, 0, 500, 500});
+    MAIN_SCENE->camera.viewport_update_func = update_viewport_for_window_resize;
 
     // load textures
     init_textures();

mercurial