Restricting Aspect Ratio on a Graphics Device
Demonstrates how to create a custom GraphicsDeviceManager that only selects graphics devices with widescreen aspect ratios in full-screen mode.
The Complete Sample
The code in this topic shows you the technique. You can download a complete code sample for this topic, including full source code and any additional supporting files required by the sample.
Download AspectRatio_Sample.zip.
Restricting Graphics Devices
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 is used to turn on and off the widescreen-only behavior.
private bool isWideScreenOnly; public bool IsWideScreenOnly { get { return isWideScreenOnly; } set { isWideScreenOnly = value; } }
Determine the minimum desired aspect ratio.
static float WideScreenRatio = 1.6f; //1.77777779f;
Override the RankDevices method of GraphicsDeviceManager.
Note the call to base.RankDevices. This call ensures that the new version of RankDevices has an already ranked list of available devices with which to work.
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 found devices, and 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.
public Game1() { graphics = new CustomGraphicsDeviceManager(this); Content.RootDirectory = "Content"; this.graphics.PreferMultiSampling = false; #if WINDOWS this.graphics.PreferredBackBufferWidth = 1280; this.graphics.PreferredBackBufferHeight = 720; #endif #if WINDOWS_PHONE this.graphics.PreferredBackBufferWidth = 400; this.graphics.PreferredBackBufferHeight = 600; #endif this.graphics.IsFullScreen = true; this.graphics.IsWideScreenOnly = true; graphics.ApplyChanges(); }