some cleanup
This commit is contained in:
+2
-2
@@ -18,7 +18,7 @@ public class Program
|
||||
public static void Main()
|
||||
{
|
||||
SDL3Window window = new SDL3Window("SDL3 Testing Suite", 100, 100, 1280, 720);
|
||||
window.RenderCallback = () =>
|
||||
window.RenderCallback += () =>
|
||||
{
|
||||
ImGuiViewportPtr viewport = ImGui.GetMainViewport();
|
||||
ImGui.SetNextWindowPos(viewport.WorkPos);
|
||||
@@ -65,7 +65,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>();
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
+6
-8
@@ -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)
|
||||
|
||||
+438
-507
File diff suppressed because it is too large
Load Diff
+200
-178
@@ -1,4 +1,6 @@
|
||||
using System.Diagnostics;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using Hexa.NET.ImGui;
|
||||
using SDL3;
|
||||
@@ -11,8 +13,6 @@ namespace SDL3_TestingSuite.SDL3;
|
||||
/// </summary>
|
||||
public unsafe static class ImGuiSDL3Renderer
|
||||
{
|
||||
public static SDL3Window Parent;
|
||||
|
||||
public class RendererData
|
||||
{
|
||||
public nint Renderer; // Main viewport's renderer
|
||||
@@ -25,34 +25,15 @@ public unsafe static class ImGuiSDL3Renderer
|
||||
private struct BackupSDLRendererState
|
||||
{
|
||||
public SDL.Rect Viewport;
|
||||
public bool ViewportEnabled;
|
||||
public bool ClipEnabled;
|
||||
public SDL.Rect ClipRect;
|
||||
}
|
||||
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
private delegate void RendererCreateWindowFn(ImGuiViewportPtr viewport);
|
||||
public static RendererData GetRendererData() => ImGui.GetCurrentContext().Handle != null ? ImGuiUserData<RendererData>.Get(ImGui.GetIO().BackendRendererUserData)! : null!;
|
||||
|
||||
private static readonly RendererCreateWindowFn RendererCreateWindowDelegate = RendererCreateWindow;
|
||||
public static SDL3Window? Parent;
|
||||
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
private delegate void RendererDestroyWindowFn(ImGuiViewportPtr viewport);
|
||||
|
||||
private static readonly RendererDestroyWindowFn RendererDestroyWindowDelegate = RendererDestroyWindow;
|
||||
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
private delegate void RendererRenderWindowFn(ImGuiViewportPtr viewport);
|
||||
|
||||
private static readonly RendererRenderWindowFn RendererRenderWindowDelegate = RendererRenderWindow;
|
||||
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
private delegate void RendererSwapBuffersFn(ImGuiViewportPtr viewport);
|
||||
|
||||
private static readonly RendererSwapBuffersFn RendererSwapBuffersDelegate = RendererSwapBuffers;
|
||||
|
||||
public static RendererData Data => ImGui.GetCurrentContext().Handle != null ? ImGuiUserData<RendererData>.Get(ImGui.GetIO().BackendRendererUserData)! : null!;
|
||||
|
||||
public static bool Init(nint renderer, SDL3Window parentWindow = null)
|
||||
public static bool Init(nint renderer, SDL3Window? parentWindow = null)
|
||||
{
|
||||
Parent = parentWindow;
|
||||
ImGuiIOPtr io = ImGui.GetIO();
|
||||
@@ -67,88 +48,45 @@ public unsafe static class ImGuiSDL3Renderer
|
||||
bd.Renderer = renderer;
|
||||
|
||||
ImGuiPlatformIOPtr platformIO = ImGui.GetPlatformIO();
|
||||
platformIO.RendererCreateWindow = (void*)Marshal.GetFunctionPointerForDelegate(RendererCreateWindowDelegate);
|
||||
platformIO.RendererDestroyWindow = (void*)Marshal.GetFunctionPointerForDelegate(RendererDestroyWindowDelegate);
|
||||
platformIO.RendererRenderWindow = (void*)Marshal.GetFunctionPointerForDelegate(RendererRenderWindowDelegate);
|
||||
platformIO.RendererSwapBuffers = (void*)Marshal.GetFunctionPointerForDelegate(RendererSwapBuffersDelegate);
|
||||
platformIO.RendererCreateWindow = DelegateStorage.RendererCreateWindowDelegate.GetPtrForDelegate();
|
||||
platformIO.RendererDestroyWindow = DelegateStorage.RendererDestroyWindowDelegate.GetPtrForDelegate();
|
||||
platformIO.RendererRenderWindow = DelegateStorage.RendererRenderWindowDelegate.GetPtrForDelegate();
|
||||
platformIO.RendererSwapBuffers = DelegateStorage.RendererSwapBuffersDelegate.GetPtrForDelegate();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void Dispose()
|
||||
{
|
||||
var io = ImGui.GetIO();
|
||||
var platformIO = ImGui.GetPlatformIO();
|
||||
ImGuiIOPtr io = ImGui.GetIO();
|
||||
ImGuiPlatformIOPtr platformIO = ImGui.GetPlatformIO();
|
||||
|
||||
DestroyDeviceObjects();
|
||||
if (io.BackendRendererName != null)
|
||||
{
|
||||
Marshal.FreeHGlobal((nint)io.BackendRendererName);
|
||||
}
|
||||
|
||||
ImGuiUserData<RendererData>.Free(io.BackendRendererUserData);
|
||||
io.BackendRendererName = null;
|
||||
io.BackendRendererUserData = null;
|
||||
io.BackendFlags &= ~(ImGuiBackendFlags.RendererHasVtxOffset | ImGuiBackendFlags.RendererHasTextures);
|
||||
io.BackendFlags &= ~(ImGuiBackendFlags.RendererHasVtxOffset | ImGuiBackendFlags.RendererHasTextures | ImGuiBackendFlags.RendererHasViewports);
|
||||
platformIO.RendererTextureMaxWidth = 0;
|
||||
platformIO.RendererTextureMaxHeight = 0;
|
||||
platformIO.RendererRenderState = null;
|
||||
platformIO.RendererCreateWindow = null;
|
||||
platformIO.RendererDestroyWindow = null;
|
||||
platformIO.RendererSetWindowSize = null;
|
||||
platformIO.RendererRenderWindow = null;
|
||||
platformIO.RendererSwapBuffers = null;
|
||||
}
|
||||
|
||||
public static void NewFrame() { }
|
||||
|
||||
private static void RendererCreateWindow(ImGuiViewportPtr viewport)
|
||||
public static void SetupRenderState(nint renderer)
|
||||
{
|
||||
ImGuiSDL3Platform.ViewPortData? vd = ImGuiUserData<ImGuiSDL3Platform.ViewPortData>.Get(viewport.PlatformUserData);
|
||||
if (vd == null || vd.Window == nint.Zero)
|
||||
return;
|
||||
|
||||
if (vd.Renderer == nint.Zero)
|
||||
{
|
||||
vd.Renderer = SDL.CreateRenderer(vd.Window, null);
|
||||
vd.RendererOwned = true;
|
||||
}
|
||||
// Clear out any viewports and cliprect set by the user
|
||||
// FIXME: Technically speaking there are lots of other things we could backup/setup/restore during our render process.
|
||||
SDL.SetRenderViewport(renderer, nint.Zero);
|
||||
SDL.SetRenderClipRect(renderer, nint.Zero);
|
||||
}
|
||||
|
||||
private static void RendererDestroyWindow(ImGuiViewportPtr viewport)
|
||||
public static void NewFrame()
|
||||
{
|
||||
ImGuiSDL3Platform.ViewPortData? vd = ImGuiUserData<ImGuiSDL3Platform.ViewPortData>.Get(viewport.PlatformUserData);
|
||||
if (vd == null)
|
||||
return;
|
||||
|
||||
if (vd.RendererOwned && vd.Renderer != nint.Zero)
|
||||
{
|
||||
SDL.DestroyRenderer(vd.Renderer);
|
||||
}
|
||||
|
||||
vd.Renderer = nint.Zero;
|
||||
vd.RendererOwned = false;
|
||||
}
|
||||
|
||||
private static void RendererRenderWindow(ImGuiViewportPtr viewport)
|
||||
{
|
||||
ImGuiSDL3Platform.ViewPortData? vd = ImGuiUserData<ImGuiSDL3Platform.ViewPortData>.Get(viewport.PlatformUserData);
|
||||
if (vd == null || vd.Renderer == nint.Zero)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (Parent != null)
|
||||
{
|
||||
SDL.SetRenderDrawColorFloat(vd.Renderer, Parent.ClearColor.X, Parent.ClearColor.Y, Parent.ClearColor.Z, 0f);
|
||||
SDL.RenderClear(vd.Renderer);
|
||||
}
|
||||
|
||||
RenderDrawData(viewport.DrawData, vd.Renderer);
|
||||
}
|
||||
|
||||
private static void RendererSwapBuffers(ImGuiViewportPtr viewport)
|
||||
{
|
||||
ImGuiSDL3Platform.ViewPortData? vd = ImGuiUserData<ImGuiSDL3Platform.ViewPortData>.Get(viewport.PlatformUserData);
|
||||
if (vd == null || vd.Renderer == nint.Zero)
|
||||
return;
|
||||
|
||||
SDL.RenderPresent(vd.Renderer);
|
||||
RendererData bd = GetRendererData();
|
||||
Debug.Assert(bd != null, "Context or backend not initialized! Did you call ImGuiSDL3Renderer.Init()?");
|
||||
}
|
||||
|
||||
public static void RenderDrawData(ImDrawDataPtr drawData, nint renderer)
|
||||
@@ -167,22 +105,23 @@ public unsafe static class ImGuiSDL3Renderer
|
||||
|
||||
for (int i = 0; i < drawData.Textures.Size; i++)
|
||||
{
|
||||
var texture = drawData.Textures[i];
|
||||
UpdateTexture(texture, renderer);
|
||||
ImTextureDataPtr texture = drawData.Textures[i];
|
||||
if (texture.Handle != null)
|
||||
{
|
||||
UpdateTexture(texture, renderer);
|
||||
}
|
||||
}
|
||||
|
||||
// Backup SDL renderer state
|
||||
BackupSDLRendererState old = new BackupSDLRendererState
|
||||
{
|
||||
ViewportEnabled = SDL.RenderViewportSet(renderer),
|
||||
ClipEnabled = SDL.RenderClipEnabled(renderer)
|
||||
};
|
||||
SDL.GetRenderViewport(renderer, out old.Viewport);
|
||||
SDL.GetRenderClipRect(renderer, out old.ClipRect);
|
||||
|
||||
// Set up render state
|
||||
SDL.SetRenderViewport(renderer, nint.Zero);
|
||||
SDL.SetRenderClipRect(renderer, nint.Zero);
|
||||
SetupRenderState(renderer);
|
||||
|
||||
// Set render state in platform IO
|
||||
ImGuiPlatformIOPtr platformIo = ImGui.GetPlatformIO();
|
||||
@@ -193,45 +132,103 @@ public unsafe static class ImGuiSDL3Renderer
|
||||
// Render command lists
|
||||
for (int n = 0; n < drawData.CmdListsCount; n++)
|
||||
{
|
||||
ImDrawListPtr cmdList = drawData.CmdLists[n];
|
||||
ImDrawListPtr drawList = drawData.CmdLists[n];
|
||||
|
||||
for (int cmdIndex = 0; cmdIndex < cmdList.CmdBuffer.Size; cmdIndex++)
|
||||
for (int cmdIndex = 0; cmdIndex < drawList.CmdBuffer.Size; cmdIndex++)
|
||||
{
|
||||
ImDrawCmd cmd = cmdList.CmdBuffer[cmdIndex];
|
||||
ImDrawCmd cmd = drawList.CmdBuffer[cmdIndex];
|
||||
|
||||
if (cmd.UserCallback != null)
|
||||
{
|
||||
continue; // User callback not implemented
|
||||
if (cmd.UserCallback == DelegateStorage.DrawCallbackResetRenderStateDelegate.GetPtrForDelegate())
|
||||
{
|
||||
SetupRenderState(renderer);
|
||||
}
|
||||
else
|
||||
{
|
||||
// this is cursed af and idk if it even works :)
|
||||
ImDrawCmdPtr cmdPtr = new ImDrawCmdPtr
|
||||
{
|
||||
Handle = (ImDrawCmd*)Unsafe.AsPointer(ref cmd)
|
||||
};
|
||||
((delegate* unmanaged[Cdecl]<ImDrawListPtr, ImDrawCmdPtr, void>)cmd.UserCallback)(drawList, cmdPtr);
|
||||
}
|
||||
}
|
||||
|
||||
// Apply clipping rectangle
|
||||
Vector4 clipRect = cmd.ClipRect;
|
||||
Vector2 clipMin = new Vector2((clipRect.X - clipOffset.X) * renderScale.X, (clipRect.Y - clipOffset.Y) * renderScale.Y);
|
||||
Vector2 clipMax = new Vector2((clipRect.Z - clipOffset.X) * renderScale.X, (clipRect.W - clipOffset.Y) * renderScale.Y);
|
||||
|
||||
clipMin.X = Math.Max(0, clipMin.X);
|
||||
clipMin.Y = Math.Max(0, clipMin.Y);
|
||||
clipMax.X = Math.Min(fbWidth, clipMax.X);
|
||||
clipMax.Y = Math.Min(fbHeight, clipMax.Y);
|
||||
if (clipMax.X <= clipMin.X || clipMax.Y <= clipMin.Y)
|
||||
continue;
|
||||
|
||||
SDL.Rect r = new SDL.Rect
|
||||
else
|
||||
{
|
||||
X = (int)clipMin.X,
|
||||
Y = (int)clipMin.Y,
|
||||
W = (int)(clipMax.X - clipMin.X),
|
||||
H = (int)(clipMax.Y - clipMin.Y)
|
||||
};
|
||||
SDL.SetRenderClipRect(renderer, r);
|
||||
// Apply clipping rectangle
|
||||
Vector4 clipRect = cmd.ClipRect;
|
||||
Vector2 clipMin = new Vector2((clipRect.X - clipOffset.X) * renderScale.X, (clipRect.Y - clipOffset.Y) * renderScale.Y);
|
||||
Vector2 clipMax = new Vector2((clipRect.Z - clipOffset.X) * renderScale.X, (clipRect.W - clipOffset.Y) * renderScale.Y);
|
||||
|
||||
// Get texture
|
||||
nint texId = cmd.GetTexID();
|
||||
clipMin.X = Math.Max(0, clipMin.X);
|
||||
clipMin.Y = Math.Max(0, clipMin.Y);
|
||||
clipMax.X = Math.Min(fbWidth, clipMax.X);
|
||||
clipMax.Y = Math.Min(fbHeight, clipMax.Y);
|
||||
if (clipMax.X <= clipMin.X || clipMax.Y <= clipMin.Y)
|
||||
continue;
|
||||
|
||||
// Convert ImGui vertices to SDL vertices
|
||||
if (!RenderDrawCommand(cmdList, cmd, renderer, texId, renderScale, clipOffset))
|
||||
{
|
||||
Console.WriteLine($"Failed to render ImGui draw command: {SDL.GetError()}");
|
||||
SDL.Rect r = new SDL.Rect
|
||||
{
|
||||
X = (int)clipMin.X,
|
||||
Y = (int)clipMin.Y,
|
||||
W = (int)(clipMax.X - clipMin.X),
|
||||
H = (int)(clipMax.Y - clipMin.Y)
|
||||
};
|
||||
SDL.SetRenderClipRect(renderer, r);
|
||||
|
||||
// Convert ImGui vertices to SDL vertices
|
||||
uint indexOffset = cmd.IdxOffset;
|
||||
uint vertexOffset = cmd.VtxOffset;
|
||||
uint elemCount = cmd.ElemCount;
|
||||
|
||||
SDL.Vertex[] vertices = new SDL.Vertex[elemCount];
|
||||
int[] indices = new int[elemCount];
|
||||
|
||||
for (int i = 0; i < elemCount; i++)
|
||||
{
|
||||
ushort idx = drawList.IdxBuffer[(int)indexOffset + i];
|
||||
int vertIdx = (int)(vertexOffset + idx);
|
||||
|
||||
ImDrawVert srcVert = drawList.VtxBuffer[vertIdx];
|
||||
|
||||
uint col = srcVert.Col;
|
||||
|
||||
byte colR = (byte)((col >> 0) & 0xFF);
|
||||
byte colG = (byte)((col >> 8) & 0xFF);
|
||||
byte colB = (byte)((col >> 16) & 0xFF);
|
||||
byte colA = (byte)((col >> 24) & 0xFF);
|
||||
|
||||
vertices[i] = new SDL.Vertex
|
||||
{
|
||||
Position = new SDL.FPoint
|
||||
{
|
||||
X = (srcVert.Pos.X - clipOffset.X) * renderScale.X,
|
||||
Y = (srcVert.Pos.Y - clipOffset.Y) * renderScale.Y
|
||||
},
|
||||
Color = new SDL.FColor
|
||||
{
|
||||
R = colR / 255f,
|
||||
G = colG / 255f,
|
||||
B = colB / 255f,
|
||||
A = colA / 255f
|
||||
},
|
||||
TexCoord = new SDL.FPoint
|
||||
{
|
||||
X = srcVert.Uv.X,
|
||||
Y = srcVert.Uv.Y
|
||||
}
|
||||
};
|
||||
|
||||
indices[i] = i;
|
||||
}
|
||||
|
||||
// Get texture
|
||||
ImTextureID texId = cmd.GetTexID();
|
||||
if (!SDL.RenderGeometry(renderer, texId, vertices, vertices.Length, indices, indices.Length))
|
||||
{
|
||||
Program.Logger.Log($"Failed to render ImGui draw command: {SDL.GetError()}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -240,7 +237,7 @@ public unsafe static class ImGuiSDL3Renderer
|
||||
platformIo.RendererRenderState = null;
|
||||
|
||||
// Restore renderer state
|
||||
SDL.SetRenderViewport(renderer, old.ViewportEnabled ? old.Viewport : new SDL.Rect());
|
||||
SDL.SetRenderViewport(renderer, old.Viewport);
|
||||
SDL.SetRenderClipRect(renderer, old.ClipEnabled ? old.ClipRect : new SDL.Rect());
|
||||
}
|
||||
|
||||
@@ -281,7 +278,7 @@ public unsafe static class ImGuiSDL3Renderer
|
||||
nint sdlTexture = tex.TexID;
|
||||
for (int i = 0; i < tex.Updates.Size; i++)
|
||||
{
|
||||
var r = tex.Updates[i];
|
||||
ImTextureRect r = tex.Updates[i];
|
||||
SDL.Rect sdlR = new SDL.Rect { X = r.X, Y = r.Y, W = r.W, H = r.H };
|
||||
SDL.UpdateTexture(sdlTexture, sdlR, (nint)tex.GetPixelsAt(r.X, r.Y), tex.GetPitch());
|
||||
}
|
||||
@@ -289,67 +286,92 @@ public unsafe static class ImGuiSDL3Renderer
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static bool RenderDrawCommand(ImDrawListPtr drawList, ImDrawCmd cmd, nint renderer, nint texId, Vector2 scale, Vector2 displayPos)
|
||||
{
|
||||
uint indexOffset = cmd.IdxOffset;
|
||||
uint vertexOffset = cmd.VtxOffset;
|
||||
uint elemCount = cmd.ElemCount;
|
||||
|
||||
SDL.Vertex[] vertices = new SDL.Vertex[elemCount];
|
||||
int[] indices = new int[elemCount];
|
||||
|
||||
for (int i = 0; i < elemCount; i++)
|
||||
{
|
||||
ushort idx = drawList.IdxBuffer[(int)indexOffset + i];
|
||||
int vertIdx = (int)(vertexOffset + idx);
|
||||
|
||||
ImDrawVert srcVert = drawList.VtxBuffer[vertIdx];
|
||||
|
||||
uint col = srcVert.Col;
|
||||
|
||||
byte r = (byte)((col >> 0) & 0xFF);
|
||||
byte g = (byte)((col >> 8) & 0xFF);
|
||||
byte b = (byte)((col >> 16) & 0xFF);
|
||||
byte a = (byte)((col >> 24) & 0xFF);
|
||||
|
||||
vertices[i] = new SDL.Vertex
|
||||
{
|
||||
Position = new SDL.FPoint
|
||||
{
|
||||
X = (srcVert.Pos.X - displayPos.X) * scale.X,
|
||||
Y = (srcVert.Pos.Y - displayPos.Y) * scale.Y
|
||||
},
|
||||
Color = new SDL.FColor
|
||||
{
|
||||
R = r / 255f,
|
||||
G = g / 255f,
|
||||
B = b / 255f,
|
||||
A = a / 255f
|
||||
},
|
||||
TexCoord = new SDL.FPoint
|
||||
{
|
||||
X = srcVert.Uv.X,
|
||||
Y = srcVert.Uv.Y
|
||||
}
|
||||
};
|
||||
|
||||
indices[i] = i;
|
||||
}
|
||||
|
||||
return SDL.RenderGeometry(renderer, texId, vertices, vertices.Length, indices, indices.Length);
|
||||
}
|
||||
|
||||
public static void CreateDeviceObjects() { }
|
||||
|
||||
public static void DestroyDeviceObjects()
|
||||
{
|
||||
var texures = ImGui.GetPlatformIO().Textures;
|
||||
ImVector<ImTextureDataPtr> texures = ImGui.GetPlatformIO().Textures;
|
||||
for (int i = 0; i < texures.Size; i++)
|
||||
{
|
||||
var texture = texures[i];
|
||||
ImTextureDataPtr texture = texures[i];
|
||||
texture.Status = ImTextureStatus.WantDestroy;
|
||||
UpdateTexture(texture, Data.Renderer);
|
||||
UpdateTexture(texture, GetRendererData().Renderer);
|
||||
}
|
||||
}
|
||||
|
||||
// @formatter:off — disable formatter after this line
|
||||
public static class DelegateStorage
|
||||
{
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
internal delegate void DrawCallbackResetRenderStateFn(ImDrawListPtr drawList, ImDrawCmdPtr cmd);
|
||||
internal static readonly DrawCallbackResetRenderStateFn DrawCallbackResetRenderStateDelegate = DrawCallbackResetRenderState;
|
||||
private static void DrawCallbackResetRenderState(ImDrawListPtr drawList, ImDrawCmdPtr cmd) { } // Intentionally empty. Used as an identifier for rendering loop to call its code. Simpler to implement this way.
|
||||
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
internal delegate void RendererCreateWindowFn(ImGuiViewportPtr viewport);
|
||||
internal static readonly RendererCreateWindowFn RendererCreateWindowDelegate = RendererCreateWindow;
|
||||
private static void RendererCreateWindow(ImGuiViewportPtr viewport)
|
||||
{
|
||||
ImGuiSDL3Platform.ViewPortData? vd = ImGuiUserData<ImGuiSDL3Platform.ViewPortData>.Get(viewport.PlatformUserData);
|
||||
if (vd == null || vd.Window == nint.Zero)
|
||||
return;
|
||||
|
||||
if (vd.Renderer == nint.Zero)
|
||||
{
|
||||
vd.Renderer = SDL.CreateRenderer(vd.Window, null);
|
||||
vd.RendererOwned = true;
|
||||
}
|
||||
}
|
||||
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
internal delegate void RendererDestroyWindowFn(ImGuiViewportPtr viewport);
|
||||
internal static readonly RendererDestroyWindowFn RendererDestroyWindowDelegate = RendererDestroyWindow;
|
||||
private static void RendererDestroyWindow(ImGuiViewportPtr viewport)
|
||||
{
|
||||
ImGuiSDL3Platform.ViewPortData? vd = ImGuiUserData<ImGuiSDL3Platform.ViewPortData>.Get(viewport.PlatformUserData);
|
||||
if (vd == null)
|
||||
return;
|
||||
|
||||
if (vd.RendererOwned && vd.Renderer != nint.Zero)
|
||||
{
|
||||
SDL.DestroyRenderer(vd.Renderer);
|
||||
}
|
||||
|
||||
vd.Renderer = nint.Zero;
|
||||
vd.RendererOwned = false;
|
||||
}
|
||||
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
internal delegate void RendererRenderWindowFn(ImGuiViewportPtr viewport);
|
||||
internal static readonly RendererRenderWindowFn RendererRenderWindowDelegate = RendererRenderWindow;
|
||||
private static void RendererRenderWindow(ImGuiViewportPtr viewport)
|
||||
{
|
||||
ImGuiSDL3Platform.ViewPortData? vd = ImGuiUserData<ImGuiSDL3Platform.ViewPortData>.Get(viewport.PlatformUserData);
|
||||
if (vd == null || vd.Renderer == nint.Zero)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (Parent != null)
|
||||
{
|
||||
SDL.SetRenderDrawColorFloat(vd.Renderer, Parent.ClearColor.X, Parent.ClearColor.Y, Parent.ClearColor.Z, 0f);
|
||||
SDL.RenderClear(vd.Renderer);
|
||||
}
|
||||
|
||||
RenderDrawData(viewport.DrawData, vd.Renderer);
|
||||
}
|
||||
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
internal delegate void RendererSwapBuffersFn(ImGuiViewportPtr viewport);
|
||||
internal static readonly RendererSwapBuffersFn RendererSwapBuffersDelegate = RendererSwapBuffers;
|
||||
private static void RendererSwapBuffers(ImGuiViewportPtr viewport)
|
||||
{
|
||||
ImGuiSDL3Platform.ViewPortData? vd = ImGuiUserData<ImGuiSDL3Platform.ViewPortData>.Get(viewport.PlatformUserData);
|
||||
if (vd == null || vd.Renderer == nint.Zero)
|
||||
return;
|
||||
|
||||
SDL.RenderPresent(vd.Renderer);
|
||||
}
|
||||
}
|
||||
// @formatter:on — enable formatter after this line
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
+76
-69
@@ -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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user