This commit is contained in:
2026-05-20 04:23:47 -05:00
parent 96e90a09d9
commit 7d5f242a43
5 changed files with 502 additions and 454 deletions
+93 -107
View File
@@ -26,154 +26,140 @@ public class Program
ImGui.SetNextWindowSize(viewport.WorkSize); ImGui.SetNextWindowSize(viewport.WorkSize);
ImGui.SetNextWindowViewport(viewport.ID); ImGui.SetNextWindowViewport(viewport.ID);
if (ImGui.Begin("MainWindow", ImGuiWindowFlags.NoBackground | ImGuiWindowFlags.MenuBar | ImGuiWindowFlags.NoDocking | ImGuiWindowFlags.NoTitleBar | ImGuiWindowFlags.NoResize | ImGuiWindowFlags.NoMove | ImGuiWindowFlags.NoCollapse | ImGuiWindowFlags.NoBringToFrontOnFocus)) ImGui.Begin("MainWindow", ImGuiWindowFlags.NoBackground | ImGuiWindowFlags.MenuBar | ImGuiWindowFlags.NoDocking | ImGuiWindowFlags.NoTitleBar | ImGuiWindowFlags.NoResize | ImGuiWindowFlags.NoMove | ImGuiWindowFlags.NoCollapse | ImGuiWindowFlags.NoBringToFrontOnFocus);
if (ImGui.BeginMenuBar())
{ {
if (ImGui.BeginMenuBar()) if (ImGui.BeginMenu("Stuff"))
{ {
if (ImGui.BeginMenu("Stuff")) if (ImGui.MenuItem("Quit"))
{ {
if (ImGui.MenuItem("Quit")) window.ShouldClose = true;
{
window.ShouldClose = true;
}
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.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.End(); ImGui.EndMenuBar();
} }
ImGui.End();
if (_fontStuff) if (_fontStuff)
{ {
ImGui.SetNextWindowSize(new Vector2(250, 500), ImGuiCond.FirstUseEver); ImGui.SetNextWindowSize(new Vector2(250, 500), ImGuiCond.FirstUseEver);
if (ImGui.Begin("FontStuff", ref _fontStuff)) ImGui.Begin("FontStuff", ref _fontStuff);
ImGui.ShowFontSelector("Font");
bool change = false;
if (ImGui.GetFont() != _font)
{ {
ImGui.ShowFontSelector("Font"); _font = ImGui.GetFont();
change = true;
}
bool change = false; string fontName = _font.GetDebugNameS();
if (ImGui.GetFont() != _font) fontName = string.IsNullOrEmpty(fontName) ? _font.FontId.ToString() : fontName;
if (!_initialized || change)
{
if (!GlyphsByName.TryGetValue(fontName, out List<uint>? glyphs))
{ {
_font = ImGui.GetFont(); glyphs = new List<uint>();
change = true;
}
string fontName = _font.GetDebugNameS(); ImFontPtr font = _font;
fontName = string.IsNullOrEmpty(fontName) ? _font.FontId.ToString() : fontName;
if (!_initialized || change) for (uint codepoint = 0; codepoint <= 0x10FFFF; codepoint++)
{
if (!GlyphsByName.TryGetValue(fontName, out List<uint>? glyphs))
{ {
glyphs = new List<uint>(); if (codepoint >= 0xD800 && codepoint <= 0xDFFF)
ImFontPtr font = _font;
for (uint codepoint = 0; codepoint <= 0x10FFFF; codepoint++)
{ {
if (codepoint >= 0xD800 && codepoint <= 0xDFFF) continue;
{
continue;
}
if (!font.IsGlyphInFont(codepoint)) continue;
glyphs.Add(codepoint);
} }
GlyphsByName[fontName] = glyphs; if (!font.IsGlyphInFont(codepoint)) continue;
Logger.Log($"Initialized font {fontName} with {glyphs.Count} glyphs");
glyphs.Add(codepoint);
} }
_initialized = true; GlyphsByName[fontName] = glyphs;
Logger.Log($"Initialized font {fontName} with {glyphs.Count} glyphs");
} }
ImGui.PushFont(null, 24); _initialized = true;
if (GlyphsByName.TryGetValue(fontName, out List<uint>? glyphs2))
{
float cursorXStart = ImGui.GetCursorPosX();
float maxWidth = ImGui.GetContentRegionAvail().X;
foreach (uint codepoint in glyphs2!)
{
string text = char.ConvertFromUtf32((int)codepoint);
float glyphWidth = ImGui.CalcTextSize(text).X;
float cursorX = ImGui.GetCursorPosX();
if (cursorX > cursorXStart && (cursorX + glyphWidth) > (cursorXStart + maxWidth))
{
ImGui.NewLine();
}
ImGui.TextUnformatted(text);
ImGui.SameLine();
}
}
ImGui.PopFont();
ImGui.End();
} }
}
if (ImGui.Begin("Thing")) ImGui.PushFont(null, 24);
{
float time = (float)ImGui.GetTime() * 5; if (GlyphsByName.TryGetValue(fontName, out List<uint>? glyphs2))
if (ImPlot.BeginPlot("Moving Rainbow Sine Wave"))
{ {
int count = 200; float cursorXStart = ImGui.GetCursorPosX();
float maxWidth = ImGui.GetContentRegionAvail().X;
float[] xs = new float[2]; foreach (uint codepoint in glyphs2!)
float[] ys = new float[2];
for (int i = 0; i < count - 1; i++)
{ {
float x0 = i * 0.1f; string text = char.ConvertFromUtf32((int)codepoint);
float x1 = (i + 1) * 0.1f; float glyphWidth = ImGui.CalcTextSize(text).X;
float y0 = MathF.Sin(x0 * size + time); float cursorX = ImGui.GetCursorPosX();
float y1 = MathF.Sin(x1 * size + time); if (cursorX > cursorXStart && (cursorX + glyphWidth) > (cursorXStart + maxWidth))
{
ImGui.NewLine();
}
xs[0] = x0; ImGui.TextUnformatted(text);
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();
ImGui.SameLine();
} }
ImPlot.EndPlot();
} }
ImGui.SliderFloat("Sine", ref size, 1f, 120f);
ImGui.PopFont();
ImGui.End(); ImGui.End();
} }
if (ImGui.Begin("ImageThing")) ImGui.Begin("Thing");
float time = (float)ImGui.GetTime() * 5;
if (ImPlot.BeginPlot("Moving Rainbow Sine Wave"))
{ {
int count = 200;
ImGui.End(); 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();
} }
ImGui.SliderFloat("Sine", ref size, 1f, 120f);
ImGui.End();
if (_demoWindowVisible) if (_demoWindowVisible)
{ {
+1 -1
View File
@@ -15,7 +15,7 @@
<PackageReference Include="Hexa.NET.ImGui" Version="2.2.9" /> <PackageReference Include="Hexa.NET.ImGui" Version="2.2.9" />
<PackageReference Include="Hexa.NET.ImGui.Widgets" Version="1.2.18" /> <PackageReference Include="Hexa.NET.ImGui.Widgets" Version="1.2.18" />
<PackageReference Include="Hexa.NET.ImPlot" Version="2.2.9" /> <PackageReference Include="Hexa.NET.ImPlot" Version="2.2.9" />
<PackageReference Include="Hexa.NET.OpenGL3" Version="1.1.0" /> <PackageReference Include="Hexa.NET.OpenGL4" Version="1.1.0" />
<PackageReference Include="Hexa.NET.Utilities" Version="2.2.12" /> <PackageReference Include="Hexa.NET.Utilities" Version="2.2.12" />
<PackageReference Include="SDL3-CS" Version="3.2.18" /> <PackageReference Include="SDL3-CS" Version="3.2.18" />
<PackageReference Include="SDL3-CS.Native" Version="3.2.18" /> <PackageReference Include="SDL3-CS.Native" Version="3.2.18" />
+351 -300
View File
File diff suppressed because it is too large Load Diff
+18 -10
View File
@@ -10,6 +10,8 @@ public unsafe static class ImGuiSDL3Renderer
{ {
public class RendererData public class RendererData
{ {
public GL Gl = null!;
public uint GlVersion; // e.g. 320 for GL 3.2 public uint GlVersion; // e.g. 320 for GL 3.2
public string GlslVersionString = ""; // e.g. "#version 330 core\n" public string GlslVersionString = ""; // e.g. "#version 330 core\n"
public bool GlProfileIsES2; public bool GlProfileIsES2;
@@ -35,8 +37,6 @@ public unsafe static class ImGuiSDL3Renderer
public bool UseTexParameterToSetSampler; public bool UseTexParameterToSetSampler;
public int NextSampler; // GL_LINEAR or GL_NEAREST public int NextSampler; // GL_LINEAR or GL_NEAREST
public GL Gl = null!;
} }
public static RendererData? GetRendererData() => ImGui.GetCurrentContext().Handle != null ? ImGuiUserData<RendererData>.Get(ImGui.GetIO().BackendRendererUserData) : null; public static RendererData? GetRendererData() => ImGui.GetCurrentContext().Handle != null ? ImGuiUserData<RendererData>.Get(ImGui.GetIO().BackendRendererUserData) : null;
@@ -143,7 +143,6 @@ public unsafe static class ImGuiSDL3Renderer
io.BackendRendererName = null; io.BackendRendererName = null;
io.BackendRendererUserData = null; io.BackendRendererUserData = null;
io.BackendFlags &= ~(ImGuiBackendFlags.RendererHasVtxOffset | ImGuiBackendFlags.RendererHasTextures | ImGuiBackendFlags.RendererHasViewports); io.BackendFlags &= ~(ImGuiBackendFlags.RendererHasVtxOffset | ImGuiBackendFlags.RendererHasTextures | ImGuiBackendFlags.RendererHasViewports);
} }
public static void NewFrame() public static void NewFrame()
@@ -222,7 +221,6 @@ public unsafe static class ImGuiSDL3Renderer
int idxSize = drawList.IdxBuffer.Size * sizeof(ushort); int idxSize = drawList.IdxBuffer.Size * sizeof(ushort);
gl.BufferData(GLBufferTargetARB.ArrayBuffer, vtxSize, drawList.VtxBuffer.Data, GLBufferUsageARB.StreamDraw); gl.BufferData(GLBufferTargetARB.ArrayBuffer, vtxSize, drawList.VtxBuffer.Data, GLBufferUsageARB.StreamDraw);
gl.BufferData(GLBufferTargetARB.ElementArrayBuffer, idxSize, drawList.IdxBuffer.Data, GLBufferUsageARB.StreamDraw); gl.BufferData(GLBufferTargetARB.ElementArrayBuffer, idxSize, drawList.IdxBuffer.Data, GLBufferUsageARB.StreamDraw);
for (int cmdIdx = 0; cmdIdx < drawList.CmdBuffer.Size; cmdIdx++) for (int cmdIdx = 0; cmdIdx < drawList.CmdBuffer.Size; cmdIdx++)
@@ -234,11 +232,17 @@ public unsafe static class ImGuiSDL3Renderer
nint cbPtr = (nint)cmd.UserCallback; nint cbPtr = (nint)cmd.UserCallback;
if (cbPtr == (nint)DelegateStorage.DrawCallbackResetRenderStateDelegate.GetPtrForDelegate()) if (cbPtr == (nint)DelegateStorage.DrawCallbackResetRenderStateDelegate.GetPtrForDelegate())
{
SetupRenderState(drawData, fbWidth, fbHeight, vao); SetupRenderState(drawData, fbWidth, fbHeight, vao);
}
else if (cbPtr == (nint)DelegateStorage.DrawCallbackSetSamplerLinearDelegate.GetPtrForDelegate()) else if (cbPtr == (nint)DelegateStorage.DrawCallbackSetSamplerLinearDelegate.GetPtrForDelegate())
{
ApplySamplerLinear(bd); ApplySamplerLinear(bd);
}
else if (cbPtr == (nint)DelegateStorage.DrawCallbackSetSamplerNearestDelegate.GetPtrForDelegate()) else if (cbPtr == (nint)DelegateStorage.DrawCallbackSetSamplerNearestDelegate.GetPtrForDelegate())
{
ApplySamplerNearest(bd); ApplySamplerNearest(bd);
}
else else
{ {
ImDrawCmdPtr cmdPtr = new ImDrawCmdPtr { Handle = &cmd }; ImDrawCmdPtr cmdPtr = new ImDrawCmdPtr { Handle = &cmd };
@@ -352,7 +356,7 @@ public unsafe static class ImGuiSDL3Renderer
2.0f / (R - L), 0.0f, 0.0f, 0.0f, 2.0f / (R - L), 0.0f, 0.0f, 0.0f,
0.0f, 2.0f / (T - B), 0.0f, 0.0f, 0.0f, 2.0f / (T - B), 0.0f, 0.0f,
0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f,
(R + L) / (L - R), (T + B) / (B - T), 0.0f, 1.0f, (R + L) / (L - R), (T + B) / (B - T), 0.0f, 1.0f
}; };
gl.UseProgram(bd.ShaderHandle); gl.UseProgram(bd.ShaderHandle);
@@ -467,7 +471,7 @@ public unsafe static class ImGuiSDL3Renderer
< 130 => (vertSrc120, fragSrc120), < 130 => (vertSrc120, fragSrc120),
300 => (vertSrc300es, fragSrc300es), 300 => (vertSrc300es, fragSrc300es),
>= 410 => (vertSrc410, fragSrc410), >= 410 => (vertSrc410, fragSrc410),
_ => (vertSrc130, fragSrc130), _ => (vertSrc130, fragSrc130)
}; };
string fullVert = bd.GlslVersionString + vertSrc; string fullVert = bd.GlslVersionString + vertSrc;
@@ -577,7 +581,10 @@ public unsafe static class ImGuiSDL3Renderer
private static void ApplySamplerLinear(RendererData bd) private static void ApplySamplerLinear(RendererData bd)
{ {
if (bd.HasBindSampler) bd.Gl.BindSampler(0, bd.TexSamplers[0]); if (bd.HasBindSampler)
{
bd.Gl.BindSampler(0, bd.TexSamplers[0]);
}
else else
{ {
bd.UseTexParameterToSetSampler = true; bd.UseTexParameterToSetSampler = true;
@@ -587,7 +594,10 @@ public unsafe static class ImGuiSDL3Renderer
private static void ApplySamplerNearest(RendererData bd) private static void ApplySamplerNearest(RendererData bd)
{ {
if (bd.HasBindSampler) bd.Gl.BindSampler(0, bd.TexSamplers[1]); if (bd.HasBindSampler)
{
bd.Gl.BindSampler(0, bd.TexSamplers[1]);
}
else else
{ {
bd.UseTexParameterToSetSampler = true; bd.UseTexParameterToSetSampler = true;
@@ -630,8 +640,6 @@ public unsafe static class ImGuiSDL3Renderer
public static class DelegateStorage public static class DelegateStorage
{ {
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal delegate void DrawCallbackFn(ImDrawListPtr drawList, ImDrawCmdPtr cmd); internal delegate void DrawCallbackFn(ImDrawListPtr drawList, ImDrawCmdPtr cmd);
+6 -3
View File
@@ -35,13 +35,16 @@ public sealed unsafe class SDL3Window : IDisposable
public SDL3Window(string name, int posX, int posY, int width, int height, SDL.WindowFlags flags = SDL.WindowFlags.Resizable | SDL.WindowFlags.HighPixelDensity) public SDL3Window(string name, int posX, int posY, int width, int height, SDL.WindowFlags flags = SDL.WindowFlags.Resizable | SDL.WindowFlags.HighPixelDensity)
{ {
if (OperatingSystem.IsLinux())
SDL.SetHint(SDL.Hints.VideoDriver, "x11");
if (!SDL.Init(SDL.InitFlags.Events | SDL.InitFlags.Video | SDL.InitFlags.Gamepad)) if (!SDL.Init(SDL.InitFlags.Events | SDL.InitFlags.Video | SDL.InitFlags.Gamepad))
throw new Exception($"SDL_Init failed: {SDL.GetError()}"); throw new Exception($"SDL_Init failed: {SDL.GetError()}");
SDL.GLSetAttribute(SDL.GLAttr.ContextFlags, (int)SDL.GLContextFlag.ForwardCompatible); SDL.GLSetAttribute(SDL.GLAttr.ContextFlags, (int)SDL.GLContextFlag.ForwardCompatible);
SDL.GLSetAttribute(SDL.GLAttr.ContextProfileMask, (int)SDL.GLProfile.Core); SDL.GLSetAttribute(SDL.GLAttr.ContextProfileMask, (int)SDL.GLProfile.Core);
SDL.GLSetAttribute(SDL.GLAttr.ContextMajorVersion, 3); SDL.GLSetAttribute(SDL.GLAttr.ContextMajorVersion, 4);
SDL.GLSetAttribute(SDL.GLAttr.ContextMinorVersion, 3); SDL.GLSetAttribute(SDL.GLAttr.ContextMinorVersion, 6);
SDL.SetHint(SDL.Hints.IMEImplementedUI, "1"); SDL.SetHint(SDL.Hints.IMEImplementedUI, "1");
@@ -116,7 +119,7 @@ public sealed unsafe class SDL3Window : IDisposable
_gl = new GL(new BindingsContext(Window, glContext)); _gl = new GL(new BindingsContext(Window, glContext));
// Init platform and renderer // Init platform and renderer
ImGuiSDL3Platform.Init(_gl, glContext, Window, this); ImGuiSDL3Platform.Init(_gl, Window, glContext);
ImGuiSDL3Renderer.Init(_gl, "#version 330"); ImGuiSDL3Renderer.Init(_gl, "#version 330");
} }