5 Commits

Author SHA1 Message Date
NepuShiro 96e90a09d9 This works :) 2026-05-19 11:43:24 -05:00
NepuShiro 519b06367a push wtv 2026-05-19 10:05:38 -05:00
NepuShiro 2970fdc3c6 Remove ImPlot3D 2026-05-18 23:58:49 -05:00
NepuShiro 221cfa5e9c some cleanup 2026-05-18 23:19:40 -05:00
NepuShiro 7167d98cb6 Actual working viewports :) 2026-05-18 10:43:19 -05:00
8 changed files with 1453 additions and 1066 deletions
+96 -66
View File
@@ -2,15 +2,14 @@
using System.Runtime.CompilerServices;
using Hexa.NET.ImGui;
using Hexa.NET.ImPlot;
using Hexa.NET.ImPlot3D;
using SDL3_TestingSuite.SDL3;
using SDL3;
namespace SDL3_TestingSuite;
public class Program
{
private static bool _demoWindowVisible = true;
private static bool _imPlotDemoVisible;
private static bool _fontStuff;
private static readonly Dictionary<string, List<uint>?> GlyphsByName = new Dictionary<string, List<uint>?>();
private static bool _initialized;
@@ -19,38 +18,40 @@ public class Program
public static void Main()
{
const SDL.WindowFlags flags = SDL.WindowFlags.Resizable | SDL.WindowFlags.HighPixelDensity | SDL.WindowFlags.Transparent;
SDL3Window window = new SDL3Window("SDL3 Testing Suite", 100, 100, 1280, 720, flags);
window.ClearColor.W = 0f;
window.RenderCallback = () =>
SDL3Window window = new SDL3Window("SDL3 Testing Suite", 100, 100, 1280, 720);
window.RenderCallback += () =>
{
ImGuiViewportPtr viewport = ImGui.GetMainViewport();
ImGui.SetNextWindowPos(viewport.WorkPos);
ImGui.SetNextWindowSize(viewport.WorkSize);
ImGui.SetNextWindowViewport(viewport.ID);
ImGui.Begin("MainWindow", ImGuiWindowFlags.NoBackground | ImGuiWindowFlags.MenuBar | ImGuiWindowFlags.NoDocking | ImGuiWindowFlags.NoTitleBar | ImGuiWindowFlags.NoResize | ImGuiWindowFlags.NoMove | ImGuiWindowFlags.NoCollapse | ImGuiWindowFlags.NoBringToFrontOnFocus);
if (ImGui.BeginMenuBar())
if (ImGui.Begin("MainWindow", ImGuiWindowFlags.NoBackground | ImGuiWindowFlags.MenuBar | ImGuiWindowFlags.NoDocking | ImGuiWindowFlags.NoTitleBar | ImGuiWindowFlags.NoResize | ImGuiWindowFlags.NoMove | ImGuiWindowFlags.NoCollapse | ImGuiWindowFlags.NoBringToFrontOnFocus))
{
if (ImGui.BeginMenu("Stuff"))
if (ImGui.BeginMenuBar())
{
if (ImGui.MenuItem("Quit"))
if (ImGui.BeginMenu("Stuff"))
{
window.ShouldClose = true;
if (ImGui.MenuItem("Quit"))
{
window.ShouldClose = true;
}
ImGui.EndMenu();
}
ImGui.EndMenu();
ImGui.Spacing();
ImGui.MenuItem("Demo Window", "", ref _demoWindowVisible);
ImGui.Spacing();
ImGui.MenuItem("ImPlot Demo Window", "", ref _imPlotDemoVisible);
ImGui.Spacing();
ImGui.MenuItem("Font Stuff", "", ref _fontStuff);
ImGui.EndMenuBar();
}
ImGui.Spacing();
ImGui.MenuItem("Demo Window", "", ref _demoWindowVisible);
ImGui.Spacing();
ImGui.MenuItem("Font Stuff", "", ref _fontStuff);
ImGui.EndMenuBar();
ImGui.End();
}
ImGui.End();
if (_fontStuff)
{
ImGui.SetNextWindowSize(new Vector2(250, 500), ImGuiCond.FirstUseEver);
@@ -69,7 +70,7 @@ public class Program
fontName = string.IsNullOrEmpty(fontName) ? _font.FontId.ToString() : fontName;
if (!_initialized || change)
{
if (!GlyphsByName.TryGetValue(fontName, out List<uint> glyphs))
if (!GlyphsByName.TryGetValue(fontName, out List<uint>? glyphs))
{
glyphs = new List<uint>();
@@ -96,12 +97,12 @@ public class Program
ImGui.PushFont(null, 24);
if (GlyphsByName.TryGetValue(fontName, out List<uint> glyphs2))
if (GlyphsByName.TryGetValue(fontName, out List<uint>? glyphs2))
{
float cursorXStart = ImGui.GetCursorPosX();
float maxWidth = ImGui.GetContentRegionAvail().X;
foreach (uint codepoint in glyphs2)
foreach (uint codepoint in glyphs2!)
{
string text = char.ConvertFromUtf32((int)codepoint);
float glyphWidth = ImGui.CalcTextSize(text).X;
@@ -123,52 +124,64 @@ public class Program
ImGui.End();
}
}
float time = (float)ImGui.GetTime() * 5;
if (ImPlot.BeginPlot("Moving Rainbow Sine Wave"))
if (ImGui.Begin("Thing"))
{
int count = 200;
float[] xs = new float[2];
float[] ys = new float[2];
for (int i = 0; i < count - 1; i++)
float time = (float)ImGui.GetTime() * 5;
if (ImPlot.BeginPlot("Moving Rainbow Sine Wave"))
{
float x0 = i * 0.1f;
float x1 = (i + 1) * 0.1f;
float y0 = MathF.Sin(x0 * size + time);
float y1 = MathF.Sin(x1 * size + time);
xs[0] = x0;
xs[1] = x1;
ys[0] = y0;
ys[1] = y1;
float t = i / (float)count;
float r = 0.5f + 1f * MathF.Sin(6.2831f * (t));
float g = 0.5f + 1f * MathF.Sin(6.2831f * (t + 0.33f));
float b = 0.5f + 1f * MathF.Sin(6.2831f * (t + 0.66f));
ImPlot.PushStyleColor(ImPlotCol.Line, new Vector4(r, g, b, 1f));
ImPlot.PlotLine("##seg", ref xs[0], ref ys[0], 2);
ImPlot.PopStyleColor();
int count = 200;
float[] xs = new float[2];
float[] ys = new float[2];
for (int i = 0; i < count - 1; i++)
{
float x0 = i * 0.1f;
float x1 = (i + 1) * 0.1f;
float y0 = MathF.Sin(x0 * size + time);
float y1 = MathF.Sin(x1 * size + time);
xs[0] = x0;
xs[1] = x1;
ys[0] = y0;
ys[1] = y1;
float t = i / (float)count;
float r = 0.5f + 1f * MathF.Sin(6.2831f * (t));
float g = 0.5f + 1f * MathF.Sin(6.2831f * (t + 0.33f));
float b = 0.5f + 1f * MathF.Sin(6.2831f * (t + 0.66f));
ImPlot.PushStyleColor(ImPlotCol.Line, new Vector4(r, g, b, 1f));
ImPlot.PlotLine("##seg", ref xs[0], ref ys[0], 2);
ImPlot.PopStyleColor();
}
ImPlot.EndPlot();
}
ImPlot.EndPlot();
ImGui.SliderFloat("Sine", ref size, 1f, 120f);
ImGui.End();
}
if (ImGui.Begin("ImageThing"))
{
ImGui.End();
}
ImGui.SliderFloat("Sine", ref size, 1f, 120f);
if (_demoWindowVisible)
{
ImGui.ShowDemoWindow(ref _demoWindowVisible);
ImPlot.ShowDemoWindow(ref _demoWindowVisible);
// ImPlot3D.ShowDemoWindow(ref _demoWindowVisible);
}
if (_imPlotDemoVisible)
{
ImPlot.ShowDemoWindow(ref _imPlotDemoVisible);
}
};
@@ -177,13 +190,30 @@ public class Program
public static class Logger
{
public static void Log(string? str, [CallerMemberName] string caller = "", [CallerLineNumber] int line = 0)
private const int MessageWidth = 80;
private const int CallerWidth = 35;
public static void Log(object? value, [CallerFilePath] string path = "", [CallerMemberName] string caller = "", [CallerLineNumber] int line = 0)
{
Console.WriteLine($"{caller}({line}): {str}");
Write(value?.ToString(), path, caller, line);
}
public static void Log(object? str, [CallerMemberName] string caller = "", [CallerLineNumber] int line = 0)
public static void Log(object?[] values, [CallerFilePath] string path = "", [CallerMemberName] string caller = "", [CallerLineNumber] int line = 0)
{
Log(str?.ToString(), caller, line);
if (values == null)
{
Write("null", path, caller, line);
return;
}
Write(string.Join(" ", values), path, caller, line);
}
private static void Write(string? str, string path, string caller, int line)
{
string message = (str ?? string.Empty).PadRight(MessageWidth);
string method = caller.PadRight(CallerWidth);
Console.WriteLine($"{message} | {method} | {path}:{line}");
}
}
}
+1 -1
View File
@@ -15,7 +15,7 @@
<PackageReference Include="Hexa.NET.ImGui" Version="2.2.9" />
<PackageReference Include="Hexa.NET.ImGui.Widgets" Version="1.2.18" />
<PackageReference Include="Hexa.NET.ImPlot" Version="2.2.9" />
<PackageReference Include="Hexa.NET.ImPlot3D" Version="2.2.9" />
<PackageReference Include="Hexa.NET.OpenGL3" Version="1.1.0" />
<PackageReference Include="Hexa.NET.Utilities" Version="2.2.12" />
<PackageReference Include="SDL3-CS" Version="3.2.18" />
<PackageReference Include="SDL3-CS.Native" Version="3.2.18" />
+8
View File
@@ -0,0 +1,8 @@
using System.Runtime.InteropServices;
namespace SDL3_TestingSuite.SDL3;
public unsafe static class DelegateHelpers
{
public static void* GetPtrForDelegate<TDelegate>(this TDelegate _delegate) where TDelegate : notnull => (void*)Marshal.GetFunctionPointerForDelegate(_delegate);
}
+8 -10
View File
@@ -29,8 +29,8 @@ public static class FontFind
FileInfo info = new FileInfo(fontPath);
if (!info.Exists || info.Length <= 0) return null;
FontMetrics metrics = FontFind.ReadFontMetrics(fontPath);
float size = FontFind.GetRecommendedPixelSize(metrics);
FontMetrics metrics = ReadFontMetrics(fontPath);
float size = GetRecommendedPixelSize(metrics);
ImFontConfigPtr config = ImGui.ImFontConfig();
config.FontLoaderFlags |= (uint)ImGuiFreeTypeLoaderFlags.LoadColor;
@@ -51,7 +51,7 @@ public static class FontFind
bool flag = false;
ImVector<ImFontPtr> fonts = io.Fonts.Fonts;
List<ImFontPtr> toRemove = new List<ImFontPtr>();
for (int i = 0; i < fonts.Size; i++)
{
ImFontPtr font = fonts[i];
@@ -61,13 +61,13 @@ public static class FontFind
flag = true;
}
}
foreach (ImFontPtr font in toRemove)
{
io.Fonts.RemoveFont(font);
Program.Logger.Log("Removed font: " + fontName);
}
return flag;
}
}
@@ -110,11 +110,9 @@ public static class FontFind
metrics.WinDescent = ReadS16Be(os2Offset + 76);
}
metrics.TypoLineHeight =
metrics.TypoAscender - metrics.TypoDescender + metrics.TypoLineGap;
metrics.TypoLineHeight = metrics.TypoAscender - metrics.TypoDescender + metrics.TypoLineGap;
metrics.WinLineHeight =
metrics.WinAscent + metrics.WinDescent;
metrics.WinLineHeight = metrics.WinAscent + metrics.WinDescent;
return metrics;
@@ -142,7 +140,7 @@ public static class FontFind
return new string(new char[] { c1, c2, c3, c4 });
}
}
public static int GetRecommendedPixelSize(FontMetrics metrics)
{
if (metrics.UnitsPerEm == 0 || metrics.TypoLineHeight == 0)
+566 -564
View File
File diff suppressed because it is too large Load Diff
+581 -340
View File
File diff suppressed because it is too large Load Diff
+35
View File
@@ -0,0 +1,35 @@
using System.Runtime.InteropServices;
namespace SDL3_TestingSuite.SDL3;
public unsafe static class ImGuiUserData<T> where T : class
{
public static void* Store(T value)
{
if (value == null)
return null;
GCHandle handle = GCHandle.Alloc(value, GCHandleType.Normal);
return (void*)GCHandle.ToIntPtr(handle);
}
public static T? Get(void* ptr)
{
if (ptr == null)
return null;
GCHandle handle = GCHandle.FromIntPtr((nint)ptr);
return (T?)handle.Target;
}
public static void Free(void* ptr)
{
if (ptr == null)
return;
GCHandle handle = GCHandle.FromIntPtr((nint)ptr);
if (handle.IsAllocated)
handle.Free();
}
}
+158 -85
View File
@@ -1,12 +1,9 @@
using System;
using System.Diagnostics;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Numerics;
using Hexa.NET.ImGui;
using Hexa.NET.ImGui.Utilities;
using Hexa.NET.ImGuizmo;
using Hexa.NET.ImPlot;
using Hexa.NET.ImPlot3D;
using Hexa.NET.OpenGL;
using HexaGen.Runtime;
using SDL3;
namespace SDL3_TestingSuite.SDL3;
@@ -14,49 +11,70 @@ namespace SDL3_TestingSuite.SDL3;
public sealed unsafe class SDL3Window : IDisposable
{
public readonly nint Window;
public readonly nint Renderer;
internal Action? RenderCallback { get; set; }
public readonly Vector4 DefaultClearColor = new Vector4(0.06f, 0.06f, 0.06f, 1f);
public Vector4 ClearColor = new Vector4(0.06f, 0.05882353f, 0.05882353f, 1f);
private Vector4? _clearColor;
public Vector4 ClearColor
{
get => _clearColor ?? DefaultClearColor;
set => _clearColor = value;
}
private readonly Stopwatch _timer = Stopwatch.StartNew();
private TimeSpan _time = TimeSpan.Zero;
public event Action? RenderCallback;
private SDL.Rect _screenClipRect;
private ImGuiContextPtr _imGuiContext;
private ImPlotContextPtr _imPlotContext;
public bool Disposed => _disposed;
private bool _disposed;
private FileSystemWatcher? _watcher;
private readonly FileSystemWatcher? _watcher;
private readonly GL _gl;
public SDL3Window(string name, int posX, int posY, int width, int height, SDL.WindowFlags flags = SDL.WindowFlags.Resizable | SDL.WindowFlags.HighPixelDensity)
{
if (!SDL.Init(SDL.InitFlags.Events | SDL.InitFlags.Video | SDL.InitFlags.Gamepad))
throw new Exception($"SDL_Init failed: {SDL.GetError()}");
SDL.GLSetAttribute(SDL.GLAttr.ContextFlags, (int)SDL.GLContextFlag.ForwardCompatible);
SDL.GLSetAttribute(SDL.GLAttr.ContextProfileMask, (int)SDL.GLProfile.Core);
SDL.GLSetAttribute(SDL.GLAttr.ContextMajorVersion, 3);
SDL.GLSetAttribute(SDL.GLAttr.ContextMinorVersion, 3);
SDL.SetHint(SDL.Hints.IMEImplementedUI, "1");
// Create window & renderer
if (!SDL.CreateWindowAndRenderer(name, width, height, flags, out Window, out Renderer))
SDL.GLSetAttribute(SDL.GLAttr.DoubleBuffer, 1);
SDL.GLSetAttribute(SDL.GLAttr.DepthSize, 24);
SDL.GLSetAttribute(SDL.GLAttr.StencilSize, 8);
Window = SDL.CreateWindow(name, width, height, flags | SDL.WindowFlags.OpenGL);
if (Window == nint.Zero)
throw new Exception($"SDL_CreateWindowAndRenderer failed: {SDL.GetError()}");
nint glContext = SDL.GLCreateContext(Window);
if (glContext == nint.Zero)
throw new Exception($"SDL_GL_CreateContext failed: {SDL.GetError()}");
SDL.GLMakeCurrent(Window, glContext);
SDL.GLSetSwapInterval(1);
SDL.SetWindowPosition(Window, posX, posY);
SDL.SetRenderVSync(Renderer, 1);
// SDL.SetRenderVSync(Renderer, 1);
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();
ImPlot.SetImGuiContext(_imGuiContext);
ImGuizmo.SetImGuiContext(_imGuiContext);
ImGuiIOPtr io = ImGui.GetIO();
io.ConfigFlags |= ImGuiConfigFlags.NavEnableKeyboard | ImGuiConfigFlags.NavEnableGamepad | ImGuiConfigFlags.DockingEnable;
// io.ConfigFlags |= ImGuiConfigFlags.ViewportsEnable;
io.Fonts.Flags |= ImFontAtlasFlags.NoBakedLines;
io.ConfigFlags |= ImGuiConfigFlags.ViewportsEnable;
io.Fonts.Flags |= ImFontAtlasFlags.NoBakedLines;
io.Fonts.AddFontDefault();
try
@@ -66,7 +84,7 @@ public sealed unsafe class SDL3Window : IDisposable
_watcher = new FileSystemWatcher(fontsPath);
_watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.CreationTime;
_watcher.Created += (a, b) =>
_watcher.Created += (_, b) =>
{
if (!File.Exists(b.FullPath)) return;
@@ -74,7 +92,7 @@ public sealed unsafe class SDL3Window : IDisposable
ImGui.GetIO().AddFont(b.FullPath);
};
_watcher.Deleted += (a, b) =>
_watcher.Deleted += (_, b) =>
{
if (Path.GetExtension(b.FullPath) != ".ttf") return;
@@ -94,26 +112,67 @@ public sealed unsafe class SDL3Window : IDisposable
{
Console.WriteLine(e);
}
_gl = new GL(new BindingsContext(Window, glContext));
// Init platform and renderer
ImGuiSDL3Platform.Init(Window, Renderer);
ImGuiSDL3Renderer.Init(Renderer);
ImGuiSDL3Platform.Init(_gl, glContext, Window, this);
ImGuiSDL3Renderer.Init(_gl, "#version 330");
}
public bool ShouldClose;
public void Run()
{
while (!_disposed)
{
ImGui.GetIO().DeltaTime = (float)(_timer.Elapsed - _time).TotalSeconds;
_time = _timer.Elapsed;
SDL.PumpEvents();
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;
}
}
_gl.MakeCurrent();
_gl.ClearColor(ClearColor.X, ClearColor.Y, ClearColor.Z, ClearColor.W);
_gl.Clear(GLClearBufferMask.ColorBufferBit);
ImGuiSDL3Platform.NewFrame();
ImGuiSDL3Renderer.NewFrame();
ImGui.NewFrame();
RenderCallback?.Invoke();
Render();
ImGuiIOPtr io = ImGui.GetIO();
ImGui.Render();
_gl.MakeCurrent();
ImGuiSDL3Renderer.RenderDrawData(ImGui.GetDrawData());
if ((io.ConfigFlags & ImGuiConfigFlags.ViewportsEnable) != 0)
{
ImGui.UpdatePlatformWindows();
ImGui.RenderPlatformWindowsDefault();
}
_gl.MakeCurrent();
_gl.SwapBuffers();
if (ShouldClose)
{
@@ -126,56 +185,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);
@@ -194,6 +203,15 @@ public sealed unsafe class SDL3Window : IDisposable
if (disposing)
{
if (_imGuiContext.Handle != null)
{
ImGui.SetCurrentContext(_imGuiContext);
}
if (_imPlotContext.Handle != null)
{
ImPlot.SetCurrentContext(_imPlotContext);
}
ImGuiSDL3Renderer.Dispose();
ImGuiSDL3Platform.Dispose();
@@ -206,16 +224,71 @@ public sealed unsafe class SDL3Window : IDisposable
ImGui.DestroyContext(_imGuiContext);
_imGuiContext = null;
}
if (Renderer != nint.Zero)
if (_imPlotContext.Handle != null)
{
SDL.DestroyRenderer(Renderer);
ImPlot.SetCurrentContext(null);
ImPlot.DestroyContext(_imPlotContext);
_imPlotContext = null;
}
// if (Renderer != nint.Zero)
// {
// SDL.DestroyRenderer(Renderer);
// }
if (Window != nint.Zero)
{
SDL.DestroyWindow(Window);
}
}
}
public unsafe class BindingsContext : IGLContext
{
private readonly nint window;
private readonly nint context;
public BindingsContext(nint window, nint context)
{
this.window = window;
this.context = context;
}
public nint Handle => window;
public bool IsCurrent => SDL.GLGetCurrentContext() == context;
public void Dispose() { }
public nint GetProcAddress(string procName)
{
return (nint)SDL.GLGetProcAddress(procName).GetPtrForDelegate();
}
public bool IsExtensionSupported(string extensionName)
{
return SDL.GLExtensionSupported(extensionName);
}
public void MakeCurrent()
{
SDL.GLMakeCurrent(window, context);
}
public void SwapBuffers()
{
SDL.GLSwapWindow(window);
}
public void SwapInterval(int interval)
{
SDL.GLSetSwapInterval(interval);
}
public bool TryGetProcAddress(string procName, out nint procAddress)
{
procAddress = (nint)SDL.GLGetProcAddress(procName).GetPtrForDelegate();
return procAddress != 0;
}
}