From 261df176323c186c710b09c0464126c24860d655 Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Wed, 11 Oct 2006 22:48:39 +0000 Subject: [PATCH] Updated the Context class hierarchy. Now WindowsVistaContext and WindowsContext inherit from WindowsBaseContext, avoiding code duplication. Some minor updates to the XApi, Framework and GLContext files. Updated todo.txt and changelog.txt --- Source/Framework/Framework.cs | 62 +++++++++++-------- Source/OpenGL/OpenGL/Contexts/GLContext.cs | 3 +- .../OpenGL/Contexts/WindowsBaseContext.cs | 52 ++++++++++++++++ .../OpenGL/OpenGL/Contexts/WindowsContext.cs | 43 +------------ .../OpenGL/Contexts/WindowsVistaContext.cs | 43 +------------ Source/OpenGL/OpenGL/OpenTK.OpenGL.csproj | 5 +- Source/Platform/X/XApi.cs | 2 +- changelog.txt | 5 +- todo.txt | 12 ++-- 9 files changed, 106 insertions(+), 121 deletions(-) create mode 100644 Source/OpenGL/OpenGL/Contexts/WindowsBaseContext.cs diff --git a/Source/Framework/Framework.cs b/Source/Framework/Framework.cs index f0e54dd8..25e0bc38 100644 --- a/Source/Framework/Framework.cs +++ b/Source/Framework/Framework.cs @@ -49,6 +49,16 @@ namespace OpenTK public void Open(string title, int width, int height, int red, int green, int blue, int alpha, int depth, int stencil, bool fullscreen) { Application.Idle += new EventHandler(OnIdle); + + Context = GLContext.Create(this, red, green, blue, alpha, depth, stencil); + + // Code taken from NeHe tutorials + this.CreateParams.Style |= (int)Api.WindowClassStyle.HRedraw | (int)Api.WindowClassStyle.VRedraw | (int)Api.WindowClassStyle.OwnDC; + this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); // No Need To Erase Form Background + this.SetStyle(ControlStyles.Opaque, true); // No Need To Draw Form Background + //this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); // Buffer Control + //this.SetStyle(ControlStyles.ResizeRedraw, true); // Redraw On Resize + this.SetStyle(ControlStyles.UserPaint, true); // We'll Handle Painting Ourselves try { @@ -73,24 +83,14 @@ namespace OpenTK } } + #region Open functions + public void WindowsOpen(string title, int width, int height, int red, int green, int blue, int alpha, int depth, int stencil, bool fullscreen) { - // Hack! Should add more constructors to the GLContext class. - Context = GLContext.Create(this, 8, 8, 8, 8, 16, 0); - - // Todo: Why doesn't this work? if (title == null) - title = "OpenTK application"; + title = "OpenTK Windows application"; this.Text = title; - // Code taken from NeHe tutorials - this.CreateParams.Style |= (int)Api.WindowClassStyle.HRedraw | (int)Api.WindowClassStyle.VRedraw | (int)Api.WindowClassStyle.OwnDC; - this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); // No Need To Erase Form Background - this.SetStyle(ControlStyles.Opaque, true); // No Need To Draw Form Background - //this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); // Buffer Control - //this.SetStyle(ControlStyles.ResizeRedraw, true); // Redraw On Resize - this.SetStyle(ControlStyles.UserPaint, true); // We'll Handle Painting Ourselves - if (fullscreen) { Api.DeviceMode ScreenSettings = new Api.DeviceMode(); // Device Mode @@ -118,27 +118,27 @@ namespace OpenTK } this.Size = new Size(width, height); - - // Cross-platformness? - } + public void XOpen(string title, int width, int height, int red, int green, int blue, int alpha, int depth, int stencil, bool fullscreen) + { + Context = GLContext.Create(this, red, green, blue, alpha, depth, stencil); + + if (title == null) + title = "OpenTK X application"; + this.Text = title; + + this.Size = new Size(width, height); + } + + #endregion + //override protected void WndProc(ref Message m) //{ // base.WndProc(ref m); // //OnPaint(null); //} - #region IDisposable Members - - void IDisposable.Dispose() - { - Application.Idle -= OnIdle; - } - - #endregion - - #region Event Handlers /// @@ -172,5 +172,15 @@ namespace OpenTK } #endregion + + #region IDisposable Members + + void IDisposable.Dispose() + { + //GC.SuppressFinalize(true); + Application.Idle -= OnIdle; + } + + #endregion } } diff --git a/Source/OpenGL/OpenGL/Contexts/GLContext.cs b/Source/OpenGL/OpenGL/Contexts/GLContext.cs index fd235ff0..2da6bcb1 100644 --- a/Source/OpenGL/OpenGL/Contexts/GLContext.cs +++ b/Source/OpenGL/OpenGL/Contexts/GLContext.cs @@ -34,8 +34,7 @@ namespace OpenTK.OpenGL { try { - if (Environment.OSVersion.Platform == PlatformID.Win32NT && Environment.OSVersion.Version.Major < 6 || - Environment.OSVersion.Platform == PlatformID.Win32Windows) + if (Environment.OSVersion.Platform == PlatformID.Win32NT || Environment.OSVersion.Platform == PlatformID.Win32Windows) { return new WindowsContext(c, red, green, blue, alpha, depth, stencil); } diff --git a/Source/OpenGL/OpenGL/Contexts/WindowsBaseContext.cs b/Source/OpenGL/OpenGL/Contexts/WindowsBaseContext.cs new file mode 100644 index 00000000..5ef38c0f --- /dev/null +++ b/Source/OpenGL/OpenGL/Contexts/WindowsBaseContext.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.Text; +using OpenTK.OpenGL; +using System.Runtime.InteropServices; + +namespace OpenTK.OpenGL.Platform +{ + public class WindowsBaseContext : GLContext + { + protected const string _dll_name = "OPENGL32.DLL"; + protected int _dll_handle; + protected int _device_context; + protected int _render_context; + protected IntPtr _window_handle; + + public override void SwapBuffers() + { + OpenTK.Platform.Windows.Api.SwapBuffers(_device_context); + } + + public override Delegate GetAddress(string function_string, Type function_type) + { + IntPtr address = Wgl.GetProcAddress(function_string); + if (address == IntPtr.Zero) + return null; + else + return Marshal.GetDelegateForFunctionPointer(address, function_type); + } + + public override void MakeCurrent() + { + Wgl.MakeCurrent(_device_context, _render_context); + } + + public override void Dispose() + { + if (_render_context != 0) + Wgl.DeleteContext(_render_context); + + if (_device_context != 0) + OpenTK.Platform.Windows.Api.ReleaseDC(_window_handle.ToInt32(), _device_context); + + if (_dll_handle != 0) + OpenTK.Platform.Windows.Api.FreeLibrary(_dll_handle); + + _render_context = 0; + _device_context = 0; + _dll_handle = 0; + } + } +} diff --git a/Source/OpenGL/OpenGL/Contexts/WindowsContext.cs b/Source/OpenGL/OpenGL/Contexts/WindowsContext.cs index 7a9ce85e..1abeebdf 100644 --- a/Source/OpenGL/OpenGL/Contexts/WindowsContext.cs +++ b/Source/OpenGL/OpenGL/Contexts/WindowsContext.cs @@ -13,14 +13,8 @@ using System.Runtime.InteropServices; namespace OpenTK.OpenGL.Platform { - public partial class WindowsContext : OpenTK.OpenGL.GLContext + public partial class WindowsContext : WindowsBaseContext { - const string _dll_name = "OPENGL32.DLL"; - int _dll_handle; - int _device_context; - int _render_context; - IntPtr _window_handle; - public WindowsContext(Control c, int red, int green, int blue, int alpha, int depth, int stencil) { int error_code = 0; @@ -100,40 +94,5 @@ namespace OpenTK.OpenGL.Platform //if (load_extensions) // LoadExtensions(); } - - public override void SwapBuffers() - { - OpenTK.Platform.Windows.Api.SwapBuffers(_device_context); - } - - public override Delegate GetAddress(string function_string, Type function_type) - { - IntPtr address = Wgl.GetProcAddress(function_string); - if (address == IntPtr.Zero) - return null; - else - return Marshal.GetDelegateForFunctionPointer(address, function_type); - } - - public override void MakeCurrent() - { - Wgl.MakeCurrent(_device_context, _render_context); - } - - public override void Dispose() - { - if (_render_context != 0) - Wgl.DeleteContext(_render_context); - - if (_device_context != 0) - OpenTK.Platform.Windows.Api.ReleaseDC(_window_handle.ToInt32(), _device_context); - - if (_dll_handle != 0) - OpenTK.Platform.Windows.Api.FreeLibrary(_dll_handle); - - _render_context = 0; - _device_context = 0; - _dll_handle = 0; - } } } diff --git a/Source/OpenGL/OpenGL/Contexts/WindowsVistaContext.cs b/Source/OpenGL/OpenGL/Contexts/WindowsVistaContext.cs index 5c35db73..ea0397eb 100644 --- a/Source/OpenGL/OpenGL/Contexts/WindowsVistaContext.cs +++ b/Source/OpenGL/OpenGL/Contexts/WindowsVistaContext.cs @@ -11,14 +11,8 @@ using OpenTK.OpenGL; namespace OpenTK.OpenGL.Platform { - public partial class WindowsVistaContext : OpenTK.OpenGL.GLContext + public partial class WindowsVistaContext : WindowsBaseContext { - const string _dll_name = "OPENGL32.DLL"; - int _dll_handle; - int _device_context; - int _render_context; - IntPtr _window_handle; - public WindowsVistaContext(Control c, int red, int green, int blue, int alpha, int depth, int stencil) { int error_code = 0; @@ -98,40 +92,5 @@ namespace OpenTK.OpenGL.Platform //if (load_extensions) // LoadExtensions(); } - - public override void SwapBuffers() - { - OpenTK.Platform.Windows.Api.SwapBuffers(_device_context); - } - - public override Delegate GetAddress(string function_string, Type function_type) - { - IntPtr address = Wgl.GetProcAddress(function_string); - if (address == IntPtr.Zero) - return null; - else - return Marshal.GetDelegateForFunctionPointer(address, function_type); - } - - public override void MakeCurrent() - { - Wgl.MakeCurrent(_device_context, _render_context); - } - - public override void Dispose() - { - if (_render_context != 0) - Wgl.DeleteContext(_render_context); - - if (_device_context != 0) - OpenTK.Platform.Windows.Api.ReleaseDC(_window_handle.ToInt32(), _device_context); - - if (_dll_handle != 0) - OpenTK.Platform.Windows.Api.FreeLibrary(_dll_handle); - - _render_context = 0; - _device_context = 0; - _dll_handle = 0; - } } } \ No newline at end of file diff --git a/Source/OpenGL/OpenGL/OpenTK.OpenGL.csproj b/Source/OpenGL/OpenGL/OpenTK.OpenGL.csproj index c38bf6e6..7eeb8d38 100644 --- a/Source/OpenGL/OpenGL/OpenTK.OpenGL.csproj +++ b/Source/OpenGL/OpenGL/OpenTK.OpenGL.csproj @@ -48,10 +48,13 @@ + + + Code + - diff --git a/Source/Platform/X/XApi.cs b/Source/Platform/X/XApi.cs index c9cd9d0f..c6aa38e5 100644 --- a/Source/Platform/X/XApi.cs +++ b/Source/Platform/X/XApi.cs @@ -47,6 +47,7 @@ namespace OpenTK.Platform.X [DllImport(_dll_name, EntryPoint = "XCloseDisplay")] extern public static void CloseDisplay(IntPtr display); + // [DllImport(_dll_name, EntryPoint = "XDefaultScreen")] extern public static int DefaultScreen(IntPtr display); @@ -57,7 +58,6 @@ namespace OpenTK.Platform.X [DllImport(_dll_name, EntryPoint = "XFree")] extern public static void Free(IntPtr data); - // Queue management [DllImport(_dll_name, EntryPoint = "XEventsQueued")] extern public static int EventsQueued(IntPtr Display, int mode); diff --git a/changelog.txt b/changelog.txt index 87b3c03e..f03f7325 100644 --- a/changelog.txt +++ b/changelog.txt @@ -11,7 +11,10 @@ OpenTK 0.3.4 -> 0.3.5 + Added bindings to some glx functions. + Added the OpenTK.Platform.X class. + Minor updates to the wgl bindings (int -> IntPtr). -+ Tested the binding generator with the glx specs (the reader and the enum writer work ok, the trnaslator and the other writers need updating). ++ Tested the binding generator with the glx specs (the reader and the enum writer work ok, the trnaslator and the other writers will need updating). ++ Renamed the GLForm to Framework. + + The Framework now resides in its own project directory. ++ Revamped the GLContext class hierarchy. WindowsContext and WindowsVistaContext now both inherit from WindowsBaseContext. This allows for less code-duplication (and thus less bugs). OpenTK 0.3.3 -> 0.3.4 + Corrected the crash error in Release mode (it was caused by trying to Marshal the System.Windows.Forms.Message struct to PeekMessage - adding my own Message struct corrected the issue). diff --git a/todo.txt b/todo.txt index 21346616..ff038c4e 100644 --- a/todo.txt +++ b/todo.txt @@ -1,13 +1,13 @@ Todo: -+ + + Add basic GLX bindings. -+ + + Add basic X bindings. -+ + + Add context creation support for X. ++ + + Correct the Dispose methods to correctly clean resources (they should also call GC.SupressFinalize(true)) ++ + + Add basic GLX bindings (in progress) ++ + + Add basic X bindings (in progress) ++ + + Add context creation support for X (needs testing) + + + Test X support. + + + Add the CLS compliant attribute to the GL class. -+ + Add cross-platform way for overriding the Application.Idle handler. -+ + Change the hierarchy of the WindowsContext and WindowsVistaContext classes. The should have a common ancestor who manages the windows creation (it should be the same for both). ++ + Add cross-platform way for defining the Application.Idle handler (needs testing) + + Add more constructors to the Context classes. -+ + Add more GLForm constructors. ++ + Add more Framework constructors. + + Add comments and documentation (faqs, pitfalls, best practices). + + Add a cross-platform build system (probably NAnt?) + Add more examples.