From 40ce65ab0925a5db1a880bb72ff98cb19ef12db7 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Mon, 17 Aug 2015 20:00:03 +1000 Subject: [PATCH] Combine factory definitions into one file, simplify WinGLContext. --- OpenTK/Graphics/GraphicsModeException.cs | 2 - OpenTK/Input/Interfaces.cs | 6 +- OpenTK/OpenTK.csproj | 3 - OpenTK/Platform/IPlatformFactory.cs | 101 ++++++++-- OpenTK/Platform/MacOS/MacOSFactory.cs | 61 ------ OpenTK/Platform/Windows/WinFactory.cs | 55 ------ OpenTK/Platform/Windows/WinGLContext.cs | 228 ++++++----------------- OpenTK/Platform/Windows/WinKeyMap.cs | 14 +- OpenTK/Platform/Windows/WinWindowInfo.cs | 106 +++-------- OpenTK/Platform/X11/Structs.cs | 16 -- OpenTK/Platform/X11/X11DisplayDevice.cs | 29 +-- OpenTK/Platform/X11/X11Factory.cs | 60 ------ 12 files changed, 186 insertions(+), 495 deletions(-) delete mode 100644 OpenTK/Platform/MacOS/MacOSFactory.cs delete mode 100644 OpenTK/Platform/Windows/WinFactory.cs delete mode 100644 OpenTK/Platform/X11/X11Factory.cs diff --git a/OpenTK/Graphics/GraphicsModeException.cs b/OpenTK/Graphics/GraphicsModeException.cs index 11d8f7df4..8b629ecb3 100644 --- a/OpenTK/Graphics/GraphicsModeException.cs +++ b/OpenTK/Graphics/GraphicsModeException.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Text; namespace OpenTK.Graphics { diff --git a/OpenTK/Input/Interfaces.cs b/OpenTK/Input/Interfaces.cs index 23752e58a..8729d3fcf 100644 --- a/OpenTK/Input/Interfaces.cs +++ b/OpenTK/Input/Interfaces.cs @@ -31,10 +31,10 @@ namespace OpenTK.Input { /// Defines the interface for an input driver. public interface IInputDriver : IJoystickDriver, IDisposable { - /// Gets the list of available KeyboardDevices. - KeyboardDevice Keyboard { get; } + /// Gets the list of available KeyboardDevices. + KeyboardDevice Keyboard { get; } - /// Gets the list of available MouseDevices. + /// Gets the list of available MouseDevices. MouseDevice Mouse { get; } /// Updates the state of the driver. diff --git a/OpenTK/OpenTK.csproj b/OpenTK/OpenTK.csproj index 976f2b725..f918e9d85 100644 --- a/OpenTK/OpenTK.csproj +++ b/OpenTK/OpenTK.csproj @@ -96,14 +96,12 @@ - - @@ -114,7 +112,6 @@ - diff --git a/OpenTK/Platform/IPlatformFactory.cs b/OpenTK/Platform/IPlatformFactory.cs index 0b2be984b..c166bf397 100644 --- a/OpenTK/Platform/IPlatformFactory.cs +++ b/OpenTK/Platform/IPlatformFactory.cs @@ -6,7 +6,7 @@ // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights to +// in the Software without restriction, including without limitation the rights to // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of // the Software, and to permit persons to whom the Software is furnished to do // so, subject to the following conditions: @@ -28,27 +28,90 @@ using System; using OpenTK.Graphics; -namespace OpenTK.Platform -{ - interface IPlatformFactory - { - INativeWindow CreateNativeWindow(int x, int y, int width, int height, string title, GraphicsMode mode, GameWindowFlags options, DisplayDevice device); +namespace OpenTK.Platform { + + interface IPlatformFactory { + INativeWindow CreateNativeWindow(int x, int y, int width, int height, string title, GraphicsMode mode, GameWindowFlags options, DisplayDevice device); - IDisplayDeviceDriver CreateDisplayDeviceDriver(); + IDisplayDeviceDriver CreateDisplayDeviceDriver(); - IGraphicsContext CreateGLContext(GraphicsMode mode, IWindowInfo window); - - IGraphicsMode CreateGraphicsMode(); - } - - internal static class Factory { - public static readonly IPlatformFactory Default; + IGraphicsContext CreateGLContext(GraphicsMode mode, IWindowInfo window); + + IGraphicsMode CreateGraphicsMode(); + } + + internal static class Factory { + public static readonly IPlatformFactory Default; - static Factory() { - if (Configuration.RunningOnWindows) Default = new Windows.WinFactory(); - else if (Configuration.RunningOnMacOS) Default = new MacOS.MacOSFactory(); - else if (Configuration.RunningOnX11) Default = new X11.X11Factory(); - else throw new NotSupportedException( "Running on an unsupported platform, please refer to http://www.opentk.com for more information." ); + static Factory() { + if (Configuration.RunningOnWindows) Default = new Windows.WinFactory(); + else if (Configuration.RunningOnMacOS) Default = new MacOS.MacOSFactory(); + else if (Configuration.RunningOnX11) Default = new X11.X11Factory(); + else throw new NotSupportedException( "Running on an unsupported platform, please refer to http://www.opentk.com for more information." ); + } + } +} + +namespace OpenTK.Platform.MacOS { + class MacOSFactory : IPlatformFactory { + + public INativeWindow CreateNativeWindow(int x, int y, int width, int height, string title, GraphicsMode mode, GameWindowFlags options, DisplayDevice device) { + return new CarbonGLNative(x, y, width, height, title, mode, options, device); + } + + public IDisplayDeviceDriver CreateDisplayDeviceDriver() { + return new QuartzDisplayDeviceDriver(); + } + + public IGraphicsContext CreateGLContext(GraphicsMode mode, IWindowInfo window) { + return new AglContext(mode, window); + } + + public IGraphicsMode CreateGraphicsMode() { + return new IGraphicsMode(); + } + } +} + +namespace OpenTK.Platform.Windows { + class WinFactory : IPlatformFactory { + + public INativeWindow CreateNativeWindow(int x, int y, int width, int height, string title, GraphicsMode mode, GameWindowFlags options, DisplayDevice device) { + return new WinGLNative(x, y, width, height, title, options, device); + } + + public IDisplayDeviceDriver CreateDisplayDeviceDriver() { + return new WinDisplayDeviceDriver(); + } + + public IGraphicsContext CreateGLContext(GraphicsMode mode, IWindowInfo window) { + return new WinGLContext(mode, (WinWindowInfo)window); + } + + public IGraphicsMode CreateGraphicsMode() { + return new IGraphicsMode(); } } } + +namespace OpenTK.Platform.X11 { + class X11Factory : IPlatformFactory { + + public INativeWindow CreateNativeWindow(int x, int y, int width, int height, string title, GraphicsMode mode, GameWindowFlags options, DisplayDevice device) { + return new X11GLNative(x, y, width, height, title, mode, options, device); + } + + public IDisplayDeviceDriver CreateDisplayDeviceDriver() { + return new X11DisplayDevice(); + } + + public IGraphicsContext CreateGLContext(GraphicsMode mode, IWindowInfo window) { + return new X11GLContext(mode, window); + } + + public IGraphicsMode CreateGraphicsMode() { + return new X11GraphicsMode(); + } + } +} + diff --git a/OpenTK/Platform/MacOS/MacOSFactory.cs b/OpenTK/Platform/MacOS/MacOSFactory.cs deleted file mode 100644 index 25dc70bc4..000000000 --- a/OpenTK/Platform/MacOS/MacOSFactory.cs +++ /dev/null @@ -1,61 +0,0 @@ -#region License -// -// The Open Toolkit Library License -// -// Copyright (c) 2006 - 2009 the Open Toolkit library. -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights to -// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of -// the Software, and to permit persons to whom the Software is furnished to do -// so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in all -// copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. -// -#endregion - -using System; -using System.Collections.Generic; -using System.Text; -using OpenTK.Graphics; - -namespace OpenTK.Platform.MacOS -{ - class MacOSFactory : IPlatformFactory - { - #region IPlatformFactory Members - - public virtual INativeWindow CreateNativeWindow(int x, int y, int width, int height, string title, GraphicsMode mode, GameWindowFlags options, DisplayDevice device) - { - return new CarbonGLNative(x, y, width, height, title, mode, options, device); - } - - public virtual IDisplayDeviceDriver CreateDisplayDeviceDriver() - { - return new QuartzDisplayDeviceDriver(); - } - - public virtual IGraphicsContext CreateGLContext(GraphicsMode mode, IWindowInfo window) - { - return new AglContext(mode, window); - } - - public virtual IGraphicsMode CreateGraphicsMode() - { - return new IGraphicsMode(); - } - - #endregion - } -} diff --git a/OpenTK/Platform/Windows/WinFactory.cs b/OpenTK/Platform/Windows/WinFactory.cs deleted file mode 100644 index a42582e48..000000000 --- a/OpenTK/Platform/Windows/WinFactory.cs +++ /dev/null @@ -1,55 +0,0 @@ -#region License -// -// The Open Toolkit Library License -// -// Copyright (c) 2006 - 2009 the Open Toolkit library. -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights to -// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of -// the Software, and to permit persons to whom the Software is furnished to do -// so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in all -// copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. -// -#endregion - -using System; -using OpenTK.Graphics; - -namespace OpenTK.Platform.Windows -{ - class WinFactory : IPlatformFactory - { - #region IPlatformFactory Members - - public virtual INativeWindow CreateNativeWindow(int x, int y, int width, int height, string title, GraphicsMode mode, GameWindowFlags options, DisplayDevice device) { - return new WinGLNative(x, y, width, height, title, options, device); - } - - public virtual IDisplayDeviceDriver CreateDisplayDeviceDriver() { - return new WinDisplayDeviceDriver(); - } - - public virtual IGraphicsContext CreateGLContext(GraphicsMode mode, IWindowInfo window) { - return new WinGLContext(mode, (WinWindowInfo)window); - } - - public virtual IGraphicsMode CreateGraphicsMode() { - return new IGraphicsMode(); - } - - #endregion - } -} diff --git a/OpenTK/Platform/Windows/WinGLContext.cs b/OpenTK/Platform/Windows/WinGLContext.cs index 4ac318c9d..533b46875 100644 --- a/OpenTK/Platform/Windows/WinGLContext.cs +++ b/OpenTK/Platform/Windows/WinGLContext.cs @@ -5,21 +5,13 @@ */ #endregion -#region --- Using Directives --- - using System; -using System.Collections.Generic; -using System.Text; -using System.Runtime.InteropServices; using System.Diagnostics; - +using System.Runtime.InteropServices; using OpenTK.Graphics; -using OpenTK.Graphics.OpenGL; -#endregion - -namespace OpenTK.Platform.Windows -{ +namespace OpenTK.Platform.Windows { + /// \internal /// /// Provides methods to create and control an opengl context on the Windows platform. @@ -27,81 +19,51 @@ namespace OpenTK.Platform.Windows /// internal sealed class WinGLContext : GraphicsContextBase { - static object SyncRoot = new object(); - static IntPtr opengl32Handle; const string opengl32Name = "OPENGL32.DLL"; - bool vsync_supported; - #region --- Contructors --- - - static WinGLContext() - { - lock (SyncRoot) - { - // Dynamically load the OpenGL32.dll in order to use the extension loading capabilities of Wgl. + static WinGLContext() { + // Dynamically load the OpenGL32.dll in order to use the extension loading capabilities of Wgl. + if (opengl32Handle == IntPtr.Zero) { + opengl32Handle = Functions.LoadLibrary(opengl32Name); if (opengl32Handle == IntPtr.Zero) - { - opengl32Handle = Functions.LoadLibrary(opengl32Name); - if (opengl32Handle == IntPtr.Zero) - throw new ApplicationException(String.Format("LoadLibrary(\"{0}\") call failed with code {1}", - opengl32Name, Marshal.GetLastWin32Error())); - Debug.WriteLine(String.Format("Loaded opengl32.dll: {0}", opengl32Handle)); - } + throw new ApplicationException(String.Format("LoadLibrary(\"{0}\") call failed with code {1}", + opengl32Name, Marshal.GetLastWin32Error())); + Debug.Print( "Loaded opengl32.dll: {0}", opengl32Handle ); } } - public WinGLContext(GraphicsMode format, WinWindowInfo window) - { - // There are many ways this code can break when accessed by multiple threads. The biggest offender is - // the sharedContext stuff, which will only become valid *after* this constructor returns. - // The easiest solution is to serialize all context construction - hence the big lock, below. - lock (SyncRoot) - { - if (window == null) - throw new ArgumentNullException("window", "Must point to a valid window."); - if (window.WindowHandle == IntPtr.Zero) - throw new ArgumentException("window", "Must be a valid window."); + public WinGLContext(GraphicsMode format, WinWindowInfo window) { + if (window == null) + throw new ArgumentNullException("window", "Must point to a valid window."); + if (window.WindowHandle == IntPtr.Zero) + throw new ArgumentException("window", "Must be a valid window."); - Debug.Print("OpenGL will be bound to handle: {0}", window.WindowHandle); - SelectGraphicsModePFD(format, (WinWindowInfo)window); - Debug.Write("Setting pixel format... "); - SetGraphicsModePFD(format, (WinWindowInfo)window); + Debug.Print( "OpenGL will be bound to handle: {0}", window.WindowHandle ); + SelectGraphicsModePFD(format, (WinWindowInfo)window); + Debug.Write( "Setting pixel format... " ); + SetGraphicsModePFD(format, (WinWindowInfo)window); - Debug.Write("Falling back to GL2... "); + ContextHandle = Wgl.wglCreateContext(window.DeviceContext); + if (ContextHandle == IntPtr.Zero) ContextHandle = Wgl.wglCreateContext(window.DeviceContext); - if (ContextHandle == IntPtr.Zero) - ContextHandle = Wgl.wglCreateContext(window.DeviceContext); - if (ContextHandle == IntPtr.Zero) - throw new GraphicsContextException( - String.Format("Context creation failed. Wgl.CreateContext() error: {0}.", - Marshal.GetLastWin32Error())); - - Debug.WriteLine(String.Format("success! (id: {0})", ContextHandle)); - } + if (ContextHandle == IntPtr.Zero) + throw new GraphicsContextException( + String.Format("Context creation failed. Wgl.CreateContext() error: {0}.", + Marshal.GetLastWin32Error())); + + Debug.Print( "success! (id: {0})", ContextHandle ); } - #endregion - - #region --- IGraphicsContext Members --- - - #region SwapBuffers - - public override void SwapBuffers() - { + public override void SwapBuffers() { if (!Functions.SwapBuffers(dc)) throw new GraphicsContextException(String.Format( "Failed to swap buffers for context {0} current. Error: {1}", this, Marshal.GetLastWin32Error())); } - #endregion - - #region MakeCurrent - IntPtr dc; - public override void MakeCurrent(IWindowInfo window) - { + public override void MakeCurrent(IWindowInfo window) { bool success; if (window != null) { @@ -118,61 +80,27 @@ namespace OpenTK.Platform.Windows "Failed to make context {0} current. Error: {1}", this, Marshal.GetLastWin32Error())); dc = Wgl.wglGetCurrentDC(); } - #endregion - #region IsCurrent - - public override bool IsCurrent - { + public override bool IsCurrent { get { return Wgl.wglGetCurrentContext() == ContextHandle; } } - #endregion - - #region public bool VSync - - /// - /// Gets or sets a System.Boolean indicating whether SwapBuffer calls are synced to the screen refresh rate. - /// + /// Gets or sets a System.Boolean indicating whether SwapBuffer calls are synced to the screen refresh rate. public override bool VSync { - get { - return vsync_supported && Wgl.wglGetSwapIntervalEXT() != 0; - } + get { return vsync_supported && Wgl.wglGetSwapIntervalEXT() != 0; } set { if (vsync_supported) Wgl.wglSwapIntervalEXT(value ? 1 : 0); } } - #endregion - - #region void LoadAll() - - public override void LoadAll() - { + public override void LoadAll() { new Wgl().LoadEntryPoints(); - vsync_supported = Wgl.wglGetSwapIntervalEXT != null + vsync_supported = Wgl.wglGetSwapIntervalEXT != null && Wgl.wglSwapIntervalEXT != null; new OpenTK.Graphics.OpenGL.GL().LoadEntryPoints( this ); } - #endregion - - #endregion - - #region --- IGLContextInternal Members --- - - #region IWindowInfo IGLContextInternal.Info - /* - IWindowInfo IGraphicsContextInternal.Info - { - get { return (IWindowInfo)windowInfo; } - } - */ - #endregion - - #region GetAddress - public override IntPtr GetAddress(string funcName) { IntPtr dynAddress = Wgl.wglGetProcAddress(funcName); if( !BindingsBase.IsInvalidAddress( dynAddress ) ) @@ -180,11 +108,6 @@ namespace OpenTK.Platform.Windows return Functions.GetProcAddress( opengl32Handle, funcName ); } - #endregion - - #endregion - - #region --- Private Methods --- int modeIndex; void SetGraphicsModePFD(GraphicsMode mode, WinWindowInfo window) { @@ -237,75 +160,46 @@ namespace OpenTK.Platform.Windows throw new GraphicsModeException("The requested GraphicsMode is not available."); } - #endregion - - #region --- Overrides --- - - /// Returns a System.String describing this OpenGL context. - /// A System.String describing this OpenGL context. - public override string ToString() - { - return (this as IGraphicsContextInternal).Context.ToString(); + public override string ToString() { + return ContextHandle.ToString(); } - #endregion - - #region --- IDisposable Members --- - - public override void Dispose() - { + public override void Dispose() { Dispose(true); GC.SuppressFinalize(this); } - private void Dispose(bool calledManually) - { - if (!IsDisposed) - { - if (calledManually) - { - DestroyContext(); - } - else - { - Debug.Print("[Warning] OpenGL context {0} leaked. Did you forget to call IGraphicsContext.Dispose()?", ContextHandle); - } - IsDisposed = true; + private void Dispose(bool calledManually) { + if (IsDisposed) return; + + if (calledManually) { + DestroyContext(); + } else { + Debug.Print("[Warning] OpenGL context {0} leaked. Did you forget to call IGraphicsContext.Dispose()?", ContextHandle); } + IsDisposed = true; } - ~WinGLContext() - { + ~WinGLContext() { Dispose(false); } - #region private void DestroyContext() - - private void DestroyContext() - { - if (ContextHandle != IntPtr.Zero) - { - try - { - // This will fail if the user calls Dispose() on thread X when the context is current on thread Y. - if (!Wgl.wglDeleteContext(ContextHandle)) - Debug.Print("Failed to destroy OpenGL context {0}. Error: {1}", - ContextHandle.ToString(), Marshal.GetLastWin32Error()); - } - catch (AccessViolationException e) - { - Debug.WriteLine("An access violation occured while destroying the OpenGL context. Please report at http://www.opentk.com."); - Debug.Indent(); - Debug.Print("Marshal.GetLastWin32Error(): {0}", Marshal.GetLastWin32Error().ToString()); - Debug.WriteLine(e.ToString()); - Debug.Unindent(); - } - ContextHandle = IntPtr.Zero; + private void DestroyContext() { + if (ContextHandle == IntPtr.Zero) return; + + try { + // This will fail if the user calls Dispose() on thread X when the context is current on thread Y. + if (!Wgl.wglDeleteContext(ContextHandle)) + Debug.Print("Failed to destroy OpenGL context {0}. Error: {1}", + ContextHandle.ToString(), Marshal.GetLastWin32Error()); + } catch (AccessViolationException e) { + Debug.WriteLine("An access violation occured while destroying the OpenGL context. Please report at http://www.opentk.com."); + Debug.Indent(); + Debug.Print("Marshal.GetLastWin32Error(): {0}", Marshal.GetLastWin32Error().ToString()); + Debug.WriteLine(e.ToString()); + Debug.Unindent(); } + ContextHandle = IntPtr.Zero; } - - #endregion - - #endregion } } diff --git a/OpenTK/Platform/Windows/WinKeyMap.cs b/OpenTK/Platform/Windows/WinKeyMap.cs index c2990b6ea..462863988 100644 --- a/OpenTK/Platform/Windows/WinKeyMap.cs +++ b/OpenTK/Platform/Windows/WinKeyMap.cs @@ -8,13 +8,12 @@ using System; using System.Collections.Generic; using OpenTK.Input; -namespace OpenTK.Platform.Windows -{ - internal class WinKeyMap : Dictionary - { +namespace OpenTK.Platform.Windows { + + internal class WinKeyMap : Dictionary { + /// Initializes the map between VirtualKeys and OpenTK.Key - internal WinKeyMap() - { + internal WinKeyMap() { AddKey(VirtualKeys.ESCAPE, Key.Escape); // Function keys @@ -76,8 +75,7 @@ namespace OpenTK.Platform.Windows AddKey(VirtualKeys.SLEEP, Key.Sleep); // Keypad - for (int i = 0; i <= 9; i++) - { + for (int i = 0; i <= 9; i++) { AddKey((VirtualKeys)((int)VirtualKeys.NUMPAD0 + i), Key.Keypad0 + i); } AddKey(VirtualKeys.DECIMAL, Key.KeypadDecimal); diff --git a/OpenTK/Platform/Windows/WinWindowInfo.cs b/OpenTK/Platform/Windows/WinWindowInfo.cs index ae795f332..06c9eb0a9 100644 --- a/OpenTK/Platform/Windows/WinWindowInfo.cs +++ b/OpenTK/Platform/Windows/WinWindowInfo.cs @@ -26,62 +26,39 @@ #endregion using System; -using System.Collections.Generic; -using System.Text; using System.Diagnostics; using System.Runtime.InteropServices; -namespace OpenTK.Platform.Windows -{ +namespace OpenTK.Platform.Windows { + /// \internal /// Describes a win32 window. - public sealed class WinWindowInfo : IWindowInfo - { + public sealed class WinWindowInfo : IWindowInfo { IntPtr handle, dc; WinWindowInfo parent; bool disposed; - #region --- Constructors --- - - /// - /// Constructs a new instance. - /// - public WinWindowInfo() - { + /// Constructs a new instance. + public WinWindowInfo() { } - /// - /// Constructs a new instance with the specified window handle and paren.t - /// + /// Constructs a new instance with the specified window handle and parent. /// The window handle for this instance. /// The parent window of this instance (may be null). - public WinWindowInfo(IntPtr handle, WinWindowInfo parent) - { + public WinWindowInfo(IntPtr handle, WinWindowInfo parent) { this.handle = handle; this.parent = parent; } - #endregion - - #region --- Public Methods --- - - /// - /// Gets or sets the handle of the window. - /// + /// Gets or sets the handle of the window. public IntPtr WindowHandle { get { return handle; } set { handle = value; } } - /// - /// Gets or sets the Parent of the window (may be null). - /// + /// Gets or sets the Parent of the window (may be null). public WinWindowInfo Parent { get { return parent; } set { parent = value; } } - /// - /// Gets the device context for this window instance. - /// - public IntPtr DeviceContext - { - get - { + /// Gets the device context for this window instance. + public IntPtr DeviceContext { + get { if (dc == IntPtr.Zero) dc = Functions.GetDC(this.WindowHandle); //dc = Functions.GetWindowDC(this.WindowHandle); @@ -89,21 +66,17 @@ namespace OpenTK.Platform.Windows } } - #region public override string ToString() - /// Returns a System.String that represents the current window. /// A System.String that represents the current window. - public override string ToString() - { + public override string ToString() { return String.Format("Windows.WindowInfo: Handle {0}, Parent ({1})", - this.WindowHandle, this.Parent != null ? this.Parent.ToString() : "null"); + WindowHandle, Parent != null ? Parent.ToString() : "null"); } /// Checks if this and obj reference the same win32 window. /// The object to check against. /// True if this and obj reference the same win32 window; false otherwise. - public override bool Equals(object obj) - { + public override bool Equals(object obj) { if (obj == null) return false; if (this.GetType() != obj.GetType()) return false; WinWindowInfo info = (WinWindowInfo)obj; @@ -115,59 +88,30 @@ namespace OpenTK.Platform.Windows /// Returns the hash code for this instance. /// A hash code for the current WinWindowInfo. - public override int GetHashCode() - { + public override int GetHashCode() { return handle.GetHashCode(); } - #endregion - - #endregion - - #region --- IDisposable --- - - #region public void Dispose() - /// Releases the unmanaged resources consumed by this instance. - public void Dispose() - { - this.Dispose(true); + public void Dispose() { + Dispose(true); GC.SuppressFinalize(this); } - #endregion - - #region void Dispose(bool manual) - - void Dispose(bool manual) - { - if (!disposed) - { - if (this.dc != IntPtr.Zero) + void Dispose(bool manual) { + if (!disposed) { + if (dc != IntPtr.Zero) if (!Functions.ReleaseDC(this.handle, this.dc)) Debug.Print("[Warning] Failed to release device context {0}. Windows error: {1}.", this.dc, Marshal.GetLastWin32Error()); - if (manual) - { - if (parent != null) - parent.Dispose(); - } - + if (manual && parent != null) + parent.Dispose(); disposed = true; } } - #endregion - - #region ~WinWindowInfo() - - ~WinWindowInfo() - { - this.Dispose(false); + ~WinWindowInfo() { + Dispose(false); } - - #endregion - - #endregion } } diff --git a/OpenTK/Platform/X11/Structs.cs b/OpenTK/Platform/X11/Structs.cs index b812fd66e..2749e5d16 100644 --- a/OpenTK/Platform/X11/Structs.cs +++ b/OpenTK/Platform/X11/Structs.cs @@ -1010,22 +1010,6 @@ namespace OpenTK.Platform.X11 BorderWidth = 1 << 4, Sibling = 1 << 5, StackMode = 1 << 6, - - //BackPixmap (1L<<0) - //BackPixel (1L<<1) - //SaveUnder (1L<<10) - //EventMask (1L<<11) - //DontPropagate (1L<<12) - //Colormap (1L<<13) - //Cursor (1L<<14) - //BorderPixmap (1L<<2) - //BorderPixel (1L<<3) - //BitGravity (1L<<4) - //WinGravity (1L<<5) - //BackingStore (1L<<6) - //BackingPlanes (1L<<7) - //BackingPixel (1L<<8) - OverrideRedirect = 1<<9, } internal enum StackMode diff --git a/OpenTK/Platform/X11/X11DisplayDevice.cs b/OpenTK/Platform/X11/X11DisplayDevice.cs index caae06165..5c3e4079d 100644 --- a/OpenTK/Platform/X11/X11DisplayDevice.cs +++ b/OpenTK/Platform/X11/X11DisplayDevice.cs @@ -16,7 +16,6 @@ namespace OpenTK.Platform.X11 { internal class X11DisplayDevice : IDisplayDeviceDriver { - static object display_lock = new object(); // 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 @@ -95,7 +94,7 @@ namespace OpenTK.Platform.X11 if (NativeMethods.XineramaQueryExtension(API.DefaultDisplay, out event_base, out error_base) && NativeMethods.XineramaIsActive(API.DefaultDisplay)) { - IList screens = NativeMethods.XineramaQueryScreens(API.DefaultDisplay); + XineramaScreenInfo[] screens = NativeMethods.XineramaQueryScreens(API.DefaultDisplay); bool first = true; foreach (XineramaScreenInfo screen in screens) { @@ -310,31 +309,21 @@ namespace OpenTK.Platform.X11 [DllImport(Xinerama)] public static extern bool XineramaQueryExtension(IntPtr dpy, out int event_basep, out int error_basep); - [DllImport(Xinerama)] - public static extern int XineramaQueryVersion (IntPtr dpy, out int major_versionp, out int minor_versionp); - [DllImport(Xinerama)] public static extern bool XineramaIsActive(IntPtr dpy); [DllImport(Xinerama)] static extern IntPtr XineramaQueryScreens(IntPtr dpy, out int number); - public static IList XineramaQueryScreens(IntPtr dpy) - { - int number; - IntPtr screen_ptr = XineramaQueryScreens(dpy, out number); - List screens = new List(number); - - unsafe - { - XineramaScreenInfo* ptr = (XineramaScreenInfo*)screen_ptr; - while (--number >= 0) - { - screens.Add(*ptr); - ptr++; - } + public unsafe static XineramaScreenInfo[] XineramaQueryScreens(IntPtr dpy) { + int count; + IntPtr screen_ptr = XineramaQueryScreens(dpy, out count); + XineramaScreenInfo* ptr = (XineramaScreenInfo*)screen_ptr; + + XineramaScreenInfo[] screens = new XineramaScreenInfo[count]; + for( int i = 0; i < screens.Length; i++ ) { + screens[i] = *ptr++; } - return screens; } } diff --git a/OpenTK/Platform/X11/X11Factory.cs b/OpenTK/Platform/X11/X11Factory.cs deleted file mode 100644 index d9baa7548..000000000 --- a/OpenTK/Platform/X11/X11Factory.cs +++ /dev/null @@ -1,60 +0,0 @@ -#region License -// -// The Open Toolkit Library License -// -// Copyright (c) 2006 - 2009 the Open Toolkit library. -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights to -// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of -// the Software, and to permit persons to whom the Software is furnished to do -// so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in all -// copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. -// -#endregion - -using System; -using System.Diagnostics; -using OpenTK.Graphics; - -namespace OpenTK.Platform.X11 -{ - class X11Factory : IPlatformFactory - { - #region IPlatformFactory Members - - public virtual INativeWindow CreateNativeWindow(int x, int y, int width, int height, string title, GraphicsMode mode, GameWindowFlags options, DisplayDevice device) - { - return new X11GLNative(x, y, width, height, title, mode, options, device); - } - - public virtual IDisplayDeviceDriver CreateDisplayDeviceDriver() - { - return new X11DisplayDevice(); - } - - public virtual IGraphicsContext CreateGLContext(GraphicsMode mode, IWindowInfo window) - { - return new X11GLContext(mode, window); - } - - public virtual IGraphicsMode CreateGraphicsMode() - { - return new X11GraphicsMode(); - } - - #endregion - } -}