Skip to content

Instantly share code, notes, and snippets.

@thatcosmonaut
Last active February 9, 2020 03:40
Show Gist options
  • Save thatcosmonaut/990f50e2be3e0d8286832ea147bdac16 to your computer and use it in GitHub Desktop.
Save thatcosmonaut/990f50e2be3e0d8286832ea147bdac16 to your computer and use it in GitHub Desktop.
Resolution-independent rendering for FNA
//////////////////////////////////////////////////////////////////////////
////License: The MIT License (MIT)
////Copyright (c) 2010 David Amador (http://www.david-amador.com)
////
////Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
////
////The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
////
////THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//////////////////////////////////////////////////////////////////////////
// Modified for FNA in 2020 by Evan Hemsley
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace IndependentResolutionRendering
{
static class Resolution
{
static private GraphicsDeviceManager s_device = null;
static public int ViewportWidth { get; private set; } = 1024;
static public int ViewportHeight { get; private set; } = 768;
static private int s_width = 800;
static private int s_height = 600;
static private Matrix s_scaleMatrix;
static private bool s_fullScreen = false;
static private bool s_dirtyMatrix = true;
static public void Init(GraphicsDeviceManager device)
{
s_width = device.PreferredBackBufferWidth;
s_height = device.PreferredBackBufferHeight;
s_device = device;
s_dirtyMatrix = true;
}
static public Matrix GetTransformationMatrix()
{
if (s_dirtyMatrix) RecreateScaleMatrix();
return s_scaleMatrix;
}
static public void SetResolution(int width, int height, bool fullScreen)
{
s_width = width;
s_height = height;
s_fullScreen = fullScreen;
ApplyResolutionSettings();
}
static public void SetVirtualResolution(int width, int height)
{
ViewportWidth = width;
ViewportHeight = height;
s_dirtyMatrix = true;
}
static private void ApplyResolutionSettings()
{
// If we aren't using a full screen mode, the height and width of the window can
// be set to anything equal to or smaller than the actual screen size.
if (!s_fullScreen)
{
if ((s_width <= GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width)
&& (s_height <= GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height))
{
s_device.PreferredBackBufferWidth = s_width;
s_device.PreferredBackBufferHeight = s_height;
s_device.IsFullScreen = s_fullScreen;
s_device.ApplyChanges();
}
}
else
{
// If we are using full screen mode, we should check to make sure that the display
// adapter can handle the video mode we are trying to set. To do this, we will
// iterate through the display modes supported by the adapter and check them against
// the mode we want to set.
foreach (var dm in GraphicsAdapter.DefaultAdapter.SupportedDisplayModes)
{
// Check the width and height of each mode against the passed values
if ((dm.Width == s_width) && (dm.Height == s_height))
{
// The mode is supported, so set the buffer formats, apply changes and return
s_device.PreferredBackBufferWidth = s_width;
s_device.PreferredBackBufferHeight = s_height;
s_device.IsFullScreen = s_fullScreen;
s_device.ApplyChanges();
}
}
}
s_dirtyMatrix = true;
s_width = s_device.PreferredBackBufferWidth;
s_height = s_device.PreferredBackBufferHeight;
}
/// <summary>
/// Sets the device to use the draw pump
/// Sets correct aspect ratio
/// </summary>
static public void BeginDraw()
{
// Start by reseting viewport to (0,0,1,1)
FullViewport();
// Clear to Black
s_device.GraphicsDevice.Clear(Color.Black);
// Calculate Proper Viewport according to Aspect Ratio
ResetViewport();
// Clear for letterbox
s_device.GraphicsDevice.Clear(Color.Black);
}
static private void RecreateScaleMatrix()
{
s_dirtyMatrix = false;
s_scaleMatrix = Matrix.CreateScale(
(float)s_device.GraphicsDevice.Viewport.Width / ViewportWidth,
(float)s_device.GraphicsDevice.Viewport.Width / ViewportWidth,
1f);
}
static public void FullViewport()
{
var vp = new Viewport();
vp.X = vp.Y = 0;
vp.Width = s_width;
vp.Height = s_height;
s_device.GraphicsDevice.Viewport = vp;
}
/// <summary>
/// Get virtual Mode Aspect Ratio
/// </summary>
/// <returns>aspect ratio</returns>
static public float GetVirtualAspectRatio()
{
return ViewportWidth / (float)ViewportHeight;
}
static public void ResetViewport()
{
var targetAspectRatio = GetVirtualAspectRatio();
// figure out the largest area that fits in this resolution at the desired aspect ratio
var width = s_device.PreferredBackBufferWidth;
var height = (int)((width / targetAspectRatio) + .5f);
var changed = false;
if (height > s_device.PreferredBackBufferHeight)
{
height = s_device.PreferredBackBufferHeight;
// PillarBox
width = (int)((height * targetAspectRatio) + .5f);
changed = true;
}
// set up the new viewport centered in the backbuffer
var viewport = new Viewport
{
X = (s_device.PreferredBackBufferWidth / 2) - (width / 2),
Y = (s_device.PreferredBackBufferHeight / 2) - (height / 2),
Width = width,
Height = height,
MinDepth = 0,
MaxDepth = 1
};
if (changed)
{
s_dirtyMatrix = true;
}
s_device.GraphicsDevice.Viewport = viewport;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment