From e6950a6128e4a93deafc794beb5e190a9257667f Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Mon, 6 Aug 2007 11:22:18 +0000 Subject: [PATCH] Added Create and Destroy events to NativeGLWindow. --- Source/Examples/Tests/S02_RawInput_Logger.cs | 8 +- Source/Examples/Tutorial/T03_RotatingCube.cs | 31 ++- .../Tutorial/T07_DisplayLists_Cube.cs | 32 +-- Source/Examples/Tutorial/T08_VBO.cs | 116 ++++---- Source/Examples/Tutorial/T10_GLSL_Cube.cs | 83 ++++-- Source/OpenTK/GameWindow.cs | 173 ++++++++---- Source/OpenTK/InputDriver.cs | 2 +- Source/OpenTK/OpenGL/GLHelper.cs | 61 ++-- Source/OpenTK/Platform/IGLControl.cs | 3 - Source/OpenTK/Platform/IGameWindow.cs | 11 +- Source/OpenTK/Platform/INativeGLWindow.cs | 10 + Source/OpenTK/Platform/IResizable.cs | 5 + .../OpenTK/Platform/Windows/WinGLControl.cs | 2 - Source/OpenTK/Platform/Windows/WinGLNative.cs | 260 ++++++++---------- Source/OpenTK/Platform/Windows/WinRawInput.cs | 13 +- Source/OpenTK/Platform/Windows/WindowInfo.cs | 2 +- Source/OpenTK/Platform/X11/X11GLNative.cs | 58 ++-- 17 files changed, 504 insertions(+), 366 deletions(-) diff --git a/Source/Examples/Tests/S02_RawInput_Logger.cs b/Source/Examples/Tests/S02_RawInput_Logger.cs index 900cb74d..5d765ec6 100644 --- a/Source/Examples/Tests/S02_RawInput_Logger.cs +++ b/Source/Examples/Tests/S02_RawInput_Logger.cs @@ -60,9 +60,9 @@ namespace Examples.Tests GL.ClearColor(0.0f, 0.0f, 0.0f, 0.0f); } - public override void OnRenderFrame() + public override void OnRenderFrame(EventArgs e) { - base.OnRenderFrame(); + base.OnRenderFrame(e); GL.Clear(GL.Enums.ClearBufferMask.COLOR_BUFFER_BIT); Context.SwapBuffers(); @@ -73,8 +73,8 @@ namespace Examples.Tests while (!Quit) { ProcessEvents(); - OnUpdateFrame(); - OnRenderFrame(); + OnUpdateFrame(EventArgs.Empty); + OnRenderFrame(EventArgs.Empty); Thread.Sleep(10); } } diff --git a/Source/Examples/Tutorial/T03_RotatingCube.cs b/Source/Examples/Tutorial/T03_RotatingCube.cs index c347f909..81f84aeb 100644 --- a/Source/Examples/Tutorial/T03_RotatingCube.cs +++ b/Source/Examples/Tutorial/T03_RotatingCube.cs @@ -35,18 +35,23 @@ namespace Examples.Tutorial public T03_RotatingCube() { CreateWindow(new DisplayMode(800, 600)); - - Context.MakeCurrent(); - - GL.ClearColor(0.1f, 0.1f, 0.5f, 0.0f); - GL.Enable(Enums.EnableCap.DEPTH_TEST); - - this.OnResize(new ResizeEventArgs(this.Width, this.Height)); } #endregion - - #region Resize event handler + + #region OnLoad + + public override void OnLoad(EventArgs e) + { + base.OnLoad(e); + + GL.ClearColor(0.1f, 0.1f, 0.5f, 0.0f); + GL.Enable(Enums.EnableCap.DEPTH_TEST); + } + + #endregion + + #region OnResize /// /// Called when the user resizes the window. @@ -70,7 +75,7 @@ namespace Examples.Tutorial #endregion - #region OnUpdateFrame function + #region OnUpdateFrame /// /// Prepares the next frame for rendering. @@ -79,7 +84,7 @@ namespace Examples.Tutorial /// Place your control logic here. This is the place to respond to user input, /// update object positions etc. /// - public override void OnUpdateFrame() + public override void OnUpdateFrame(EventArgs e) { if (Keyboard[0][OpenTK.Input.Key.Escape]) { @@ -107,12 +112,12 @@ namespace Examples.Tutorial #endregion - #region OnRenderFrame function + #region OnRenderFrame /// /// Place your rendering code here. /// - public override void OnRenderFrame() + public override void OnRenderFrame(EventArgs e) { GL.Clear(Enums.ClearBufferMask.COLOR_BUFFER_BIT | Enums.ClearBufferMask.DEPTH_BUFFER_BIT); diff --git a/Source/Examples/Tutorial/T07_DisplayLists_Cube.cs b/Source/Examples/Tutorial/T07_DisplayLists_Cube.cs index d0f46264..5c1e50ef 100644 --- a/Source/Examples/Tutorial/T07_DisplayLists_Cube.cs +++ b/Source/Examples/Tutorial/T07_DisplayLists_Cube.cs @@ -120,11 +120,25 @@ namespace Examples.Tutorial #endregion + #region OnUpdateFrame + + public override void OnUpdateFrame(EventArgs e) + { + base.OnUpdateFrame(e); + + if (Keyboard[0][OpenTK.Input.Key.Escape]) + { + this.Exit(); + } + } + + #endregion + #region OnRenderFrame - public override void OnRenderFrame() + public override void OnRenderFrame(EventArgs e) { - base.OnRenderFrame(); + base.OnRenderFrame(e); GL.MatrixMode(Enums.MatrixMode.MODELVIEW); GL.LoadIdentity(); @@ -148,20 +162,6 @@ namespace Examples.Tutorial #endregion - #region OnUpdateFrame - - public override void OnUpdateFrame() - { - base.OnUpdateFrame(); - - if (Keyboard[0][OpenTK.Input.Key.Escape]) - { - this.Exit(); - } - } - - #endregion - #endregion --- Event Handlers --- } } \ No newline at end of file diff --git a/Source/Examples/Tutorial/T08_VBO.cs b/Source/Examples/Tutorial/T08_VBO.cs index 57071407..721548a9 100644 --- a/Source/Examples/Tutorial/T08_VBO.cs +++ b/Source/Examples/Tutorial/T08_VBO.cs @@ -73,8 +73,22 @@ namespace Examples.Tutorial public T08_VBO() { this.CreateWindow(new DisplayMode(800, 600)); + } - this.Context.MakeCurrent(); + #endregion + + #region OnLoad + + public override void OnLoad(EventArgs e) + { + base.OnLoad(e); + + if (!GL.IsExtensionSupported("VERSION_1_4")) + { + System.Windows.Forms.MessageBox.Show("You need at least OpenGL 1.4 to run this example. Aborting.", "VBOs not supported", + System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation); + this.Exit(); + } GL.ClearColor(0.1f, 0.1f, 0.5f, 0.0f); GL.Enable(GL.Enums.EnableCap.DEPTH_TEST); @@ -88,11 +102,59 @@ namespace Examples.Tutorial #endregion + #region OnResize + + protected override void OnResize(OpenTK.Platform.ResizeEventArgs e) + { + base.OnResize(e); + + GL.Viewport(0, 0, e.Width, e.Height); + + double ratio = e.Width / (double)e.Height; + + GL.MatrixMode(GL.Enums.MatrixMode.PROJECTION); + GL.LoadIdentity(); + Glu.Perspective(45.0, ratio, 1.0, 64.0); + } + + #endregion + + #region OnUpdateFrame + + /// + /// Prepares the next frame for rendering. + /// + /// + /// Place your control logic here. This is the place to respond to user input, + /// update object positions etc. + /// + public override void OnUpdateFrame(EventArgs e) + { + if (Keyboard[0][OpenTK.Input.Key.Escape]) + { + this.Exit(); + return; + } + + GL.MatrixMode(GL.Enums.MatrixMode.MODELVIEW); + GL.LoadIdentity(); + Glu.LookAt( + 0.0, 5.0, 5.0, + 0.0, 0.0, 0.0, + 0.0, 1.0, 0.0 + ); + + //GL.Rotatef(angle, 0.0f, 1.0f, 0.0f); + //angle += 0.5f; + } + + #endregion + #region OnRenderFrame - public override void OnRenderFrame() + public override void OnRenderFrame(EventArgs e) { - base.OnRenderFrame(); + base.OnRenderFrame(e); GL.Clear( GL.Enums.ClearBufferMask.COLOR_BUFFER_BIT | @@ -119,54 +181,6 @@ namespace Examples.Tutorial #endregion - #region OnUpdateFrame - - /// - /// Prepares the next frame for rendering. - /// - /// - /// Place your control logic here. This is the place to respond to user input, - /// update object positions etc. - /// - public override void OnUpdateFrame() - { - if (Keyboard[0][OpenTK.Input.Key.Escape]) - { - this.Exit(); - return; - } - - GL.MatrixMode(GL.Enums.MatrixMode.MODELVIEW); - GL.LoadIdentity(); - Glu.LookAt( - 0.0, 5.0, 5.0, - 0.0, 0.0, 0.0, - 0.0, 1.0, 0.0 - ); - - //GL.Rotatef(angle, 0.0f, 1.0f, 0.0f); - //angle += 0.5f; - } - - #endregion - - #region Resize event - - protected override void OnResize(OpenTK.Platform.ResizeEventArgs e) - { - base.OnResize(e); - - GL.Viewport(0, 0, e.Width, e.Height); - - double ratio = e.Width / (double)e.Height; - - GL.MatrixMode(GL.Enums.MatrixMode.PROJECTION); - GL.LoadIdentity(); - Glu.Perspective(45.0, ratio, 1.0, 64.0); - } - - #endregion - #region LoadCube private void LoadCube() diff --git a/Source/Examples/Tutorial/T10_GLSL_Cube.cs b/Source/Examples/Tutorial/T10_GLSL_Cube.cs index 437cce99..0c68b77b 100644 --- a/Source/Examples/Tutorial/T10_GLSL_Cube.cs +++ b/Source/Examples/Tutorial/T10_GLSL_Cube.cs @@ -51,12 +51,62 @@ namespace Examples.Tutorial public T10_GLSL_Cube() { this.CreateWindow(new OpenTK.Platform.DisplayMode(800, 600)); - Context.MakeCurrent(); + } + + #endregion + + #region public void Launch() + + /// + /// Launches this example. + /// + /// + /// Provides a simple way for the example launcher to launch the examples. + /// + public void Launch() + { + Run(); + } + + #endregion + + #region OnCreate + + /// + /// This is the place to change window parameters. + /// + /// Not used. + public override void OnCreate(EventArgs e) + { + base.OnCreate(e); //Text = // GL.GetString(Enums.StringName.VENDOR) + " " + // GL.GetString(Enums.StringName.RENDERER) + " " + // GL.GetString(Enums.StringName.VERSION); + } + + #endregion + + #region OnLoad + + /// + /// This is the place to load resources that change little + /// during the lifetime of the GameWindow. In this case, we + /// check for GLSL support, and load the shaders. + /// + /// Not used. + public override void OnLoad(EventArgs e) + { + base.OnLoad(e); + + // Check for necessary capabilities: + if (!GL.IsExtensionSupported("VERSION_2_0")) + { + MessageBox.Show("You need at least OpenGL 2.0 to run this example. Aborting.", "GLSL not supported", + MessageBoxButtons.OK, MessageBoxIcon.Exclamation); + this.Exit(); + } GL.ClearColor(0.1f, 0.1f, 0.5f, 0.0f); GL.Enable(GL.Enums.EnableCap.DEPTH_TEST); @@ -106,25 +156,6 @@ namespace Examples.Tutorial #endregion - #region public void Launch() - - /// - /// Launches this example. - /// - /// - /// Provides a simple way for the example launcher to launch the examples. - /// - public void Launch() - { - //using (T10_GLSL_Cube ex = new T10_GLSL_Cube()) - { - //ex.Run(); - Run(); - } - } - - #endregion - #region OnResize protected override void OnResize(OpenTK.Platform.ResizeEventArgs e) @@ -145,9 +176,13 @@ namespace Examples.Tutorial #region OnUpdateFrame - public override void OnUpdateFrame() + /// + /// Occurs when it is time to update the next frame. + /// + /// Not used yet. + public override void OnUpdateFrame(EventArgs e) { - base.OnUpdateFrame(); + base.OnUpdateFrame(e); if (Keyboard[0][OpenTK.Input.Key.Escape]) { @@ -169,9 +204,9 @@ namespace Examples.Tutorial #region OnRenderFrame - public override void OnRenderFrame() + public override void OnRenderFrame(EventArgs e) { - base.OnRenderFrame(); + base.OnRenderFrame(e); GL.Clear(GL.Enums.ClearBufferMask.COLOR_BUFFER_BIT | GL.Enums.ClearBufferMask.DEPTH_BUFFER_BIT); diff --git a/Source/OpenTK/GameWindow.cs b/Source/OpenTK/GameWindow.cs index 5465446d..8160185d 100644 --- a/Source/OpenTK/GameWindow.cs +++ b/Source/OpenTK/GameWindow.cs @@ -55,6 +55,12 @@ namespace OpenTK glWindow.Resize += new ResizeEvent(glWindow_Resize); glWindow.Create += new CreateEvent(glWindow_CreateInputDriver); + glWindow.Destroy += new DestroyEvent(glWindow_Destroy); + } + + void glWindow_Destroy(object sender, EventArgs e) + { + this.isExiting = true; } void glWindow_CreateInputDriver(object sender, EventArgs e) @@ -98,27 +104,6 @@ namespace OpenTK #region --- INativeGLWindow Members --- - #region public void CreateWindow(DisplayMode mode) - - /// - /// Creates a new render window. - /// - /// The DisplayMode of the render window. - /// Occurs when a render window already exists. - public void CreateWindow(DisplayMode mode) - { - if (!Exists) - { - glWindow.CreateWindow(mode); - } - else - { - throw new ApplicationException("A render window already exists"); - } - } - - #endregion - #region public void Exit() /// @@ -219,6 +204,71 @@ namespace OpenTK #endregion + #region public void CreateWindow(DisplayMode mode) + + /// + /// Creates a new render window. + /// + /// The DisplayMode of the render window. + /// Occurs when a render window already exists. + public void CreateWindow(DisplayMode mode) + { + if (!Exists) + { + glWindow.CreateWindow(mode); + } + else + { + throw new ApplicationException("A render window already exists"); + } + } + + #endregion + + #region OnCreate + + public event CreateEvent Create; + + public virtual void OnCreate(EventArgs e) + { + if (this.Create != null) + { + this.Create(this, e); + } + } + + #endregion + + #region public void DestroyWindow() + + public void DestroyWindow() + { + if (Exists) + { + glWindow.DestroyWindow(); + } + else + { + throw new ApplicationException("Tried to destroy inexistent window."); + } + } + + #endregion + + #region OnDestroy + + public virtual void OnDestroy(EventArgs e) + { + if (this.Destroy != null) + { + this.Destroy(this, e); + } + } + + public event DestroyEvent Destroy; + + #endregion + #endregion #region --- IGameWindow Members --- @@ -242,17 +292,30 @@ namespace OpenTK /// public virtual void Run() { + this.OnLoad(EventArgs.Empty); + resizeEventArgs.Width = this.Width; + resizeEventArgs.Height = this.Height; + this.OnResize(resizeEventArgs); + + Debug.Print("Entering main loop"); + while (!this.Quit && !IsExiting) { this.ProcessEvents(); - this.OnUpdateFrame(); - this.OnRenderFrame(); + if (!IsExiting) + { + this.OnUpdateFrame(EventArgs.Empty); + this.OnRenderFrame(EventArgs.Empty); + } } - glWindow.Exit(); - while (glWindow.Exists) + if (glWindow.Exists) { - this.ProcessEvents(); + glWindow.Exit(); + while (glWindow.Exists) + { + this.ProcessEvents(); + } } } @@ -280,21 +343,7 @@ namespace OpenTK #endregion - #region public event CreateEvent Create; - - public event CreateEvent Create; - - private void OnCreate(EventArgs e) - { - if (this.Create != null) - { - this.Create(this, e); - } - } - - #endregion - - #region public virtual void OnRenderFrame() + #region public virtual void OnRenderFrame(EventArgs e) /// /// Raises the RenderFrame event. Override in derived classes to render a frame. @@ -303,7 +352,7 @@ namespace OpenTK /// If overriden, the base.OnRenderFrame() function should be called, to ensure /// listeners are notified of RenderFrame events. /// - public virtual void OnRenderFrame() + public virtual void OnRenderFrame(EventArgs e) { if (!this.Exists) { @@ -312,12 +361,17 @@ namespace OpenTK this.CreateWindow(mode); } if (RenderFrame != null) - RenderFrame(EventArgs.Empty); + RenderFrame(this, e); } + /// + /// Occurs when it is time to render the next frame. + /// + public event RenderFrameEvent RenderFrame; + #endregion - #region public virtual void OnUpdateFrame() + #region public virtual void OnUpdateFrame(EventArgs e) /// /// Raises the UpdateFrame event. Override in derived classes to update a frame. @@ -326,7 +380,7 @@ namespace OpenTK /// If overriden, the base.OnUpdateFrame() function should be called, to ensure /// listeners are notified of UpdateFrame events. /// - public virtual void OnUpdateFrame() + public virtual void OnUpdateFrame(EventArgs e) { if (!this.Exists) { @@ -335,20 +389,38 @@ namespace OpenTK this.CreateWindow(mode); } if (UpdateFrame != null) - UpdateFrame(EventArgs.Empty); + UpdateFrame(this, e); } - #endregion - /// /// Occurs when it is time to update the next frame. /// public event UpdateFrameEvent UpdateFrame; + #endregion + + #region public virtual void OnLoad(EventArgs e) + /// - /// Occurs when it is time to render the next frame. + /// Raises the Load event. Override to load resources that should + /// be maintained for the lifetime of the application. /// - public event RenderFrameEvent RenderFrame; + public virtual void OnLoad(EventArgs e) + { + Debug.Print("Firing GameWindow.Load event."); + if (this.Load != null) + { + this.Load(this, e); + } + } + + /// + /// Occurs after the GameWindow has been created, but before + /// entering the main loop. + /// + public event LoadEvent Load; + + #endregion #region public bool IsExiting @@ -446,6 +518,7 @@ namespace OpenTK /// Contains the new Width and Height of the window. protected virtual void OnResize(ResizeEventArgs e) { + Debug.Print("Firing GameWindow.Resize event: {0}.", e.ToString()); if (this.Resize != null) this.Resize(this, e); } diff --git a/Source/OpenTK/InputDriver.cs b/Source/OpenTK/InputDriver.cs index f2d51aa9..296eb62b 100644 --- a/Source/OpenTK/InputDriver.cs +++ b/Source/OpenTK/InputDriver.cs @@ -16,7 +16,7 @@ namespace OpenTK if (Environment.OSVersion.Version.Major > 5 || (Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor >= 1)) { - inputDriver = new OpenTK.Platform.Windows.WinRawInput(parent.Handle); + inputDriver = new OpenTK.Platform.Windows.WinRawInput(parent as OpenTK.Platform.Windows.WindowInfo); } else if (Environment.OSVersion.Platform == PlatformID.Unix) { diff --git a/Source/OpenTK/OpenGL/GLHelper.cs b/Source/OpenTK/OpenGL/GLHelper.cs index e0983b8e..78b928f7 100644 --- a/Source/OpenTK/OpenGL/GLHelper.cs +++ b/Source/OpenTK/OpenGL/GLHelper.cs @@ -90,7 +90,7 @@ namespace OpenTK.OpenGL #endregion private enum Platform private static Platform platform = Platform.Unknown; - private static System.Collections.Generic.Dictionary AvailableExtensions; + private static System.Collections.Generic.Dictionary AvailableExtensions = new Dictionary(); #region internal static extern IntPtr glxGetProcAddressARB(string s); // also linux, for our ARB-y friends @@ -295,8 +295,12 @@ Did you remember to copy OpenTK.OpenGL.dll.config to your binary's folder? // Use cached results if (useCache) { - // Build cache if it is not available - if (AvailableExtensions == null) + // Build cache if it is not available. We assume that all drivers + // will support at least one extension to opengl 1.0 (for example + // opengl 1.1). This should hold true even for software contexts + // (microsoft's soft implementation is gl 1.1 compatible), at least + // for any system capable of running .Net/Mono. + if (AvailableExtensions.Count == 0) { ParseAvailableExtensions(); } @@ -305,8 +309,8 @@ Did you remember to copy OpenTK.OpenGL.dll.config to your binary's folder? // strings "1.0" to "2.1" with "GL_VERSION_1_0" to "GL_VERSION_2_1" if (AvailableExtensions.ContainsKey(name)) return AvailableExtensions[name]; - else - return false; + + return false; } // Do not use cached results @@ -340,8 +344,6 @@ Did you remember to copy OpenTK.OpenGL.dll.config to your binary's folder? { // Assumes there is a current context. - AvailableExtensions = new Dictionary(); - string version_string = GL.GetString(OpenTK.OpenGL.GL.Enums.StringName.VERSION); if (String.IsNullOrEmpty(version_string)) return; // this shoudn't happen @@ -350,42 +352,42 @@ Did you remember to copy OpenTK.OpenGL.dll.config to your binary's folder? if (version.StartsWith("1.2")) { - AvailableExtensions.Add("GL_VERSION_1_2", true); + AvailableExtensions.Add("VERSION_1_2", true); } else if (version.StartsWith("1.3")) { - AvailableExtensions.Add("GL_VERSION_1_2", true); - AvailableExtensions.Add("GL_VERSION_1_3", true); + AvailableExtensions.Add("VERSION_1_2", true); + AvailableExtensions.Add("VERSION_1_3", true); } else if (version.StartsWith("1.4")) { - AvailableExtensions.Add("GL_VERSION_1_2", true); - AvailableExtensions.Add("GL_VERSION_1_3", true); - AvailableExtensions.Add("GL_VERSION_1_4", true); + AvailableExtensions.Add("VERSION_1_2", true); + AvailableExtensions.Add("VERSION_1_3", true); + AvailableExtensions.Add("VERSION_1_4", true); } else if (version.StartsWith("1.5")) { - AvailableExtensions.Add("GL_VERSION_1_2", true); - AvailableExtensions.Add("GL_VERSION_1_3", true); - AvailableExtensions.Add("GL_VERSION_1_4", true); - AvailableExtensions.Add("GL_VERSION_1_5", true); + AvailableExtensions.Add("VERSION_1_2", true); + AvailableExtensions.Add("VERSION_1_3", true); + AvailableExtensions.Add("VERSION_1_4", true); + AvailableExtensions.Add("VERSION_1_5", true); } else if (version.StartsWith("2.0")) { - AvailableExtensions.Add("GL_VERSION_1_2", true); - AvailableExtensions.Add("GL_VERSION_1_3", true); - AvailableExtensions.Add("GL_VERSION_1_4", true); - AvailableExtensions.Add("GL_VERSION_1_5", true); - AvailableExtensions.Add("GL_VERSION_2_0", true); + AvailableExtensions.Add("VERSION_1_2", true); + AvailableExtensions.Add("VERSION_1_3", true); + AvailableExtensions.Add("VERSION_1_4", true); + AvailableExtensions.Add("VERSION_1_5", true); + AvailableExtensions.Add("VERSION_2_0", true); } else if (version.StartsWith("2.1")) { - AvailableExtensions.Add("GL_VERSION_1_2", true); - AvailableExtensions.Add("GL_VERSION_1_3", true); - AvailableExtensions.Add("GL_VERSION_1_4", true); - AvailableExtensions.Add("GL_VERSION_1_5", true); - AvailableExtensions.Add("GL_VERSION_2_0", true); - AvailableExtensions.Add("GL_VERSION_2_1", true); + AvailableExtensions.Add("VERSION_1_2", true); + AvailableExtensions.Add("VERSION_1_3", true); + AvailableExtensions.Add("VERSION_1_4", true); + AvailableExtensions.Add("VERSION_1_5", true); + AvailableExtensions.Add("VERSION_2_0", true); + AvailableExtensions.Add("VERSION_2_1", true); } string extension_string = GL.GetString(OpenTK.OpenGL.GL.Enums.StringName.EXTENSIONS); @@ -428,7 +430,8 @@ Did you remember to copy OpenTK.OpenGL.dll.config to your binary's folder? f.SetValue(null, GetDelegateForMethod(f.Name, f.FieldType)); } - ParseAvailableExtensions(); + //ParseAvailableExtensions(); + AvailableExtensions.Clear(); } #endregion diff --git a/Source/OpenTK/Platform/IGLControl.cs b/Source/OpenTK/Platform/IGLControl.cs index b62eb117..3d709c61 100644 --- a/Source/OpenTK/Platform/IGLControl.cs +++ b/Source/OpenTK/Platform/IGLControl.cs @@ -14,12 +14,9 @@ namespace OpenTK.Platform { public interface IGLControl : IDisposable { - event CreateEvent Create; bool IsIdle { get; } bool Fullscreen { get; set; } IGLContext Context { get; } } - - public delegate void CreateEvent(object sender, EventArgs e); } diff --git a/Source/OpenTK/Platform/IGameWindow.cs b/Source/OpenTK/Platform/IGameWindow.cs index fbf4d917..1400b6a3 100644 --- a/Source/OpenTK/Platform/IGameWindow.cs +++ b/Source/OpenTK/Platform/IGameWindow.cs @@ -9,16 +9,19 @@ namespace OpenTK.Platform { void Run(); - void OnRenderFrame(); - void OnUpdateFrame(); + void OnRenderFrame(EventArgs e); + void OnUpdateFrame(EventArgs e); + void OnLoad(EventArgs e); event UpdateFrameEvent UpdateFrame; event RenderFrameEvent RenderFrame; + event LoadEvent Load; bool IsExiting { get; } IList Keyboard { get; } } - public delegate void UpdateFrameEvent(EventArgs e); - public delegate void RenderFrameEvent(EventArgs e); + public delegate void UpdateFrameEvent(object sender, EventArgs e); + public delegate void RenderFrameEvent(object sender, EventArgs e); + public delegate void LoadEvent(object sender, EventArgs e); } diff --git a/Source/OpenTK/Platform/INativeGLWindow.cs b/Source/OpenTK/Platform/INativeGLWindow.cs index febbb819..f2bcdedb 100644 --- a/Source/OpenTK/Platform/INativeGLWindow.cs +++ b/Source/OpenTK/Platform/INativeGLWindow.cs @@ -10,11 +10,21 @@ namespace OpenTK.Platform interface INativeGLWindow : IGLControl, IResizable { void CreateWindow(DisplayMode mode); + void DestroyWindow(); void ProcessEvents(); void Exit(); + void OnCreate(EventArgs e); + void OnDestroy(EventArgs e); + bool Exists { get; } bool Quit { get; } IWindowInfo WindowInfo { get; } + + event CreateEvent Create; + event DestroyEvent Destroy; } + + public delegate void CreateEvent(object sender, EventArgs e); + public delegate void DestroyEvent(object sender, EventArgs e); } diff --git a/Source/OpenTK/Platform/IResizable.cs b/Source/OpenTK/Platform/IResizable.cs index 94e24fe8..017a6af8 100644 --- a/Source/OpenTK/Platform/IResizable.cs +++ b/Source/OpenTK/Platform/IResizable.cs @@ -27,5 +27,10 @@ namespace OpenTK.Platform this.Width = width; this.Height = height; } + + public override string ToString() + { + return String.Format("New size: {0}x{1}", Width, Height); + } } } diff --git a/Source/OpenTK/Platform/Windows/WinGLControl.cs b/Source/OpenTK/Platform/Windows/WinGLControl.cs index 4a44162c..c19a0ea1 100644 --- a/Source/OpenTK/Platform/Windows/WinGLControl.cs +++ b/Source/OpenTK/Platform/Windows/WinGLControl.cs @@ -47,8 +47,6 @@ namespace OpenTK.Platform.Windows #region --- IGLControl membmers --- - public event CreateEvent Create; - #region public bool IsIdle public bool IsIdle diff --git a/Source/OpenTK/Platform/Windows/WinGLNative.cs b/Source/OpenTK/Platform/Windows/WinGLNative.cs index 7b3a6ad4..b59f5e4a 100644 --- a/Source/OpenTK/Platform/Windows/WinGLNative.cs +++ b/Source/OpenTK/Platform/Windows/WinGLNative.cs @@ -21,7 +21,7 @@ namespace OpenTK.Platform.Windows /// Drives GameWindow on Windows. /// This class supports OpenTK, and is not intended for use by OpenTK programs. /// - sealed class WinGLNative : NativeWindow, OpenTK.Platform.INativeGLWindow, IDisposable + sealed class WinGLNative : NativeWindow, INativeGLWindow, IDisposable { #region --- Fields --- @@ -32,7 +32,12 @@ namespace OpenTK.Platform.Windows private bool disposed; private bool quit; private bool exists; - private WindowInfo info; + private WindowInfo window; + + /// + /// For use in WndProc only. + /// + private int width, height; #endregion @@ -51,11 +56,6 @@ namespace OpenTK.Platform.Windows #region protected override void WndProc(ref Message m) - /// - /// For use in WndProc only. - /// - private int width, height; - /// /// Processes incoming WM_* messages. /// @@ -86,12 +86,6 @@ namespace OpenTK.Platform.Windows mode.Width = Marshal.ReadInt32(m.LParam, (int)Marshal.OffsetOf(typeof(API.CreateStruct), "cx")); mode.Height = Marshal.ReadInt32(m.LParam, (int)Marshal.OffsetOf(typeof(API.CreateStruct), "cy")); - info = new WindowInfo(); - info.Handle = this.Handle; - info.Parent = null; - - Debug.Print("Window handle: {0}", this.Handle); - // Raise the Create event this.OnCreate(EventArgs.Empty); return; @@ -101,14 +95,8 @@ namespace OpenTK.Platform.Windows return; case API.Constants.WM_DESTROY: - if (this.Handle != IntPtr.Zero) - { - Debug.Print("Window handle {0} destroyed.", this.Handle); - this.DestroyHandle(); - exists = false; - } - API.PostQuitMessage(0); - return; + this.OnDestroy(EventArgs.Empty); + break; case API.Constants.WM_QUIT: quit = true; @@ -116,7 +104,6 @@ namespace OpenTK.Platform.Windows return; } - //base.WndProc(ref m); DefWndProc(ref m); } @@ -124,106 +111,6 @@ namespace OpenTK.Platform.Windows #region --- INativeGLWindow Members --- - #region private void CreateWindow(DisplayMode mode) - - public void CreateWindow(DisplayMode mode) - { - Debug.Print("Creating native window with mode: {0}", mode.ToString()); - Debug.Indent(); - - CreateParams cp = new CreateParams(); - cp.ClassStyle = - (int)API.WindowClassStyle.OwnDC | - (int)API.WindowClassStyle.VRedraw | - (int)API.WindowClassStyle.HRedraw | (int)API.WindowClassStyle.Ime; - cp.Style = - (int)API.WindowStyle.Visible | - (int)API.WindowStyle.ClipChildren | - (int)API.WindowStyle.ClipSiblings | - (int)API.WindowStyle.OverlappedWindow; - cp.Width = mode.Width; - cp.Height = mode.Height; - cp.Caption = "OpenTK Game Window"; - - // Keep in mind that some construction code runs in WM_CREATE, - // which is raised CreateHandle() - CreateHandle(cp); - - glContext = new WinGLContext( - this.Handle, - new DisplayMode( - width, height, - new ColorDepth(32), - 16, 0, 0, 2, - fullscreen, - false, - false, - 0.0f - ) - ); - - if (this.Handle != IntPtr.Zero && glContext != null) - { - Debug.WriteLine("Window creation was succesful."); - exists = true; - } - else - { - throw new ApplicationException(String.Format( - "Could not create native window and/or context. Handle: {0}, Context {1}", - this.Handle, this.Context.ToString())); - } - - Debug.Unindent(); - } - - /* - private void CreateWindow() - { - WinApi.WindowClass wc = new WinApi.WindowClass(); - wc.style = - WinApi.WindowClassStyle.HRedraw | - WinApi.WindowClassStyle.VRedraw | - WinApi.WindowClassStyle.OwnDC; - wc.WindowProcedure = new WinApi.WindowProcedureEventHandler(WndProc); - wc.Instance = instance; - //wc.ClassName = Marshal.StringToHGlobalAuto(className); - wc.ClassName = className; - - classAtom = WinApi.RegisterClass(wc); - - if (classAtom == 0) - { - throw new Exception("Could not register class, error: " + Marshal.GetLastWin32Error()); - } - - // Change for fullscreen! - handle = WinApi.CreateWindowEx( - WinApi.ExtendedWindowStyle.ApplicationWindow | - WinApi.ExtendedWindowStyle.OverlappedWindow | - WinApi.ExtendedWindowStyle.Topmost, - className, - //Marshal.StringToHGlobalAuto("OpenTK Game Window"), - "OpenTK Game Window", - WinApi.WindowStyle.OverlappedWindow | - WinApi.WindowStyle.ClipChildren | - WinApi.WindowStyle.ClipSiblings, - 0, 0, - 640, 480, - IntPtr.Zero, - IntPtr.Zero, - instance, - IntPtr.Zero - ); - - if (handle == IntPtr.Zero) - { - throw new Exception("Could not create window, error: " + Marshal.GetLastWin32Error()); - } - } - */ - #endregion - #region public void Exit() /// @@ -231,7 +118,7 @@ namespace OpenTK.Platform.Windows /// public void Exit() { - API.PostMessage(this.Handle, API.Constants.WM_DESTROY, IntPtr.Zero, IntPtr.Zero); + DestroyWindow(); } #endregion @@ -252,21 +139,7 @@ namespace OpenTK.Platform.Windows Marshal.GetLastWin32Error())); } API.DispatchMessage(ref msg); - WndProc(ref msg); - } - } - - #endregion - - #region public event CreateEvent Create; - - public event CreateEvent Create; - - private void OnCreate(EventArgs e) - { - if (this.Create != null) - { - this.Create(this, e); + //WndProc(ref msg); } } @@ -338,11 +211,120 @@ namespace OpenTK.Platform.Windows public IWindowInfo WindowInfo { - get { return info; } + get { return window; } } #endregion + #region private void CreateWindow(DisplayMode mode) + + public void CreateWindow(DisplayMode mode) + { + Debug.Print("Creating native window with mode: {0}", mode.ToString()); + Debug.Indent(); + + CreateParams cp = new CreateParams(); + cp.ClassStyle = + (int)API.WindowClassStyle.OwnDC | + (int)API.WindowClassStyle.VRedraw | + (int)API.WindowClassStyle.HRedraw | (int)API.WindowClassStyle.Ime; + cp.Style = + (int)API.WindowStyle.Visible | + (int)API.WindowStyle.ClipChildren | + (int)API.WindowStyle.ClipSiblings | + (int)API.WindowStyle.OverlappedWindow; + cp.Width = mode.Width; + cp.Height = mode.Height; + cp.Caption = "OpenTK Game Window"; + + // Keep in mind that some construction code runs in WM_CREATE, + // which is raised CreateHandle() + CreateHandle(cp); + + if (this.Handle != IntPtr.Zero && glContext != null) + { + Debug.WriteLine("Window creation was succesful."); + exists = true; + } + else + { + throw new ApplicationException(String.Format( + "Could not create native window and/or context. Handle: {0}, Context {1}", + this.Handle, this.Context.ToString())); + } + + Debug.Unindent(); + } + + #endregion + + #region OnCreate + + public event CreateEvent Create; + + public void OnCreate(EventArgs e) + { + window = new WindowInfo(); + window.Handle = this.Handle; + window.Parent = null; + + Debug.Print("Window created: {0}", window); + + glContext = new WinGLContext( + this.Handle, + new DisplayMode( + width, height, + new ColorDepth(32), + 16, 0, 0, 2, + fullscreen, + false, + false, + 0.0f + ) + ); + + glContext.MakeCurrent(); + + if (this.Create != null) + { + this.Create(this, e); + } + } + + #endregion + + #region private void DestroyWindow() + + public void DestroyWindow() + { + Debug.Print("Destroying window: {0}", window.ToString()); + API.PostMessage(this.Handle, API.Constants.WM_DESTROY, IntPtr.Zero, IntPtr.Zero); + } + + #endregion + + #region OnDestroy + + public void OnDestroy(EventArgs e) + { + if (this.Handle != IntPtr.Zero) + { + Debug.Print("Window handle {0} destroyed.", this.Handle); + //this.DestroyHandle(); // Destroyed automatically by DefWndProc + exists = false; + } + API.PostQuitMessage(0); + + if (this.Destroy != null) + { + this.Destroy(this, e); + } + } + + public event DestroyEvent Destroy; + + #endregion + #endregion #region --- IDisposable Members --- diff --git a/Source/OpenTK/Platform/Windows/WinRawInput.cs b/Source/OpenTK/Platform/Windows/WinRawInput.cs index f80f0750..a48ab2ab 100644 --- a/Source/OpenTK/Platform/Windows/WinRawInput.cs +++ b/Source/OpenTK/Platform/Windows/WinRawInput.cs @@ -29,18 +29,17 @@ namespace OpenTK.Platform.Windows /// private static int deviceCount; - private WinRawKeyboard keyboardDriver; #region --- Constructors --- - internal WinRawInput(IntPtr parentHandle) + internal WinRawInput(WindowInfo parent) { Debug.WriteLine("Initalizing windows raw input driver."); Debug.Indent(); - - AssignHandle(parentHandle); - Debug.Print("Input window attached to parent {0}", this.Handle); + + AssignHandle(parent.Handle); + Debug.Print("Input window attached to parent {0}", parent); keyboardDriver = new WinRawKeyboard(this.Handle); Debug.Unindent(); @@ -48,6 +47,8 @@ namespace OpenTK.Platform.Windows #endregion + #region internal static int DeviceCount + internal static int DeviceCount { get @@ -57,6 +58,8 @@ namespace OpenTK.Platform.Windows } } + #endregion + #region protected override void WndProc(ref Message msg) int size = 0; diff --git a/Source/OpenTK/Platform/Windows/WindowInfo.cs b/Source/OpenTK/Platform/Windows/WindowInfo.cs index 8b93d8e0..11518c14 100644 --- a/Source/OpenTK/Platform/Windows/WindowInfo.cs +++ b/Source/OpenTK/Platform/Windows/WindowInfo.cs @@ -31,7 +31,7 @@ namespace OpenTK.Platform.Windows public override string ToString() { - return String.Format("Windows.WindowInfo: Handle {0}, Parent {1}", + return String.Format("Windows.WindowInfo: Handle {0}, Parent ({1})", this.Handle, this.Parent != null ? this.Parent.ToString() : "null"); } diff --git a/Source/OpenTK/Platform/X11/X11GLNative.cs b/Source/OpenTK/Platform/X11/X11GLNative.cs index 43605f26..89906ed2 100644 --- a/Source/OpenTK/Platform/X11/X11GLNative.cs +++ b/Source/OpenTK/Platform/X11/X11GLNative.cs @@ -144,12 +144,12 @@ namespace OpenTK.Platform.X11 glContext.windowInfo.Handle = window.Handle; glContext.CreateContext(null, true); - + API.MapRaised(window.Display, window.Handle); Debug.WriteLine("Mapped window."); - //glContext.MakeCurrent(); + glContext.MakeCurrent(); Debug.WriteLine("Our shiny new context is now current - ready to rock 'n' roll!"); Debug.Unindent(); @@ -165,10 +165,8 @@ namespace OpenTK.Platform.X11 { Debug.WriteLine("X11GLNative shutdown sequence initiated."); quit = true; + Functions.XUnmapWindow(window.Display, window.Handle); Functions.XDestroyWindow(window.Display, window.Handle); - window = null; - glContext.Dispose(); - glContext = null; } #endregion @@ -203,10 +201,8 @@ namespace OpenTK.Platform.X11 break; case XEventName.DestroyNotify: - //glContext.Dispose(); - //window = null; - //glContext = null; - //quit = true; + glContext.Dispose(); + quit = true; Debug.WriteLine("Window destroyed, shutting down."); break; @@ -239,21 +235,6 @@ namespace OpenTK.Platform.X11 #endregion - #region public event CreateEvent Create; - - public event CreateEvent Create; - - private void OnCreate(EventArgs e) - { - if (this.Create != null) - { - Debug.Print("Create event fired from window: {0}", window.ToString()); - this.Create(this, e); - } - } - - #endregion - #region public bool Exists /// @@ -333,6 +314,33 @@ namespace OpenTK.Platform.X11 #endregion + public void DestroyWindow() + { + throw new Exception("The method or operation is not implemented."); + } + + #region OnCreate + + public event CreateEvent Create; + + public void OnCreate(EventArgs e) + { + if (this.Create != null) + { + Debug.Print("Create event fired from window: {0}", window.ToString()); + this.Create(this, e); + } + } + + #endregion + + public void OnDestroy(EventArgs e) + { + throw new Exception("The method or operation is not implemented."); + } + + public event DestroyEvent Destroy; + #endregion #region --- IResizable Members --- @@ -446,5 +454,7 @@ namespace OpenTK.Platform.X11 } #endregion + + } }