working on window handling

This commit is contained in:
max 2024-07-10 23:18:56 +02:00
parent 2c839d8fad
commit 42b978e8c9
3 changed files with 25 additions and 29 deletions

View File

@ -19,7 +19,6 @@ public static class Engine
private static Stopwatch gameTimer; private static Stopwatch gameTimer;
private static long previousTicks = 0; private static long previousTicks = 0;
private static TimeSpan accumulatedUpdateTime = TimeSpan.Zero; private static TimeSpan accumulatedUpdateTime = TimeSpan.Zero;
private static TimeSpan accumulatedDrawTime = 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 // 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"); throw new Exception("Failed to claim window");
} }
MainWindow.OnCloseEvent += (w) => {
quit = true;
};
AudioDevice = new AudioDevice(); AudioDevice = new AudioDevice();
Controller = new Gui.GuiController(GraphicsDevice, MainWindow, Color.DarkOliveGreen); Controller = new Gui.GuiController(GraphicsDevice, MainWindow, Color.DarkOliveGreen);

View File

@ -37,7 +37,8 @@ internal class GuiController : IDisposable
private readonly Shader imGuiFragmentShader; private readonly Shader imGuiFragmentShader;
private readonly Sampler imGuiSampler; private readonly Sampler imGuiSampler;
private readonly GuiTextureStorage textureStorage = new GuiTextureStorage(); private readonly GuiTextureStorage textureStorage = new GuiTextureStorage();
private readonly Dictionary<Window, GCHandle> windows = new Dictionary<Window, GCHandle>(16); private readonly Dictionary<Window, GCHandle> windowToHandle = new Dictionary<Window, GCHandle>(16);
private readonly Dictionary<ImGuiViewportPtr, Window> imguiToWindow = new Dictionary<ImGuiViewportPtr, Window>(16);
private Texture fontTexture = null; private Texture fontTexture = null;
private uint vertexCount = 0; 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) public GuiController(GraphicsDevice graphicsDevice, Window mainWindow, Color clearColor, ImGuiConfigFlags configFlags = ImGuiConfigFlags.NavEnableKeyboard | ImGuiConfigFlags.DockingEnable | ImGuiConfigFlags.ViewportsEnable)
{ {
this.mainWindow = mainWindow; this.mainWindow = mainWindow;
this.graphicsDevice = graphicsDevice;
this.clearColor = clearColor; this.clearColor = clearColor;
resourceUploader = new ResourceUploader(graphicsDevice); resourceUploader = new ResourceUploader(graphicsDevice);
@ -100,7 +100,6 @@ public GuiController(GraphicsDevice graphicsDevice, Window mainWindow, Color cle
BuildFontAtlas(); BuildFontAtlas();
io.ConfigFlags = configFlags; io.ConfigFlags = configFlags;
//io.MouseDrawCursor = true;
if (!OperatingSystem.IsWindows()) if (!OperatingSystem.IsWindows())
{ {
@ -167,11 +166,6 @@ public void Update(float deltaTime)
OnGui?.Invoke(); OnGui?.Invoke();
{ // Debug
ImGuiIOPtr io = ImGui.GetIO();
ImGui.Text($"mouse pos: {io.MousePos}");
}
ImGui.EndFrame(); ImGui.EndFrame();
} }
@ -557,21 +551,21 @@ private void CreateWindow(ImGuiViewportPtr vp)
private void DestroyWindow(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; if(imguiToWindow.TryGetValue(vp, out Window window))
graphicsDevice.UnclaimWindow(window);
if (windows.TryGetValue(window, out GCHandle handle))
{ {
handle.Free(); graphicsDevice.UnclaimWindow(window);
windows.Remove(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) 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++) for (int i = 0; i < platformIO.Viewports.Capacity; i++)
{ {
ImGuiViewportPtr vp = platformIO.Viewports[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) private void ShowWindow(ImGuiViewportPtr vp)
@ -697,7 +689,7 @@ public void Dispose()
imGuiSampler?.Dispose(); imGuiSampler?.Dispose();
resourceUploader?.Dispose(); resourceUploader?.Dispose();
foreach (KeyValuePair<Window, GCHandle> window in windows) foreach (KeyValuePair<Window, GCHandle> window in windowToHandle)
{ {
if (window.Key == mainWindow) continue; if (window.Key == mainWindow) continue;
@ -705,6 +697,7 @@ public void Dispose()
window.Key.Dispose(); window.Key.Dispose();
window.Value.Free(); window.Value.Free();
} }
windows.Clear(); windowToHandle.Clear();
imguiToWindow.Clear();
} }
} }

View File

@ -136,9 +136,9 @@ private void ProcessMovedChangedEvent(ref SDL.SDL_WindowEvent ev)
private void ProcessCloseEvent(ref SDL.SDL_WindowEvent ev) private void ProcessCloseEvent(ref SDL.SDL_WindowEvent ev)
{ {
OnCloseEvent?.Invoke(this);
Engine.GraphicsDevice.UnclaimWindow(this); Engine.GraphicsDevice.UnclaimWindow(this);
Dispose(); Dispose();
OnCloseEvent?.Invoke(this);
} }
/// <summary> /// <summary>