Skip to content

Commit

Permalink
Closes #6; Fixed callbacks destruction
Browse files Browse the repository at this point in the history
  • Loading branch information
Tornado-Technology committed Jul 11, 2024
1 parent 19f5e23 commit 27c9d95
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,43 @@
using Hypercube.Client.Graphics.Event;
using Hypercube.Client.Input;
using Hypercube.Client.Input;
using Hypercube.Client.Utilities;
using OpenTK.Windowing.GraphicsLibraryFramework;
using GlfwKeyModifiers = OpenTK.Windowing.GraphicsLibraryFramework.KeyModifiers;
using KeyModifiers = Hypercube.Client.Input.KeyModifiers;
using static OpenTK.Windowing.GraphicsLibraryFramework.GLFWCallbacks;

namespace Hypercube.Client.Graphics.Windows.Manager;

public sealed unsafe partial class GlfwWindowManager
{
private void OnWindowClosed(Window* window)
private ErrorCallback? _errorCallback;

private KeyCallback? _keyCallback;

private WindowCloseCallback? _windowCloseCallback;
private WindowSizeCallback? _windowSizeCallback;
private WindowFocusCallback? _windowFocusCallback;

/// <summary>
/// GC doesn't think our callbacks are stored anywhere,
/// so it cleans them up,
/// we need to indicate that there is a link to them so they don't get lost.
/// </summary>
private void HandleCallbacks()
{
if (!TryGetWindow(window, out var registration))
return;
_errorCallback = OnErrorHandled;

_keyCallback = OnWindowKeyHandled;

_renderer.CloseWindow(registration);
_windowCloseCallback = OnWindowClosed;
_windowSizeCallback = OnWindowResized;
_windowFocusCallback = OnWindowFocusChanged;
}


private void OnErrorHandled(ErrorCode error, string description)
{
_logger.Error(GLFWHelper.FormatError(error, description));
}

private void OnWindowKeyHandled(Window* window, Keys glfwKey, int scanCode, InputAction action, GlfwKeyModifiers mods)
{
var key = (Key)glfwKey;
Expand Down Expand Up @@ -49,6 +70,14 @@ private void OnWindowKeyHandled(Window* window, Keys glfwKey, int scanCode, Inpu
(KeyModifiers)mods,
scanCode));
}

private void OnWindowClosed(Window* window)
{
if (!TryGetWindow(window, out var registration))
return;

_renderer.CloseWindow(registration);
}

private void OnWindowResized(Window* window, int width, int height)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,12 @@ private bool GlfwInit()
}

// Set callback to handle errors
GLFW.SetErrorCallback(OnErrorHandled);
GLFW.SetErrorCallback(_errorCallback);

_initialized = true;

var version = GLFW.GetVersionString();
_logger.EngineInfo($"Initialize, version: {version}");
return true;
}

private void OnErrorHandled(ErrorCode error, string description)
{
_logger.Error(GLFWHelper.FormatError(error, description));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ private GlfwWindowRegistration WindowSetup(Window* window, WindowCreateSettings
SetWindowIcons(registration, settings.WindowImages.ToList());

// Setting callbacks
GLFW.SetKeyCallback(window, OnWindowKeyHandled);
GLFW.SetWindowCloseCallback(window, OnWindowClosed);
GLFW.SetWindowSizeCallback(window, OnWindowResized);
GLFW.SetWindowFocusCallback(window, OnWindowFocusChanged);
GLFW.SetKeyCallback(window, _keyCallback);
GLFW.SetWindowCloseCallback(window, _windowCloseCallback);
GLFW.SetWindowSizeCallback(window, _windowSizeCallback);
GLFW.SetWindowFocusCallback(window, _windowFocusCallback);

return registration;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public bool Init()
{
DependencyManager.Inject(this);

// We don't let GC take our callbacks
HandleCallbacks();

if (!GlfwInit())
return false;

Expand Down

0 comments on commit 27c9d95

Please sign in to comment.