Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 96e90a09d9 | |||
| 519b06367a | |||
| 2970fdc3c6 | |||
| 221cfa5e9c | |||
| 7167d98cb6 |
+84
-54
@@ -2,15 +2,14 @@
|
|||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using Hexa.NET.ImGui;
|
using Hexa.NET.ImGui;
|
||||||
using Hexa.NET.ImPlot;
|
using Hexa.NET.ImPlot;
|
||||||
using Hexa.NET.ImPlot3D;
|
|
||||||
using SDL3_TestingSuite.SDL3;
|
using SDL3_TestingSuite.SDL3;
|
||||||
using SDL3;
|
|
||||||
|
|
||||||
namespace SDL3_TestingSuite;
|
namespace SDL3_TestingSuite;
|
||||||
|
|
||||||
public class Program
|
public class Program
|
||||||
{
|
{
|
||||||
private static bool _demoWindowVisible = true;
|
private static bool _demoWindowVisible = true;
|
||||||
|
private static bool _imPlotDemoVisible;
|
||||||
private static bool _fontStuff;
|
private static bool _fontStuff;
|
||||||
private static readonly Dictionary<string, List<uint>?> GlyphsByName = new Dictionary<string, List<uint>?>();
|
private static readonly Dictionary<string, List<uint>?> GlyphsByName = new Dictionary<string, List<uint>?>();
|
||||||
private static bool _initialized;
|
private static bool _initialized;
|
||||||
@@ -19,38 +18,40 @@ public class Program
|
|||||||
|
|
||||||
public static void Main()
|
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);
|
||||||
SDL3Window window = new SDL3Window("SDL3 Testing Suite", 100, 100, 1280, 720, flags);
|
window.RenderCallback += () =>
|
||||||
window.ClearColor.W = 0f;
|
|
||||||
window.RenderCallback = () =>
|
|
||||||
{
|
{
|
||||||
ImGuiViewportPtr viewport = ImGui.GetMainViewport();
|
ImGuiViewportPtr viewport = ImGui.GetMainViewport();
|
||||||
ImGui.SetNextWindowPos(viewport.WorkPos);
|
ImGui.SetNextWindowPos(viewport.WorkPos);
|
||||||
ImGui.SetNextWindowSize(viewport.WorkSize);
|
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.Begin("MainWindow", ImGuiWindowFlags.NoBackground | ImGuiWindowFlags.MenuBar | ImGuiWindowFlags.NoDocking | ImGuiWindowFlags.NoTitleBar | ImGuiWindowFlags.NoResize | ImGuiWindowFlags.NoMove | ImGuiWindowFlags.NoCollapse | ImGuiWindowFlags.NoBringToFrontOnFocus))
|
||||||
|
|
||||||
if (ImGui.BeginMenuBar())
|
|
||||||
{
|
{
|
||||||
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.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.EndMenu();
|
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)
|
if (_fontStuff)
|
||||||
{
|
{
|
||||||
ImGui.SetNextWindowSize(new Vector2(250, 500), ImGuiCond.FirstUseEver);
|
ImGui.SetNextWindowSize(new Vector2(250, 500), ImGuiCond.FirstUseEver);
|
||||||
@@ -69,7 +70,7 @@ public class Program
|
|||||||
fontName = string.IsNullOrEmpty(fontName) ? _font.FontId.ToString() : fontName;
|
fontName = string.IsNullOrEmpty(fontName) ? _font.FontId.ToString() : fontName;
|
||||||
if (!_initialized || change)
|
if (!_initialized || change)
|
||||||
{
|
{
|
||||||
if (!GlyphsByName.TryGetValue(fontName, out List<uint> glyphs))
|
if (!GlyphsByName.TryGetValue(fontName, out List<uint>? glyphs))
|
||||||
{
|
{
|
||||||
glyphs = new List<uint>();
|
glyphs = new List<uint>();
|
||||||
|
|
||||||
@@ -96,12 +97,12 @@ public class Program
|
|||||||
|
|
||||||
ImGui.PushFont(null, 24);
|
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 cursorXStart = ImGui.GetCursorPosX();
|
||||||
float maxWidth = ImGui.GetContentRegionAvail().X;
|
float maxWidth = ImGui.GetContentRegionAvail().X;
|
||||||
|
|
||||||
foreach (uint codepoint in glyphs2)
|
foreach (uint codepoint in glyphs2!)
|
||||||
{
|
{
|
||||||
string text = char.ConvertFromUtf32((int)codepoint);
|
string text = char.ConvertFromUtf32((int)codepoint);
|
||||||
float glyphWidth = ImGui.CalcTextSize(text).X;
|
float glyphWidth = ImGui.CalcTextSize(text).X;
|
||||||
@@ -124,51 +125,63 @@ public class Program
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
float time = (float)ImGui.GetTime() * 5;
|
if (ImGui.Begin("Thing"))
|
||||||
if (ImPlot.BeginPlot("Moving Rainbow Sine Wave"))
|
|
||||||
{
|
{
|
||||||
int count = 200;
|
float time = (float)ImGui.GetTime() * 5;
|
||||||
|
if (ImPlot.BeginPlot("Moving Rainbow Sine Wave"))
|
||||||
float[] xs = new float[2];
|
|
||||||
float[] ys = new float[2];
|
|
||||||
|
|
||||||
for (int i = 0; i < count - 1; i++)
|
|
||||||
{
|
{
|
||||||
float x0 = i * 0.1f;
|
int count = 200;
|
||||||
float x1 = (i + 1) * 0.1f;
|
|
||||||
|
|
||||||
float y0 = MathF.Sin(x0 * size + time);
|
float[] xs = new float[2];
|
||||||
float y1 = MathF.Sin(x1 * size + time);
|
float[] ys = new float[2];
|
||||||
|
|
||||||
xs[0] = x0;
|
for (int i = 0; i < count - 1; i++)
|
||||||
xs[1] = x1;
|
{
|
||||||
|
float x0 = i * 0.1f;
|
||||||
|
float x1 = (i + 1) * 0.1f;
|
||||||
|
|
||||||
ys[0] = y0;
|
float y0 = MathF.Sin(x0 * size + time);
|
||||||
ys[1] = y1;
|
float y1 = MathF.Sin(x1 * size + time);
|
||||||
|
|
||||||
float t = i / (float)count;
|
xs[0] = x0;
|
||||||
|
xs[1] = x1;
|
||||||
|
|
||||||
float r = 0.5f + 1f * MathF.Sin(6.2831f * (t));
|
ys[0] = y0;
|
||||||
float g = 0.5f + 1f * MathF.Sin(6.2831f * (t + 0.33f));
|
ys[1] = y1;
|
||||||
float b = 0.5f + 1f * MathF.Sin(6.2831f * (t + 0.66f));
|
|
||||||
|
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.PushStyleColor(ImPlotCol.Line, new Vector4(r, g, b, 1f));
|
||||||
ImPlot.PlotLine("##seg", ref xs[0], ref ys[0], 2);
|
ImPlot.PlotLine("##seg", ref xs[0], ref ys[0], 2);
|
||||||
ImPlot.PopStyleColor();
|
ImPlot.PopStyleColor();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
ImPlot.EndPlot();
|
||||||
}
|
}
|
||||||
|
ImGui.SliderFloat("Sine", ref size, 1f, 120f);
|
||||||
|
|
||||||
ImPlot.EndPlot();
|
ImGui.End();
|
||||||
}
|
}
|
||||||
|
|
||||||
ImGui.SliderFloat("Sine", ref size, 1f, 120f);
|
if (ImGui.Begin("ImageThing"))
|
||||||
|
{
|
||||||
|
|
||||||
|
ImGui.End();
|
||||||
|
}
|
||||||
|
|
||||||
if (_demoWindowVisible)
|
if (_demoWindowVisible)
|
||||||
{
|
{
|
||||||
ImGui.ShowDemoWindow(ref _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 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}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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.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="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" />
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
+4
-6
@@ -29,8 +29,8 @@ public static class FontFind
|
|||||||
FileInfo info = new FileInfo(fontPath);
|
FileInfo info = new FileInfo(fontPath);
|
||||||
if (!info.Exists || info.Length <= 0) return null;
|
if (!info.Exists || info.Length <= 0) return null;
|
||||||
|
|
||||||
FontMetrics metrics = FontFind.ReadFontMetrics(fontPath);
|
FontMetrics metrics = ReadFontMetrics(fontPath);
|
||||||
float size = FontFind.GetRecommendedPixelSize(metrics);
|
float size = GetRecommendedPixelSize(metrics);
|
||||||
|
|
||||||
ImFontConfigPtr config = ImGui.ImFontConfig();
|
ImFontConfigPtr config = ImGui.ImFontConfig();
|
||||||
config.FontLoaderFlags |= (uint)ImGuiFreeTypeLoaderFlags.LoadColor;
|
config.FontLoaderFlags |= (uint)ImGuiFreeTypeLoaderFlags.LoadColor;
|
||||||
@@ -110,11 +110,9 @@ public static class FontFind
|
|||||||
metrics.WinDescent = ReadS16Be(os2Offset + 76);
|
metrics.WinDescent = ReadS16Be(os2Offset + 76);
|
||||||
}
|
}
|
||||||
|
|
||||||
metrics.TypoLineHeight =
|
metrics.TypoLineHeight = metrics.TypoAscender - metrics.TypoDescender + metrics.TypoLineGap;
|
||||||
metrics.TypoAscender - metrics.TypoDescender + metrics.TypoLineGap;
|
|
||||||
|
|
||||||
metrics.WinLineHeight =
|
metrics.WinLineHeight = metrics.WinAscent + metrics.WinDescent;
|
||||||
metrics.WinAscent + metrics.WinDescent;
|
|
||||||
|
|
||||||
return metrics;
|
return metrics;
|
||||||
|
|
||||||
|
|||||||
+562
-560
File diff suppressed because it is too large
Load Diff
+578
-337
File diff suppressed because it is too large
Load Diff
@@ -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
@@ -1,12 +1,9 @@
|
|||||||
using System;
|
using System.Numerics;
|
||||||
using System.Diagnostics;
|
|
||||||
using System.Numerics;
|
|
||||||
using System.Runtime.InteropServices;
|
|
||||||
using Hexa.NET.ImGui;
|
using Hexa.NET.ImGui;
|
||||||
using Hexa.NET.ImGui.Utilities;
|
|
||||||
using Hexa.NET.ImGuizmo;
|
using Hexa.NET.ImGuizmo;
|
||||||
using Hexa.NET.ImPlot;
|
using Hexa.NET.ImPlot;
|
||||||
using Hexa.NET.ImPlot3D;
|
using Hexa.NET.OpenGL;
|
||||||
|
using HexaGen.Runtime;
|
||||||
using SDL3;
|
using SDL3;
|
||||||
|
|
||||||
namespace SDL3_TestingSuite.SDL3;
|
namespace SDL3_TestingSuite.SDL3;
|
||||||
@@ -14,49 +11,70 @@ namespace SDL3_TestingSuite.SDL3;
|
|||||||
public sealed unsafe class SDL3Window : IDisposable
|
public sealed unsafe class SDL3Window : IDisposable
|
||||||
{
|
{
|
||||||
public readonly nint Window;
|
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();
|
public event Action? RenderCallback;
|
||||||
private TimeSpan _time = TimeSpan.Zero;
|
|
||||||
|
|
||||||
private SDL.Rect _screenClipRect;
|
|
||||||
private ImGuiContextPtr _imGuiContext;
|
private ImGuiContextPtr _imGuiContext;
|
||||||
|
private ImPlotContextPtr _imPlotContext;
|
||||||
|
|
||||||
public bool Disposed => _disposed;
|
public bool Disposed => _disposed;
|
||||||
private bool _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)
|
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))
|
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.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
|
// 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()}");
|
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.SetWindowPosition(Window, posX, posY);
|
||||||
SDL.SetRenderVSync(Renderer, 1);
|
// SDL.SetRenderVSync(Renderer, 1);
|
||||||
SDL.ShowWindow(Window);
|
SDL.ShowWindow(Window);
|
||||||
|
|
||||||
// Create ImGui context
|
// Create ImGui context
|
||||||
var context = ImGui.CreateContext();
|
_imGuiContext = ImGui.CreateContext();
|
||||||
ImPlot.CreateContext();
|
_imPlotContext = ImPlot.CreateContext();
|
||||||
ImPlot.SetImGuiContext(context);
|
ImPlot.SetImGuiContext(_imGuiContext);
|
||||||
ImGuizmo.SetImGuiContext(context);
|
ImGuizmo.SetImGuiContext(_imGuiContext);
|
||||||
// ImPlot3D.SetImGuiContext(context);
|
|
||||||
// ImPlot3D.CreateContext();
|
|
||||||
|
|
||||||
ImGuiIOPtr io = ImGui.GetIO();
|
ImGuiIOPtr io = ImGui.GetIO();
|
||||||
io.ConfigFlags |= ImGuiConfigFlags.NavEnableKeyboard | ImGuiConfigFlags.NavEnableGamepad | ImGuiConfigFlags.DockingEnable;
|
io.ConfigFlags |= ImGuiConfigFlags.NavEnableKeyboard | ImGuiConfigFlags.NavEnableGamepad | ImGuiConfigFlags.DockingEnable;
|
||||||
// io.ConfigFlags |= ImGuiConfigFlags.ViewportsEnable;
|
io.ConfigFlags |= ImGuiConfigFlags.ViewportsEnable;
|
||||||
io.Fonts.Flags |= ImFontAtlasFlags.NoBakedLines;
|
|
||||||
|
|
||||||
|
io.Fonts.Flags |= ImFontAtlasFlags.NoBakedLines;
|
||||||
io.Fonts.AddFontDefault();
|
io.Fonts.AddFontDefault();
|
||||||
|
|
||||||
try
|
try
|
||||||
@@ -66,7 +84,7 @@ public sealed unsafe class SDL3Window : IDisposable
|
|||||||
|
|
||||||
_watcher = new FileSystemWatcher(fontsPath);
|
_watcher = new FileSystemWatcher(fontsPath);
|
||||||
_watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.CreationTime;
|
_watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.CreationTime;
|
||||||
_watcher.Created += (a, b) =>
|
_watcher.Created += (_, b) =>
|
||||||
{
|
{
|
||||||
if (!File.Exists(b.FullPath)) return;
|
if (!File.Exists(b.FullPath)) return;
|
||||||
|
|
||||||
@@ -74,7 +92,7 @@ public sealed unsafe class SDL3Window : IDisposable
|
|||||||
|
|
||||||
ImGui.GetIO().AddFont(b.FullPath);
|
ImGui.GetIO().AddFont(b.FullPath);
|
||||||
};
|
};
|
||||||
_watcher.Deleted += (a, b) =>
|
_watcher.Deleted += (_, b) =>
|
||||||
{
|
{
|
||||||
if (Path.GetExtension(b.FullPath) != ".ttf") return;
|
if (Path.GetExtension(b.FullPath) != ".ttf") return;
|
||||||
|
|
||||||
@@ -95,25 +113,66 @@ public sealed unsafe class SDL3Window : IDisposable
|
|||||||
Console.WriteLine(e);
|
Console.WriteLine(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_gl = new GL(new BindingsContext(Window, glContext));
|
||||||
|
|
||||||
// Init platform and renderer
|
// Init platform and renderer
|
||||||
ImGuiSDL3Platform.Init(Window, Renderer);
|
ImGuiSDL3Platform.Init(_gl, glContext, Window, this);
|
||||||
ImGuiSDL3Renderer.Init(Renderer);
|
ImGuiSDL3Renderer.Init(_gl, "#version 330");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool ShouldClose;
|
||||||
|
|
||||||
public void Run()
|
public void Run()
|
||||||
{
|
{
|
||||||
while (!_disposed)
|
while (!_disposed)
|
||||||
{
|
{
|
||||||
ImGui.GetIO().DeltaTime = (float)(_timer.Elapsed - _time).TotalSeconds;
|
SDL.PumpEvents();
|
||||||
_time = _timer.Elapsed;
|
|
||||||
|
|
||||||
PollEvents();
|
if (ImGui.GetIO().WantTextInput && !SDL.TextInputActive(Window))
|
||||||
|
SDL.StartTextInput(Window);
|
||||||
|
else if (!ImGui.GetIO().WantTextInput && SDL.TextInputActive(Window))
|
||||||
|
SDL.StopTextInput(Window);
|
||||||
|
|
||||||
Update();
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_gl.MakeCurrent();
|
||||||
|
_gl.ClearColor(ClearColor.X, ClearColor.Y, ClearColor.Z, ClearColor.W);
|
||||||
|
_gl.Clear(GLClearBufferMask.ColorBufferBit);
|
||||||
|
|
||||||
|
ImGuiSDL3Platform.NewFrame();
|
||||||
|
ImGuiSDL3Renderer.NewFrame();
|
||||||
|
ImGui.NewFrame();
|
||||||
|
|
||||||
RenderCallback?.Invoke();
|
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)
|
if (ShouldClose)
|
||||||
{
|
{
|
||||||
@@ -126,56 +185,6 @@ public sealed unsafe class SDL3Window : IDisposable
|
|||||||
Dispose();
|
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()
|
~SDL3Window()
|
||||||
{
|
{
|
||||||
Dispose(false);
|
Dispose(false);
|
||||||
@@ -194,6 +203,15 @@ public sealed unsafe class SDL3Window : IDisposable
|
|||||||
|
|
||||||
if (disposing)
|
if (disposing)
|
||||||
{
|
{
|
||||||
|
if (_imGuiContext.Handle != null)
|
||||||
|
{
|
||||||
|
ImGui.SetCurrentContext(_imGuiContext);
|
||||||
|
}
|
||||||
|
if (_imPlotContext.Handle != null)
|
||||||
|
{
|
||||||
|
ImPlot.SetCurrentContext(_imPlotContext);
|
||||||
|
}
|
||||||
|
|
||||||
ImGuiSDL3Renderer.Dispose();
|
ImGuiSDL3Renderer.Dispose();
|
||||||
ImGuiSDL3Platform.Dispose();
|
ImGuiSDL3Platform.Dispose();
|
||||||
|
|
||||||
@@ -206,12 +224,18 @@ public sealed unsafe class SDL3Window : IDisposable
|
|||||||
ImGui.DestroyContext(_imGuiContext);
|
ImGui.DestroyContext(_imGuiContext);
|
||||||
_imGuiContext = null;
|
_imGuiContext = null;
|
||||||
}
|
}
|
||||||
|
if (_imPlotContext.Handle != null)
|
||||||
if (Renderer != nint.Zero)
|
|
||||||
{
|
{
|
||||||
SDL.DestroyRenderer(Renderer);
|
ImPlot.SetCurrentContext(null);
|
||||||
|
ImPlot.DestroyContext(_imPlotContext);
|
||||||
|
_imPlotContext = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if (Renderer != nint.Zero)
|
||||||
|
// {
|
||||||
|
// SDL.DestroyRenderer(Renderer);
|
||||||
|
// }
|
||||||
|
|
||||||
if (Window != nint.Zero)
|
if (Window != nint.Zero)
|
||||||
{
|
{
|
||||||
SDL.DestroyWindow(Window);
|
SDL.DestroyWindow(Window);
|
||||||
@@ -219,3 +243,52 @@ public sealed unsafe class SDL3Window : IDisposable
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user