From 7a59d85dfead20dc34081badad6c8940e7190001 Mon Sep 17 00:00:00 2001
From: 3gg <3gg@shellblade.net>
Date: Thu, 29 Aug 2024 20:11:29 -0700
Subject: Sort out window vs screen dimensions.

---
 gfx-iso/include/isogfx/app.h |  4 +++-
 gfx-iso/src/app.c            | 30 +++++++++++++-----------------
 2 files changed, 16 insertions(+), 18 deletions(-)

diff --git a/gfx-iso/include/isogfx/app.h b/gfx-iso/include/isogfx/app.h
index 769af6d..fe60d04 100644
--- a/gfx-iso/include/isogfx/app.h
+++ b/gfx-iso/include/isogfx/app.h
@@ -4,13 +4,15 @@
 
 #include <stdbool.h>
 
+// TODO: Define an isogfx-gl backend library. Remove all these callbacks.
+
 typedef struct IsoGfx    IsoGfx;
 typedef struct IsoGfxApp IsoGfxApp;
 
 typedef struct IsoGfxAppState IsoGfxAppState;
 
 typedef struct IsoGfxApp {
-  int             pixel_scale; // 0 or 1 for 1:1 scale.
+  int             pixel_scale; // Use 0 or 1 for 1:1 scaling.
   IsoGfxAppState* state;
 
   bool (*init)(IsoGfxAppState*, IsoGfx*, int argc, const char** argv);
diff --git a/gfx-iso/src/app.c b/gfx-iso/src/app.c
index e07f318..aaeb205 100644
--- a/gfx-iso/src/app.c
+++ b/gfx-iso/src/app.c
@@ -13,8 +13,8 @@
 #include <stdbool.h>
 #include <stdlib.h>
 
-static const int SCREEN_WIDTH  = 1408;
-static const int SCREEN_HEIGHT = 960;
+static const int WINDOW_WIDTH  = 1408;
+static const int WINDOW_HEIGHT = 960;
 static const int MAX_FPS       = 60;
 
 typedef struct AppState {
@@ -35,8 +35,13 @@ static bool init(GfxAppState* gfx_app_state, int argc, const char** argv) {
 
   IsoGfxApp* app = state->app;
 
+  // Virtual screen dimensions.
+  const int scale         = app->pixel_scale == 0 ? 1 : app->pixel_scale;
+  const int screen_width  = WINDOW_WIDTH / scale;
+  const int screen_height = WINDOW_HEIGHT / scale;
+
   if (!(state->iso = isogfx_new(&(IsoGfxDesc){
-            .screen_width = SCREEN_WIDTH, .screen_height = SCREEN_HEIGHT}))) {
+            .screen_width = screen_width, .screen_height = screen_height}))) {
     goto cleanup;
   }
 
@@ -44,16 +49,7 @@ static bool init(GfxAppState* gfx_app_state, int argc, const char** argv) {
     goto cleanup;
   }
 
-  // Apply pixel scaling if requested by the app.
-  int texture_width, texture_height;
-  if (app->pixel_scale > 1) {
-    texture_width  = SCREEN_WIDTH / app->pixel_scale;
-    texture_height = SCREEN_HEIGHT / app->pixel_scale;
-    isogfx_resize(state->iso, texture_width, texture_height);
-  } else {
-    texture_width  = SCREEN_WIDTH;
-    texture_height = SCREEN_HEIGHT;
-  }
+  isogfx_resize(state->iso, screen_width, screen_height);
 
   if (!(state->gfx = gfx_init())) {
     goto cleanup;
@@ -62,8 +58,8 @@ static bool init(GfxAppState* gfx_app_state, int argc, const char** argv) {
 
   if (!(state->screen_texture = gfx_make_texture(
             gfxcore, &(TextureDesc){
-                         .width     = texture_width,
-                         .height    = texture_height,
+                         .width     = screen_width,
+                         .height    = screen_height,
                          .dimension = Texture2D,
                          .format    = TextureSRGBA8,
                          .filtering = NearestFiltering,
@@ -183,8 +179,8 @@ void iso_run(int argc, const char** argv, IsoGfxApp* app) {
       &(GfxAppDesc){
           .argc              = argc,
           .argv              = argv,
-          .width             = SCREEN_WIDTH,
-          .height            = SCREEN_HEIGHT,
+          .width             = WINDOW_WIDTH,
+          .height            = WINDOW_HEIGHT,
           .max_fps           = MAX_FPS,
           .update_delta_time = MAX_FPS > 0 ? 1.0 / (double)MAX_FPS : 0.0,
           .title             = "Isometric Renderer",
-- 
cgit v1.2.3