diff --git a/Nerfed.Runtime/Engine.cs b/Nerfed.Runtime/Engine.cs index 7a6a21b..da76aa9 100644 --- a/Nerfed.Runtime/Engine.cs +++ b/Nerfed.Runtime/Engine.cs @@ -19,7 +19,6 @@ public static class Engine private static Stopwatch gameTimer; private static long previousTicks = 0; private static TimeSpan accumulatedUpdateTime = TimeSpan.Zero; - private static TimeSpan accumulatedDrawTime = TimeSpan.Zero; // must be a power of 2 so we can do a bitmask optimization when checking worst case @@ -65,6 +64,10 @@ internal static void Run(string[] args) throw new Exception("Failed to claim window"); } + MainWindow.OnCloseEvent += (w) => { + quit = true; + }; + AudioDevice = new AudioDevice(); Controller = new Gui.GuiController(GraphicsDevice, MainWindow, Color.DarkOliveGreen); diff --git a/Nerfed.Runtime/Gui/GuiController.cs b/Nerfed.Runtime/Gui/GuiController.cs index 5b278de..f8e36ff 100644 --- a/Nerfed.Runtime/Gui/GuiController.cs +++ b/Nerfed.Runtime/Gui/GuiController.cs @@ -37,7 +37,8 @@ internal class GuiController : IDisposable private readonly Shader imGuiFragmentShader; private readonly Sampler imGuiSampler; private readonly GuiTextureStorage textureStorage = new GuiTextureStorage(); - private readonly Dictionary windows = new Dictionary(16); + private readonly Dictionary windowToHandle = new Dictionary(16); + private readonly Dictionary imguiToWindow = new Dictionary(16); private Texture fontTexture = null; private uint vertexCount = 0; @@ -49,7 +50,6 @@ internal class GuiController : IDisposable public GuiController(GraphicsDevice graphicsDevice, Window mainWindow, Color clearColor, ImGuiConfigFlags configFlags = ImGuiConfigFlags.NavEnableKeyboard | ImGuiConfigFlags.DockingEnable | ImGuiConfigFlags.ViewportsEnable) { this.mainWindow = mainWindow; - this.graphicsDevice = graphicsDevice; this.clearColor = clearColor; resourceUploader = new ResourceUploader(graphicsDevice); @@ -100,7 +100,6 @@ public GuiController(GraphicsDevice graphicsDevice, Window mainWindow, Color cle BuildFontAtlas(); io.ConfigFlags = configFlags; - //io.MouseDrawCursor = true; if (!OperatingSystem.IsWindows()) { @@ -167,11 +166,6 @@ public void Update(float deltaTime) OnGui?.Invoke(); - { // Debug - ImGuiIOPtr io = ImGui.GetIO(); - ImGui.Text($"mouse pos: {io.MousePos}"); - } - ImGui.EndFrame(); } @@ -557,21 +551,21 @@ private void CreateWindow(ImGuiViewportPtr vp) private void DestroyWindow(ImGuiViewportPtr vp) { - if (vp.PlatformUserData == IntPtr.Zero) return; + //Window window = (Window)GCHandle.FromIntPtr(vp.PlatformUserData).Target; - Window window = (Window)GCHandle.FromIntPtr(vp.PlatformUserData).Target; - graphicsDevice.UnclaimWindow(window); - - if (windows.TryGetValue(window, out GCHandle handle)) + if(imguiToWindow.TryGetValue(vp, out Window window)) { - handle.Free(); - windows.Remove(window); + graphicsDevice.UnclaimWindow(window); + + if (windowToHandle.TryGetValue(window, out GCHandle handle)) + { + handle.Free(); + windowToHandle.Remove(window); + } + + imguiToWindow.Remove(vp); + window.Dispose(); } - - //graphicsDevice.Wait(); - window.Dispose(); - - vp.PlatformUserData = IntPtr.Zero; } private void AddWindow(Window window, ImGuiViewportPtr vp, GCHandle handle) @@ -592,14 +586,12 @@ private void AddWindow(Window window, ImGuiViewportPtr vp, GCHandle handle) for (int i = 0; i < platformIO.Viewports.Capacity; i++) { ImGuiViewportPtr vp = platformIO.Viewports[i]; - if(win == (Window)GCHandle.FromIntPtr(vp.PlatformUserData).Target) - { - DestroyWindow(vp); - } + DestroyWindow(vp); } }; - windows.Add(window, handle); + windowToHandle.Add(window, handle); + imguiToWindow.Add(vp, window); } private void ShowWindow(ImGuiViewportPtr vp) @@ -697,7 +689,7 @@ public void Dispose() imGuiSampler?.Dispose(); resourceUploader?.Dispose(); - foreach (KeyValuePair window in windows) + foreach (KeyValuePair window in windowToHandle) { if (window.Key == mainWindow) continue; @@ -705,6 +697,7 @@ public void Dispose() window.Key.Dispose(); window.Value.Free(); } - windows.Clear(); + windowToHandle.Clear(); + imguiToWindow.Clear(); } } \ No newline at end of file diff --git a/Nerfed.Runtime/Window/Window.cs b/Nerfed.Runtime/Window/Window.cs index 56ca056..d9f5cb3 100644 --- a/Nerfed.Runtime/Window/Window.cs +++ b/Nerfed.Runtime/Window/Window.cs @@ -136,9 +136,9 @@ private void ProcessMovedChangedEvent(ref SDL.SDL_WindowEvent ev) private void ProcessCloseEvent(ref SDL.SDL_WindowEvent ev) { + OnCloseEvent?.Invoke(this); Engine.GraphicsDevice.UnclaimWindow(this); Dispose(); - OnCloseEvent?.Invoke(this); } ///