diff --git a/OpenTkControl/OpenTkControl.csproj b/OpenTkControl/OpenTkControl.csproj index c0a298a..77ca10c 100644 --- a/OpenTkControl/OpenTkControl.csproj +++ b/OpenTkControl/OpenTkControl.csproj @@ -33,8 +33,8 @@ false - - ..\packages\OpenTK.2.0.0\lib\net20\OpenTK.dll + + ..\packages\OpenTK.3.2\lib\net20\OpenTK.dll diff --git a/OpenTkControl/OpenTkControlBase.xaml.cs b/OpenTkControl/OpenTkControlBase.xaml.cs index 357d3f1..77b0c16 100644 --- a/OpenTkControl/OpenTkControlBase.xaml.cs +++ b/OpenTkControl/OpenTkControlBase.xaml.cs @@ -159,6 +159,11 @@ public GlRenderEventArgs(int width, int height, bool resized, bool screenshot, b /// public event EventHandler GlRender; + /// + /// Called whenever the OpenGL context is being destroyed + /// + public event EventHandler GlCleanup; + /// /// Called whenever an exception occurs during initialization, rendering or deinitialization /// @@ -289,6 +294,21 @@ protected OpenTkControlBase() _alreadyLoaded = false; OnUnloaded(sender, args); }; + + var currentApp = Application.Current; + if(currentApp != null) + { + // This handles the case of the application exiting when all the windows are closed, which + // wont raise the Unload event + currentApp.Dispatcher.ShutdownStarted += (sender, args) => + { + if (!_alreadyLoaded) + return; + + _alreadyLoaded = false; + OnUnloaded(sender, new RoutedEventArgs()); + }; + } } /// @@ -396,6 +416,11 @@ protected void DeInitOpenGl() { try { + // With this event we give the user the chance to release all the unmanaged resources that requires + // the opengl context being active to be released + GlRenderEventArgs args = new GlRenderEventArgs(_bitmapWidth, _bitmapHeight, false, false, CheckNewContext()); + OnGlCleanup(args); + DeInitOpenGlBuffers(); _context.Dispose(); @@ -699,5 +724,18 @@ private void OnGlRender(GlRenderEventArgs args) if (error != ErrorCode.NoError) throw new GraphicsException(error.ToString()); } + + /// + /// A helper to actually invoke + /// + /// The render arguments + private void OnGlCleanup(GlRenderEventArgs args) + { + GlCleanup?.Invoke(this, args); + + ErrorCode error = GL.GetError(); + if (error != ErrorCode.NoError) + throw new GraphicsException(error.ToString()); + } } } diff --git a/OpenTkControl/Properties/AssemblyInfo.cs b/OpenTkControl/Properties/AssemblyInfo.cs index 2e1970f..a02dd45 100644 --- a/OpenTkControl/Properties/AssemblyInfo.cs +++ b/OpenTkControl/Properties/AssemblyInfo.cs @@ -31,5 +31,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.1")] -[assembly: AssemblyFileVersion("1.0.1")] +[assembly: AssemblyVersion("1.0.2")] +[assembly: AssemblyFileVersion("1.0.2")] diff --git a/OpenTkControl/packages.config b/OpenTkControl/packages.config index c617afc..f536f36 100644 --- a/OpenTkControl/packages.config +++ b/OpenTkControl/packages.config @@ -1,4 +1,4 @@  - + \ No newline at end of file