using System; using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; namespace Nuclex { internal class GraphicsDeviceInformationComparer : IComparer { // Fields private GraphicsDeviceManager graphics; // Methods public GraphicsDeviceInformationComparer(GraphicsDeviceManager graphicsComponent) { this.graphics = graphicsComponent; } public int Compare(GraphicsDeviceInformation d1, GraphicsDeviceInformation d2) { float num5; if (d1.DeviceType != d2.DeviceType) { if (d1.DeviceType >= d2.DeviceType) { return 1; } return -1; } PresentationParameters pp1 = d1.PresentationParameters; PresentationParameters pp2 = d2.PresentationParameters; if (pp1.IsFullScreen != pp2.IsFullScreen) { if (this.graphics.IsFullScreen != pp1.IsFullScreen) { return 1; } return -1; } int num = this.RankFormat(pp1.BackBufferFormat); int num2 = this.RankFormat(pp2.BackBufferFormat); if (num != num2) { if (num >= num2) { return 1; } return -1; } if (pp1.MultiSampleType != pp2.MultiSampleType) { int num3 = (pp1.MultiSampleType == MultiSampleType.NonMaskable) ? ((int)(MultiSampleType.SixteenSamples | MultiSampleType.NonMaskable)) : ((int)pp1.MultiSampleType); int num4 = (pp2.MultiSampleType == MultiSampleType.NonMaskable) ? ((int)(MultiSampleType.SixteenSamples | MultiSampleType.NonMaskable)) : ((int)pp2.MultiSampleType); if (num3 <= num4) { return 1; } return -1; } if (pp1.MultiSampleQuality != pp2.MultiSampleQuality) { if (pp1.MultiSampleQuality <= pp2.MultiSampleQuality) { return 1; } return -1; } if ((this.graphics.PreferredBackBufferWidth == 0) || (this.graphics.PreferredBackBufferHeight == 0)) { num5 = ((float)GraphicsDeviceManager.DefaultBackBufferWidth) / ((float)GraphicsDeviceManager.DefaultBackBufferHeight); } else { num5 = ((float)this.graphics.PreferredBackBufferWidth) / ((float)this.graphics.PreferredBackBufferHeight); } float num6 = ((float)pp1.BackBufferWidth) / ((float)pp1.BackBufferHeight); float num7 = ((float)pp2.BackBufferWidth) / ((float)pp2.BackBufferHeight); float num8 = Math.Abs((float)(num6 - num5)); float num9 = Math.Abs((float)(num7 - num5)); if (Math.Abs((float)(num8 - num9)) > 0.2f) { if (num8 >= num9) { return 1; } return -1; } int num10 = 0; int num11 = 0; if (this.graphics.IsFullScreen) { if ((this.graphics.PreferredBackBufferWidth == 0) || (this.graphics.PreferredBackBufferHeight == 0)) { GraphicsAdapter adapter = d1.Adapter; num10 = adapter.CurrentDisplayMode.Width * adapter.CurrentDisplayMode.Height; GraphicsAdapter adapter2 = d2.Adapter; num11 = adapter2.CurrentDisplayMode.Width * adapter2.CurrentDisplayMode.Height; } else { num10 = num11 = this.graphics.PreferredBackBufferWidth * this.graphics.PreferredBackBufferHeight; } } else if ((this.graphics.PreferredBackBufferWidth == 0) || (this.graphics.PreferredBackBufferHeight == 0)) { num10 = num11 = GraphicsDeviceManager.DefaultBackBufferWidth * GraphicsDeviceManager.DefaultBackBufferHeight; } else { num10 = num11 = this.graphics.PreferredBackBufferWidth * this.graphics.PreferredBackBufferHeight; } int num12 = Math.Abs((int)((pp1.BackBufferWidth * pp1.BackBufferHeight) - num10)); int num13 = Math.Abs((int)((pp2.BackBufferWidth * pp2.BackBufferHeight) - num11)); if (num12 != num13) { if (num12 >= num13) { return 1; } return -1; } if (this.graphics.IsFullScreen && (pp1.FullScreenRefreshRateInHz != pp2.FullScreenRefreshRateInHz)) { if (pp1.FullScreenRefreshRateInHz <= pp2.FullScreenRefreshRateInHz) { return 1; } return -1; } if (d1.Adapter != d2.Adapter) { if (d1.Adapter.IsDefaultAdapter) { return -1; } if (d2.Adapter.IsDefaultAdapter) { return 1; } } return 0; } private int RankFormat(SurfaceFormat format) { int index = Array.IndexOf(GraphicsDeviceManager.ValidBackBufferFormats, format); if (index != -1) { int num2 = Array.IndexOf(GraphicsDeviceManager.ValidBackBufferFormats, this.graphics.PreferredBackBufferFormat); if (num2 == -1) { return (GraphicsDeviceManager.ValidBackBufferFormats.Length - index); } if (index >= num2) { return (index - num2); } } return 0x7fffffff; } } } // namespace Nuclex