Export (0) Print
Expand All

How To: Restrict Graphics Devices to Widescreen Aspect Ratios in Full-Screen Mode

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 tutorial illustrates the technique described in the text. A complete code sample for this tutorial is available for you to download, including full source code and any additional supporting files required by the sample.

To Restrict Graphics Devices to Widescreen Aspect Ratios in Full-Screen Mode

  1. Create a class that derives from GraphicsDeviceManager.

    public class CustomGraphicsDeviceManager : GraphicsDeviceManager
    {
        public CustomGraphicsDeviceManager( Game game )
            : base( game )
        {
        }
    
        ...
    }
    
  2. 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; }
    }
    
  3. Determine the minimum desired aspect ratio.

    static float WideScreenRatio = 1.6f; //1.77777779f;
    
  4. Override the RankDevices method of GraphicsDeviceManager. Note the call to base.RankDevices; it ensures that 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 );
        ...
    }
    
  5. Add a check to see if the WideScreenOnly property is true.

    if (IsWideScreenOnly)
    {
        ...
    }
    
  6. 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++;
    }
    
  7. Replace the default GraphicsDeviceManager with the derived GraphicsDeviceManager.
  8. 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;
        this.graphics.PreferredBackBufferWidth = 1280;
        this.graphics.PreferredBackBufferHeight = 720;
    
        this.graphics.IsFullScreen = true;
        this.graphics.IsWideScreenOnly = true;
        graphics.ApplyChanges();
    }
    

Community Additions

ADD
Show:
© 2014 Microsoft