Added checks against invalid arguments.

Made sealed.
This commit is contained in:
the_fiddler 2008-01-23 13:26:40 +00:00
parent 5b44be105c
commit 2fb1f0f0ef
3 changed files with 236 additions and 131 deletions

View file

@ -5,7 +5,6 @@
#endregion
using System;
using System.Drawing;
using System.Globalization;
namespace OpenTK
@ -15,7 +14,7 @@ namespace OpenTK
/// <para>A ColorMode contains Red, Green, Blue and Alpha components that descibe
/// the allocated bits per pixel for the corresponding color.</para>
/// </remarks>
public class ColorMode
public sealed class ColorMode
{
byte red, green, blue, alpha;
bool isIndexed = false;
@ -29,6 +28,8 @@ namespace OpenTK
/// <param name="bpp">The bits per pixel sum for the Red, Green, Blue and Alpha color channels.</param>
public ColorMode(int bpp)
{
if (bpp < 0)
throw new ArgumentOutOfRangeException("bpp", "Must be greater or equal to zero.");
Red = Green = Blue = Alpha = 0;
BitsPerPixel = bpp;
@ -77,6 +78,8 @@ namespace OpenTK
/// <param name="alpha">Bits per pixel for the Alpha color channel.</param>
public ColorMode(int red, int green, int blue, int alpha)
{
if (red < 0 || green < 0 || blue < 0 || alpha < 0)
throw new ArgumentOutOfRangeException("Arguments must be greater or equal to zero.");
Red = (byte)red;
Green = (byte)green;
Blue = (byte)blue;
@ -105,6 +108,25 @@ namespace OpenTK
#endregion
#region --- Operator Overloads ---
/// <summary>
/// Converts the specified bpp into a new ColorMode.
/// </summary>
/// <param name="bpp">The bits per pixel to convert.</param>
/// <returns>A ColorMode with the specified bits per pixel.</returns>
public static implicit operator ColorMode(int bpp)
{
return new ColorMode(bpp);
}
//public static implicit operator int(ColorMode mode)
//{
// return mode.BitsPerPixel;
//}
#endregion
#region --- Overrides ---
public override bool Equals(object obj)

View file

@ -14,14 +14,17 @@ using System.Globalization;
namespace OpenTK
{
using OpenTK.Graphics;
/// <summary>Defines the display mode for a render window.</summary>
public sealed class DisplayMode
{
#region --- Private Variables ---
private int width, height;
private ColorMode color;
private ColorMode color_format, auxilliary_color_format;
private int depthBits, stencilBits, auxBits;
private int depthBits, stencilBits;
private float refreshRate;
private bool vsync;
@ -33,12 +36,73 @@ namespace OpenTK
#region --- Constructors ---
/// <summary>
/// Constructs a new DisplayMode from the given DisplayMode.
/// </summary>
/// <param name="mode"></param>
public DisplayMode(DisplayMode mode)
: this(mode.Width, mode.Height, mode.Color, mode.DepthBits, mode.StencilBits, mode.AuxBits, mode.Buffers,
mode.Fullscreen, mode.Stereo, mode.Vsync, mode.RefreshRate)
: this(mode.ColorFormat, mode.Depth, mode.Stencil, mode.AuxilliaryColorFormat, mode.Buffers, mode.Stereo) { }
/// <summary>Constructs a new DisplayMode with sensible default parameters.</summary>
public DisplayMode()
: this(Display.PrimaryDisplay.BitsPerPixel, 16, 0, 0, 2, false)
{
}
/// <summary>Constructs a new DisplayMode with the specified parameters.</summary>
/// <param name="color">The ColorMode of the color buffer.</param>
public DisplayMode(ColorMode color)
: this(color, 16, 0, 0, 2, false) { }
/// <summary>Constructs a new DisplayMode with the specified parameters.</summary>
/// <param name="color">The ColorMode of the color buffer.</param>
/// <param name="depth">The number of bits in the depth buffer.</param>
public DisplayMode(ColorMode color, int depth)
: this(color, depth, 0, 0, 2, false) { }
/// <summary>Constructs a new DisplayMode with the specified parameters.</summary>
/// <param name="color">The ColorMode of the color buffer.</param>
/// <param name="depth">The number of bits in the depth buffer.</param>
/// <param name="stencil">The number of bits in the stencil buffer.</param>
public DisplayMode(ColorMode color, int depth, int stencil)
: this(color, depth, stencil, 0, 2, false) { }
/// <summary>Constructs a new DisplayMode with the specified parameters.</summary>
/// <param name="color">The ColorMode of the color buffer.</param>
/// <param name="depth">The number of bits in the depth buffer.</param>
/// <param name="stencil">The number of bits in the stencil buffer.</param>
/// <param name="aux">The ColorMode of the auxilliary buffer.</param>
public DisplayMode(ColorMode color, int depth, int stencil, ColorMode aux)
: this(color, depth, stencil, aux, 2, false) { }
/// <summary>Constructs a new DisplayMode with the specified parameters.</summary>
/// <param name="color">The ColorMode of the color buffer.</param>
/// <param name="depth">The number of bits in the depth buffer.</param>
/// <param name="stencil">The number of bits in the stencil buffer.</param>
/// <param name="aux">The ColorMode of the auxilliary buffer.</param>
/// <param name="buffers">The number of render buffers. Typical values include one (single-), two (double-) or three (triple-buffering).</param>
public DisplayMode(ColorMode color, int depth, int stencil, ColorMode aux, int buffers)
: this(color, depth, stencil, aux, buffers, false) { }
/// <summary>Constructs a new DisplayMode with the specified parameters.</summary>
/// <param name="color">The ColorMode of the color buffer.</param>
/// <param name="depth">The number of bits in the depth buffer.</param>
/// <param name="stencil">The number of bits in the stencil buffer.</param>
/// <param name="aux">The ColorMode of the auxilliary buffer.</param>
/// <param name="stereo">Set to true for a DisplayMode with stereographic capabilities.</param>
/// <param name="buffers">The number of render buffers. Typical values include one (single-), two (double-) or three (triple-buffering).</param>
public DisplayMode(ColorMode color, int depth, int stencil, ColorMode aux, int buffers, bool stereo)
{
this.ColorFormat = color;
this.Depth = depth;
this.Stencil = stencil;
this.AuxilliaryColorFormat = aux;
this.Buffers = buffers;
this.Stereo = stereo;
}
#region Obsolete Constructors
/// <summary>
/// Constructs a new DisplayMode from the specified parameters.
/// </summary>
@ -53,6 +117,7 @@ namespace OpenTK
/// <param name="buffers">The number of render buffers. Typical values include one (single-), two (double-) or three (triple-buffering).</param>
/// <param name="vsync">Set to true to sync the updates to the screen refresh rate.</param>
/// <param name="refresh">The desired RefreshRate. Taken into account only for Fullscreen DisplayModes.</param>
[Obsolete]
public DisplayMode(int width, int height, ColorMode color, int depth, int stencil, int aux, int buffers,
bool fullscreen, bool stereo, bool vsync, float refresh)
{
@ -69,63 +134,14 @@ namespace OpenTK
this.RefreshRate = refresh;
}
/// <summary>
/// Constructs a new DisplayMode from the specified parameters.
/// </summary>
/// <param name="width">The Width of the DisplayMode, in pixels.</param>
/// <param name="height">The Height of the DisplayMode, in pixels.</param>
/// <param name="color">The number of bits in the color buffer.</param>
/// <param name="depth">The number of bits in the depth buffer.</param>
/// <param name="stencil">The number of bits in the stencil buffer.</param>
/// <param name="aux">The number of bits in the auxilliary buffer.</param>
/// <param name="fullscreen">Set to true for a fullscreen DisplayMode.</param>
/// <param name="stereo">Set to true for a DisplayMode with stereographic capabilities.</param>
/// <param name="buffers">The number of render buffers. Typical values include one (single-), two (double-) or three (triple-buffering).</param>
/// <param name="vsync">Set to true to sync the updates to the screen refresh rate.</param>
/// <param name="refresh">The desired RefreshRate. Taken into account only for Fullscreen DisplayModes.</param>
public DisplayMode(int width, int height, int color, int depth, int stencil, int aux, int buffers,
bool fullscreen, bool stereo, bool vsync, float refresh)
{
this.Width = width;
this.Height = height;
this.Color = new ColorMode(color);
this.DepthBits = depth;
this.StencilBits = stencil;
this.AuxBits = aux;
this.Buffers = buffers;
this.Fullscreen = fullscreen;
this.Stereo = stereo;
this.Vsync = vsync;
this.RefreshRate = refresh;
}
/// <summary>
/// Constructs a new DisplayMode with default values.
/// </summary>
public DisplayMode()
: this(0, 0, new ColorMode(32), 16, 0, 0, 0, false, false, false, 0.0f)
{
}
/// <summary>
/// Constructs a new DisplayMode.
/// </summary>
/// <param name="width">The Width of the DisplayMode in pixels.</param>
/// <param name="height">The Height of the DisplayMode in pixels.</param>
[Obsolete]
public DisplayMode(int width, int height)
: this(width, height, new ColorMode(32), 16, 0, 0, 0, false, false, false, 0.0f)
{
}
/// <summary>
/// Constructs a new DisplayMode.
/// </summary>
/// <param name="width">The Width of the DisplayMode in pixels.</param>
/// <param name="height">The Height of the DisplayMode in pixels.</param>
/// <param name="color">The number of bits in the color buffer.</param>
public DisplayMode(int width, int height, int color)
: this(width, height, new ColorMode(color), 16, 0, 0, 0, false, false, false, 0.0f)
: this(width, height, Display.PrimaryDisplay.BitsPerPixel, 16, 0, 0, 0, false, false, false, 0.0f)
{
}
@ -135,23 +151,12 @@ namespace OpenTK
/// <param name="width">The Width of the DisplayMode in pixels.</param>
/// <param name="height">The Height of the DisplayMode in pixels.</param>
/// <param name="color">The ColorMode of the color buffer.</param>
[Obsolete]
public DisplayMode(int width, int height, ColorMode color)
: this(width, height, color, 16, 0, 0, 0, false, false, false, 0.0f)
{
}
/// <summary>
/// Constructs a new DisplayMode.
/// </summary>
/// <param name="width">The Width of the DisplayMode in pixels.</param>
/// <param name="height">The Height of the DisplayMode in pixels.</param>
/// <param name="color">The number of bits in the color buffer.</param>
/// <param name="depth">The number of bits in the depth buffer.</param>
public DisplayMode(int width, int height, int color, int depth)
: this(width, height, new ColorMode(color), depth, 0, 0, 0, false, false, false, 0.0f)
{
}
/// <summary>
/// Constructs a new DisplayMode.
/// </summary>
@ -159,24 +164,12 @@ namespace OpenTK
/// <param name="height">The Height of the DisplayMode in pixels.</param>
/// <param name="color">The ColorMode of the color buffer.</param>
/// <param name="depth">The number of bits in the depth buffer.</param>
[Obsolete]
public DisplayMode(int width, int height, ColorMode color, int depth)
: this(width, height, color, depth, 0, 0, 0, false, false, false, 0.0f)
{
}
/// <summary>
/// Constructs a new DisplayMode.
/// </summary>
/// <param name="width">The Width of the DisplayMode in pixels.</param>
/// <param name="height">The Height of the DisplayMode in pixels.</param>
/// <param name="color">The number of bits in the color buffer.</param>
/// <param name="depth">The number of bits in the depth buffer.</param>
/// <param name="fullscreen">True for a fullscreen DisplayMode, false otherwise.</param>
public DisplayMode(int width, int height, int color, int depth, bool fullscreen)
: this(width, height, color, depth, 0, 0, 0, fullscreen, false, false, 0.0f)
{
}
/// <summary>
/// Constructs a new DisplayMode.
/// </summary>
@ -185,6 +178,7 @@ namespace OpenTK
/// <param name="color">The ColorMode of the color buffer.</param>
/// <param name="depth">The number of bits in the depth buffer.</param>
/// <param name="fullscreen">True for a fullscreen DisplayMode, false otherwise.</param>
[Obsolete]
public DisplayMode(int width, int height, ColorMode color, int depth, bool fullscreen)
: this(width, height, color, depth, 0, 0, 0, fullscreen, false, false, 0.0f)
{
@ -192,8 +186,125 @@ namespace OpenTK
#endregion
#endregion
#region --- Public Properties ---
#region public int ColorFormat
/// <summary>
/// Gets an OpenTK.Graphics.ColorMode that describes the color format of this DisplayMode.
/// </summary>
public ColorMode ColorFormat
{
get { return color_format; }
private set { color_format = value; }
}
#endregion
#region public int AuxilliaryColorFormat
/// <summary>
/// Gets an OpenTK.Graphics.ColorMode that describes the color format of this DisplayMode.
/// </summary>
public ColorMode AuxilliaryColorFormat
{
get { return auxilliary_color_format; }
private set { auxilliary_color_format = value; }
}
#endregion
#region public int Depth
/// <summary>
/// Gets a System.Int32 that contains the bits per pixel for the depth buffer
/// of this DisplayMode.
/// </summary>
public int Depth
{
get { return depthBits; }
private set { depthBits = value; }
}
#endregion
#region public int Stencil
/// <summary>
/// Gets a System.Int32 that contains the bits per pixel for the stencil buffer
/// of this DisplayMode.
/// </summary>
public int Stencil
{
get { return stencilBits; }
private set { stencilBits = value; }
}
#endregion
#region public bool Stereo
/// <summary>
/// Gets a System.Boolean indicating whether this DisplayMode is stereoscopic.
/// </summary>
public bool Stereo
{
get { return this.stereo; }
private set { this.stereo = value; }
}
#endregion
#region public int Buffers
/// <summary>
/// Gets a System.Int32 containing the number of buffers associated with this
/// DisplayMode.
/// </summary>
public int Buffers
{
get { return this.buffers; }
private set { this.buffers = value; }
}
#endregion
#region Obsolete Properties
[Obsolete("Use GameWindow.Fullscreen instead.")]
public bool Fullscreen
{
get { return this.fullscreen; }
internal set { this.fullscreen = value; }
}
[Obsolete("Use GraphicsContext.VSync, GLControl.VSync or GameWindow.VSync instead.")]
public bool Vsync
{
get { return this.vsync; }
internal set { this.vsync = value; }
}
[Obsolete("Use OpenTK.Graphics.Display.RefreshRate instead.")]
public float RefreshRate
{
get { return this.refreshRate; }
private set { this.refreshRate = value; }
}
#region public ColorDepth Color
[Obsolete("Use DisplayMode.ColorFormat instead.")]
public ColorMode Color
{
get { return this.color_format; }
internal set { this.color_format = value; }
}
#endregion
#region public int Height
/// <summary>
@ -234,77 +345,49 @@ namespace OpenTK
#endregion
#region public ColorDepth Color
public ColorMode Color
{
get { return this.color; }
set { this.color = value; }
}
#endregion
[Obsolete("Use DisplayMode.Depth instead.")]
public int DepthBits
{
get { return this.depthBits; }
set { this.depthBits = value; }
internal set { this.depthBits = value; }
}
[Obsolete("Use DisplayMode.Stencil instead.")]
public int StencilBits
{
get { return this.stencilBits; }
set { this.stencilBits = value; }
internal set { this.stencilBits = value; }
}
[Obsolete("Use DisplayMode.AuxilliaryColorFormat instead.")]
public int AuxBits
{
get { return this.auxBits; }
set { this.auxBits = value; }
}
public bool Stereo
{
get { return this.stereo; }
set { this.stereo = value; }
}
public bool Fullscreen
{
get { return this.fullscreen; }
set { this.fullscreen = value; }
}
public bool Vsync
{
get { return this.vsync; }
set { this.vsync = value; }
}
public int Buffers
{
get { return this.buffers; }
set { this.buffers = value; }
}
public float RefreshRate
{
get { return this.refreshRate; }
set { this.refreshRate = value; }
get { return this.AuxilliaryColorFormat.BitsPerPixel; }
internal set { this.AuxilliaryColorFormat = value; }
}
#endregion
#endregion
#region --- Overrides ---
/// <summary>
/// Describes this DisplayMode instance.
/// </summary>
/// <returns>Returns a System.String that describes this DisplayMode instance.</returns>
public override string ToString()
{
return string.Format(
CultureInfo.CurrentCulture,
"{0}x{1}, rgba: {2}, depth: {3}, refresh {4}Hz",
Width, Height,
return string.Format("Display Mode: {0}, depth: {1}, stencil {2}, aux {3} refresh {4}Hz",
Color.ToString(),
DepthBits,
StencilBits,
AuxBits,
RefreshRate
);
}
#endregion
}
public class DisplayModeMatchOptions { }

View file

@ -71,7 +71,7 @@ namespace OpenTK.Graphics
public int BitsPerPixel { get { return bits_per_pixel; } }
/// <summary>Gets a System.Boolean that indicates whether this Display is the primary Display in systems with multiple Displays.</summary>
public bool Primary { get { return primary; } }
public bool IsPrimary { get { return primary; } }
/// <summary>
/// Gets an array of OpenTK.Display objects, which describe all available display devices.