From 3a8defcf3f3881c567f4da759424327b5b07f0b5 Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Tue, 25 Feb 2020 20:23:42 -0500 Subject: [PATCH] Letterbox the scene if the aspect ratio is out-of-whack. Fixes #23. GitOrigin-RevId: 2da580115e44f2658e7f9b9d3d39a366d3a1bbfd --- Shared/Scene.cs | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/Shared/Scene.cs b/Shared/Scene.cs index 70aa027..a08fb4f 100644 --- a/Shared/Scene.cs +++ b/Shared/Scene.cs @@ -4,6 +4,8 @@ using System; namespace SemiColinGames { class Scene : IDisposable { + const float DESIRED_ASPECT_RATIO = 1920.0f / 1080.0f; + public bool Enabled = false; Color backgroundColor = Color.CornflowerBlue; @@ -92,13 +94,28 @@ namespace SemiColinGames { // Draw debug rects & lines on top. Debug.Draw(graphics, basicEffect); - // Draw sceneTarget to screen. + // Get ready to draw sceneTarget to screen. graphics.SetRenderTarget(null); + + // Letterbox the scene if needed. + float aspectRatio = 1.0f * graphics.Viewport.Width / graphics.Viewport.Height; + Rectangle drawRect; + if (aspectRatio > DESIRED_ASPECT_RATIO) { + // Need to letterbox the sides. + int desiredWidth = (int) (graphics.Viewport.Height * DESIRED_ASPECT_RATIO); + int padding = (graphics.Viewport.Width - desiredWidth) / 2; + drawRect = new Rectangle(padding, 0, desiredWidth, graphics.Viewport.Height); + } else { + // Need to letterbox the top / bottom. + int desiredHeight = (int) (graphics.Viewport.Width / DESIRED_ASPECT_RATIO); + int padding = (graphics.Viewport.Height - desiredHeight) / 2; + drawRect = new Rectangle(0, padding, graphics.Viewport.Width, desiredHeight); + } + + // Actually draw to screen. spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullNone); - Rectangle drawRect = new Rectangle( - 0, 0, graphics.Viewport.Width, graphics.Viewport.Height); spriteBatch.Draw(sceneTarget, drawRect, Color.White); // Draw debug toasts.