From 67c1ae9838fd8e815ef87dab137ccbb3623ac8db Mon Sep 17 00:00:00 2001 From: Nikos Kastellanos Date: Wed, 22 May 2024 19:08:09 +0300 Subject: [PATCH] refactor UpdateBackBufferSize() (#1598) * use clientBounds * move UpdateBackBufferSize() * inline UpdateBackBufferSize() * swap statement * join code --- .../ConcreteGraphicsDeviceManager.cs | 14 +++++++ .../Platform/Windows/WinFormsGameWindow.cs | 39 ++++--------------- 2 files changed, 22 insertions(+), 31 deletions(-) diff --git a/MonoGame.Framework/.WindowsDX11/ConcreteGraphicsDeviceManager.cs b/MonoGame.Framework/.WindowsDX11/ConcreteGraphicsDeviceManager.cs index fccb1e68240..b94923500ff 100644 --- a/MonoGame.Framework/.WindowsDX11/ConcreteGraphicsDeviceManager.cs +++ b/MonoGame.Framework/.WindowsDX11/ConcreteGraphicsDeviceManager.cs @@ -177,6 +177,20 @@ private void GraphicsDevice_PresentationChanged_UpdateGamePlatform(object sender ((WinFormsGameWindow)this.Game.Window).OnPresentationChanged(pp); } + internal void UpdateBackBufferSize(Rectangle clientBounds) + { + if (this.GraphicsDevice != null) + { + if (clientBounds.Width != this.PreferredBackBufferWidth + || clientBounds.Height != this.PreferredBackBufferHeight) + { + // Set the default new back buffer size + this.PreferredBackBufferWidth = clientBounds.Width; + this.PreferredBackBufferHeight = clientBounds.Height; + this.ApplyChanges(); + } + } + } #region IGraphicsDeviceManager strategy diff --git a/MonoGame.Framework/Platform/Windows/WinFormsGameWindow.cs b/MonoGame.Framework/Platform/Windows/WinFormsGameWindow.cs index a7f796bfbf3..464926cc961 100644 --- a/MonoGame.Framework/Platform/Windows/WinFormsGameWindow.cs +++ b/MonoGame.Framework/Platform/Windows/WinFormsGameWindow.cs @@ -290,16 +290,15 @@ private void OnResize(object sender, EventArgs eventArgs) if (_concreteGame.Window == this && Form.WindowState != FormWindowState.Minimized) { - // we may need to restore full screen when coming back from a minimized window - if (_lastFormState == FormWindowState.Minimized) + GraphicsDeviceManager gdm = _concreteGame.GraphicsDeviceManager; + if (gdm != null) { - GraphicsDeviceManager gdm = _concreteGame.GraphicsDeviceManager; - if (gdm != null) - { + // we may need to restore full screen when coming back from a minimized window + if (_lastFormState == FormWindowState.Minimized) ((IPlatformGraphicsDevice)gdm.GraphicsDevice).Strategy.ToConcrete().SetHardwareFullscreen(); - } + + ((IPlatformGraphicsDeviceManager)gdm).GetStrategy().UpdateBackBufferSize(this.ClientBounds); } - UpdateBackBufferSize(); } _lastFormState = Form.WindowState; @@ -311,13 +310,13 @@ private void OnResizeEnd(object sender, EventArgs eventArgs) _wasMoved = true; if (_concreteGame.Window == this) { - UpdateBackBufferSize(); - // the display that the window is on might have changed, so we need to // check and possibly update the Adapter of the GraphicsDevice GraphicsDeviceManager gdm = _concreteGame.GraphicsDeviceManager; if (gdm != null) { + ((IPlatformGraphicsDeviceManager)gdm).GetStrategy().UpdateBackBufferSize(this.ClientBounds); + if (_concreteGame.GraphicsDevice != null) ((IPlatformGraphicsDevice)gdm.GraphicsDevice).Strategy.ToConcrete().RefreshAdapter(); } @@ -326,28 +325,6 @@ private void OnResizeEnd(object sender, EventArgs eventArgs) OnClientSizeChanged(); } - private void UpdateBackBufferSize() - { - GraphicsDeviceManager gdm = _concreteGame.GraphicsDeviceManager; - if (gdm != null) - { - if (gdm.GraphicsDevice == null) - return; - - SysDrawing.Size newSize = Form.ClientSize; - int newWidth = newSize.Width; - int newHeight = newSize.Height; - if (newWidth != gdm.PreferredBackBufferWidth - || newHeight != gdm.PreferredBackBufferHeight) - { - // Set the default new back buffer size - gdm.PreferredBackBufferWidth = newWidth; - gdm.PreferredBackBufferHeight = newHeight; - gdm.ApplyChanges(); - } - } - } - protected override void SetTitle(string title) { Form.Text = title;