From 56cbbb8cbbc7691bafabd4a1040da86a0c9072fe Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Thu, 31 Jan 2008 13:15:17 +0000 Subject: [PATCH] Added setter to GLContext.CurrentContext and all implementations, so that the user can clear the context of any thread. Updated GameWindow and its implementations to use GraphicsFormat instead of the obsolete DisplayMode. Updated license. Updated some docs. Fixed CLSCopmliant warnings in XRamExtension.cs --- Source/OpenTK/DisplayMode.cs | 108 +++--------- Source/OpenTK/GLContext.cs | 28 ++-- Source/OpenTK/GameWindow.cs | 158 +++++++----------- Source/OpenTK/OpenAL/XRamExtension.cs | 4 +- Source/OpenTK/Platform/DummyGLContext.cs | 2 +- Source/OpenTK/Platform/IGLContext.cs | 28 ++-- Source/OpenTK/Platform/INativeGLWindow.cs | 12 +- .../OpenTK/Platform/Windows/WinGLContext.cs | 44 +++-- Source/OpenTK/Platform/Windows/WinGLNative.cs | 10 +- Source/OpenTK/Platform/X11/API.cs | 14 +- Source/OpenTK/Platform/X11/Functions.cs | 26 +-- Source/OpenTK/Platform/X11/X11GLContext.cs | 49 ++++-- Source/OpenTK/Platform/X11/X11GLNative.cs | 45 +++-- .../Platform/X11/X11XrandrDisplayDevice.cs | 6 +- 14 files changed, 247 insertions(+), 287 deletions(-) diff --git a/Source/OpenTK/DisplayMode.cs b/Source/OpenTK/DisplayMode.cs index 90c0c1b9..17410045 100644 --- a/Source/OpenTK/DisplayMode.cs +++ b/Source/OpenTK/DisplayMode.cs @@ -1,6 +1,8 @@ #region --- License --- -/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos - * See license.txt for license info +/* Licensed under the MIT/X11 license. + * Copyright (c) 2006-2008 the OpenTK Team. + * This notice may not be removed from any source distribution. + * See license.txt for licensing detailed licensing details. */ #endregion @@ -17,6 +19,7 @@ namespace OpenTK using OpenTK.Graphics; /// Defines the display mode for a render window. + [Obsolete] public sealed class DisplayMode { #region --- Private Variables --- @@ -36,72 +39,11 @@ namespace OpenTK #region --- Constructors --- - /// - /// Constructs a new DisplayMode from the given DisplayMode. - /// - /// public DisplayMode(DisplayMode mode) - : this(mode.ColorFormat, mode.Depth, mode.Stencil, mode.AuxilliaryColorFormat, mode.Buffers, mode.Stereo) { } + : this(mode.width, mode.height, mode.color_format, mode.depthBits, mode.stencilBits, mode.auxilliary_color_format.BitsPerPixel, + mode.buffers, mode.fullscreen, mode.stereo, mode.vsync, mode.refreshRate) { } - /// Constructs a new DisplayMode with sensible default parameters. - public DisplayMode() - : this(DisplayDevice.PrimaryDisplay.BitsPerPixel, 16, 0, 0, 2, false) - { - } - - /// Constructs a new DisplayMode with the specified parameters. - /// The ColorMode of the color buffer. - public DisplayMode(ColorMode color) - : this(color, 16, 0, 0, 2, false) { } - - /// Constructs a new DisplayMode with the specified parameters. - /// The ColorMode of the color buffer. - /// The number of bits in the depth buffer. - public DisplayMode(ColorMode color, int depth) - : this(color, depth, 0, 0, 2, false) { } - - /// Constructs a new DisplayMode with the specified parameters. - /// The ColorMode of the color buffer. - /// The number of bits in the depth buffer. - /// The number of bits in the stencil buffer. - public DisplayMode(ColorMode color, int depth, int stencil) - : this(color, depth, stencil, 0, 2, false) { } - - /// Constructs a new DisplayMode with the specified parameters. - /// The ColorMode of the color buffer. - /// The number of bits in the depth buffer. - /// The number of bits in the stencil buffer. - /// The ColorMode of the auxilliary buffer. - public DisplayMode(ColorMode color, int depth, int stencil, ColorMode aux) - : this(color, depth, stencil, aux, 2, false) { } - - /// Constructs a new DisplayMode with the specified parameters. - /// The ColorMode of the color buffer. - /// The number of bits in the depth buffer. - /// The number of bits in the stencil buffer. - /// The ColorMode of the auxilliary buffer. - /// The number of render buffers. Typical values include one (single-), two (double-) or three (triple-buffering). - public DisplayMode(ColorMode color, int depth, int stencil, ColorMode aux, int buffers) - : this(color, depth, stencil, aux, buffers, false) { } - - /// Constructs a new DisplayMode with the specified parameters. - /// The ColorMode of the color buffer. - /// The number of bits in the depth buffer. - /// The number of bits in the stencil buffer. - /// The ColorMode of the auxilliary buffer. - /// Set to true for a DisplayMode with stereographic capabilities. - /// The number of render buffers. Typical values include one (single-), two (double-) or three (triple-buffering). - 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 + public DisplayMode() : this(640, 480) { } /// /// Constructs a new DisplayMode from the specified parameters. @@ -117,7 +59,6 @@ namespace OpenTK /// The number of render buffers. Typical values include one (single-), two (double-) or three (triple-buffering). /// Set to true to sync the updates to the screen refresh rate. /// The desired RefreshRate. Taken into account only for Fullscreen DisplayModes. - [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) { @@ -139,7 +80,6 @@ namespace OpenTK /// /// The Width of the DisplayMode in pixels. /// The Height of the DisplayMode in pixels. - [Obsolete] public DisplayMode(int width, int height) : this(width, height, DisplayDevice.PrimaryDisplay.BitsPerPixel, 16, 0, 0, 0, false, false, false, 0.0f) { @@ -151,7 +91,6 @@ namespace OpenTK /// The Width of the DisplayMode in pixels. /// The Height of the DisplayMode in pixels. /// The ColorMode of the color buffer. - [Obsolete] public DisplayMode(int width, int height, ColorMode color) : this(width, height, color, 16, 0, 0, 0, false, false, false, 0.0f) { @@ -164,7 +103,6 @@ namespace OpenTK /// The Height of the DisplayMode in pixels. /// The ColorMode of the color buffer. /// The number of bits in the depth buffer. - [Obsolete] public DisplayMode(int width, int height, ColorMode color, int depth) : this(width, height, color, depth, 0, 0, 0, false, false, false, 0.0f) { @@ -178,7 +116,6 @@ namespace OpenTK /// The ColorMode of the color buffer. /// The number of bits in the depth buffer. /// True for a fullscreen DisplayMode, false otherwise. - [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) { @@ -186,10 +123,8 @@ namespace OpenTK #endregion - #endregion - #region --- Public Properties --- - +#if false #region public int ColorFormat /// @@ -243,12 +178,15 @@ namespace OpenTK } #endregion +#endif + #region Obsolete Properties #region public bool Stereo /// /// Gets a System.Boolean indicating whether this DisplayMode is stereoscopic. /// + [Obsolete("Use GraphicsFormat.Stereo instead.")] public bool Stereo { get { return this.stereo; } @@ -263,6 +201,7 @@ namespace OpenTK /// Gets a System.Int32 containing the number of buffers associated with this /// DisplayMode. /// + [Obsolete("Use GraphicsFormat.Buffers instead.")] public int Buffers { get { return this.buffers; } @@ -271,8 +210,6 @@ namespace OpenTK #endregion - #region Obsolete Properties - [Obsolete("Use GameWindow.Fullscreen instead.")] public bool Fullscreen { @@ -287,7 +224,7 @@ namespace OpenTK internal set { this.vsync = value; } } - [Obsolete("Use OpenTK.Graphics.Display.RefreshRate instead.")] + [Obsolete("Use OpenTK.Graphics.DisplayDevice.RefreshRate instead.")] public float RefreshRate { get { return this.refreshRate; } @@ -296,7 +233,7 @@ namespace OpenTK #region public ColorDepth Color - [Obsolete("Use DisplayMode.ColorFormat instead.")] + [Obsolete("Use GraphicsFormat.Color instead.")] public ColorMode Color { get { return this.color_format; } @@ -345,31 +282,36 @@ namespace OpenTK #endregion - [Obsolete("Use DisplayMode.Depth instead.")] + [Obsolete("Use GraphicsFormat.Depth instead.")] public int DepthBits { get { return this.depthBits; } internal set { this.depthBits = value; } } - [Obsolete("Use DisplayMode.Stencil instead.")] + [Obsolete("Use GraphicsFormat.Stencil instead.")] public int StencilBits { get { return this.stencilBits; } internal set { this.stencilBits = value; } } - [Obsolete("Use DisplayMode.AuxilliaryColorFormat instead.")] + [Obsolete("Use GraphicsFormat.AuxilliaryColorFormat instead.")] public int AuxBits { - get { return this.AuxilliaryColorFormat.BitsPerPixel; } - internal set { this.AuxilliaryColorFormat = value; } + get { return this.auxilliary_color_format.BitsPerPixel; } + internal set { this.auxilliary_color_format = value; } } #endregion #endregion + internal GraphicsFormat ToGraphicsFormat() + { + return new GraphicsFormat(this.Color, this.DepthBits, this.StencilBits, this.AuxBits, this.Buffers, this.Stereo); + } + #region --- Overrides --- /// diff --git a/Source/OpenTK/GLContext.cs b/Source/OpenTK/GLContext.cs index 3c2507b3..04dc4efc 100644 --- a/Source/OpenTK/GLContext.cs +++ b/Source/OpenTK/GLContext.cs @@ -100,7 +100,7 @@ namespace OpenTK internal static GetCurrentContextDelegate GetCurrentContext; /// - /// Returns the context which is current in the calling thread. + /// Gets or sets the current GraphicsContext in the calling thread. /// public static GLContext CurrentContext { @@ -112,6 +112,13 @@ namespace OpenTK return null; //return (GLContext)available_contexts[StaticGetCurrentContext().ToInt64()].Target; } + set + { + if (value != null) + value.MakeCurrent(); + else if (CurrentContext != null) + CurrentContext.IsCurrent = false; + } } #endregion @@ -204,6 +211,7 @@ namespace OpenTK public bool IsCurrent { get { return implementation.IsCurrent; } + set { implementation.IsCurrent = value; } } /// @@ -253,15 +261,15 @@ namespace OpenTK get { return (implementation as IGLContextInternal).Mode; } } - /// - /// Gets a System.IntPtr containing the handle to the OpenGL context which is current in the - /// calling thread, or IntPtr.Zero if no OpenGL context is current. - /// - /// A System.IntPtr that holds the handle to the current OpenGL context. - ContextHandle IGLContextInternal.GetCurrentContext() - { - return (implementation as IGLContextInternal).GetCurrentContext(); - } + ///// + ///// Gets a System.IntPtr containing the handle to the OpenGL context which is current in the + ///// calling thread, or IntPtr.Zero if no OpenGL context is current. + ///// + ///// A System.IntPtr that holds the handle to the current OpenGL context. + //ContextHandle IGLContextInternal.GetCurrentContext() + //{ + // return (implementation as IGLContextInternal).GetCurrentContext(); + //} /// /// Registers an OpenGL resource for disposal. diff --git a/Source/OpenTK/GameWindow.cs b/Source/OpenTK/GameWindow.cs index 8d7a5d0f..6ba17c48 100644 --- a/Source/OpenTK/GameWindow.cs +++ b/Source/OpenTK/GameWindow.cs @@ -1,6 +1,8 @@ #region --- License --- -/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos - * See license.txt for license info +/* Licensed under the MIT/X11 license. + * Copyright (c) 2006-2008 the OpenTK Team. + * This notice may not be removed from any source distribution. + * See license.txt for licensing detailed licensing details. */ #endregion @@ -8,12 +10,13 @@ using System; using System.Collections.Generic; using System.Text; using System.Diagnostics; +using System.Threading; using OpenTK.Platform; using OpenTK.Input; -using System.Threading; using OpenTK.OpenGL; using OpenTK.OpenGL.Enums; +using OpenTK.Graphics; namespace OpenTK { @@ -86,25 +89,23 @@ namespace OpenTK #region --- Contructors --- - /// - /// Constructs a new GameWindow using a safe DisplayMode. - /// - public GameWindow() : this(new DisplayMode(640, 480, 0, 16, false), "OpenTK game window") - { } + public GameWindow() : this("OpenTK Game Window", 640, 480, null, GraphicsFormat.Default) { } - /// - /// Constructs a new GameWindow, and opens a render window with the specified DisplayMode. - /// - /// The DisplayMode of the GameWindow. - public GameWindow(DisplayMode mode) : this(mode, "OpenTK game window") { } + public GameWindow(string title) : this(title, 640, 480, null, GraphicsFormat.Default) { } - /// - /// Constructs a new GameWindow with the specified title, and opens a render window with the - /// specified DisplayMode. - /// - /// The DisplayMode of the GameWindow. - /// The Title of the GameWindow. - public GameWindow(DisplayMode mode, string title) + public GameWindow(string title, int width, int height) + : this(title, width, height, null, GraphicsFormat.Default) { } + + public GameWindow(string title, int width, int height, GraphicsFormat format) + : this(title, width, height, null, format) { } + + public GameWindow(string title, DisplayResolution resolution) + : this(title, resolution.Width, resolution.Height, resolution, GraphicsFormat.Default) { } + + public GameWindow(string title, DisplayResolution resolution, GraphicsFormat format) + : this(title, resolution.Width, resolution.Height, resolution, format) { } + + GameWindow(string title, int width, int height, DisplayResolution resolution, GraphicsFormat format) { switch (Environment.OSVersion.Platform) { @@ -122,15 +123,43 @@ namespace OpenTK default: throw new PlatformNotSupportedException( - "Your platform is not supported currently. Please, refer to http://opentk.sourceforge.net for more information."); + "Your platform is not supported currently. Please, refer to http://www.opentk.com for more information."); } glWindow.Destroy += glWindow_Destroy; - CreateWindow(mode, title); + // TODO: GLContext is created inside this call. + glWindow.CreateWindow(width, height, format, out glContext); + this.Title = title; - //this.vsync = VSyncMode.Adaptive; - this.VSync = VSyncMode.On; + if (resolution != null) + { + DisplayDevice.PrimaryDisplay.ChangeResolution(resolution); + this.Fullscreen = true; + } + + this.VSync = VSyncMode.On; //VSyncMode.Adaptive; + } + + /// + /// Constructs a new GameWindow, and opens a render window with the specified DisplayMode. + /// + /// The DisplayMode of the GameWindow. + [Obsolete] + public GameWindow(DisplayMode mode) + : this("OpenTK Game Window", mode.Width, mode.Height, + mode.Fullscreen ? DisplayDevice.PrimaryDisplay.SelectResolution( + mode.Width, mode.Height, mode.Color.BitsPerPixel, 0) : null, mode.ToGraphicsFormat()) { } + + /// + /// Constructs a new GameWindow with the specified title, and opens a render window with the + /// specified DisplayMode. + /// + /// The DisplayMode of the GameWindow. + /// The Title of the GameWindow. + [Obsolete] + public GameWindow(DisplayMode mode, string title) + { } void glWindow_Destroy(object sender, EventArgs e) @@ -215,17 +244,7 @@ namespace OpenTK /// public IGLContext Context { - get - { - if (!this.Exists && !this.IsExiting) - { - Debug.WriteLine("WARNING: OpenGL Context accessed before creating a render window. This may indicate a programming error. Force-creating a render window."); - mode = new DisplayMode(640, 480); - this.CreateWindow(mode); - } - //return glWindow.Context; - return glContext; - } + get { return glContext; } } #endregion @@ -311,38 +330,6 @@ namespace OpenTK #endif - #region public void CreateWindow(DisplayMode mode) - - /// - /// Creates a render window for the calling GameWindow. - /// - /// The DisplayMode of the render window. - /// - /// It is an error to call this function when a render window already exists. - /// Call DestroyWindow to close the render window. - /// - /// Occurs when a render window already exists. - public void CreateWindow(DisplayMode mode) - { - if (!Exists) - { - try - { - glWindow.CreateWindow(mode.Width, mode.Height, mode, out glContext); - } - catch (ApplicationException expt) - { - Debug.Print(expt.ToString()); - throw; - } - } - else - { - throw new ApplicationException("A render window already exists for this GameWindow."); - } - } - - #endregion #region public void DestroyWindow() @@ -362,34 +349,7 @@ namespace OpenTK #endregion - #region --- GameWindow Methods --- - - #region public void CreateWindow(DisplayMode mode, string title) - - /// - /// Creates a render window for the calling GameWindow, with the specified DisplayMode and Title. - /// - /// The DisplayMode of the render window. - /// The Title of the render window. - /// - /// It is an error to call this function when a render window already exists. - /// Call DestroyWindow to close the render window. - /// - /// Occurs when a render window already exists. - private void CreateWindow(DisplayMode mode, string title) - { - if (!Exists) - { - // TODO: This is a hack - reslove in 0.3.15 once and for all! - // GLContext is created inside the CreateWindow call. - glWindow.CreateWindow(mode.Width, mode.Height, mode, out glContext); - this.Title = title; - } - else - throw new InvalidOperationException("A render window already exists for this GameWindow."); - } - - #endregion + #region --- Public Methods --- #region void Run() @@ -519,7 +479,7 @@ namespace OpenTK if (VSync == VSyncMode.Adaptive) { // Check if we have enough time for a vsync - if (RenderTime > 2.0 * TargetRenderPeriod) + if (TargetRenderPeriod != 0 && RenderTime > 2.0 * TargetRenderPeriod) Context.VSync = false; else Context.VSync = true; @@ -609,12 +569,6 @@ namespace OpenTK /// private void OnRenderFrameInternal(RenderFrameEventArgs e) { - if (!this.Exists && !this.IsExiting) - { - Debug.Print("WARNING: RenderFrame event raised, without a valid render window. This may indicate a programming error. Creating render window."); - mode = new DisplayMode(640, 480); - this.CreateWindow(mode); - } if (RenderFrame != null) RenderFrame(this, e); diff --git a/Source/OpenTK/OpenAL/XRamExtension.cs b/Source/OpenTK/OpenAL/XRamExtension.cs index b9da042d..9680d874 100644 --- a/Source/OpenTK/OpenAL/XRamExtension.cs +++ b/Source/OpenTK/OpenAL/XRamExtension.cs @@ -31,7 +31,7 @@ namespace OpenTK.OpenAL #region X-RAM Function pointer definitions - [CLSCompliant( false )] + //[CLSCompliant(false)] private delegate bool Delegate_SetBufferMode( int n, ref uint buffers, int value ); //typedef ALboolean (__cdecl *EAXSetBufferMode)(ALsizei n, ALuint *buffers, ALint value); @@ -159,4 +159,4 @@ namespace OpenTK.OpenAL #endregion Public Methods } -} \ No newline at end of file +} diff --git a/Source/OpenTK/Platform/DummyGLContext.cs b/Source/OpenTK/Platform/DummyGLContext.cs index 7a2051ba..d361c65b 100644 --- a/Source/OpenTK/Platform/DummyGLContext.cs +++ b/Source/OpenTK/Platform/DummyGLContext.cs @@ -38,7 +38,7 @@ namespace OpenTK.Platform public void SwapBuffers() { } public void MakeCurrent() { } - public bool IsCurrent { get { return true; } } + public bool IsCurrent { get { return true; } set { } } public IntPtr GetCurrentContext() { return IntPtr.Zero; } public event DestroyEvent Destroy; diff --git a/Source/OpenTK/Platform/IGLContext.cs b/Source/OpenTK/Platform/IGLContext.cs index 59259f22..e7ca3398 100644 --- a/Source/OpenTK/Platform/IGLContext.cs +++ b/Source/OpenTK/Platform/IGLContext.cs @@ -24,20 +24,20 @@ namespace OpenTK.Platform /// void CreateContext(bool direct, IGLContext source); - /// - /// Swaps buffers, presenting the rendered scene to the user. - /// + /// Swaps buffers, presenting the rendered scene to the user. void SwapBuffers(); - /// - /// Makes the Context current in the calling thread, i.e. future OpenGL commands will affect this Context. - /// + /// Makes the GraphicsContext current in the calling thread. + /// + /// OpenGL commands in one thread, affect the GraphicsContext which is current in that thread. + /// It is an error to issue an OpenGL command in a thread without a current GraphicsContext. + /// void MakeCurrent(); /// - /// Gets a System.Boolean indicating whether this Context is current in the calling thread. + /// Gets or sets a System.Boolean indicating whether the GraphicsContext is current in the calling thread. /// - bool IsCurrent { get; } + bool IsCurrent { get; set; } /// /// Raised when a Context is destroyed. @@ -79,12 +79,12 @@ namespace OpenTK.Platform /// DisplayMode Mode { get; } - /// - /// Gets a System.IntPtr containing the handle to the OpenGL context which is current in the - /// calling thread, or IntPtr.Zero if no OpenGL context is current. - /// - /// A System.IntPtr that holds the handle to the current OpenGL context. - ContextHandle GetCurrentContext(); + ///// + ///// Gets a System.IntPtr containing the handle to the OpenGL context which is current in the + ///// calling thread, or IntPtr.Zero if no OpenGL context is current. + ///// + ///// A System.IntPtr that holds the handle to the current OpenGL context. + //ContextHandle GetCurrentContext(); /// /// Registers an OpenGL resource for disposal. diff --git a/Source/OpenTK/Platform/INativeGLWindow.cs b/Source/OpenTK/Platform/INativeGLWindow.cs index 29a96b35..9d8e5a73 100644 --- a/Source/OpenTK/Platform/INativeGLWindow.cs +++ b/Source/OpenTK/Platform/INativeGLWindow.cs @@ -1,6 +1,8 @@ #region --- License --- -/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos - * See license.txt for license info +/* Licensed under the MIT/X11 license. + * Copyright (c) 2006-2008 the OpenTK Team. + * This notice may not be removed from any source distribution. + * See license.txt for licensing detailed licensing details. */ #endregion @@ -9,15 +11,17 @@ using System.Collections.Generic; using System.Text; using OpenTK.Input; +using OpenTK.Graphics; namespace OpenTK.Platform { /// /// This interface supports OpenTK, and is not intended for use by OpenTK programs. /// - interface INativeGLWindow : IResizable, IDisposable + internal interface INativeGLWindow : IResizable, IDisposable { - void CreateWindow(int width, int height, DisplayMode mode, out IGLContext context); + //void CreateWindow(int width, int height, DisplayMode mode, out IGLContext context); + void CreateWindow(int width, int height, GraphicsFormat mode, out IGLContext context); void DestroyWindow(); void ProcessEvents(); void PointToClient(ref System.Drawing.Point p); diff --git a/Source/OpenTK/Platform/Windows/WinGLContext.cs b/Source/OpenTK/Platform/Windows/WinGLContext.cs index 3da336e9..df508c8f 100644 --- a/Source/OpenTK/Platform/Windows/WinGLContext.cs +++ b/Source/OpenTK/Platform/Windows/WinGLContext.cs @@ -38,13 +38,15 @@ namespace OpenTK.Platform.Windows #region --- Contructors --- - /// - /// Constructs a new WinGLContext object. - /// + static WinGLContext() + { + // Set the GetCurrentContext implementation. + if (GLContext.GetCurrentContext == null) + GLContext.GetCurrentContext = WinGLContext.GetCurrentContext; + } + public WinGLContext() { - if (GLContext.GetCurrentContext == null) - GLContext.GetCurrentContext = this.GetCurrentContext; } #endregion @@ -127,15 +129,14 @@ namespace OpenTK.Platform.Windows public bool IsCurrent { get { return Wgl.GetCurrentContext() == this.renderContext; } - } + set + { + if (value) + Wgl.MakeCurrent(this.deviceContext, this.renderContext); + else + Wgl.MakeCurrent(IntPtr.Zero, IntPtr.Zero); + } - #endregion - - #region public ContextHandle GetCurrentContext() - - public ContextHandle GetCurrentContext() - { - return Wgl.GetCurrentContext(); } #endregion @@ -292,10 +293,10 @@ namespace OpenTK.Platform.Windows pixelFormat.AccumAlphaBits = (byte)accum.Alpha; } */ - pixelFormat.DepthBits = (byte)mode.Depth; - pixelFormat.StencilBits = (byte)mode.Stencil; + pixelFormat.DepthBits = (byte)mode.DepthBits; + pixelFormat.StencilBits = (byte)mode.StencilBits; - if (mode.Depth <= 0) + if (mode.DepthBits <= 0) { pixelFormat.Flags |= PixelFormatDescriptorFlags.DEPTH_DONTCARE; } @@ -348,7 +349,7 @@ namespace OpenTK.Platform.Windows #endregion - #region --- Internal Members --- + #region --- Methods --- #region internal IntPtr Device @@ -356,6 +357,15 @@ namespace OpenTK.Platform.Windows #endregion + #region static ContextHandle GetCurrentContext() + + static ContextHandle GetCurrentContext() + { + return Wgl.GetCurrentContext(); + } + + #endregion + #endregion #region --- IDisposable Members --- diff --git a/Source/OpenTK/Platform/Windows/WinGLNative.cs b/Source/OpenTK/Platform/Windows/WinGLNative.cs index 40db886f..ef92055a 100644 --- a/Source/OpenTK/Platform/Windows/WinGLNative.cs +++ b/Source/OpenTK/Platform/Windows/WinGLNative.cs @@ -14,6 +14,7 @@ using System.Windows.Forms; using System.Diagnostics; using OpenTK.OpenGL; using OpenTK.Input; +using OpenTK.Graphics; #endregion @@ -306,12 +307,13 @@ namespace OpenTK.Platform.Windows #endregion - #region public void CreateWindow(DisplayMode mode, out IGLContext context) + #region public void CreateWindow(int width, int height, GraphicsFormat format, out IGLContext context) - public void CreateWindow(int width, int height, DisplayMode windowMode, out IGLContext context) + public void CreateWindow(int width, int height, GraphicsFormat format, out IGLContext context) { - Debug.Print("Creating native window with mode: {0}", windowMode.ToString()); + Debug.Print("Creating native window."); Debug.Indent(); + Debug.Print("GraphicsFormat: {0}", format.ToString()); CreateParams cp = new CreateParams(); cp.ClassStyle = @@ -346,7 +348,7 @@ namespace OpenTK.Platform.Windows cp.Width = rect.right - rect.left; cp.Height = rect.bottom - rect.top; cp.Caption = "OpenTK Game Window"; - + // Keep in mind that some construction code runs in WM_CREATE, // which is raised CreateHandle() CreateHandle(cp); diff --git a/Source/OpenTK/Platform/X11/API.cs b/Source/OpenTK/Platform/X11/API.cs index 7220005e..12db994a 100644 --- a/Source/OpenTK/Platform/X11/API.cs +++ b/Source/OpenTK/Platform/X11/API.cs @@ -679,16 +679,22 @@ XF86VidModeGetGammaRampSize( #region internal struct XRRScreenSize + #pragma warning disable 0169, 0649 + internal struct XRRScreenSize { internal int Width, Height; internal int MWidth, MHeight; }; + #pragma warning restore 0169, 0649 + #endregion #region unsafe internal struct Screen + #pragma warning disable 0169 + unsafe internal struct Screen { XExtData ext_data; /* hook for extension to hang data */ @@ -710,10 +716,14 @@ XF86VidModeGetGammaRampSize( long root_input_mask; /* initial root input mask */ } + #pragma warning restore 0169 + #endregion #region unsafe internal class XExtData +#pragma warning disable 0169 + unsafe internal class XExtData { int number; /* number returned by XRegisterExtension */ @@ -723,6 +733,8 @@ XF86VidModeGetGammaRampSize( XPointer private_data; /* data private to this extension. */ }; + #pragma warning restore 0169 + #endregion #endregion @@ -1263,7 +1275,7 @@ XF86VidModeGetGammaRampSize( /// The XCreateSimpleWindow function creates an unmapped InputOutput subwindow for a specified parent window, returns the window ID of the created window, and causes the X server to generate a CreateNotify event. The created window is placed on top in the stacking order with respect to siblings. Any part of the window that extends outside its parent window is clipped. The border_width for an InputOnly window must be zero, or a BadMatch error results. XCreateSimpleWindow inherits its depth, class, and visual from its parent. All other window attributes, except background and border, have their default values. /// XCreateSimpleWindow can generate BadAlloc, BadMatch, BadValue, and BadWindow errors. /// - [DllImport(X11Library, EntryPoint = "XCreateWindow"), CLSCompliant(false)] + [DllImport(X11Library, EntryPoint = "XCreateWindow")]//, CLSCompliant(false)] public extern static Window XCreateWindow(Display display, Window parent, int x, int y, int width, int height, int border_width, int depth, int @class, IntPtr visual, UIntPtr valuemask, ref XSetWindowAttributes attributes); diff --git a/Source/OpenTK/Platform/X11/Functions.cs b/Source/OpenTK/Platform/X11/Functions.cs index 0e07ad1c..c55ae36f 100644 --- a/Source/OpenTK/Platform/X11/Functions.cs +++ b/Source/OpenTK/Platform/X11/Functions.cs @@ -19,7 +19,7 @@ namespace OpenTK.Platform.X11 [DllImport("libX11", EntryPoint = "XCreateWindow")] public extern static IntPtr XCreateWindow(IntPtr display, IntPtr parent, int x, int y, int width, int height, int border_width, int depth, int xclass, IntPtr visual, IntPtr valuemask, ref XSetWindowAttributes attributes); - [DllImport("libX11", EntryPoint = "XCreateSimpleWindow"), CLSCompliant(false)] + [DllImport("libX11", EntryPoint = "XCreateSimpleWindow")]//, CLSCompliant(false)] public extern static IntPtr XCreateSimpleWindow(IntPtr display, IntPtr parent, int x, int y, int width, int height, int border_width, UIntPtr border, UIntPtr background); [DllImport("libX11", EntryPoint = "XCreateSimpleWindow")] public extern static IntPtr XCreateSimpleWindow(IntPtr display, IntPtr parent, int x, int y, int width, int height, int border_width, IntPtr border, IntPtr background); @@ -81,10 +81,10 @@ namespace OpenTK.Platform.X11 [DllImport("libX11", EntryPoint = "XRaiseWindow")] public extern static int XRaiseWindow(IntPtr display, IntPtr window); - [DllImport("libX11", EntryPoint = "XLowerWindow"), CLSCompliant(false)] + [DllImport("libX11", EntryPoint = "XLowerWindow")]//, CLSCompliant(false)] public extern static uint XLowerWindow(IntPtr display, IntPtr window); - [DllImport("libX11", EntryPoint = "XConfigureWindow"), CLSCompliant(false)] + [DllImport("libX11", EntryPoint = "XConfigureWindow")]//, CLSCompliant(false)] public extern static uint XConfigureWindow(IntPtr display, IntPtr window, ChangeWindowAttributes value_mask, ref XWindowChanges values); [DllImport("libX11", EntryPoint = "XInternAtom")] @@ -120,7 +120,7 @@ namespace OpenTK.Platform.X11 [DllImport("libX11", EntryPoint = "XGetGeometry")] public extern static bool XGetGeometry(IntPtr display, IntPtr window, IntPtr root, IntPtr x, IntPtr y, out int width, out int height, IntPtr border_width, IntPtr depth); - [DllImport("libX11", EntryPoint = "XWarpPointer"), CLSCompliant(false)] + [DllImport("libX11", EntryPoint = "XWarpPointer")]//, CLSCompliant(false)] public extern static uint XWarpPointer(IntPtr display, IntPtr src_w, IntPtr dest_w, int src_x, int src_y, uint src_width, uint src_height, int dest_x, int dest_y); [DllImport("libX11", EntryPoint = "XClearWindow")] @@ -139,7 +139,7 @@ namespace OpenTK.Platform.X11 [DllImport("libX11", EntryPoint = "XDefaultVisual")] public extern static IntPtr XDefaultVisual(IntPtr display, int screen_number); - [DllImport("libX11", EntryPoint = "XDefaultDepth"), CLSCompliant(false)] + [DllImport("libX11", EntryPoint = "XDefaultDepth")]//, CLSCompliant(false)] public extern static uint XDefaultDepth(IntPtr display, int screen_number); [DllImport("libX11", EntryPoint = "XDefaultScreen")] @@ -148,10 +148,10 @@ namespace OpenTK.Platform.X11 [DllImport("libX11", EntryPoint = "XDefaultColormap")] public extern static IntPtr XDefaultColormap(IntPtr display, int screen_number); - [DllImport("libX11", EntryPoint = "XLookupColor"), CLSCompliant(false)] + [DllImport("libX11", EntryPoint = "XLookupColor")]//, CLSCompliant(false)] public extern static int XLookupColor(IntPtr display, IntPtr Colormap, string Coloranem, ref XColor exact_def_color, ref XColor screen_def_color); - [DllImport("libX11", EntryPoint = "XAllocColor"), CLSCompliant(false)] + [DllImport("libX11", EntryPoint = "XAllocColor")]//, CLSCompliant(false)] public extern static int XAllocColor(IntPtr display, IntPtr Colormap, ref XColor colorcell_def); [DllImport("libX11", EntryPoint = "XSetTransientForHint")] @@ -160,15 +160,15 @@ namespace OpenTK.Platform.X11 [DllImport("libX11", EntryPoint = "XChangeProperty")] public extern static int XChangeProperty(IntPtr display, IntPtr window, IntPtr property, IntPtr type, int format, PropertyMode mode, ref MotifWmHints data, int nelements); - [DllImport("libX11", EntryPoint = "XChangeProperty"), CLSCompliant(false)] + [DllImport("libX11", EntryPoint = "XChangeProperty")]//, CLSCompliant(false)] public extern static int XChangeProperty(IntPtr display, IntPtr window, IntPtr property, IntPtr type, int format, PropertyMode mode, ref uint value, int nelements); [DllImport("libX11", EntryPoint = "XChangeProperty")] public extern static int XChangeProperty(IntPtr display, IntPtr window, IntPtr property, IntPtr type, int format, PropertyMode mode, ref int value, int nelements); - [DllImport("libX11", EntryPoint = "XChangeProperty"), CLSCompliant(false)] + [DllImport("libX11", EntryPoint = "XChangeProperty")]//, CLSCompliant(false)] public extern static int XChangeProperty(IntPtr display, IntPtr window, IntPtr property, IntPtr type, int format, PropertyMode mode, ref IntPtr value, int nelements); - [DllImport("libX11", EntryPoint = "XChangeProperty"), CLSCompliant(false)] + [DllImport("libX11", EntryPoint = "XChangeProperty")]//, CLSCompliant(false)] public extern static int XChangeProperty(IntPtr display, IntPtr window, IntPtr property, IntPtr type, int format, PropertyMode mode, uint[] data, int nelements); [DllImport("libX11", EntryPoint = "XChangeProperty")] @@ -235,7 +235,7 @@ namespace OpenTK.Platform.X11 [DllImport("libX11", EntryPoint = "XCreateFontCursor")] public extern static IntPtr XCreateFontCursor(IntPtr display, CursorFontShape shape); - [DllImport("libX11", EntryPoint = "XCreatePixmapCursor"), CLSCompliant(false)] + [DllImport("libX11", EntryPoint = "XCreatePixmapCursor")]//, CLSCompliant(false)] public extern static IntPtr XCreatePixmapCursor(IntPtr display, IntPtr source, IntPtr mask, ref XColor foreground_color, ref XColor background_color, int x_hot, int y_hot); [DllImport("libX11", EntryPoint = "XCreatePixmapFromBitmapData")] @@ -301,12 +301,12 @@ namespace OpenTK.Platform.X11 [DllImport("libX11", EntryPoint = "XSetPlaneMask")] public extern static int XSetPlaneMask(IntPtr display, IntPtr gc, IntPtr mask); - [DllImport("libX11", EntryPoint = "XSetForeground"), CLSCompliant(false)] + [DllImport("libX11", EntryPoint = "XSetForeground")]//, CLSCompliant(false)] public extern static int XSetForeground(IntPtr display, IntPtr gc, UIntPtr foreground); [DllImport("libX11", EntryPoint = "XSetForeground")] public extern static int XSetForeground(IntPtr display, IntPtr gc, IntPtr foreground); - [DllImport("libX11", EntryPoint = "XSetBackground"), CLSCompliant(false)] + [DllImport("libX11", EntryPoint = "XSetBackground")]//, CLSCompliant(false)] public extern static int XSetBackground(IntPtr display, IntPtr gc, UIntPtr background); [DllImport("libX11", EntryPoint = "XSetBackground")] public extern static int XSetBackground(IntPtr display, IntPtr gc, IntPtr background); diff --git a/Source/OpenTK/Platform/X11/X11GLContext.cs b/Source/OpenTK/Platform/X11/X11GLContext.cs index 3a5282c8..3300bac6 100644 --- a/Source/OpenTK/Platform/X11/X11GLContext.cs +++ b/Source/OpenTK/Platform/X11/X11GLContext.cs @@ -31,9 +31,15 @@ namespace OpenTK.Platform.X11 #region --- Constructors --- - /// - /// Constructs a new X11GLContext object. - /// + static X11GLContext() + { + // Set the GetCurrentContext implementation. + if (GLContext.GetCurrentContext == null) + GLContext.GetCurrentContext = X11GLContext.GetCurrentContext; + } + + /// + /// Constructs a new X11GLContext object. public X11GLContext() { } #endregion @@ -85,7 +91,7 @@ namespace OpenTK.Platform.X11 visualAttributes.Add((int)Glx.Enums.GLXAttribute.ALPHA_SIZE); visualAttributes.Add((int)mode.Color.Alpha); visualAttributes.Add((int)Glx.Enums.GLXAttribute.DEPTH_SIZE); - visualAttributes.Add((int)mode.Depth); + visualAttributes.Add((int)mode.DepthBits); visualAttributes.Add((int)Glx.Enums.GLXAttribute.DOUBLEBUFFER); visualAttributes.Add((int)0); } @@ -130,16 +136,24 @@ namespace OpenTK.Platform.X11 #endregion + #region public void CreateContext() + public void CreateContext() { this.CreateContext(true, null); } + #endregion + + #region public void CreateContext(bool direct) + public void CreateContext(bool direct) { this.CreateContext(direct, null); } + #endregion + #region public void CreateContext(bool direct, IGLContext shareContext) public void CreateContext(bool direct, IGLContext shareContext) @@ -241,6 +255,13 @@ namespace OpenTK.Platform.X11 public bool IsCurrent { get { return Glx.GetCurrentContext() == this.context; } + set + { + if (value) + Glx.MakeCurrent(windowInfo.Display, windowInfo.Handle, context); + else + Glx.MakeCurrent(windowInfo.Handle, IntPtr.Zero, IntPtr.Zero); + } } #endregion @@ -313,18 +334,9 @@ namespace OpenTK.Platform.X11 #endregion - #region ContextHandle IGLContextInternal.GetCurrentContext() - - ContextHandle IGLContextInternal.GetCurrentContext() - { - return (ContextHandle)Glx.GetCurrentContext(); - } - #endregion - #endregion - - #region --- Public Methods --- + #region --- Methods --- void OnDestroy() { @@ -332,6 +344,15 @@ namespace OpenTK.Platform.X11 Destroy(this, EventArgs.Empty); } + #region static ContextHandle GetCurrentContext() + + static ContextHandle GetCurrentContext() + { + return (ContextHandle)Glx.GetCurrentContext(); + } + + #endregion + #endregion #region --- IDisposable Members --- diff --git a/Source/OpenTK/Platform/X11/X11GLNative.cs b/Source/OpenTK/Platform/X11/X11GLNative.cs index 7c61ee22..a4d95352 100644 --- a/Source/OpenTK/Platform/X11/X11GLNative.cs +++ b/Source/OpenTK/Platform/X11/X11GLNative.cs @@ -392,20 +392,9 @@ namespace OpenTK.Platform.X11 #endregion - #region public void CreateWindow(DisplayMode mode, out IGLContext glContext) + #region public void CreateWindow(int width, int height, GraphicsFormat format, out IGLContext glContext) - /// - /// Opens a new render window with the given DisplayMode. - /// - /// The DisplayMode of the render window. - /// - /// Creates the window visual and colormap. Associates the colormap/visual - /// with the window and raises the window on top of the window stack. - /// - /// Colormap creation is currently disabled. - /// - /// - public void CreateWindow(int width, int height, DisplayMode mode, out IGLContext glContext) + public void CreateWindow(int width, int height, GraphicsFormat format, out IGLContext glContext) { if (exists) throw new ApplicationException("Render window already exists!"); @@ -489,6 +478,26 @@ namespace OpenTK.Platform.X11 #endregion + #region public void CreateWindow(int width, int height, DisplayMode mode, out IGLContext glContext) + + /// + /// Opens a new render window with the given DisplayMode. + /// + /// The DisplayMode of the render window. + /// + /// Creates the window visual and colormap. Associates the colormap/visual + /// with the window and raises the window on top of the window stack. + /// + /// Colormap creation is currently disabled. + /// + /// + public void CreateWindow(int width, int height, DisplayMode mode, out IGLContext glContext) + { + this.CreateWindow(width, height, new GraphicsFormat(), out glContext); + } + + #endregion + #region OnCreate public event CreateEvent Create; @@ -727,8 +736,8 @@ namespace OpenTK.Platform.X11 { MotifWmHints hints = new MotifWmHints(); hints.flags = (IntPtr)MotifFlags.Decorations; - Functions.XChangeProperty(this.window.Display, this.Handle, atom, atom, 32, PropertyMode.Replace, ref hints, - Marshal.SizeOf(hints) / 4); + Functions.XChangeProperty(this.window.Display, this.Handle, atom, atom, 32, PropertyMode.Replace, ref hints, 5 + /*Marshal.SizeOf(hints) / 4*/); return true; } return false; @@ -763,7 +772,7 @@ namespace OpenTK.Platform.X11 { IntPtr hints = IntPtr.Zero; Functions.XChangeProperty(this.window.Display, this.Handle, atom, atom, 32, PropertyMode.Replace, ref hints, - Marshal.SizeOf(hints) / 4); + /*Marshal.SizeOf(hints) / 4*/ 1); return true; } @@ -780,8 +789,8 @@ namespace OpenTK.Platform.X11 if (atom != IntPtr.Zero) { IntPtr hints = Functions.XInternAtom(this.window.Display, "_NET_WM_STATE_FULLSCREEN", true); - Functions.XChangeProperty(this.window.Display, this.Handle, atom, atom, 32, PropertyMode.Replace, ref hints, - Marshal.SizeOf(hints) / 4); + Functions.XChangeProperty(this.window.Display, this.Handle, atom, atom, 32, PropertyMode.Replace, ref hints, 1 + /*Marshal.SizeOf(hints) / 4*/); return true; } return false; diff --git a/Source/OpenTK/Platform/X11/X11XrandrDisplayDevice.cs b/Source/OpenTK/Platform/X11/X11XrandrDisplayDevice.cs index 5195ac95..f44fbc6a 100644 --- a/Source/OpenTK/Platform/X11/X11XrandrDisplayDevice.cs +++ b/Source/OpenTK/Platform/X11/X11XrandrDisplayDevice.cs @@ -1,7 +1,7 @@ #region --- License --- /* Licensed under the MIT/X11 license. - * Copyright (c) 2006-2008 the OpenTK team. - * This notice may not be removed. + * Copyright (c) 2006-2008 the OpenTK Team. + * This notice may not be removed from any source distribution. * See license.txt for licensing detailed licensing details. */ #endregion @@ -18,8 +18,6 @@ namespace OpenTK.Platform.X11 internal class X11XrandrDisplayDevice : IDisplayDeviceDriver { static object display_lock = new object(); - int current_size = -1; // current_size == -1 means it hasn't been changed. - int current_refresh; // Store a mapping between resolutions and their respective // size_index (needed for XRRSetScreenConfig). The size_index // is simply the sequence number of the resolution as returned by