From 8f28bfad9c754b8a07713b608e034a64690af2a1 Mon Sep 17 00:00:00 2001
From: FunkyFr3sh <cc.red.alert.1@googlemail.com>
Date: Wed, 14 Aug 2019 17:53:36 +0200
Subject: [PATCH] resize window on bnet

---
 inc/main.h  |  3 +++
 src/main.c  | 31 +++++++++++++++++++++++++++++++
 src/mouse.c | 36 ++++++++++++++++++++++++++++++++++--
 3 files changed, 68 insertions(+), 2 deletions(-)

diff --git a/inc/main.h b/inc/main.h
index bfaa25a..db8b773 100644
--- a/inc/main.h
+++ b/inc/main.h
@@ -41,6 +41,7 @@ extern BOOL ChildWindowExists;
 BOOL detect_cutscene();
 void LimitGameTicks();
 void ToggleFullscreen();
+void SetWindowRect(int x, int y, int width, int height, UINT flags);
 DWORD WINAPI render_main(void);
 DWORD WINAPI render_soft_main(void);
 BOOL CALLBACK EnumChildProc(HWND hWnd, LPARAM lParam);
@@ -131,6 +132,8 @@ typedef struct IDirectDrawImpl
     BOOL resizable;
     BOOL bnetActive;
     BOOL bnetWasFullscreen;
+    RECT bnetWinRect;
+    POINT bnetPos;
     SpeedLimiter ticksLimiter;
     SpeedLimiter flipLimiter;
     SpeedLimiter fpsLimiter;
diff --git a/src/main.c b/src/main.c
index 4f720cf..470d503 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1002,6 +1002,37 @@ BOOL UnadjustWindowRectEx(LPRECT prc, DWORD dwStyle, BOOL fMenu, DWORD dwExStyle
     return fRc;
 }
 
+void SetWindowRect(int x, int y, int width, int height, UINT flags)
+{
+    if (ddraw->windowed)
+    {
+        if (ddraw->render.thread)
+        {
+            EnterCriticalSection(&ddraw->cs);
+            ddraw->render.run = FALSE;
+            ReleaseSemaphore(ddraw->render.sem, 1, NULL);
+            LeaveCriticalSection(&ddraw->cs);
+
+            WaitForSingleObject(ddraw->render.thread, INFINITE);
+            ddraw->render.thread = NULL;
+        }
+
+        if ((flags & SWP_NOMOVE) == 0)
+        {
+            WindowRect.left = x;
+            WindowRect.top = y;
+        }
+
+        if ((flags & SWP_NOSIZE) == 0)
+        {
+            WindowRect.bottom = height;
+            WindowRect.right = width;
+        }
+
+        ddraw_SetDisplayMode(ddraw, ddraw->width, ddraw->height, ddraw->bpp);
+    }
+}
+
 LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
 {
     RECT rc = { 0, 0, ddraw->render.width, ddraw->render.height };
diff --git a/src/mouse.c b/src/mouse.c
index d265ac9..a4b3dcd 100644
--- a/src/mouse.c
+++ b/src/mouse.c
@@ -435,9 +435,26 @@ BOOL WINAPI fake_DestroyWindow(HWND hWnd)
             ddraw->bnetActive = FALSE;
             mouse_lock();
 
-            if (ddraw->windowed && ddraw->bnetWasFullscreen)
+            if (ddraw->windowed)
             {
-                SetTimer(ddraw->hWnd, IDT_TIMER_LEAVE_BNET, 1000, (TIMERPROC)NULL);
+                if (!ddraw->fullscreen)
+                {
+                    ddraw->bnetPos.x = ddraw->bnetPos.y = 0;
+                    ClientToScreen(ddraw->hWnd, &ddraw->bnetPos);
+
+                    int width = ddraw->bnetWinRect.right - ddraw->bnetWinRect.left;
+                    int height = ddraw->bnetWinRect.bottom - ddraw->bnetWinRect.top;
+                    UINT flags = width != ddraw->width || height != ddraw->height ? 0 : SWP_NOMOVE;
+
+                    SetWindowRect(ddraw->bnetWinRect.left, ddraw->bnetWinRect.top, width, height, flags);
+                }
+
+                if (ddraw->bnetWasFullscreen)
+                {
+                    SetTimer(ddraw->hWnd, IDT_TIMER_LEAVE_BNET, 1000, (TIMERPROC)NULL);
+                }
+
+                ddraw->resizable = TRUE;
             }
         }
     }
@@ -461,6 +478,21 @@ HWND WINAPI fake_CreateWindowExA(
                 ddraw->bnetWasFullscreen = TRUE;
             }
 
+            if (!ddraw->fullscreen)
+            {
+                GetClientRect(ddraw->hWnd, &ddraw->bnetWinRect);
+                MapWindowPoints(ddraw->hWnd, HWND_DESKTOP, (LPPOINT)&ddraw->bnetWinRect, 2);
+
+                int width = ddraw->bnetWinRect.right - ddraw->bnetWinRect.left;
+                int height = ddraw->bnetWinRect.bottom - ddraw->bnetWinRect.top;
+                int x = ddraw->bnetPos.x || ddraw->bnetPos.y ? ddraw->bnetPos.x : -32000;
+                int y = ddraw->bnetPos.x || ddraw->bnetPos.y ? ddraw->bnetPos.y : -32000;
+                UINT flags = width != ddraw->width || height != ddraw->height ? 0 : SWP_NOMOVE;
+
+                SetWindowRect(x, y, ddraw->width, ddraw->height, flags);
+                ddraw->resizable = FALSE;
+            }
+
             ddraw->bnetActive = TRUE;
             mouse_unlock();
         }