From 64571a09bdca05cb9614f9997099aec7cb866222 Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Thu, 24 Jan 2008 09:13:50 +0000 Subject: [PATCH] Added DisplayDevice, DisplayResolution, IDisplayDeviceDriver, GraphicsContextException and GraphicsModeException. --- Source/OpenTK/Graphics/DisplayDevice.cs | 176 +++++++++++++++--- Source/OpenTK/Graphics/DisplayResolution.cs | 122 ++++++++++++ Source/OpenTK/Graphics/GraphicsExceptions.cs | 28 +++ .../OpenTK/Graphics/IDisplayDeviceDriver.cs | 22 +++ 4 files changed, 322 insertions(+), 26 deletions(-) create mode 100644 Source/OpenTK/Graphics/DisplayResolution.cs create mode 100644 Source/OpenTK/Graphics/GraphicsExceptions.cs create mode 100644 Source/OpenTK/Graphics/IDisplayDeviceDriver.cs diff --git a/Source/OpenTK/Graphics/DisplayDevice.cs b/Source/OpenTK/Graphics/DisplayDevice.cs index be0f5e34..330a154b 100644 --- a/Source/OpenTK/Graphics/DisplayDevice.cs +++ b/Source/OpenTK/Graphics/DisplayDevice.cs @@ -2,7 +2,8 @@ /* Licensed under the MIT/X11 license. * Copyright (c) 2006-2008 the OpenTK team. * This notice may not be removed. - * See license.txt for licensing detailed licensing information. */ + * See license.txt for licensing detailed licensing details. + */ #endregion using System; @@ -24,57 +25,126 @@ namespace OpenTK.Graphics // TODO: Does not detect changes to primary device. // TODO: Mono does not support System.Windows.Forms.Screen.BitsPerPixel -- find workaround! - int width, height; - int bits_per_pixel; - float refresh_rate; + DisplayResolution current_resolution; + List available_resolutions = new List(); bool primary; static List available_displays = new List(); static object display_lock = new object(); static DisplayDevice primary_display; + static IDisplayDeviceDriver implementation = new OpenTK.Platform.Windows.WinDisplayDeviceDriver(); + #region --- Constructors --- static DisplayDevice() { - lock (display_lock) - { - int i = 0; - foreach (System.Windows.Forms.Screen scr in System.Windows.Forms.Screen.AllScreens) - { - available_displays.Add(new DisplayDevice(scr.Bounds.Width, scr.Bounds.Height, 32, 0, scr.Primary)); - if (scr.Primary) - primary_display = available_displays[i]; - ++i; - } - } + //lock (display_lock) + //{ + // int i = 0; + // foreach (System.Windows.Forms.Screen scr in System.Windows.Forms.Screen.AllScreens) + // { + // available_displays.Add(new DisplayDevice(scr.Bounds.Width, scr.Bounds.Height, 32, 0, scr.Primary)); + // if (scr.Primary) + // primary_display = available_displays[i]; + // ++i; + // } + //} } - DisplayDevice(int width, int height, int bitsPerPixel, float refreshRate, bool primary) + internal DisplayDevice(DisplayResolution currentResolution, bool primary, + IEnumerable availableResolutions) { - this.width = width; - this.height = height; - this.bits_per_pixel = bitsPerPixel; - this.refresh_rate = refreshRate; + this.current_resolution = currentResolution; this.primary = primary; + this.available_resolutions.AddRange(availableResolutions); + available_displays.Add(this); + if (primary) + primary_display = this; } #endregion #region --- Public Methods --- - /// Gets a System.Int32 that contains the width of this Display in pixels. - public int Width { get { return width; } } - /// Gets a System.Int32 that contains the height of this Display in pixels. - public int Height { get { return height; } } + #region public int Width - /// Gets a System.Int32 that contains number of bits per pixel of this Display. Typical values include 8, 16, 24 and 32. - public int BitsPerPixel { get { Debug.Print("This method is not supported currently."); return bits_per_pixel; } } + /// Gets a System.Int32 that contains the width of this display in pixels. + public int Width { get { return current_resolution.Width; } } + + #endregion + + #region public int Height + + /// Gets a System.Int32 that contains the height of this display in pixels. + public int Height { get { return current_resolution.Height; } } + + #endregion + + #region public int BitsPerPixel + + /// Gets a System.Int32 that contains number of bits per pixel of this display. Typical values include 8, 16, 24 and 32. + public int BitsPerPixel { get { Debug.Print("This method is not supported currently."); return current_resolution.BitsPerPixel; } } + + #endregion + + #region public float RefreshRate + + /// + /// Gets a System.Single representing the vertical refresh rate of this display. + /// + public float RefreshRate + { + get { return current_resolution.RefreshRate; } + } + + #endregion + + #region public bool IsPrimary /// Gets a System.Boolean that indicates whether this Display is the primary Display in systems with multiple Displays. public bool IsPrimary { get { return primary; } } + #endregion + + #region public void ChangeResolution(int width, int height, int bitsPerPixel, float refreshRate) + + /// Changes the resolution of the DisplayDevice. + /// The new width of the DisplayDevice. + /// The new height of the DisplayDevice. + /// The new bits per pixel of the DisplayDevice. + /// The new refresh rate of the DisplayDevice. + /// Thrown if the requested resolution change failed. + public void ChangeResolution(int width, int height, int bitsPerPixel, float refreshRate) + { + if (width <= 0) throw new ArgumentOutOfRangeException("width", "Must be greater than zero."); + if (height <= 0) throw new ArgumentOutOfRangeException("height", "Must be greater than zero."); + if (bitsPerPixel <= 0) throw new ArgumentOutOfRangeException("bitsPerPixel", "Must be greater than zero."); + if (refreshRate <= 0) throw new ArgumentOutOfRangeException("refreshRate", "Must be greater than zero."); + + if (implementation.TryChangeResolution(width, height, bitsPerPixel, refreshRate)) + { + current_resolution = new DisplayResolution(width, height, bitsPerPixel, refreshRate); + } + else + throw new GraphicsModeException(String.Format("Device {0}: Failed to change resolution to {1}x{2}x{3]@{4]Hz", + ToString(), width, height, bitsPerPixel, refreshRate)); + } + + #endregion + + #region public void RestoreResolution() + + public void RestoreResolution() + { + implementation.RestoreResolution(); + } + + #endregion + + #region public static DisplayDevice[] AvailableDisplays + /// /// Gets an array of OpenTK.Display objects, which describe all available display devices. /// @@ -89,11 +159,65 @@ namespace OpenTK.Graphics } } + #endregion + + #region public static DisplayDevice PrimaryDisplay + /// Gets the primary display of this system. public static DisplayDevice PrimaryDisplay { get { return primary_display; } } #endregion + #endregion + #region --- Overrides --- + + #region public override string ToString() + + /// + /// Returns a System.String representing this DisplayDevice. + /// + /// A System.String representing this DisplayDevice. + public override string ToString() + { + return String.Format("{0}: {1} ({2} modes available)", IsPrimary ? "Primary" : "Secondary", + current_resolution.ToString(), available_resolutions.Count); + } + + #endregion + + #region public override bool Equals(object obj) + + /// Determines whether the specified DisplayDevices are equal. + /// The System.Object to check against. + /// True if the System.Object is an equal DisplayDevice; false otherwise. + public override bool Equals(object obj) + { + if (obj is DisplayDevice) + { + DisplayDevice dev = (DisplayDevice)obj; + return + IsPrimary == dev.IsPrimary && + current_resolution == dev.current_resolution && + available_resolutions.Count == dev.available_resolutions.Count; + } + + return false; + } + + #endregion + + #region public override int GetHashCode() + + /// Returns a unique hash representing this DisplayDevice. + /// A System.Int32 that may serve as a hash code for this DisplayDevice. + public override int GetHashCode() + { + return current_resolution.GetHashCode() ^ IsPrimary.GetHashCode() ^ available_resolutions.Count; + } + + #endregion + + #endregion } } diff --git a/Source/OpenTK/Graphics/DisplayResolution.cs b/Source/OpenTK/Graphics/DisplayResolution.cs new file mode 100644 index 00000000..46457bff --- /dev/null +++ b/Source/OpenTK/Graphics/DisplayResolution.cs @@ -0,0 +1,122 @@ +#region --- License --- +/* Licensed under the MIT/X11 license. + * Copyright (c) 2006-2008 the OpenTK team. + * This notice may not be removed. + * See license.txt for licensing detailed licensing details. + */ +#endregion + +using System; +using System.Collections.Generic; +using System.Text; +using System.Diagnostics; + +namespace OpenTK.Graphics +{ + /// Contains information regarding a monitor's display resolution. + public class DisplayResolution + { + int width, height; + int bits_per_pixel; + float refresh_rate; + + #region --- Constructors --- + + internal DisplayResolution(int width, int height, int bitsPerPixel, float refreshRate) + { + this.width = width; + this.height = height; + this.bits_per_pixel = bitsPerPixel; + this.refresh_rate = refreshRate; + } + + #endregion + + #region --- Public Methods --- + + #region public int Width + + /// Gets a System.Int32 that contains the width of this display in pixels. + public int Width { get { return width; } } + + #endregion + + #region public int Height + + /// Gets a System.Int32 that contains the height of this display in pixels. + public int Height { get { return height; } } + + #endregion + + #region public int BitsPerPixel + + /// Gets a System.Int32 that contains number of bits per pixel of this display. Typical values include 8, 16, 24 and 32. + public int BitsPerPixel { get { Debug.Print("This method is not supported currently."); return bits_per_pixel; } } + + #endregion + + #region public float RefreshRate + + /// + /// Gets a System.Single representing the vertical refresh rate of this display. + /// + public float RefreshRate + { + get { return refresh_rate; } + } + + #endregion + + #endregion + + #region --- Overrides --- + + #region public override string ToString() + + /// + /// Returns a System.String representing this DisplayResolution. + /// + /// A System.String representing this DisplayResolution. + public override string ToString() + { + return String.Format("{0}x{1}x{2}@{3}Hz", width, height, bits_per_pixel, refresh_rate); + } + + #endregion + + #region public override bool Equals(object obj) + + /// Determines whether the specified resolutions are equal. + /// The System.Object to check against. + /// True if the System.Object is an equal DisplayResolution; false otherwise. + public override bool Equals(object obj) + { + if (obj is DisplayResolution) + { + DisplayResolution res = (DisplayResolution)obj; + return + Width == res.Width && + Height == res.Height && + BitsPerPixel == res.BitsPerPixel && + RefreshRate == res.RefreshRate; + } + + return false; + } + + #endregion + + #region public override int GetHashCode() + + /// Returns a unique hash representing this resolution. + /// A System.Int32 that may serve as a hash code for this resolution. + public override int GetHashCode() + { + return width ^ height ^ bits_per_pixel ^ (int)refresh_rate; + } + + #endregion + + #endregion + } +} diff --git a/Source/OpenTK/Graphics/GraphicsExceptions.cs b/Source/OpenTK/Graphics/GraphicsExceptions.cs new file mode 100644 index 00000000..4d3c6832 --- /dev/null +++ b/Source/OpenTK/Graphics/GraphicsExceptions.cs @@ -0,0 +1,28 @@ +#region --- License --- +/* Licensed under the MIT/X11 license. + * Copyright (c) 2006-2008 the OpenTK team. + * This notice may not be removed. + * See license.txt for licensing detailed licensing details. + */ +#endregion + +using System; +using System.Collections.Generic; +using System.Text; + +namespace OpenTK.Graphics +{ + /// Represents errors related to unavailable graphics parameters.. + public class GraphicsModeException : Exception + { + public GraphicsModeException() : base() { } + public GraphicsModeException(string message) : base(message) { } + } + + /// Represents errors related to a GraphicsContext. + public class GraphicsContextException : Exception + { + public GraphicsContextException() : base() { } + public GraphicsContextException(string message) : base(message) { } + } +} diff --git a/Source/OpenTK/Graphics/IDisplayDeviceDriver.cs b/Source/OpenTK/Graphics/IDisplayDeviceDriver.cs new file mode 100644 index 00000000..6845def2 --- /dev/null +++ b/Source/OpenTK/Graphics/IDisplayDeviceDriver.cs @@ -0,0 +1,22 @@ +#region --- License --- +/* Licensed under the MIT/X11 license. + * Copyright (c) 2006-2008 the OpenTK team. + * This notice may not be removed. + * See license.txt for licensing detailed licensing details. + */ +#endregion + +using System; +using System.Collections.Generic; +using System.Text; + +namespace OpenTK.Graphics +{ + internal interface IDisplayDeviceDriver + { + bool TryChangeResolution(int width, int height, int bitsPerPixel, float refreshRate); + void RestoreResolution(); + //DisplayDevice[] AvailableDevices { get; } + //DisplayResolution[] + } +}