From f0f03d58db7719dd8fbbcd0163a23978d7dffc4a Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Sun, 15 Oct 2006 23:31:37 +0000 Subject: [PATCH] Updates to fullscreen toggling for windows. Added the ColorDepth, ZDepth and StencilDepth properties to the Framework. Merged Context creation for windows into a single file. --- Source/Examples/OpenGL/GLSL/Lesson01/Cube.cs | 25 ++++- Source/Framework/Framework.cs | 66 ++++++++++-- Source/Framework/FrameworkImplementation.cs | 8 +- Source/Framework/WindowsImplementation.cs | 25 +++-- Source/OpenGL/Bind/Bind.suo | Bin 2560 -> 0 bytes Source/OpenGL/Bind/WriteSpecs.cs | 4 - Source/OpenGL/OpenGL/Bindings/GL.cs | 3 - Source/OpenGL/OpenGL/Contexts/GLContext.cs | 6 +- .../OpenGL/Contexts/WindowsBaseContext.cs | 52 ---------- .../OpenGL/OpenGL/Contexts/WindowsContext.cs | 58 +++++++++-- .../OpenGL/Contexts/WindowsVistaContext.cs | 97 ------------------ Source/OpenGL/OpenGL/Contexts/X11Context.cs | 2 +- Source/OpenGL/OpenGL/OpenTK.OpenGL.csproj | 7 +- Source/OpenGL/OpenGL/Structures.cs | 27 +++++ 14 files changed, 183 insertions(+), 197 deletions(-) delete mode 100644 Source/OpenGL/Bind/Bind.suo delete mode 100644 Source/OpenGL/OpenGL/Contexts/WindowsBaseContext.cs delete mode 100644 Source/OpenGL/OpenGL/Contexts/WindowsVistaContext.cs create mode 100644 Source/OpenGL/OpenGL/Structures.cs diff --git a/Source/Examples/OpenGL/GLSL/Lesson01/Cube.cs b/Source/Examples/OpenGL/GLSL/Lesson01/Cube.cs index a485cda2..c706b625 100644 --- a/Source/Examples/OpenGL/GLSL/Lesson01/Cube.cs +++ b/Source/Examples/OpenGL/GLSL/Lesson01/Cube.cs @@ -1,4 +1,10 @@ -using System; +#region License +/* Copyright (c) 2006 Stephen Apostolopoulos + * See license.txt for license info + */ +#endregion + +using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; @@ -110,6 +116,7 @@ namespace OpenTK.Examples.OpenGL.GLSL #endregion #region Paint event handler + protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); @@ -129,18 +136,28 @@ namespace OpenTK.Examples.OpenGL.GLSL DrawCube(); Context.SwapBuffers(); - this.Invalidate(); } + #endregion #region KeyDown event handler + protected override void OnKeyDown(KeyEventArgs e) { base.OnKeyDown(e); - if (e.KeyData == Keys.Escape) - Application.Exit(); + switch (e.KeyData) + { + case Keys.Escape: + Application.Exit(); + break; + + case Keys.F1: + //this.Fullscreen = !this.Fullscreen; + break; + } } + #endregion #region DrawCube diff --git a/Source/Framework/Framework.cs b/Source/Framework/Framework.cs index 509bb97f..1fcc2188 100644 --- a/Source/Framework/Framework.cs +++ b/Source/Framework/Framework.cs @@ -40,7 +40,43 @@ namespace OpenTK.Frameworks public bool Fullscreen { get { return _fullscreen; } - set { _fullscreen = Implementation.ToggleFullscreen(_fullscreen); } + private set { _fullscreen = value; } + } + + #endregion + + #region ColorDepth property + + private OpenTK.OpenGL.ColorDepth _color_depth; + + public OpenTK.OpenGL.ColorDepth ColorDepth + { + get { return _color_depth; } + set { _color_depth = value; } + } + + #endregion + + #region ZDepth property + + private int _z_depth; + + public int ZDepth + { + get { return _z_depth; } + set { _z_depth = value; } + } + + #endregion + + #region StencilDepth property + + private int _stencil_depth; + + public int StencilDepth + { + get { return _stencil_depth; } + set { _stencil_depth = value; } } #endregion @@ -77,18 +113,24 @@ namespace OpenTK.Frameworks public Framework() { - Setup(null, 640, 480, 8, 8, 8, 8, 16, 0, false); + Setup(null, 640, 480, new OpenTK.OpenGL.ColorDepth(8, 8, 8, 8), 16, 0, true); } - public Framework(string title, int width, int height, int red, int green, int blue, int alpha, int depth, int stencil, bool fullscreen) + + public Framework(string title, int width, int height, OpenTK.OpenGL.ColorDepth color, int depth, int stencil, bool fullscreen) { - Setup(title, width, height, red, green, blue, alpha, depth, stencil, fullscreen); + Setup(title, width, height, color, depth, stencil, fullscreen); } #endregion - public void Setup(string title, int width, int height, int red, int green, int blue, int alpha, int depth, int stencil, bool fullscreen) + public void Setup(string title, int width, int height, OpenTK.OpenGL.ColorDepth color, int depth, int stencil, bool fullscreen) { + // Initialise components. + ColorDepth = color; + ZDepth = depth; + StencilDepth = stencil; + // Set platform. try { @@ -111,7 +153,6 @@ namespace OpenTK.Frameworks throw e; } - Implementation.Setup(); this.HandleCreated += new EventHandler(Implementation.OnHandleCreated); //Type xplatui = Type.GetType("System.Windows.Forms.XplatUIX11, System.Windows.Forms"); @@ -120,23 +161,26 @@ namespace OpenTK.Frameworks // Context = GLContext.Create(this, 8, 8, 8, 8, 16, 0); // //Context.MakeCurrent(); //} - - Context = GLContext.Create(this, red, green, blue, alpha, depth, stencil); + + Context = GLContext.Create(this, color, 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.ResizeRedraw, true); // Redraw On Resize this.SetStyle(ControlStyles.UserPaint, true); // We'll Handle Painting Ourselves + this.Width = width; + this.Height = height; + + Fullscreen = Implementation.ToggleFullscreen(fullscreen); + if (title == null) title = "OpenTK Windows application"; this.Text = title; - this.Size = new Size(width, height); - Application.Idle += new EventHandler(OnIdle); } diff --git a/Source/Framework/FrameworkImplementation.cs b/Source/Framework/FrameworkImplementation.cs index 35ba90ca..deb88a8f 100644 --- a/Source/Framework/FrameworkImplementation.cs +++ b/Source/Framework/FrameworkImplementation.cs @@ -1,4 +1,10 @@ -using System; +#region License +/* Copyright (c) 2006 Stephen Apostolopoulos + * See license.txt for license info + */ +#endregion + +using System; using System.Collections.Generic; using System.Text; using System.Drawing; diff --git a/Source/Framework/WindowsImplementation.cs b/Source/Framework/WindowsImplementation.cs index 818548b5..f7bf3298 100644 --- a/Source/Framework/WindowsImplementation.cs +++ b/Source/Framework/WindowsImplementation.cs @@ -26,6 +26,8 @@ namespace OpenTK.Frameworks public WindowsImplementation(Framework f) { framework = f; + + Setup(); // Set desktop resolution, refresh rate, pixel depth } @@ -52,13 +54,22 @@ namespace OpenTK.Frameworks { if (fullscreen) { - Api.DeviceMode ScreenSettings = new Api.DeviceMode(); // Device Mode + Application.Idle -= framework.OnIdle; + //framework.Context.Dispose(); + + Api.DeviceMode ScreenSettings = new Api.DeviceMode(); // Device Mode ScreenSettings.Size = (short)Marshal.SizeOf(ScreenSettings); // Size Of The Devmode Structure - ScreenSettings.PelsWidth = 640;// width; // Selected Screen Width - ScreenSettings.PelsHeight = 480;// height; // Selected Screen Height - ScreenSettings.BitsPerPel = 32;// red + green + blue + alpha; // Selected Bits Per Pixel + ScreenSettings.PelsWidth = framework.Width; // Selected Screen Width + ScreenSettings.PelsHeight = framework.Height; // Selected Screen Height + ScreenSettings.BitsPerPel = framework.ColorDepth.Alpha + // Selected Bits Per Pixel + framework.ColorDepth.Red + + framework.ColorDepth.Green + + framework.ColorDepth.Blue; ScreenSettings.Fields = Api.Constants.DM_BITSPERPEL | Api.Constants.DM_PELSWIDTH | Api.Constants.DM_PELSHEIGHT; + //framework.Context = GLContext.Create(framework, framework.ColorDepth, 16, 0); + Application.Idle += framework.OnIdle; + // Try To Set Selected Mode And Get Results. NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar. if (Api.ChangeDisplaySettings(ref ScreenSettings, Api.Constants.CDS_FULLSCREEN) == Api.Constants.DISP_CHANGE_SUCCESSFUL) { @@ -70,15 +81,15 @@ namespace OpenTK.Frameworks framework.SetTopLevel(true); Cursor.Hide(); - return !fullscreen; + return true; } else { - // Handle failure. + return false; } } - return fullscreen; + return false; } } } diff --git a/Source/OpenGL/Bind/Bind.suo b/Source/OpenGL/Bind/Bind.suo deleted file mode 100644 index 5f4483e3191e6efc3d657eb8c841e7967ad15503..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2560 zcmca`Uhu)fjZzO8(10BSGsD0CoD6J8;*3Bx2!nwD0|OI~0pkDr|NlQkkbwcn90fxt z1pWfu3W`4%9zsqZbt)A?Ac!F!2um0g7+e|hfOHW-W{^BoaRAW9e1R= 6) //{ @@ -44,7 +44,7 @@ namespace OpenTK.OpenGL //} else if (Environment.OSVersion.Platform == PlatformID.Unix) { - return new X11Context(c, red, green, blue, alpha, depth, stencil); + return new X11Context(c, color, depth, stencil); } else { diff --git a/Source/OpenGL/OpenGL/Contexts/WindowsBaseContext.cs b/Source/OpenGL/OpenGL/Contexts/WindowsBaseContext.cs deleted file mode 100644 index 5ef38c0f..00000000 --- a/Source/OpenGL/OpenGL/Contexts/WindowsBaseContext.cs +++ /dev/null @@ -1,52 +0,0 @@ -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 37d54827..ba43512a 100644 --- a/Source/OpenGL/OpenGL/Contexts/WindowsContext.cs +++ b/Source/OpenGL/OpenGL/Contexts/WindowsContext.cs @@ -1,4 +1,4 @@ -#region License +#region License /* Copyright (c) 2006 Stephen Apostolopoulos * See license.txt for license info */ @@ -7,15 +7,20 @@ using System; using System.Collections.Generic; using System.Text; -using System.Windows.Forms; using OpenTK.OpenGL; using System.Runtime.InteropServices; namespace OpenTK.OpenGL.Platform { - public partial class WindowsContext : WindowsBaseContext + public class WindowsContext : GLContext { - public WindowsContext(Control c, int red, int green, int blue, int alpha, int depth, int stencil) + protected const string _dll_name = "OPENGL32.DLL"; + protected int _dll_handle; + protected int _device_context; + protected int _render_context; + protected IntPtr _window_handle; + + public WindowsContext(System.Windows.Forms.Control c, ColorDepth color, int depth, int stencil) { int error_code = 0; _window_handle = c.Handle; @@ -39,11 +44,11 @@ namespace OpenTK.OpenGL.Platform _device_context = OpenTK.Platform.Windows.Api.GetDC(_window_handle.ToInt32()); OpenTK.Platform.Windows.Api.PixelFormatDescriptor pixel_format = new OpenTK.Platform.Windows.Api.PixelFormatDescriptor(); - pixel_format.ColorBits = (byte)(red + green + blue); - pixel_format.RedBits = (byte)red; - pixel_format.GreenBits = (byte)green; - pixel_format.BlueBits = (byte)blue; - pixel_format.AlphaBits = (byte)alpha; + pixel_format.ColorBits = (byte)(color.Red + color.Green + color.Blue); + pixel_format.RedBits = (byte)color.Red; + pixel_format.GreenBits = (byte)color.Green; + pixel_format.BlueBits = (byte)color.Blue; + pixel_format.AlphaBits = (byte)color.Alpha; pixel_format.DepthBits = (byte)depth; pixel_format.StencilBits = (byte)stencil; @@ -95,5 +100,40 @@ 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 deleted file mode 100644 index ebca61cf..00000000 --- a/Source/OpenGL/OpenGL/Contexts/WindowsVistaContext.cs +++ /dev/null @@ -1,97 +0,0 @@ -/* Copyright (c) 2006 Stephen Apostolopoulos - * See license.txt for license info - */ - -using System; -using System.Collections.Generic; -using System.Text; -using System.Windows.Forms; -using System.Runtime.InteropServices; -using OpenTK.OpenGL; - -namespace OpenTK.OpenGL.Platform -{ - public partial class WindowsVistaContext : WindowsBaseContext - { - public WindowsVistaContext(Control c, int red, int green, int blue, int alpha, int depth, int stencil) - { - int error_code = 0; - _window_handle = c.Handle; - - // Dynamically load the OpenGL32.dll in order to use the extension loading capabilities of Wgl. - if (_dll_handle == 0) - { - _dll_handle = OpenTK.Platform.Windows.Api.LoadLibrary(_dll_name); - - error_code = Marshal.GetLastWin32Error(); - if (error_code != 0) - { - Console.WriteLine("LoadLibrary({0}) set error code: {1}. Will not load extensions.", _dll_name, error_code); - } - else - { - Console.WriteLine("Loaded dll: {0}", _dll_name); - } - } - - _device_context = OpenTK.Platform.Windows.Api.GetDC(_window_handle.ToInt32()); - OpenTK.Platform.Windows.Api.PixelFormatDescriptor pixel_format = new OpenTK.Platform.Windows.Api.PixelFormatDescriptor(); - - pixel_format.ColorBits = (byte)(red + green + blue); - pixel_format.RedBits = (byte)red; - pixel_format.GreenBits = (byte)green; - pixel_format.BlueBits = (byte)blue; - pixel_format.AlphaBits = (byte)alpha; - - pixel_format.DepthBits = (byte)depth; - pixel_format.StencilBits = (byte)stencil; - - /* - pixel_format.AccumBits = (byte)(AccumRed + AccumGreen + AccumBlue); - pixel_format.AccumRedBits = (byte)AccumRed; - pixel_format.AccumGreenBits = (byte)AccumGreen; - pixel_format.AccumBlueBits = (byte)AccumBlue; - pixel_format.AccumAlphaBits = (byte)AccumAlpha; - */ - - if (depth <= 0) - { - pixel_format.Flags |= OpenTK.Platform.Windows.Api.PixelFormatDescriptorFlags.DEPTH_DONTCARE; - } - - /* - if (Stereo) - { - pixel_format.Flags |= OpenTK.Platform.Windows.Api.PixelFormatDescriptorFlags.STEREO; - } - */ - - /* - if (DoubleBuffer) - { - pixel_format.Flags |= OpenTK.Platform.Windows.Api.PixelFormatDescriptorFlags.DOUBLEBUFFER; - } - */ - - int pixel = OpenTK.Platform.Windows.Api.ChoosePixelFormat(_device_context, pixel_format); - - if (pixel == 0) - { - // "The pixel format requested is not supported by the hardware configuration." - throw new Exception("Pixel format not supported."); - } - - OpenTK.Platform.Windows.Api.SetPixelFormat(_device_context, pixel, pixel_format); - - _render_context = Wgl.CreateContext(_device_context); - - MakeCurrent(); - - //GL.Init(); - //new GL(); - - //if (load_extensions) - // LoadExtensions(); - } - } -} \ No newline at end of file diff --git a/Source/OpenGL/OpenGL/Contexts/X11Context.cs b/Source/OpenGL/OpenGL/Contexts/X11Context.cs index 0937774b..ea98c738 100644 --- a/Source/OpenGL/OpenGL/Contexts/X11Context.cs +++ b/Source/OpenGL/OpenGL/Contexts/X11Context.cs @@ -22,7 +22,7 @@ namespace OpenTK.OpenGL.Platform private IntPtr display; const string _dll_name = "libGL.so.1"; - public X11Context(Control c, int red, int green, int blue, int alpha, int depth, int stencil) + public X11Context(Control c, ColorDepth color, int depth, int stencil) { Type xplatui = Type.GetType("System.Windows.Forms.XplatUIX11, System.Windows.Forms"); if (xplatui != null) diff --git a/Source/OpenGL/OpenGL/OpenTK.OpenGL.csproj b/Source/OpenGL/OpenGL/OpenTK.OpenGL.csproj index 0bd517dd..ba81abd7 100644 --- a/Source/OpenGL/OpenGL/OpenTK.OpenGL.csproj +++ b/Source/OpenGL/OpenGL/OpenTK.OpenGL.csproj @@ -54,16 +54,13 @@ - - - Code - + - + diff --git a/Source/OpenGL/OpenGL/Structures.cs b/Source/OpenGL/OpenGL/Structures.cs new file mode 100644 index 00000000..352e623c --- /dev/null +++ b/Source/OpenGL/OpenGL/Structures.cs @@ -0,0 +1,27 @@ +#region License +/* Copyright (c) 2006 Stephen Apostolopoulos + * See license.txt for license info + */ +#endregion + +using System; + +namespace OpenTK.OpenGL +{ + #region ColorDepth struct + + public struct ColorDepth + { + public ColorDepth(byte red, byte green, byte blue, byte alpha) + { + Red = red; + Green = green; + Blue = blue; + Alpha = alpha; + } + + public byte Red, Green, Blue, Alpha; + } + + #endregion +} \ No newline at end of file