Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgraded OpenTK and added a cleanup #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions OpenTkControl/OpenTkControl.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="OpenTK, Version=2.0.0.0, Culture=neutral, PublicKeyToken=bad199fe84eb3df4, processorArchitecture=MSIL">
<HintPath>..\packages\OpenTK.2.0.0\lib\net20\OpenTK.dll</HintPath>
<Reference Include="OpenTK, Version=3.2.0.0, Culture=neutral, PublicKeyToken=bad199fe84eb3df4, processorArchitecture=MSIL">
<HintPath>..\packages\OpenTK.3.2\lib\net20\OpenTK.dll</HintPath>
</Reference>
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
Expand Down
38 changes: 38 additions & 0 deletions OpenTkControl/OpenTkControlBase.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ public GlRenderEventArgs(int width, int height, bool resized, bool screenshot, b
/// </summary>
public event EventHandler<GlRenderEventArgs> GlRender;

/// <summary>
/// Called whenever the OpenGL context is being destroyed
/// </summary>
public event EventHandler<GlRenderEventArgs> GlCleanup;

/// <summary>
/// Called whenever an exception occurs during initialization, rendering or deinitialization
/// </summary>
Expand Down Expand Up @@ -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());
};
Comment on lines +304 to +310
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This event handler can be moved to an inner function that's shared with the Unloaded event.

}
}

/// <summary>
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -699,5 +724,18 @@ private void OnGlRender(GlRenderEventArgs args)
if (error != ErrorCode.NoError)
throw new GraphicsException(error.ToString());
}

/// <summary>
/// A helper to actually invoke <see cref="GlCleanup"/>
/// </summary>
/// <param name="args">The render arguments</param>
private void OnGlCleanup(GlRenderEventArgs args)
{
GlCleanup?.Invoke(this, args);

ErrorCode error = GL.GetError();
if (error != ErrorCode.NoError)
throw new GraphicsException(error.ToString());
}
}
}
4 changes: 2 additions & 2 deletions OpenTkControl/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
2 changes: 1 addition & 1 deletion OpenTkControl/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="OpenTK" version="2.0.0" />
<package id="OpenTK" version="3.2" targetFramework="net45" />
</packages>