Letterbox the scene if the aspect ratio is out-of-whack.

Fixes #23.

GitOrigin-RevId: 2da580115e44f2658e7f9b9d3d39a366d3a1bbfd
This commit is contained in:
Colin McMillen 2020-02-25 20:23:42 -05:00
parent 0a64d17db5
commit 3a8defcf3f

View File

@ -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.