Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1452 | chris | 1 | #region --- License --- |
| 2 | /* Licensed under the MIT/X11 license. |
||
| 3 | * Copyright (c) 2006-2008 the OpenTK Team. |
||
| 4 | * This notice may not be removed from any source distribution. |
||
| 5 | * See license.txt for licensing detailed licensing details. |
||
| 6 | */ |
||
| 7 | #endregion |
||
| 8 | |||
| 9 | using System; |
||
| 10 | using System.Collections.Generic; |
||
| 11 | using System.Text; |
||
| 12 | using System.Diagnostics; |
||
| 13 | |||
| 14 | namespace OpenTK.Graphics |
||
| 15 | { |
||
| 16 | /// <summary>Defines the format for graphics operations.</summary> |
||
| 17 | public class GraphicsMode : IEquatable<GraphicsMode> |
||
| 18 | { |
||
| 19 | ColorFormat color_format, accumulator_format; |
||
| 20 | int depth, stencil, buffers, samples; |
||
| 21 | bool stereo; |
||
| 22 | IntPtr? index = null; // The id of the pixel format or visual. |
||
| 23 | |||
| 24 | static GraphicsMode defaultMode; |
||
| 25 | static IGraphicsMode implementation; |
||
| 26 | static readonly object SyncRoot = new object(); |
||
| 27 | |||
| 28 | #region --- Constructors --- |
||
| 29 | |||
| 30 | #region static GraphicsMode() |
||
| 31 | |||
| 32 | static GraphicsMode() |
||
| 33 | { |
||
| 34 | lock (SyncRoot) |
||
| 35 | { |
||
| 36 | implementation = Platform.Factory.Default.CreateGraphicsMode(); |
||
| 37 | } |
||
| 38 | } |
||
| 39 | |||
| 40 | #endregion |
||
| 41 | |||
| 42 | #region internal GraphicsMode(GraphicsMode mode) |
||
| 43 | |||
| 44 | internal GraphicsMode(GraphicsMode mode) |
||
| 45 | : this(mode.ColorFormat, mode.Depth, mode.Stencil, mode.Samples, mode.AccumulatorFormat, mode.Buffers, mode.Stereo) { } |
||
| 46 | |||
| 47 | #endregion |
||
| 48 | |||
| 49 | #region internal GraphicsMode(IntPtr? index, ColorFormat color, int depth, int stencil, int samples, ColorFormat accum, int buffers, bool stereo) |
||
| 50 | |||
| 51 | internal GraphicsMode(IntPtr? index, ColorFormat color, int depth, int stencil, int samples, ColorFormat accum, |
||
| 52 | int buffers, bool stereo) |
||
| 53 | { |
||
| 54 | if (depth < 0) throw new ArgumentOutOfRangeException("depth", "Must be greater than, or equal to zero."); |
||
| 55 | if (stencil < 0) throw new ArgumentOutOfRangeException("stencil", "Must be greater than, or equal to zero."); |
||
| 56 | if (buffers <= 0) throw new ArgumentOutOfRangeException("buffers", "Must be greater than zero."); |
||
| 57 | if (samples < 0) throw new ArgumentOutOfRangeException("samples", "Must be greater than, or equal to zero."); |
||
| 58 | |||
| 59 | this.Index = index; |
||
| 60 | this.ColorFormat = color; |
||
| 61 | this.Depth = depth; |
||
| 62 | this.Stencil = stencil; |
||
| 63 | this.Samples = samples; |
||
| 64 | this.AccumulatorFormat = accum; |
||
| 65 | this.Buffers = buffers; |
||
| 66 | this.Stereo = stereo; |
||
| 67 | } |
||
| 68 | |||
| 69 | #endregion |
||
| 70 | |||
| 71 | #region public GraphicsMode() |
||
| 72 | |||
| 73 | /// <summary>Constructs a new GraphicsMode with sensible default parameters.</summary> |
||
| 74 | public GraphicsMode() |
||
| 75 | : this(Default) |
||
| 76 | { } |
||
| 77 | |||
| 78 | #endregion |
||
| 79 | |||
| 80 | #region public GraphicsMode(ColorFormat color) |
||
| 81 | |||
| 82 | /// <summary>Constructs a new GraphicsMode with the specified parameters.</summary> |
||
| 83 | /// <param name="color">The ColorFormat of the color buffer.</param> |
||
| 84 | public GraphicsMode(ColorFormat color) |
||
| 85 | : this(color, Default.Depth, Default.Stencil, Default.Samples, Default.AccumulatorFormat, Default.Buffers, Default.Stereo) |
||
| 86 | { } |
||
| 87 | |||
| 88 | #endregion |
||
| 89 | |||
| 90 | #region public GraphicsMode(ColorFormat color, int depth) |
||
| 91 | |||
| 92 | /// <summary>Constructs a new GraphicsMode with the specified parameters.</summary> |
||
| 93 | /// <param name="color">The ColorFormat of the color buffer.</param> |
||
| 94 | /// <param name="depth">The number of bits in the depth buffer.</param> |
||
| 95 | public GraphicsMode(ColorFormat color, int depth) |
||
| 96 | : this(color, depth, Default.Stencil, Default.Samples, Default.AccumulatorFormat, Default.Buffers, Default.Stereo) |
||
| 97 | { } |
||
| 98 | |||
| 99 | #endregion |
||
| 100 | |||
| 101 | #region public GraphicsMode(ColorFormat color, int depth, int stencil) |
||
| 102 | |||
| 103 | /// <summary>Constructs a new GraphicsMode with the specified parameters.</summary> |
||
| 104 | /// <param name="color">The ColorFormat of the color buffer.</param> |
||
| 105 | /// <param name="depth">The number of bits in the depth buffer.</param> |
||
| 106 | /// <param name="stencil">The number of bits in the stencil buffer.</param> |
||
| 107 | public GraphicsMode(ColorFormat color, int depth, int stencil) |
||
| 108 | : this(color, depth, stencil, Default.Samples, Default.AccumulatorFormat, Default.Buffers, Default.Stereo) |
||
| 109 | { } |
||
| 110 | |||
| 111 | #endregion |
||
| 112 | |||
| 113 | #region public GraphicsMode(ColorFormat color, int depth, int stencil, int samples) |
||
| 114 | |||
| 115 | /// <summary>Constructs a new GraphicsMode with the specified parameters.</summary> |
||
| 116 | /// <param name="color">The ColorFormat of the color buffer.</param> |
||
| 117 | /// <param name="depth">The number of bits in the depth buffer.</param> |
||
| 118 | /// <param name="stencil">The number of bits in the stencil buffer.</param> |
||
| 119 | /// <param name="samples">The number of samples for FSAA.</param> |
||
| 120 | public GraphicsMode(ColorFormat color, int depth, int stencil, int samples) |
||
| 121 | : this(color, depth, stencil, samples, Default.AccumulatorFormat, Default.Buffers, Default.Stereo) |
||
| 122 | { } |
||
| 123 | |||
| 124 | #endregion |
||
| 125 | |||
| 126 | #region public GraphicsMode(ColorFormat color, int depth, int stencil, int samples, ColorFormat accum) |
||
| 127 | |||
| 128 | /// <summary>Constructs a new GraphicsMode with the specified parameters.</summary> |
||
| 129 | /// <param name="color">The ColorFormat of the color buffer.</param> |
||
| 130 | /// <param name="depth">The number of bits in the depth buffer.</param> |
||
| 131 | /// <param name="stencil">The number of bits in the stencil buffer.</param> |
||
| 132 | /// <param name="samples">The number of samples for FSAA.</param> |
||
| 133 | /// <param name="accum">The ColorFormat of the accumilliary buffer.</param> |
||
| 134 | public GraphicsMode(ColorFormat color, int depth, int stencil, int samples, ColorFormat accum) |
||
| 135 | : this(color, depth, stencil, samples, accum, Default.Buffers, Default.Stereo) |
||
| 136 | { } |
||
| 137 | |||
| 138 | #endregion |
||
| 139 | |||
| 140 | #region public GraphicsMode(ColorFormat color, int depth, int stencil, int samples, ColorFormat accum, int buffers) |
||
| 141 | |||
| 142 | /// <summary>Constructs a new GraphicsMode with the specified parameters.</summary> |
||
| 143 | /// <param name="color">The ColorFormat of the color buffer.</param> |
||
| 144 | /// <param name="depth">The number of bits in the depth buffer.</param> |
||
| 145 | /// <param name="stencil">The number of bits in the stencil buffer.</param> |
||
| 146 | /// <param name="samples">The number of samples for FSAA.</param> |
||
| 147 | /// <param name="accum">The ColorFormat of the accumilliary buffer.</param> |
||
| 148 | /// <param name="buffers">The number of render buffers. Typical values include one (single-), two (double-) or three (triple-buffering).</param> |
||
| 149 | public GraphicsMode(ColorFormat color, int depth, int stencil, int samples, ColorFormat accum, int buffers) |
||
| 150 | : this(color, depth, stencil, samples, accum, buffers, Default.Stereo) |
||
| 151 | { } |
||
| 152 | |||
| 153 | #endregion |
||
| 154 | |||
| 155 | #region public GraphicsMode(ColorFormat color, int depth, int stencil, int samples, ColorFormat accum, int buffers, bool stereo) |
||
| 156 | |||
| 157 | /// <summary>Constructs a new GraphicsMode with the specified parameters.</summary> |
||
| 158 | /// <param name="color">The ColorFormat of the color buffer.</param> |
||
| 159 | /// <param name="depth">The number of bits in the depth buffer.</param> |
||
| 160 | /// <param name="stencil">The number of bits in the stencil buffer.</param> |
||
| 161 | /// <param name="samples">The number of samples for FSAA.</param> |
||
| 162 | /// <param name="accum">The ColorFormat of the accumilliary buffer.</param> |
||
| 163 | /// <param name="stereo">Set to true for a GraphicsMode with stereographic capabilities.</param> |
||
| 164 | /// <param name="buffers">The number of render buffers. Typical values include one (single-), two (double-) or three (triple-buffering).</param> |
||
| 165 | public GraphicsMode(ColorFormat color, int depth, int stencil, int samples, ColorFormat accum, int buffers, bool stereo) |
||
| 166 | : this(null, color, depth, stencil, samples, accum, buffers, stereo) { } |
||
| 167 | |||
| 168 | #endregion |
||
| 169 | |||
| 170 | #endregion |
||
| 171 | |||
| 172 | #region --- Public Methods --- |
||
| 173 | |||
| 174 | #region public IntPtr Index |
||
| 175 | |||
| 176 | /// <summary> |
||
| 177 | /// Gets a nullable <see cref="System.IntPtr"/> value, indicating the platform-specific index for this GraphicsMode. |
||
| 178 | /// </summary> |
||
| 179 | public IntPtr? Index |
||
| 180 | { |
||
| 181 | get |
||
| 182 | { |
||
| 183 | LazySelectGraphicsMode(); |
||
| 184 | return index; |
||
| 185 | } |
||
| 186 | set { index = value; } |
||
| 187 | } |
||
| 188 | |||
| 189 | #endregion |
||
| 190 | |||
| 191 | #region public int ColorFormat |
||
| 192 | |||
| 193 | /// <summary> |
||
| 194 | /// Gets an OpenTK.Graphics.ColorFormat that describes the color format for this GraphicsFormat. |
||
| 195 | /// </summary> |
||
| 196 | public ColorFormat ColorFormat |
||
| 197 | { |
||
| 198 | get |
||
| 199 | { |
||
| 200 | LazySelectGraphicsMode(); |
||
| 201 | return color_format; |
||
| 202 | } |
||
| 203 | private set { color_format = value; } |
||
| 204 | } |
||
| 205 | |||
| 206 | #endregion |
||
| 207 | |||
| 208 | #region public int AccumulatorFormat |
||
| 209 | |||
| 210 | /// <summary> |
||
| 211 | /// Gets an OpenTK.Graphics.ColorFormat that describes the accumulator format for this GraphicsFormat. |
||
| 212 | /// </summary> |
||
| 213 | public ColorFormat AccumulatorFormat |
||
| 214 | { |
||
| 215 | get |
||
| 216 | { |
||
| 217 | LazySelectGraphicsMode(); |
||
| 218 | return accumulator_format; |
||
| 219 | } |
||
| 220 | private set { accumulator_format = value; } |
||
| 221 | } |
||
| 222 | |||
| 223 | #endregion |
||
| 224 | |||
| 225 | #region public int Depth |
||
| 226 | |||
| 227 | /// <summary> |
||
| 228 | /// Gets a System.Int32 that contains the bits per pixel for the depth buffer |
||
| 229 | /// for this GraphicsFormat. |
||
| 230 | /// </summary> |
||
| 231 | public int Depth |
||
| 232 | { |
||
| 233 | get |
||
| 234 | { |
||
| 235 | LazySelectGraphicsMode(); |
||
| 236 | return depth; |
||
| 237 | } |
||
| 238 | private set { depth = value; } |
||
| 239 | } |
||
| 240 | |||
| 241 | #endregion |
||
| 242 | |||
| 243 | #region public int Stencil |
||
| 244 | |||
| 245 | /// <summary> |
||
| 246 | /// Gets a System.Int32 that contains the bits per pixel for the stencil buffer |
||
| 247 | /// of this GraphicsFormat. |
||
| 248 | /// </summary> |
||
| 249 | public int Stencil |
||
| 250 | { |
||
| 251 | get |
||
| 252 | { |
||
| 253 | LazySelectGraphicsMode(); |
||
| 254 | return stencil; |
||
| 255 | } |
||
| 256 | private set { stencil = value; } |
||
| 257 | } |
||
| 258 | |||
| 259 | #endregion |
||
| 260 | |||
| 261 | #region public int Samples |
||
| 262 | |||
| 263 | /// <summary> |
||
| 264 | /// Gets a System.Int32 that contains the number of FSAA samples per pixel for this GraphicsFormat. |
||
| 265 | /// </summary> |
||
| 266 | public int Samples |
||
| 267 | { |
||
| 268 | get |
||
| 269 | { |
||
| 270 | LazySelectGraphicsMode(); |
||
| 271 | return samples; |
||
| 272 | } |
||
| 273 | private set { samples = value; } |
||
| 274 | } |
||
| 275 | |||
| 276 | #endregion |
||
| 277 | |||
| 278 | #region public bool Stereo |
||
| 279 | |||
| 280 | /// <summary> |
||
| 281 | /// Gets a System.Boolean indicating whether this DisplayMode is stereoscopic. |
||
| 282 | /// </summary> |
||
| 283 | public bool Stereo |
||
| 284 | { |
||
| 285 | get |
||
| 286 | { |
||
| 287 | LazySelectGraphicsMode(); |
||
| 288 | return stereo; |
||
| 289 | } |
||
| 290 | private set { stereo = value; } |
||
| 291 | } |
||
| 292 | |||
| 293 | #endregion |
||
| 294 | |||
| 295 | #region public int Buffers |
||
| 296 | |||
| 297 | /// <summary> |
||
| 298 | /// Gets a System.Int32 containing the number of buffers associated with this |
||
| 299 | /// DisplayMode. |
||
| 300 | /// </summary> |
||
| 301 | public int Buffers |
||
| 302 | { |
||
| 303 | get |
||
| 304 | { |
||
| 305 | LazySelectGraphicsMode(); |
||
| 306 | return buffers; |
||
| 307 | } |
||
| 308 | private set { buffers = value; } |
||
| 309 | } |
||
| 310 | |||
| 311 | #endregion |
||
| 312 | |||
| 313 | #region public static GraphicsFormat Default |
||
| 314 | |||
| 315 | /// <summary>Returns an OpenTK.GraphicsFormat compatible with the underlying platform.</summary> |
||
| 316 | public static GraphicsMode Default |
||
| 317 | { |
||
| 318 | get |
||
| 319 | { |
||
| 320 | lock (SyncRoot) |
||
| 321 | { |
||
| 322 | if (defaultMode == null) |
||
| 323 | { |
||
| 324 | Debug.Print("Creating default GraphicsMode ({0}, {1}, {2}, {3}, {4}, {5}, {6}).", DisplayDevice.Default.BitsPerPixel, |
||
| 325 | 16, 0, 0, 0, 2, false); |
||
| 326 | defaultMode = new GraphicsMode(DisplayDevice.Default.BitsPerPixel, 16, 0, 0, 0, 2, false); |
||
| 327 | } |
||
| 328 | return defaultMode; |
||
| 329 | } |
||
| 330 | } |
||
| 331 | } |
||
| 332 | |||
| 333 | #endregion |
||
| 334 | |||
| 335 | #endregion |
||
| 336 | |||
| 337 | #region --- Private Methods --- |
||
| 338 | |||
| 339 | // Queries the implementation for the actual graphics mode if this hasn't been done already. |
||
| 340 | // This method allows for lazy evaluation of the actual GraphicsMode and should be called |
||
| 341 | // by all GraphicsMode properties. |
||
| 342 | void LazySelectGraphicsMode() |
||
| 343 | { |
||
| 344 | if (index == null) |
||
| 345 | { |
||
| 346 | GraphicsMode mode = implementation.SelectGraphicsMode(color_format, depth, stencil, samples, accumulator_format, buffers, stereo); |
||
| 347 | |||
| 348 | Index = mode.Index; |
||
| 349 | ColorFormat = mode.ColorFormat; |
||
| 350 | Depth = mode.Depth; |
||
| 351 | Stencil = mode.Stencil; |
||
| 352 | Samples = mode.Samples; |
||
| 353 | AccumulatorFormat = mode.AccumulatorFormat; |
||
| 354 | Buffers = mode.Buffers; |
||
| 355 | Stereo = mode.Stereo; |
||
| 356 | } |
||
| 357 | } |
||
| 358 | |||
| 359 | #endregion |
||
| 360 | |||
| 361 | #region --- Overrides --- |
||
| 362 | |||
| 363 | /// <summary>Returns a System.String describing the current GraphicsFormat.</summary> |
||
| 364 | /// <returns>! System.String describing the current GraphicsFormat.</returns> |
||
| 365 | public override string ToString() |
||
| 366 | { |
||
| 367 | return String.Format("Index: {0}, Color: {1}, Depth: {2}, Stencil: {3}, Samples: {4}, Accum: {5}, Buffers: {6}, Stereo: {7}", |
||
| 368 | Index, ColorFormat, Depth, Stencil, Samples, AccumulatorFormat, Buffers, Stereo); |
||
| 369 | } |
||
| 370 | |||
| 371 | /// <summary> |
||
| 372 | /// Returns the hashcode for this instance. |
||
| 373 | /// </summary> |
||
| 374 | /// <returns>A <see cref="System.Int32"/> hashcode for this instance.</returns> |
||
| 375 | public override int GetHashCode() |
||
| 376 | { |
||
| 377 | return Index.GetHashCode(); |
||
| 378 | } |
||
| 379 | |||
| 380 | /// <summary> |
||
| 381 | /// Indicates whether obj is equal to this instance. |
||
| 382 | /// </summary> |
||
| 383 | /// <param name="obj">An object instance to compare for equality.</param> |
||
| 384 | /// <returns>True, if obj equals this instance; false otherwise.</returns> |
||
| 385 | public override bool Equals(object obj) |
||
| 386 | { |
||
| 387 | if (obj is GraphicsMode) |
||
| 388 | { |
||
| 389 | return Equals((GraphicsMode)obj); |
||
| 390 | } |
||
| 391 | return false; |
||
| 392 | } |
||
| 393 | |||
| 394 | #endregion |
||
| 395 | |||
| 396 | #region IEquatable<GraphicsMode> Members |
||
| 397 | |||
| 398 | /// <summary> |
||
| 399 | /// Indicates whether other represents the same mode as this instance. |
||
| 400 | /// </summary> |
||
| 401 | /// <param name="other">The GraphicsMode to compare to.</param> |
||
| 402 | /// <returns>True, if other is equal to this instance; false otherwise.</returns> |
||
| 403 | public bool Equals(GraphicsMode other) |
||
| 404 | { |
||
| 405 | return Index.HasValue && Index == other.Index; |
||
| 406 | } |
||
| 407 | |||
| 408 | #endregion |
||
| 409 | } |
||
| 410 | } |