some cleanup

This commit is contained in:
2026-05-18 23:19:40 -05:00
parent 7167d98cb6
commit 221cfa5e9c
7 changed files with 765 additions and 764 deletions
+76 -69
View File
@@ -1,8 +1,8 @@
using System.Diagnostics;
using System.Numerics;
using System.Numerics;
using Hexa.NET.ImGui;
using Hexa.NET.ImGuizmo;
using Hexa.NET.ImPlot;
using Hexa.NET.ImPlot3D;
using SDL3;
namespace SDL3_TestingSuite.SDL3;
@@ -12,15 +12,13 @@ public sealed unsafe class SDL3Window : IDisposable
public readonly nint Window;
public readonly nint Renderer;
internal Action? RenderCallback { get; set; }
public event Action RenderCallback;
public Vector4 ClearColor = new Vector4(0.06f, 0.05882353f, 0.05882353f, 1f);
private readonly Stopwatch _timer = Stopwatch.StartNew();
private TimeSpan _time = TimeSpan.Zero;
private SDL.Rect _screenClipRect;
private ImGuiContextPtr _imGuiContext;
private ImPlotContextPtr _imPlotContext;
private ImPlot3DContextPtr _imPlot3DContext;
public bool Disposed => _disposed;
private bool _disposed;
@@ -41,18 +39,18 @@ public sealed unsafe class SDL3Window : IDisposable
SDL.ShowWindow(Window);
// Create ImGui context
var context = ImGui.CreateContext();
ImPlot.CreateContext();
ImPlot.SetImGuiContext(context);
ImGuizmo.SetImGuiContext(context);
// ImPlot3D.SetImGuiContext(context);
// ImPlot3D.CreateContext();
_imGuiContext = ImGui.CreateContext();
_imPlotContext = ImPlot.CreateContext();
// _imPlot3DContext = ImPlot3D.CreateContext();
ImPlot.SetImGuiContext(_imGuiContext);
ImGuizmo.SetImGuiContext(_imGuiContext);
// ImPlot3D.SetImGuiContext(_imGuiContext);
ImGuiIOPtr io = ImGui.GetIO();
io.ConfigFlags |= ImGuiConfigFlags.NavEnableKeyboard | ImGuiConfigFlags.NavEnableGamepad | ImGuiConfigFlags.DockingEnable;
io.ConfigFlags |= ImGuiConfigFlags.ViewportsEnable;
io.Fonts.Flags |= ImFontAtlasFlags.NoBakedLines;
io.Fonts.Flags |= ImFontAtlasFlags.NoBakedLines;
io.Fonts.AddFontDefault();
try
@@ -96,20 +94,54 @@ public sealed unsafe class SDL3Window : IDisposable
ImGuiSDL3Renderer.Init(Renderer, this);
}
public bool ShouldClose;
public void Run()
{
while (!_disposed)
{
ImGui.GetIO().DeltaTime = (float)(_timer.Elapsed - _time).TotalSeconds;
_time = _timer.Elapsed;
if (ImGui.GetIO().WantTextInput && !SDL.TextInputActive(Window))
SDL.StartTextInput(Window);
else if (!ImGui.GetIO().WantTextInput && SDL.TextInputActive(Window))
SDL.StopTextInput(Window);
PollEvents();
while (SDL.PollEvent(out SDL.Event ev))
{
ImGuiSDL3Platform.ProcessEvent(ev);
Update();
switch ((SDL.EventType)ev.Type)
{
case SDL.EventType.Terminating:
case SDL.EventType.WindowCloseRequested:
case SDL.EventType.Quit:
ShouldClose = true;
break;
}
}
ImGuiSDL3Platform.NewFrame();
ImGuiSDL3Renderer.NewFrame();
ImGui.NewFrame();
RenderCallback?.Invoke();
Render();
ImGuiIOPtr io = ImGui.GetIO();
ImGui.Render();
SDL.SetRenderScale(Renderer, io.DisplayFramebufferScale.X, io.DisplayFramebufferScale.Y);
SDL.SetRenderDrawColorFloat(Renderer, ClearColor.X, ClearColor.Y, ClearColor.Z, ClearColor.W);
SDL.RenderClear(Renderer);
ImGuiSDL3Renderer.RenderDrawData(ImGui.GetDrawData(), Renderer);
if ((io.ConfigFlags & ImGuiConfigFlags.ViewportsEnable) != 0)
{
ImGui.UpdatePlatformWindows();
ImGui.RenderPlatformWindowsDefault();
}
SDL.RenderPresent(Renderer);
if (ShouldClose)
{
@@ -122,56 +154,6 @@ public sealed unsafe class SDL3Window : IDisposable
Dispose();
}
public bool ShouldClose;
private void PollEvents()
{
if (ImGui.GetIO().WantTextInput && !SDL.TextInputActive(Window))
SDL.StartTextInput(Window);
else if (!ImGui.GetIO().WantTextInput && SDL.TextInputActive(Window))
SDL.StopTextInput(Window);
while (SDL.PollEvent(out SDL.Event ev))
{
ImGuiSDL3Platform.ProcessEvent(ev);
switch ((SDL.EventType)ev.Type)
{
case SDL.EventType.Terminating:
case SDL.EventType.WindowCloseRequested:
case SDL.EventType.Quit:
ShouldClose = true;
break;
}
}
}
private void Update()
{
ImGuiSDL3Platform.NewFrame();
ImGuiSDL3Renderer.NewFrame();
ImGui.NewFrame();
}
private void Render()
{
ImGuiIOPtr io = ImGui.GetIO();
ImGui.Render();
SDL.SetRenderScale(Renderer, io.DisplayFramebufferScale.X, io.DisplayFramebufferScale.Y);
SDL.SetRenderDrawColorFloat(Renderer, ClearColor.X, ClearColor.Y, ClearColor.Z, ClearColor.W);
SDL.RenderClear(Renderer);
ImGuiSDL3Renderer.RenderDrawData(ImGui.GetDrawData(), Renderer);
if ((io.ConfigFlags & ImGuiConfigFlags.ViewportsEnable) != 0)
{
ImGui.UpdatePlatformWindows();
ImGui.RenderPlatformWindowsDefault();
}
SDL.RenderPresent(Renderer);
}
~SDL3Window()
{
Dispose(false);
@@ -190,6 +172,19 @@ public sealed unsafe class SDL3Window : IDisposable
if (disposing)
{
if (_imGuiContext.Handle != null)
{
ImGui.SetCurrentContext(_imGuiContext);
}
if (_imPlotContext.Handle != null)
{
ImPlot.SetCurrentContext(_imPlotContext);
}
// if (_imPlot3DContext.Handle != null)
// {
// ImPlot3D.SetCurrentContext(_imPlot3DContext);
// }
ImGuiSDL3Renderer.Dispose();
ImGuiSDL3Platform.Dispose();
@@ -202,6 +197,18 @@ public sealed unsafe class SDL3Window : IDisposable
ImGui.DestroyContext(_imGuiContext);
_imGuiContext = null;
}
if (_imPlotContext.Handle != null)
{
ImPlot.SetCurrentContext(null);
ImPlot.DestroyContext(_imPlotContext);
_imPlotContext = null;
}
// if (_imPlot3DContext.Handle != null)
// {
// ImPlot3D.SetCurrentContext(null);
// ImPlot3D.DestroyContext(_imPlot3DContext);
// _imPlot3DContext = null;
// }
if (Renderer != nint.Zero)
{