How to: Restrict Graphics Devices to Widescreen Aspect Ratios in Full Screen
Demonstrates how to create a custom GraphicsDeviceManager that only selects graphics devices with widescreen aspect ratios in full screen mode.
To restrict graphics devices to widescreen aspect ratios in full screen mode
Create a class that derives from GraphicsDeviceManager.
public class CustomGraphicsDeviceManager : GraphicsDeviceManager { public CustomGraphicsDeviceManager( Game game ) : base( game ) { } ... }
Add a WideScreenOnly property to the class. The property will be used to turn the widescreen only behavior on and off.
private bool isWideScreenOnly; public bool IsWideScreenOnly { get { return isWideScreenOnly; } set { isWideScreenOnly = value; } }
Determine the minimum desired aspect ratio.
static float WideScreenRatio = 1.6f;
Override the RankDevices method of GraphicsDeviceManager. Note the call to base.RankDevices; it ensures the new version of RankDevices has an already ranked list of available devices to work with.
protected override void RankDevices( List<GraphicsDeviceInformation> foundDevices ) { base.RankDevices( foundDevices ); ... }
Add a check to see if the WideScreenOnly property is true.
if (IsWideScreenOnly) { ... }
In the if block, loop through all of the found devices. Check whether the PresentationParameters indicate the device is full screen. If the device is full screen, determine the aspect ratio of the device by dividing the BackBufferWidth by the BackBufferHeight. If the aspect ratio is less than the desired aspect ratio, remove the device from the list of found devices.
for (int i = 0; i < foundDevices.Count; ) { PresentationParameters pp = foundDevices[i].PresentationParameters; if (pp.IsFullScreen == true) { float aspectRatio = (float)(pp.BackBufferWidth) / (float)(pp.BackBufferHeight); // If the device does not have a widescreen aspect ratio, remove it. if (aspectRatio < WideScreenRatio) { foundDevices.RemoveAt( i ); } else { i++; } } else i++; }
Replace the default GraphicsDeviceManager with the derived GraphicsDeviceManager.
To test the new component, set the WideScreenOnly and IsFullScreen properties to true.
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
public class CustomGraphicsDeviceManager : GraphicsDeviceManager
{
public CustomGraphicsDeviceManager( Game game )
: base( game )
{
}
private bool isWideScreenOnly;
public bool IsWideScreenOnly
{
get { return isWideScreenOnly; }
set { isWideScreenOnly = value; }
}
static float WideScreenRatio = 1.6f;
protected override void RankDevices( List<GraphicsDeviceInformation> foundDevices )
{
base.RankDevices( foundDevices );
if (IsWideScreenOnly)
{
for (int i = 0; i < foundDevices.Count; )
{
PresentationParameters pp = foundDevices[i].PresentationParameters;
if (pp.IsFullScreen == true)
{
float aspectRatio = (float)(pp.BackBufferWidth) / (float)(pp.BackBufferHeight);
// If the device does not have a widescreen aspect ratio, remove it.
if (aspectRatio < WideScreenRatio) { foundDevices.RemoveAt( i ); }
else { i++; }
}
else i++;
}
}
}
}