From d644c6b41ff8b2dfeb54a00a114a700cbfac3581 Mon Sep 17 00:00:00 2001 From: Nikos Kastellanos Date: Sat, 22 Feb 2025 13:02:50 +0200 Subject: [PATCH] Inline ApplyChanges() (#2231) * ApplyChanges block * inline ApplyChanges() * remove duplicate code * place an assert, to test CreateDevice() from ApplyChanges() --- .../.Android/ConcreteGraphicsDeviceManager.cs | 25 +++++++++++++++++-- .../ConcreteGraphicsDeviceManager.cs | 25 +++++++++++++++++-- .../.iOS/ConcreteGraphicsDeviceManager.cs | 15 +++++++++-- 3 files changed, 59 insertions(+), 6 deletions(-) diff --git a/Platforms/Game/.Android/ConcreteGraphicsDeviceManager.cs b/Platforms/Game/.Android/ConcreteGraphicsDeviceManager.cs index 09af1e7cbbf..9b005e9d7bc 100644 --- a/Platforms/Game/.Android/ConcreteGraphicsDeviceManager.cs +++ b/Platforms/Game/.Android/ConcreteGraphicsDeviceManager.cs @@ -108,7 +108,27 @@ public override void CreateDevice() this.GraphicsDevice = new GraphicsDevice(GraphicsAdapter.DefaultAdapter, GraphicsProfile, this.PreferHalfPixelOffset, pp); - this.ApplyChanges(); + // ApplyChanges + { + // Trigger a change in orientation in case the supported orientations have changed + androidGameWindow.SetOrientation(base.Game.Window.CurrentOrientation, false); + + base.GraphicsDevice.PresentationParameters.DisplayOrientation = base.Game.Window.CurrentOrientation; + + // TODO: check if the PreferredBackBufferWidth/Hight is supported and throw an error similar to fullscreen Windows Desktop + base.GraphicsDevice.PresentationParameters.BackBufferWidth = surfaceView.Width; + base.GraphicsDevice.PresentationParameters.BackBufferHeight = surfaceView.Height; + + if (!((IPlatformGraphicsContext)((IPlatformGraphicsDevice)base.GraphicsDevice).Strategy.MainContext).Strategy.IsRenderTargetBound) + { + PresentationParameters pp2 = this.GraphicsDevice.PresentationParameters; + base.GraphicsDevice.Viewport = new Viewport(0, 0, pp2.BackBufferWidth, pp2.BackBufferHeight); + base.GraphicsDevice.ScissorRectangle = new Rectangle(0, 0, pp2.BackBufferWidth, pp2.BackBufferHeight); + } + + TouchPanel.DisplayWidth = base.GraphicsDevice.PresentationParameters.BackBufferWidth; + TouchPanel.DisplayHeight = base.GraphicsDevice.PresentationParameters.BackBufferHeight; + } // TODO: In XNA this seems to be done as part of the GraphicsDevice.DeviceReset event... // we need to get those working. @@ -141,7 +161,8 @@ public override void ApplyChanges() { if (base.GraphicsDevice == null) { - // TODO: Calling ApplyChanges() before Game Initialize should create the device. + // TODO: Calling ApplyChanges() before Game.Initialize() should create the device. + System.Diagnostics.Debug.Assert(false); //this.CreateDevice(); return; } diff --git a/Platforms/Game/.CardboardLegacy/ConcreteGraphicsDeviceManager.cs b/Platforms/Game/.CardboardLegacy/ConcreteGraphicsDeviceManager.cs index 26ee2770c11..7fdd172a744 100644 --- a/Platforms/Game/.CardboardLegacy/ConcreteGraphicsDeviceManager.cs +++ b/Platforms/Game/.CardboardLegacy/ConcreteGraphicsDeviceManager.cs @@ -108,7 +108,27 @@ public override void CreateDevice() this.GraphicsDevice = new GraphicsDevice(GraphicsAdapter.DefaultAdapter, GraphicsProfile, this.PreferHalfPixelOffset, pp); - this.ApplyChanges(); + // ApplyChanges + { + // Trigger a change in orientation in case the supported orientations have changed + androidGameWindow.SetOrientation(base.Game.Window.CurrentOrientation, false); + + base.GraphicsDevice.PresentationParameters.DisplayOrientation = base.Game.Window.CurrentOrientation; + + // TODO: check if the PreferredBackBufferWidth/Hight is supported and throw an error similar to fullscreen Windows Desktop + base.GraphicsDevice.PresentationParameters.BackBufferWidth = surfaceView.Width; + base.GraphicsDevice.PresentationParameters.BackBufferHeight = surfaceView.Height; + + if (!((IPlatformGraphicsContext)((IPlatformGraphicsDevice)base.GraphicsDevice).Strategy.MainContext).Strategy.IsRenderTargetBound) + { + PresentationParameters pp2 = this.GraphicsDevice.PresentationParameters; + base.GraphicsDevice.Viewport = new Viewport(0, 0, pp2.BackBufferWidth, pp2.BackBufferHeight); + base.GraphicsDevice.ScissorRectangle = new Rectangle(0, 0, pp2.BackBufferWidth, pp2.BackBufferHeight); + } + + TouchPanel.DisplayWidth = base.GraphicsDevice.PresentationParameters.BackBufferWidth; + TouchPanel.DisplayHeight = base.GraphicsDevice.PresentationParameters.BackBufferHeight; + } // TODO: In XNA this seems to be done as part of the GraphicsDevice.DeviceReset event... // we need to get those working. @@ -141,7 +161,8 @@ public override void ApplyChanges() { if (base.GraphicsDevice == null) { - // TODO: Calling ApplyChanges() before Game Initialize should create the device. + // TODO: Calling ApplyChanges() before Game.Initialize() should create the device. + System.Diagnostics.Debug.Assert(false); //this.CreateDevice(); return; } diff --git a/Platforms/Game/.iOS/ConcreteGraphicsDeviceManager.cs b/Platforms/Game/.iOS/ConcreteGraphicsDeviceManager.cs index eefa07158f3..32366ffc528 100644 --- a/Platforms/Game/.iOS/ConcreteGraphicsDeviceManager.cs +++ b/Platforms/Game/.iOS/ConcreteGraphicsDeviceManager.cs @@ -110,7 +110,17 @@ public override void CreateDevice() this.GraphicsDevice = new GraphicsDevice(GraphicsAdapter.DefaultAdapter, GraphicsProfile, this.PreferHalfPixelOffset, pp); - this.ApplyChanges(); + // ApplyChanges + { + base.GraphicsDevice.PresentationParameters.DisplayOrientation = base.Game.Window.CurrentOrientation; + + bool isLandscape = ((base.Game.Window.CurrentOrientation & (DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight)) != 0); + int w = PreferredBackBufferWidth; + int h = PreferredBackBufferHeight; + + base.GraphicsDevice.PresentationParameters.BackBufferWidth = isLandscape ? Math.Max(w, h) : Math.Min(w, h); + base.GraphicsDevice.PresentationParameters.BackBufferHeight = isLandscape ? Math.Min(w, h) : Math.Max(w, h); + } // TODO: In XNA this seems to be done as part of the GraphicsDevice.DeviceReset event... // we need to get those working. @@ -130,7 +140,8 @@ public override void ApplyChanges() { if (base.GraphicsDevice == null) { - // TODO: Calling ApplyChanges() before Game Initialize should create the device. + // TODO: Calling ApplyChanges() before Game.Initialize() should create the device. + System.Diagnostics.Debug.Assert(false); //this.CreateDevice(); return; }