some cleanup
This commit is contained in:
+2
-2
@@ -18,7 +18,7 @@ public class Program
|
|||||||
public static void Main()
|
public static void Main()
|
||||||
{
|
{
|
||||||
SDL3Window window = new SDL3Window("SDL3 Testing Suite", 100, 100, 1280, 720);
|
SDL3Window window = new SDL3Window("SDL3 Testing Suite", 100, 100, 1280, 720);
|
||||||
window.RenderCallback = () =>
|
window.RenderCallback += () =>
|
||||||
{
|
{
|
||||||
ImGuiViewportPtr viewport = ImGui.GetMainViewport();
|
ImGuiViewportPtr viewport = ImGui.GetMainViewport();
|
||||||
ImGui.SetNextWindowPos(viewport.WorkPos);
|
ImGui.SetNextWindowPos(viewport.WorkPos);
|
||||||
@@ -65,7 +65,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>();
|
||||||
|
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
+2
-4
@@ -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;
|
||||||
|
|
||||||
|
|||||||
+436
-505
File diff suppressed because it is too large
Load Diff
+179
-157
@@ -1,4 +1,6 @@
|
|||||||
|
using System.Diagnostics;
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using Hexa.NET.ImGui;
|
using Hexa.NET.ImGui;
|
||||||
using SDL3;
|
using SDL3;
|
||||||
@@ -11,8 +13,6 @@ namespace SDL3_TestingSuite.SDL3;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public unsafe static class ImGuiSDL3Renderer
|
public unsafe static class ImGuiSDL3Renderer
|
||||||
{
|
{
|
||||||
public static SDL3Window Parent;
|
|
||||||
|
|
||||||
public class RendererData
|
public class RendererData
|
||||||
{
|
{
|
||||||
public nint Renderer; // Main viewport's renderer
|
public nint Renderer; // Main viewport's renderer
|
||||||
@@ -25,34 +25,15 @@ public unsafe static class ImGuiSDL3Renderer
|
|||||||
private struct BackupSDLRendererState
|
private struct BackupSDLRendererState
|
||||||
{
|
{
|
||||||
public SDL.Rect Viewport;
|
public SDL.Rect Viewport;
|
||||||
public bool ViewportEnabled;
|
|
||||||
public bool ClipEnabled;
|
public bool ClipEnabled;
|
||||||
public SDL.Rect ClipRect;
|
public SDL.Rect ClipRect;
|
||||||
}
|
}
|
||||||
|
|
||||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
public static RendererData GetRendererData() => ImGui.GetCurrentContext().Handle != null ? ImGuiUserData<RendererData>.Get(ImGui.GetIO().BackendRendererUserData)! : null!;
|
||||||
private delegate void RendererCreateWindowFn(ImGuiViewportPtr viewport);
|
|
||||||
|
|
||||||
private static readonly RendererCreateWindowFn RendererCreateWindowDelegate = RendererCreateWindow;
|
public static SDL3Window? Parent;
|
||||||
|
|
||||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
public static bool Init(nint renderer, SDL3Window? parentWindow = null)
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
Parent = parentWindow;
|
Parent = parentWindow;
|
||||||
ImGuiIOPtr io = ImGui.GetIO();
|
ImGuiIOPtr io = ImGui.GetIO();
|
||||||
@@ -67,88 +48,45 @@ public unsafe static class ImGuiSDL3Renderer
|
|||||||
bd.Renderer = renderer;
|
bd.Renderer = renderer;
|
||||||
|
|
||||||
ImGuiPlatformIOPtr platformIO = ImGui.GetPlatformIO();
|
ImGuiPlatformIOPtr platformIO = ImGui.GetPlatformIO();
|
||||||
platformIO.RendererCreateWindow = (void*)Marshal.GetFunctionPointerForDelegate(RendererCreateWindowDelegate);
|
platformIO.RendererCreateWindow = DelegateStorage.RendererCreateWindowDelegate.GetPtrForDelegate();
|
||||||
platformIO.RendererDestroyWindow = (void*)Marshal.GetFunctionPointerForDelegate(RendererDestroyWindowDelegate);
|
platformIO.RendererDestroyWindow = DelegateStorage.RendererDestroyWindowDelegate.GetPtrForDelegate();
|
||||||
platformIO.RendererRenderWindow = (void*)Marshal.GetFunctionPointerForDelegate(RendererRenderWindowDelegate);
|
platformIO.RendererRenderWindow = DelegateStorage.RendererRenderWindowDelegate.GetPtrForDelegate();
|
||||||
platformIO.RendererSwapBuffers = (void*)Marshal.GetFunctionPointerForDelegate(RendererSwapBuffersDelegate);
|
platformIO.RendererSwapBuffers = DelegateStorage.RendererSwapBuffersDelegate.GetPtrForDelegate();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Dispose()
|
public static void Dispose()
|
||||||
{
|
{
|
||||||
var io = ImGui.GetIO();
|
ImGuiIOPtr io = ImGui.GetIO();
|
||||||
var platformIO = ImGui.GetPlatformIO();
|
ImGuiPlatformIOPtr platformIO = ImGui.GetPlatformIO();
|
||||||
|
|
||||||
DestroyDeviceObjects();
|
if (io.BackendRendererName != null)
|
||||||
|
{
|
||||||
|
Marshal.FreeHGlobal((nint)io.BackendRendererName);
|
||||||
|
}
|
||||||
|
|
||||||
|
ImGuiUserData<RendererData>.Free(io.BackendRendererUserData);
|
||||||
io.BackendRendererName = null;
|
io.BackendRendererName = null;
|
||||||
io.BackendRendererUserData = null;
|
io.BackendRendererUserData = null;
|
||||||
io.BackendFlags &= ~(ImGuiBackendFlags.RendererHasVtxOffset | ImGuiBackendFlags.RendererHasTextures);
|
io.BackendFlags &= ~(ImGuiBackendFlags.RendererHasVtxOffset | ImGuiBackendFlags.RendererHasTextures | ImGuiBackendFlags.RendererHasViewports);
|
||||||
platformIO.RendererTextureMaxWidth = 0;
|
platformIO.RendererTextureMaxWidth = 0;
|
||||||
platformIO.RendererTextureMaxHeight = 0;
|
platformIO.RendererTextureMaxHeight = 0;
|
||||||
platformIO.RendererRenderState = null;
|
platformIO.RendererRenderState = null;
|
||||||
platformIO.RendererCreateWindow = null;
|
|
||||||
platformIO.RendererDestroyWindow = null;
|
|
||||||
platformIO.RendererSetWindowSize = null;
|
|
||||||
platformIO.RendererRenderWindow = null;
|
|
||||||
platformIO.RendererSwapBuffers = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void NewFrame() { }
|
public static void SetupRenderState(nint renderer)
|
||||||
|
|
||||||
private static void RendererCreateWindow(ImGuiViewportPtr viewport)
|
|
||||||
{
|
{
|
||||||
ImGuiSDL3Platform.ViewPortData? vd = ImGuiUserData<ImGuiSDL3Platform.ViewPortData>.Get(viewport.PlatformUserData);
|
// Clear out any viewports and cliprect set by the user
|
||||||
if (vd == null || vd.Window == nint.Zero)
|
// FIXME: Technically speaking there are lots of other things we could backup/setup/restore during our render process.
|
||||||
return;
|
SDL.SetRenderViewport(renderer, nint.Zero);
|
||||||
|
SDL.SetRenderClipRect(renderer, nint.Zero);
|
||||||
if (vd.Renderer == nint.Zero)
|
|
||||||
{
|
|
||||||
vd.Renderer = SDL.CreateRenderer(vd.Window, null);
|
|
||||||
vd.RendererOwned = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void RendererDestroyWindow(ImGuiViewportPtr viewport)
|
public static void NewFrame()
|
||||||
{
|
{
|
||||||
ImGuiSDL3Platform.ViewPortData? vd = ImGuiUserData<ImGuiSDL3Platform.ViewPortData>.Get(viewport.PlatformUserData);
|
RendererData bd = GetRendererData();
|
||||||
if (vd == null)
|
Debug.Assert(bd != null, "Context or backend not initialized! Did you call ImGuiSDL3Renderer.Init()?");
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void RenderDrawData(ImDrawDataPtr drawData, nint renderer)
|
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++)
|
for (int i = 0; i < drawData.Textures.Size; i++)
|
||||||
{
|
{
|
||||||
var texture = drawData.Textures[i];
|
ImTextureDataPtr texture = drawData.Textures[i];
|
||||||
|
if (texture.Handle != null)
|
||||||
|
{
|
||||||
UpdateTexture(texture, renderer);
|
UpdateTexture(texture, renderer);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Backup SDL renderer state
|
// Backup SDL renderer state
|
||||||
BackupSDLRendererState old = new BackupSDLRendererState
|
BackupSDLRendererState old = new BackupSDLRendererState
|
||||||
{
|
{
|
||||||
ViewportEnabled = SDL.RenderViewportSet(renderer),
|
|
||||||
ClipEnabled = SDL.RenderClipEnabled(renderer)
|
ClipEnabled = SDL.RenderClipEnabled(renderer)
|
||||||
};
|
};
|
||||||
SDL.GetRenderViewport(renderer, out old.Viewport);
|
SDL.GetRenderViewport(renderer, out old.Viewport);
|
||||||
SDL.GetRenderClipRect(renderer, out old.ClipRect);
|
SDL.GetRenderClipRect(renderer, out old.ClipRect);
|
||||||
|
|
||||||
// Set up render state
|
// Set up render state
|
||||||
SDL.SetRenderViewport(renderer, nint.Zero);
|
SetupRenderState(renderer);
|
||||||
SDL.SetRenderClipRect(renderer, nint.Zero);
|
|
||||||
|
|
||||||
// Set render state in platform IO
|
// Set render state in platform IO
|
||||||
ImGuiPlatformIOPtr platformIo = ImGui.GetPlatformIO();
|
ImGuiPlatformIOPtr platformIo = ImGui.GetPlatformIO();
|
||||||
@@ -193,17 +132,30 @@ public unsafe static class ImGuiSDL3Renderer
|
|||||||
// Render command lists
|
// Render command lists
|
||||||
for (int n = 0; n < drawData.CmdListsCount; n++)
|
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)
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
// Apply clipping rectangle
|
// Apply clipping rectangle
|
||||||
Vector4 clipRect = cmd.ClipRect;
|
Vector4 clipRect = cmd.ClipRect;
|
||||||
Vector2 clipMin = new Vector2((clipRect.X - clipOffset.X) * renderScale.X, (clipRect.Y - clipOffset.Y) * renderScale.Y);
|
Vector2 clipMin = new Vector2((clipRect.X - clipOffset.X) * renderScale.X, (clipRect.Y - clipOffset.Y) * renderScale.Y);
|
||||||
@@ -225,13 +177,58 @@ public unsafe static class ImGuiSDL3Renderer
|
|||||||
};
|
};
|
||||||
SDL.SetRenderClipRect(renderer, r);
|
SDL.SetRenderClipRect(renderer, r);
|
||||||
|
|
||||||
// Get texture
|
|
||||||
nint texId = cmd.GetTexID();
|
|
||||||
|
|
||||||
// Convert ImGui vertices to SDL vertices
|
// Convert ImGui vertices to SDL vertices
|
||||||
if (!RenderDrawCommand(cmdList, cmd, renderer, texId, renderScale, clipOffset))
|
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++)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Failed to render ImGui draw command: {SDL.GetError()}");
|
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;
|
platformIo.RendererRenderState = null;
|
||||||
|
|
||||||
// Restore renderer state
|
// 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());
|
SDL.SetRenderClipRect(renderer, old.ClipEnabled ? old.ClipRect : new SDL.Rect());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -281,7 +278,7 @@ public unsafe static class ImGuiSDL3Renderer
|
|||||||
nint sdlTexture = tex.TexID;
|
nint sdlTexture = tex.TexID;
|
||||||
for (int i = 0; i < tex.Updates.Size; i++)
|
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.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());
|
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 CreateDeviceObjects() { }
|
||||||
|
|
||||||
public static void DestroyDeviceObjects()
|
public static void DestroyDeviceObjects()
|
||||||
{
|
{
|
||||||
var texures = ImGui.GetPlatformIO().Textures;
|
ImVector<ImTextureDataPtr> texures = ImGui.GetPlatformIO().Textures;
|
||||||
for (int i = 0; i < texures.Size; i++)
|
for (int i = 0; i < texures.Size; i++)
|
||||||
{
|
{
|
||||||
var texture = texures[i];
|
ImTextureDataPtr texture = texures[i];
|
||||||
texture.Status = ImTextureStatus.WantDestroy;
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
+53
-46
@@ -1,8 +1,8 @@
|
|||||||
using System.Diagnostics;
|
using System.Numerics;
|
||||||
using System.Numerics;
|
|
||||||
using Hexa.NET.ImGui;
|
using Hexa.NET.ImGui;
|
||||||
using Hexa.NET.ImGuizmo;
|
using Hexa.NET.ImGuizmo;
|
||||||
using Hexa.NET.ImPlot;
|
using Hexa.NET.ImPlot;
|
||||||
|
using Hexa.NET.ImPlot3D;
|
||||||
using SDL3;
|
using SDL3;
|
||||||
|
|
||||||
namespace SDL3_TestingSuite.SDL3;
|
namespace SDL3_TestingSuite.SDL3;
|
||||||
@@ -12,15 +12,13 @@ public sealed unsafe class SDL3Window : IDisposable
|
|||||||
public readonly nint Window;
|
public readonly nint Window;
|
||||||
public readonly nint Renderer;
|
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);
|
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 ImGuiContextPtr _imGuiContext;
|
||||||
|
private ImPlotContextPtr _imPlotContext;
|
||||||
|
private ImPlot3DContextPtr _imPlot3DContext;
|
||||||
|
|
||||||
public bool Disposed => _disposed;
|
public bool Disposed => _disposed;
|
||||||
private bool _disposed;
|
private bool _disposed;
|
||||||
@@ -41,18 +39,18 @@ public sealed unsafe class SDL3Window : IDisposable
|
|||||||
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);
|
// _imPlot3DContext = ImPlot3D.CreateContext();
|
||||||
ImGuizmo.SetImGuiContext(context);
|
ImPlot.SetImGuiContext(_imGuiContext);
|
||||||
// ImPlot3D.SetImGuiContext(context);
|
ImGuizmo.SetImGuiContext(_imGuiContext);
|
||||||
// ImPlot3D.CreateContext();
|
// ImPlot3D.SetImGuiContext(_imGuiContext);
|
||||||
|
|
||||||
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
|
||||||
@@ -96,35 +94,11 @@ public sealed unsafe class SDL3Window : IDisposable
|
|||||||
ImGuiSDL3Renderer.Init(Renderer, this);
|
ImGuiSDL3Renderer.Init(Renderer, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool ShouldClose;
|
||||||
|
|
||||||
public void Run()
|
public void Run()
|
||||||
{
|
{
|
||||||
while (!_disposed)
|
while (!_disposed)
|
||||||
{
|
|
||||||
ImGui.GetIO().DeltaTime = (float)(_timer.Elapsed - _time).TotalSeconds;
|
|
||||||
_time = _timer.Elapsed;
|
|
||||||
|
|
||||||
PollEvents();
|
|
||||||
|
|
||||||
Update();
|
|
||||||
|
|
||||||
RenderCallback?.Invoke();
|
|
||||||
|
|
||||||
Render();
|
|
||||||
|
|
||||||
if (ShouldClose)
|
|
||||||
{
|
|
||||||
Dispose();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!_disposed)
|
|
||||||
Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool ShouldClose;
|
|
||||||
|
|
||||||
private void PollEvents()
|
|
||||||
{
|
{
|
||||||
if (ImGui.GetIO().WantTextInput && !SDL.TextInputActive(Window))
|
if (ImGui.GetIO().WantTextInput && !SDL.TextInputActive(Window))
|
||||||
SDL.StartTextInput(Window);
|
SDL.StartTextInput(Window);
|
||||||
@@ -144,23 +118,21 @@ public sealed unsafe class SDL3Window : IDisposable
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private void Update()
|
|
||||||
{
|
|
||||||
ImGuiSDL3Platform.NewFrame();
|
ImGuiSDL3Platform.NewFrame();
|
||||||
ImGuiSDL3Renderer.NewFrame();
|
ImGuiSDL3Renderer.NewFrame();
|
||||||
ImGui.NewFrame();
|
ImGui.NewFrame();
|
||||||
}
|
|
||||||
|
|
||||||
private void Render()
|
RenderCallback?.Invoke();
|
||||||
{
|
|
||||||
ImGuiIOPtr io = ImGui.GetIO();
|
ImGuiIOPtr io = ImGui.GetIO();
|
||||||
|
|
||||||
ImGui.Render();
|
ImGui.Render();
|
||||||
|
|
||||||
SDL.SetRenderScale(Renderer, io.DisplayFramebufferScale.X, io.DisplayFramebufferScale.Y);
|
SDL.SetRenderScale(Renderer, io.DisplayFramebufferScale.X, io.DisplayFramebufferScale.Y);
|
||||||
SDL.SetRenderDrawColorFloat(Renderer, ClearColor.X, ClearColor.Y, ClearColor.Z, ClearColor.W);
|
SDL.SetRenderDrawColorFloat(Renderer, ClearColor.X, ClearColor.Y, ClearColor.Z, ClearColor.W);
|
||||||
SDL.RenderClear(Renderer);
|
SDL.RenderClear(Renderer);
|
||||||
|
|
||||||
ImGuiSDL3Renderer.RenderDrawData(ImGui.GetDrawData(), Renderer);
|
ImGuiSDL3Renderer.RenderDrawData(ImGui.GetDrawData(), Renderer);
|
||||||
|
|
||||||
if ((io.ConfigFlags & ImGuiConfigFlags.ViewportsEnable) != 0)
|
if ((io.ConfigFlags & ImGuiConfigFlags.ViewportsEnable) != 0)
|
||||||
@@ -170,6 +142,16 @@ public sealed unsafe class SDL3Window : IDisposable
|
|||||||
}
|
}
|
||||||
|
|
||||||
SDL.RenderPresent(Renderer);
|
SDL.RenderPresent(Renderer);
|
||||||
|
|
||||||
|
if (ShouldClose)
|
||||||
|
{
|
||||||
|
Dispose();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!_disposed)
|
||||||
|
Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
~SDL3Window()
|
~SDL3Window()
|
||||||
@@ -190,6 +172,19 @@ public sealed unsafe class SDL3Window : IDisposable
|
|||||||
|
|
||||||
if (disposing)
|
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();
|
ImGuiSDL3Renderer.Dispose();
|
||||||
ImGuiSDL3Platform.Dispose();
|
ImGuiSDL3Platform.Dispose();
|
||||||
|
|
||||||
@@ -202,6 +197,18 @@ public sealed unsafe class SDL3Window : IDisposable
|
|||||||
ImGui.DestroyContext(_imGuiContext);
|
ImGui.DestroyContext(_imGuiContext);
|
||||||
_imGuiContext = null;
|
_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)
|
if (Renderer != nint.Zero)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user