From 6ad898877d5d34209770970adbea8509f6fe5be8 Mon Sep 17 00:00:00 2001 From: lotaviods Date: Mon, 16 Mar 2026 01:11:29 -0300 Subject: [PATCH] fix: add dynamic window resize support --- Minecraft.Client/Minecraft.cpp | 23 +++++++++++++++++-- Minecraft.Client/Minecraft.h | 2 ++ .../Platform/Common/UI/UIController.h | 1 + .../Platform/Linux/Linux_Minecraft.cpp | 5 ++++ Minecraft.Client/Rendering/GameRenderer.cpp | 9 ++++++-- 5 files changed, 36 insertions(+), 4 deletions(-) diff --git a/Minecraft.Client/Minecraft.cpp b/Minecraft.Client/Minecraft.cpp index 5d804d22d..698e0b6a3 100644 --- a/Minecraft.Client/Minecraft.cpp +++ b/Minecraft.Client/Minecraft.cpp @@ -2254,14 +2254,33 @@ void Minecraft::pauseGame() { // setScreen(new PauseScreen()); // 4J - TODO put back in } +bool Minecraft::pollResize() { + int fbw, fbh; + RenderManager.GetFramebufferSize(fbw, fbh); + if (fbw != width_phys || fbh != height_phys) { + resize(fbw, fbh); + return true; + } + return false; +} + void Minecraft::resize(int width, int height) { if (width <= 0) width = 1; if (height <= 0) height = 1; - this->width = width; + // 4jcraft: store physical framebuffer size and adjust logical width + // for non-widescreen aspect ratio to fix UI scaling. + this->width_phys = width; + this->height_phys = height; + if (RenderManager.IsWidescreen()) { + this->width = width; + } else { + this->width = (width * 3) / 4; + } this->height = height; if (screen != NULL) { - ScreenSizeCalculator ssc(options, width, height); + // 4jcraft: use adjusted logical width instead of raw width for correct screen size calculation. + ScreenSizeCalculator ssc(options, this->width, height); int screenWidth = ssc.getWidth(); int screenHeight = ssc.getHeight(); // screen->init(this, screenWidth, screenHeight); // 4J - diff --git a/Minecraft.Client/Minecraft.h b/Minecraft.Client/Minecraft.h index 8493675f7..07b4c1a3b 100644 --- a/Minecraft.Client/Minecraft.h +++ b/Minecraft.Client/Minecraft.h @@ -264,6 +264,8 @@ public: void pauseGame(); // void toggleFullScreen(); // 4J - removed + bool pollResize(); + private: void resize(int width, int height); diff --git a/Minecraft.Client/Platform/Common/UI/UIController.h b/Minecraft.Client/Platform/Common/UI/UIController.h index 707e7de1a..936e8c077 100644 --- a/Minecraft.Client/Platform/Common/UI/UIController.h +++ b/Minecraft.Client/Platform/Common/UI/UIController.h @@ -218,6 +218,7 @@ public: // RENDERING float getScreenWidth() { return m_fScreenWidth; } float getScreenHeight() { return m_fScreenHeight; } + void setScreenSize(S32 w, S32 h) { m_fScreenWidth = (float)w; m_fScreenHeight = (float)h; } virtual void render() = 0; void getRenderDimensions(C4JRender::eViewportType viewport, S32 &width, S32 &height); diff --git a/Minecraft.Client/Platform/Linux/Linux_Minecraft.cpp b/Minecraft.Client/Platform/Linux/Linux_Minecraft.cpp index 7d16d9af0..b62da92d7 100644 --- a/Minecraft.Client/Platform/Linux/Linux_Minecraft.cpp +++ b/Minecraft.Client/Platform/Linux/Linux_Minecraft.cpp @@ -905,6 +905,11 @@ return -1; app.InitialiseTips(); while (!RenderManager.ShouldClose()) { RenderManager.StartFrame(); + if (pMinecraft->pollResize()) { + int fbw, fbh; + RenderManager.GetFramebufferSize(fbw, fbh); + ui.setScreenSize(fbw, fbh); + } app.UpdateTime(); PIXBeginNamedEvent(0, "Input manager tick"); InputManager.Tick(); diff --git a/Minecraft.Client/Rendering/GameRenderer.cpp b/Minecraft.Client/Rendering/GameRenderer.cpp index 6b0d32724..532319318 100644 --- a/Minecraft.Client/Rendering/GameRenderer.cpp +++ b/Minecraft.Client/Rendering/GameRenderer.cpp @@ -1046,7 +1046,9 @@ void GameRenderer::render(float a, bool bFirst) { int fbw, fbh; RenderManager.GetFramebufferSize(fbw, fbh); glViewport(0, 0, fbw, fbh); - ScreenSizeCalculator ssc(mc->options, mc->width, mc->height); + // 4jcraft: use framebuffer dimensions for ScreenSizeCalculator so the + // title screen GUI coordinates match the actual viewport size. + ScreenSizeCalculator ssc(mc->options, fbw, fbh); int screenWidth = ssc.getWidth(); int screenHeight = ssc.getHeight(); int xMouse = Mouse::getX() * screenWidth / fbw; @@ -1819,7 +1821,10 @@ void GameRenderer::renderSnowAndRain(float a) { void GameRenderer::setupGuiScreen(int forceScale /*=-1*/) { int fbw, fbh; RenderManager.GetFramebufferSize(fbw, fbh); - ScreenSizeCalculator ssc(mc->options, mc->width, mc->height, forceScale); + + // 4jcraft: use actual framebuffer dimensions instead of mc->width/height + // to ensure GUI scales correctly after a window resize. + ScreenSizeCalculator ssc(mc->options, fbw, fbh, forceScale); glClear(GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_PROJECTION);