WinDisplayDevice is now multiple-monitor aware.

Added SelectResolution function to DisplayDevice.
Updated test to change settings on all available monitors.
This commit is contained in:
the_fiddler 2008-01-24 10:50:39 +00:00
parent e9b811de17
commit a4fe343d73
5 changed files with 106 additions and 49 deletions

View file

@ -24,9 +24,11 @@ namespace Examples.Tests
foreach (DisplayDevice dev in DisplayDevice.AvailableDisplays) foreach (DisplayDevice dev in DisplayDevice.AvailableDisplays)
{ {
Trace.WriteLine(dev.ToString()); Trace.WriteLine(dev.ToString());
//MessageBox.Show(dev.ToString()); MessageBox.Show(dev.ToString());
//dev.ChangeResolution(640, 480, 32, 60.0f); // Switch to the first available resolution that has the same bpp as the current one (usually 640x480@60Hz)
//dev.RestoreResolution(); dev.ChangeResolution(dev.SelectResolution(0, 0, dev.BitsPerPixel, 0));
MessageBox.Show(dev.ToString());
dev.RestoreResolution();
} }
} }
} }

View file

@ -67,7 +67,6 @@ namespace OpenTK.Graphics
#region --- Public Methods --- #region --- Public Methods ---
#region public int Width #region public int Width
/// <summary>Gets a System.Int32 that contains the width of this display in pixels.</summary> /// <summary>Gets a System.Int32 that contains the width of this display in pixels.</summary>
@ -108,7 +107,54 @@ namespace OpenTK.Graphics
#endregion #endregion
#region public void ChangeResolution(int width, int height, int bitsPerPixel, float refreshRate) #region public DisplayResolution[] AvailableResolutions
/// <summary>
/// Gets an array of OpenTK.DisplayResolution objects, which describe all available resolutions
/// for this device.
/// </summary>
public DisplayResolution[] AvailableResolutions
{
get
{
lock (display_lock)
{
return available_resolutions.ToArray();
}
}
}
#endregion
#region public DisplayResolution SelectResolution(int width, int height, int bitsPerPixel, float refreshRate)
/// <summary>
/// Selects an available resolution that matches the specified parameters.
/// </summary>
/// <param name="width">The width of the requested resolution in pixels.</param>
/// <param name="height">The height of the requested resolution in pixels.</param>
/// <param name="bitsPerPixel">The bits per pixel of the requested resolution.</param>
/// <param name="refreshRate">The refresh rate of the requested resolution in Herz.</param>
/// <returns>The requested DisplayResolution or null if the parameters cannot be met.</returns>
/// <remarks>
/// <para>A parameter set to 0 will not be used in the search (e.g. if refreshRate is 0, any refresh rate will be considered valid).</para>
/// <para>This function generates garbage.</para>
/// </remarks>
public DisplayResolution SelectResolution(int width, int height, int bitsPerPixel, float refreshRate)
{
return available_resolutions.Find(delegate(DisplayResolution test)
{
return
((width > 0 && width == test.Width) || width == 0) &&
((height > 0 && height == test.Height) || height == 0) &&
((bitsPerPixel > 0 && bitsPerPixel == test.BitsPerPixel) || bitsPerPixel == 0) &&
((refreshRate > 0 && (int)refreshRate == (int)test.RefreshRate) || refreshRate == 0);
});
}
#endregion
#region public void ChangeResolution(DisplayResolution resolution)
/// <summary>Changes the resolution of the DisplayDevice.</summary> /// <summary>Changes the resolution of the DisplayDevice.</summary>
/// <param name="width">The new width of the DisplayDevice.</param> /// <param name="width">The new width of the DisplayDevice.</param>
@ -116,20 +162,18 @@ namespace OpenTK.Graphics
/// <param name="bitsPerPixel">The new bits per pixel of the DisplayDevice.</param> /// <param name="bitsPerPixel">The new bits per pixel of the DisplayDevice.</param>
/// <param name="refreshRate">The new refresh rate of the DisplayDevice.</param> /// <param name="refreshRate">The new refresh rate of the DisplayDevice.</param>
/// <exception cref="GraphicsModeException">Thrown if the requested resolution change failed.</exception> /// <exception cref="GraphicsModeException">Thrown if the requested resolution change failed.</exception>
public void ChangeResolution(int width, int height, int bitsPerPixel, float refreshRate) public void ChangeResolution(DisplayResolution resolution)
{ {
if (width <= 0) throw new ArgumentOutOfRangeException("width", "Must be greater than zero."); if (resolution == null)
if (height <= 0) throw new ArgumentOutOfRangeException("height", "Must be greater than zero."); throw new ArgumentNullException("resulotion", "Must be a valid resolution.");
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)) if (implementation.TryChangeResolution(this, resolution))
{ {
current_resolution = new DisplayResolution(width, height, bitsPerPixel, refreshRate); current_resolution = resolution;
} }
else else
throw new GraphicsModeException(String.Format("Device {0}: Failed to change resolution to {1}x{2}x{3]@{4]Hz", throw new GraphicsModeException(String.Format("Device {0}: Failed to change resolution to {1}.",
ToString(), width, height, bitsPerPixel, refreshRate)); this, resolution));
} }
#endregion #endregion
@ -138,7 +182,7 @@ namespace OpenTK.Graphics
public void RestoreResolution() public void RestoreResolution()
{ {
implementation.RestoreResolution(); implementation.RestoreResolution(this);
} }
#endregion #endregion
@ -146,7 +190,7 @@ namespace OpenTK.Graphics
#region public static DisplayDevice[] AvailableDisplays #region public static DisplayDevice[] AvailableDisplays
/// <summary> /// <summary>
/// Gets an array of OpenTK.Display objects, which describe all available display devices. /// Gets an array of OpenTK.DisplayDevice objects, which describe all available display devices.
/// </summary> /// </summary>
public static DisplayDevice[] AvailableDisplays public static DisplayDevice[] AvailableDisplays
{ {
@ -191,19 +235,19 @@ namespace OpenTK.Graphics
/// <summary>Determines whether the specified DisplayDevices are equal.</summary> /// <summary>Determines whether the specified DisplayDevices are equal.</summary>
/// <param name="obj">The System.Object to check against.</param> /// <param name="obj">The System.Object to check against.</param>
/// <returns>True if the System.Object is an equal DisplayDevice; false otherwise.</returns> /// <returns>True if the System.Object is an equal DisplayDevice; false otherwise.</returns>
public override bool Equals(object obj) //public override bool Equals(object obj)
{ //{
if (obj is DisplayDevice) // if (obj is DisplayDevice)
{ // {
DisplayDevice dev = (DisplayDevice)obj; // DisplayDevice dev = (DisplayDevice)obj;
return // return
IsPrimary == dev.IsPrimary && // IsPrimary == dev.IsPrimary &&
current_resolution == dev.current_resolution && // current_resolution == dev.current_resolution &&
available_resolutions.Count == dev.available_resolutions.Count; // available_resolutions.Count == dev.available_resolutions.Count;
} // }
return false; // return false;
} //}
#endregion #endregion
@ -211,10 +255,10 @@ namespace OpenTK.Graphics
/// <summary>Returns a unique hash representing this DisplayDevice.</summary> /// <summary>Returns a unique hash representing this DisplayDevice.</summary>
/// <returns>A System.Int32 that may serve as a hash code for this DisplayDevice.</returns> /// <returns>A System.Int32 that may serve as a hash code for this DisplayDevice.</returns>
public override int GetHashCode() //public override int GetHashCode()
{ //{
return current_resolution.GetHashCode() ^ IsPrimary.GetHashCode() ^ available_resolutions.Count; // return current_resolution.GetHashCode() ^ IsPrimary.GetHashCode() ^ available_resolutions.Count;
} //}
#endregion #endregion

View file

@ -24,6 +24,11 @@ namespace OpenTK.Graphics
internal DisplayResolution(int width, int height, int bitsPerPixel, float refreshRate) internal DisplayResolution(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.");
this.width = width; this.width = width;
this.height = height; this.height = height;
this.bits_per_pixel = bitsPerPixel; this.bits_per_pixel = bitsPerPixel;

View file

@ -14,8 +14,8 @@ namespace OpenTK.Graphics
{ {
internal interface IDisplayDeviceDriver internal interface IDisplayDeviceDriver
{ {
bool TryChangeResolution(int width, int height, int bitsPerPixel, float refreshRate); bool TryChangeResolution(DisplayDevice device, DisplayResolution resolution);
void RestoreResolution(); void RestoreResolution(DisplayDevice device);
//DisplayDevice[] AvailableDevices { get; } //DisplayDevice[] AvailableDevices { get; }
//DisplayResolution[] //DisplayResolution[]
} }

View file

@ -18,8 +18,9 @@ namespace OpenTK.Platform.Windows
{ {
internal class WinDisplayDeviceDriver : IDisplayDeviceDriver internal class WinDisplayDeviceDriver : IDisplayDeviceDriver
{ {
// In OpenTK nomenclature, a DisplayDevice is a screen, not a video card!
static object display_lock = new object(); static object display_lock = new object();
static Dictionary<OpenTK.Graphics.DisplayDevice, string> available_device_names =
new Dictionary<OpenTK.Graphics.DisplayDevice, string>(); // Needed for ChangeDisplaySettingsEx
#region --- Constructors --- #region --- Constructors ---
@ -71,6 +72,8 @@ namespace OpenTK.Platform.Windows
// Construct the OpenTK DisplayDevice through the accumulated parameters. // Construct the OpenTK DisplayDevice through the accumulated parameters.
opentk_dev = new OpenTK.Graphics.DisplayDevice(opentk_dev_current_res, opentk_dev_primary, opentk_dev = new OpenTK.Graphics.DisplayDevice(opentk_dev_current_res, opentk_dev_primary,
opentk_dev_available_res); opentk_dev_available_res);
available_device_names.Add(opentk_dev, dev1.DeviceName);
} }
} }
} }
@ -83,31 +86,34 @@ namespace OpenTK.Platform.Windows
#region --- IDisplayDeviceDriver Members --- #region --- IDisplayDeviceDriver Members ---
#region public bool TryChangeResolution(int width, int height, int bitsPerPixel, float refreshRate) #region public bool TryChangeResolution(OpenTK.Graphics.DisplayDevice device, DisplayResolution resolution)
public bool TryChangeResolution(int width, int height, int bitsPerPixel, float refreshRate) public bool TryChangeResolution(OpenTK.Graphics.DisplayDevice device, DisplayResolution resolution)
{ {
DeviceMode settings = new DeviceMode(); DeviceMode mode = new DeviceMode();
settings.PelsWidth = width; mode.PelsWidth = resolution.Width;
settings.PelsHeight = height; mode.PelsHeight = resolution.Height;
settings.BitsPerPel = bitsPerPixel; mode.BitsPerPel = resolution.BitsPerPixel;
settings.DisplayFrequency = (int)refreshRate; mode.DisplayFrequency = (int)resolution.RefreshRate;
settings.Fields = Constants.DM_BITSPERPEL mode.Fields = Constants.DM_BITSPERPEL
| Constants.DM_PELSWIDTH | Constants.DM_PELSWIDTH
| Constants.DM_PELSHEIGHT; | Constants.DM_PELSHEIGHT
| Constants.DM_DISPLAYFREQUENCY;
return Functions.ChangeDisplaySettings(settings, ChangeDisplaySettingsEnum.Fullscreen) == //return Functions.ChangeDisplaySettings(settings, ChangeDisplaySettingsEnum.Fullscreen) ==
// Constants.DISP_CHANGE_SUCCESSFUL;
return Functions.ChangeDisplaySettingsEx(available_device_names[device], mode, IntPtr.Zero, 0, IntPtr.Zero) ==
Constants.DISP_CHANGE_SUCCESSFUL; Constants.DISP_CHANGE_SUCCESSFUL;
} }
#endregion #endregion
#region public void RestoreResolution() #region public void RestoreResolution(OpenTK.Graphics.DisplayDevice device)
public void RestoreResolution() public void RestoreResolution(OpenTK.Graphics.DisplayDevice device)
{ {
Functions.ChangeDisplaySettings(null, (ChangeDisplaySettingsEnum)0); //Functions.ChangeDisplaySettings(null, (ChangeDisplaySettingsEnum)0);
//Functions.ChangeDisplaySettings( Functions.ChangeDisplaySettingsEx(available_device_names[device], null, IntPtr.Zero, 0, IntPtr.Zero);
} }
#endregion #endregion