From c7e4b81ad71562a4f3f91bdcf4b414a6afa89829 Mon Sep 17 00:00:00 2001
From: FunkyFr3sh <cc.red.alert.1@googlemail.com>
Date: Thu, 15 Aug 2024 10:16:20 +0200
Subject: [PATCH] add experimental aspect_ratio setting (hidden for now)

---
 inc/config.h  |  1 +
 src/config.c  |  4 ++++
 src/dd.c      | 20 ++++++++++++++++++--
 src/utils.c   | 16 +++++++++++++++-
 src/wndproc.c | 26 +++++++++++++++++++++-----
 5 files changed, 59 insertions(+), 8 deletions(-)

diff --git a/inc/config.h b/inc/config.h
index c111c6b..5f132d7 100644
--- a/inc/config.h
+++ b/inc/config.h
@@ -29,6 +29,7 @@ typedef struct CNCDDRAWCONFIG
     BOOL windowed;
     BOOL maintas;
     BOOL boxing;
+    char aspect_ratio[16];
     int maxfps;
     BOOL vsync;
     BOOL adjmouse;
diff --git a/src/config.c b/src/config.c
index 6223d28..3a28766 100644
--- a/src/config.c
+++ b/src/config.c
@@ -39,6 +39,7 @@ void cfg_load()
     GET_BOOL(g_config.windowed, "windowed", FALSE);
     GET_BOOL(g_config.maintas, "maintas", FALSE);
     GET_BOOL(g_config.boxing, "boxing", FALSE);
+    GET_STRING("aspect_ratio", "", g_config.aspect_ratio, sizeof(g_config.aspect_ratio));
     GET_INT(g_config.maxfps, "maxfps", -1);
     GET_BOOL(g_config.vsync, "vsync", FALSE);
     GET_BOOL(g_config.adjmouse, "adjmouse", TRUE);
@@ -120,6 +121,9 @@ void cfg_load()
     if (g_config.lock_mouse_top_left)
         g_config.adjmouse = FALSE;
 
+    if (g_config.aspect_ratio[0])
+        g_config.maintas = TRUE;
+
     ini_free(&g_config.ini);
 }
 
diff --git a/src/dd.c b/src/dd.c
index 819a0fd..7d25dd0 100644
--- a/src/dd.c
+++ b/src/dd.c
@@ -581,6 +581,8 @@ HRESULT dd_SetDisplayMode(DWORD dwWidth, DWORD dwHeight, DWORD dwBPP, DWORD dwFl
     if (!dwHeight)
         dwHeight = g_ddraw.height ? g_ddraw.height : 600;
 
+    //if (dwHeight == 400) dwHeight = 480;
+
     if (!dwBPP)
         dwBPP = g_ddraw.bpp ? g_ddraw.bpp : 16;
 
@@ -859,8 +861,22 @@ HRESULT dd_SetDisplayMode(DWORD dwWidth, DWORD dwHeight, DWORD dwBPP, DWORD dwFl
     }
     else if (g_config.maintas)
     {
-        double dst_ar = (double)g_ddraw.height / g_ddraw.width;
-        double src_ar = (double)g_ddraw.render.height / g_ddraw.render.width;
+        double dst_ar;
+        double src_ar = (double)g_ddraw.render.height / g_ddraw.render.width;;
+
+        if (g_config.aspect_ratio[0])
+        {
+            char* e = &g_config.aspect_ratio[0];
+
+            DWORD cx = strtoul(e, &e, 0);
+            DWORD cy = strtoul(e + 1, &e, 0);
+
+            dst_ar = (double)cy / cx;
+        }
+        else
+        {
+            dst_ar = (double)g_ddraw.height / g_ddraw.width;
+        }
 
         g_ddraw.render.viewport.width = g_ddraw.render.width;
         g_ddraw.render.viewport.height = (int)round(dst_ar * g_ddraw.render.viewport.width);
diff --git a/src/utils.c b/src/utils.c
index 16bdd37..a24da1a 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -622,9 +622,23 @@ void util_toggle_maximize()
             int w = dst_rc.right - dst_rc.left;
             int h = dst_rc.bottom - dst_rc.top;
 
-            double dst_ar = (double)g_ddraw.height / g_ddraw.width;
+            double dst_ar;
             double src_ar = (double)h / w;
 
+            if (g_config.aspect_ratio[0])
+            {
+                char* e = &g_config.aspect_ratio[0];
+
+                DWORD cx = strtoul(e, &e, 0);
+                DWORD cy = strtoul(e + 1, &e, 0);
+
+                dst_ar = (double)cy / cx;
+            }
+            else
+            {
+                dst_ar = (double)g_ddraw.height / g_ddraw.width;
+            }
+
             dst_rc.top = 0;
             dst_rc.left = 0;
             dst_rc.right = w;
diff --git a/src/wndproc.c b/src/wndproc.c
index d0e32ee..9fa4e4a 100644
--- a/src/wndproc.c
+++ b/src/wndproc.c
@@ -317,8 +317,24 @@ LRESULT CALLBACK fake_WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam
                         real_GetWindowLongA(hWnd, GWL_EXSTYLE)) &&
                     SetRect(&clientrc, 0, 0, clientrc.right - clientrc.left, clientrc.bottom - clientrc.top))
                 {
-                    double scaleH = (double)g_ddraw.height / g_ddraw.width;
-                    double scaleW = (double)g_ddraw.width / g_ddraw.height;
+                    double scale_h;
+                    double scale_w;
+
+                    if (g_config.aspect_ratio[0])
+                    {
+                        char* e = &g_config.aspect_ratio[0];
+
+                        DWORD cx = strtoul(e, &e, 0);
+                        DWORD cy = strtoul(e + 1, &e, 0);
+
+                        scale_h = (double)cy / cx;
+                        scale_w = (double)cx / cy;
+                    }
+                    else
+                    {
+                        scale_h = (double)g_ddraw.height / g_ddraw.width;
+                        scale_w = (double)g_ddraw.width / g_ddraw.height;
+                    }
 
                     switch (wParam)
                     {
@@ -327,19 +343,19 @@ LRESULT CALLBACK fake_WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam
                     case WMSZ_LEFT:
                     case WMSZ_RIGHT:
                     {
-                        windowrc->bottom += (LONG)round(scaleH * clientrc.right - clientrc.bottom);
+                        windowrc->bottom += (LONG)round(scale_h * clientrc.right - clientrc.bottom);
                         break;
                     }
                     case WMSZ_TOP:
                     case WMSZ_BOTTOM:
                     {
-                        windowrc->right += (LONG)round(scaleW * clientrc.bottom - clientrc.right);
+                        windowrc->right += (LONG)round(scale_w * clientrc.bottom - clientrc.right);
                         break;
                     }
                     case WMSZ_TOPRIGHT:
                     case WMSZ_TOPLEFT:
                     {
-                        windowrc->top -= (LONG)round(scaleH * clientrc.right - clientrc.bottom);
+                        windowrc->top -= (LONG)round(scale_h * clientrc.right - clientrc.bottom);
                         break;
                     }
                     }