Initial Commit

This commit is contained in:
2026-05-18 01:12:52 -05:00
commit fc0b04a4bc
13 changed files with 2225 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
bin/
obj/
/packages/
riderModule.iml
/_ReSharper.Caches/
+15
View File
@@ -0,0 +1,15 @@
# Default ignored files
/shelf/
/workspace.xml
# Rider ignored files
/modules.xml
/contentModel.xml
/.idea.SDL3 TestingSuite.iml
/projectSettingsUpdater.xml
# Editor-based HTTP Client requests
/httpRequests/
# Ignored default folder with query files
/queries/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
+4
View File
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" addBOMForNewFiles="with BOM under Windows, with no BOM otherwise" />
</project>
+8
View File
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="UserContentModel">
<attachedFolders />
<explicitIncludes />
<explicitExcludes />
</component>
</project>
+7
View File
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
+189
View File
@@ -0,0 +1,189 @@
using System.Numerics;
using System.Runtime.CompilerServices;
using Hexa.NET.ImGui;
using Hexa.NET.ImPlot;
using Hexa.NET.ImPlot3D;
using SDL3_TestingSuite.SDL3;
using SDL3;
namespace SDL3_TestingSuite;
public class Program
{
private static bool _demoWindowVisible = true;
private static bool _fontStuff;
private static readonly Dictionary<string, List<uint>?> GlyphsByName = new Dictionary<string, List<uint>?>();
private static bool _initialized;
private static ImFontPtr _font;
private static float size = 1f;
public static void Main()
{
const SDL.WindowFlags flags = SDL.WindowFlags.Resizable | SDL.WindowFlags.HighPixelDensity | SDL.WindowFlags.Transparent;
SDL3Window window = new SDL3Window("SDL3 Testing Suite", 100, 100, 1280, 720, flags);
window.ClearColor.W = 0f;
window.RenderCallback = () =>
{
ImGuiViewportPtr viewport = ImGui.GetMainViewport();
ImGui.SetNextWindowPos(viewport.WorkPos);
ImGui.SetNextWindowSize(viewport.WorkSize);
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.MenuItem("Quit"))
{
window.ShouldClose = true;
}
ImGui.EndMenu();
}
ImGui.Spacing();
ImGui.MenuItem("Demo Window", "", ref _demoWindowVisible);
ImGui.Spacing();
ImGui.MenuItem("Font Stuff", "", ref _fontStuff);
ImGui.EndMenuBar();
}
ImGui.End();
if (_fontStuff)
{
ImGui.SetNextWindowSize(new Vector2(250, 500), ImGuiCond.FirstUseEver);
if (ImGui.Begin("FontStuff", ref _fontStuff))
{
ImGui.ShowFontSelector("Font");
bool change = false;
if (ImGui.GetFont() != _font)
{
_font = ImGui.GetFont();
change = true;
}
string fontName = _font.GetDebugNameS();
fontName = string.IsNullOrEmpty(fontName) ? _font.FontId.ToString() : fontName;
if (!_initialized || change)
{
if (!GlyphsByName.TryGetValue(fontName, out List<uint> glyphs))
{
glyphs = new List<uint>();
ImFontPtr font = _font;
for (uint codepoint = 0; codepoint <= 0x10FFFF; codepoint++)
{
if (codepoint >= 0xD800 && codepoint <= 0xDFFF)
{
continue;
}
if (!font.IsGlyphInFont(codepoint)) continue;
glyphs.Add(codepoint);
}
GlyphsByName[fontName] = glyphs;
Logger.Log($"Initialized font {fontName} with {glyphs.Count} glyphs");
}
_initialized = true;
}
ImGui.PushFont(null, 24);
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();
}
}
float time = (float)ImGui.GetTime() * 5;
if (ImPlot.BeginPlot("Moving Rainbow Sine Wave"))
{
int count = 200;
float[] xs = new float[2];
float[] ys = new float[2];
for (int i = 0; i < count - 1; i++)
{
float x0 = i * 0.1f;
float x1 = (i + 1) * 0.1f;
float y0 = MathF.Sin(x0 * size + time);
float y1 = MathF.Sin(x1 * size + time);
xs[0] = x0;
xs[1] = x1;
ys[0] = y0;
ys[1] = y1;
float t = i / (float)count;
float r = 0.5f + 1f * MathF.Sin(6.2831f * (t));
float g = 0.5f + 1f * MathF.Sin(6.2831f * (t + 0.33f));
float b = 0.5f + 1f * MathF.Sin(6.2831f * (t + 0.66f));
ImPlot.PushStyleColor(ImPlotCol.Line, new Vector4(r, g, b, 1f));
ImPlot.PlotLine("##seg", ref xs[0], ref ys[0], 2);
ImPlot.PopStyleColor();
}
ImPlot.EndPlot();
}
ImGui.SliderFloat("Sine", ref size, 1f, 120f);
if (_demoWindowVisible)
{
ImGui.ShowDemoWindow(ref _demoWindowVisible);
ImPlot.ShowDemoWindow(ref _demoWindowVisible);
// ImPlot3D.ShowDemoWindow(ref _demoWindowVisible);
}
};
window.Run();
}
public static class Logger
{
public static void Log(string? str, [CallerMemberName] string caller = "", [CallerLineNumber] int line = 0)
{
Console.WriteLine($"{caller}({line}): {str}");
}
public static void Log(object? str, [CallerMemberName] string caller = "", [CallerLineNumber] int line = 0)
{
Log(str?.ToString(), caller, line);
}
}
}
+24
View File
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<RootNamespace>SDL3_TestingSuite</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>
<ItemGroup>
<!--<PackageReference Include="ImGui.NET" Version="*" />-->
<PackageReference Include="Hexa.NET.ImGui" Version="2.2.9" />
<PackageReference Include="Hexa.NET.ImGui.Widgets" Version="1.2.18" />
<PackageReference Include="Hexa.NET.ImPlot" Version="2.2.9" />
<PackageReference Include="Hexa.NET.ImPlot3D" Version="2.2.9" />
<PackageReference Include="Hexa.NET.Utilities" Version="2.2.12" />
<PackageReference Include="SDL3-CS" Version="3.2.18" />
<PackageReference Include="SDL3-CS.Native" Version="3.2.18" />
</ItemGroup>
</Project>
+2
View File
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AImFontConfig_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003Fhome_003Fnepushiro_003F_002Econfig_003FJetBrains_003FRider2026_002E1_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fc97073b326e54291bbfaa37650bd4af34d6200_003F7f_003F46ea95ef_003FImFontConfig_002Ecs/@EntryIndexedValue">ForceIncluded</s:String></wpf:ResourceDictionary>
+3
View File
@@ -0,0 +1,3 @@
<Solution>
<Project Path="SDL3 TestingSuite.csproj" />
</Solution>
+162
View File
@@ -0,0 +1,162 @@
using Hexa.NET.ImGui;
namespace SDL3_TestingSuite.SDL3;
public struct FontMetrics
{
public ushort UnitsPerEm;
public short TypoAscender;
public short TypoDescender;
public short TypoLineGap;
public short WinAscent;
public short WinDescent;
public int TypoLineHeight;
public int WinLineHeight;
}
public static class FontFind
{
extension(ImGuiIOPtr io)
{
public unsafe ImFontPtr AddFont(string fontPath)
{
try
{
if (fontPath == null || io.Handle == null) return null;
FileInfo info = new FileInfo(fontPath);
if (!info.Exists || info.Length <= 0) return null;
FontMetrics metrics = FontFind.ReadFontMetrics(fontPath);
float size = FontFind.GetRecommendedPixelSize(metrics);
ImFontConfigPtr config = ImGui.ImFontConfig();
config.FontLoaderFlags |= (uint)ImGuiFreeTypeLoaderFlags.LoadColor;
ImFontPtr font = io.Fonts.AddFontFromFileTTF(fontPath, size, config);
Program.Logger.Log("Added font: " + Path.GetFileName(fontPath));
return font;
}
catch (Exception e)
{
Console.WriteLine(e);
}
return null;
}
public unsafe bool RemoveFont(string? fontName)
{
if (fontName == null || io.Handle == null) return false;
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];
if (font.GetDebugNameS() == fontName)
{
toRemove.Add(font);
flag = true;
}
}
foreach (ImFontPtr font in toRemove)
{
io.Fonts.RemoveFont(font);
Program.Logger.Log("Removed font: " + fontName);
}
return flag;
}
}
public static FontMetrics ReadFontMetrics(string fontPath)
{
byte[] data = File.ReadAllBytes(fontPath);
ushort numTables = ReadU16Be(4);
const int tableDir = 12;
int headOffset = 0;
int os2Offset = 0;
for (int i = 0; i < numTables; i++)
{
int entry = tableDir + i * 16;
string tag = ReadTag(entry);
uint offset = ReadU32Be(entry + 8);
if (tag == "head")
headOffset = (int)offset;
if (tag == "OS/2")
os2Offset = (int)offset;
}
FontMetrics metrics = new FontMetrics();
metrics.UnitsPerEm = ReadU16Be(headOffset + 18);
if (os2Offset != 0)
{
metrics.TypoAscender = ReadS16Be(os2Offset + 68);
metrics.TypoDescender = ReadS16Be(os2Offset + 70);
metrics.TypoLineGap = ReadS16Be(os2Offset + 72);
metrics.WinAscent = ReadS16Be(os2Offset + 74);
metrics.WinDescent = ReadS16Be(os2Offset + 76);
}
metrics.TypoLineHeight =
metrics.TypoAscender - metrics.TypoDescender + metrics.TypoLineGap;
metrics.WinLineHeight =
metrics.WinAscent + metrics.WinDescent;
return metrics;
ushort ReadU16Be(int o)
{
return (ushort)((data[o] << 8) | data[o + 1]);
}
short ReadS16Be(int o)
{
return (short)ReadU16Be(o);
}
uint ReadU32Be(int o)
{
return (uint)((data[o] << 24) | (data[o + 1] << 16) | (data[o + 2] << 8) | data[o + 3]);
}
string ReadTag(int o)
{
char c1 = (char)data[o];
char c2 = (char)data[o + 1];
char c3 = (char)data[o + 2];
char c4 = (char)data[o + 3];
return new string(new char[] { c1, c2, c3, c4 });
}
}
public static int GetRecommendedPixelSize(FontMetrics metrics)
{
if (metrics.UnitsPerEm == 0 || metrics.TypoLineHeight == 0)
return 16;
const float targetLineHeightPx = 18.0f;
float scale = targetLineHeightPx * metrics.UnitsPerEm / metrics.TypoLineHeight;
int size = (int)MathF.Round(scale);
if (size < 10) size = 10;
if (size > 72) size = 72;
return size;
}
}
File diff suppressed because it is too large Load Diff
+433
View File
@@ -0,0 +1,433 @@
using System.Numerics;
using System.Runtime.InteropServices;
using Hexa.NET.ImGui;
using SDL3;
namespace SDL3_TestingSuite.SDL3;
/// <summary>
/// Implementation of ImGui for SDL3 Renderer backend.
/// https://github.com/ocornut/imgui/blob/master/backends/imgui_impl_sdlrenderer3.h
/// </summary>
public unsafe static class ImGuiSDL3Renderer
{
private sealed class TextureState
{
public ImTextureDataPtr Source;
public readonly Dictionary<nint, nint> RendererTextures = new();
}
public class RendererData
{
public nint Renderer; // Main viewport's renderer
public ImVector<SDL.FColor> ColorBuffer;
// Render State
public SDL.ScaleMode CurrentScaleMode;
}
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);
private static readonly RendererCreateWindowFn RendererCreateWindowDelegate = RendererCreateWindow;
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate void RendererDestroyWindowFn(ImGuiViewportPtr viewport);
private static readonly RendererDestroyWindowFn RendererDestroyWindowDelegate = RendererDestroyWindow;
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate void RendererSetWindowSizeFn(ImGuiViewportPtr viewport, Vector2 size);
private static readonly RendererSetWindowSizeFn RendererSetWindowSizeDelegate = RendererSetWindowSize;
[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)
{
ImGuiIOPtr io = ImGui.GetIO();
RendererData bd = new RendererData();
io.BackendRendererUserData = ImGuiUserData<RendererData>.Store(bd);
io.BackendRendererName = (byte*)Marshal.StringToHGlobalAnsi("NepImGuiSDL3Renderer");
io.BackendFlags |= ImGuiBackendFlags.RendererHasVtxOffset; // We can honor the ImDrawCmd.VtxOffset field, allowing for large meshes.
io.BackendFlags |= ImGuiBackendFlags.RendererHasTextures; // We can honor ImGuiPlatformIO.Textures[] requests during render.
io.BackendFlags |= ImGuiBackendFlags.RendererHasViewports;
bd.Renderer = renderer;
ImGuiPlatformIOPtr platformIO = ImGui.GetPlatformIO();
platformIO.RendererCreateWindow = (void*)Marshal.GetFunctionPointerForDelegate(RendererCreateWindowDelegate);
platformIO.RendererDestroyWindow = (void*)Marshal.GetFunctionPointerForDelegate(RendererDestroyWindowDelegate);
platformIO.RendererSetWindowSize = (void*)Marshal.GetFunctionPointerForDelegate(RendererSetWindowSizeDelegate);
platformIO.RendererRenderWindow = (void*)Marshal.GetFunctionPointerForDelegate(RendererRenderWindowDelegate);
platformIO.RendererSwapBuffers = (void*)Marshal.GetFunctionPointerForDelegate(RendererSwapBuffersDelegate);
return true;
}
public static void Dispose()
{
var io = ImGui.GetIO();
var platformIO = ImGui.GetPlatformIO();
DestroyDeviceObjects();
io.BackendRendererName = null;
io.BackendRendererUserData = null;
io.BackendFlags &= ~(ImGuiBackendFlags.RendererHasVtxOffset | ImGuiBackendFlags.RendererHasTextures);
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)
{
Program.Logger.Log(null);
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, (string?)null);
vd.RendererOwned = true;
}
Program.Logger.Log(null);
}
private static void RendererDestroyWindow(ImGuiViewportPtr viewport)
{
Program.Logger.Log(null);
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;
Program.Logger.Log(null);
}
private static void RendererSetWindowSize(ImGuiViewportPtr viewport, Vector2 size)
{
// SDL renderer windows track size through the platform window callback.
Program.Logger.Log(null);
}
private static void RendererRenderWindow(ImGuiViewportPtr viewport)
{
Program.Logger.Log(null);
ImGuiSDL3Platform.ViewPortData? vd = ImGuiUserData<ImGuiSDL3Platform.ViewPortData>.Get(viewport.PlatformUserData);
if (vd == null || vd.Renderer == nint.Zero)
{
return;
}
RenderDrawData(viewport.DrawData, vd.Renderer);
Program.Logger.Log(null);
}
private static void RendererSwapBuffers(ImGuiViewportPtr viewport)
{
Program.Logger.Log(null);
ImGuiSDL3Platform.ViewPortData? vd = ImGuiUserData<ImGuiSDL3Platform.ViewPortData>.Get(viewport.PlatformUserData);
if (vd == null || vd.Renderer == nint.Zero)
return;
SDL.RenderPresent(vd.Renderer);
Program.Logger.Log(null);
}
public static void RenderDrawData(ImDrawDataPtr drawData, nint renderer)
{
// Skip if no data to render
if (drawData.Handle == null || drawData.CmdListsCount == 0)
return;
SDL.GetRenderScale(renderer, out float renderScaleX, out float renderScaleY);
Vector2 renderScale = new Vector2(
renderScaleX == 1.0f ? drawData.FramebufferScale.X : 1.0f,
renderScaleY == 1.0f ? drawData.FramebufferScale.Y : 1.0f);
int fbWidth = (int)(drawData.DisplaySize.X * renderScale.X);
int fbHeight = (int)(drawData.DisplaySize.Y * renderScale.Y);
if (fbWidth <= 0 || fbHeight <= 0)
return;
for (int i = 0; i < drawData.Textures.Size; i++)
{
var texture = drawData.Textures[i];
UpdateTexture(texture, renderer);
}
// Backup SDL renderer state
BackupSDLRendererState old = new BackupSDLRendererState
{
ViewportEnabled = SDL.RenderViewportSet(renderer),
ClipEnabled = SDL.RenderClipEnabled(renderer)
};
SDL.GetRenderViewport(renderer, out var oldViewport);
old.Viewport = oldViewport;
SDL.GetRenderClipRect(renderer, out var oldClipRect);
old.ClipRect = oldClipRect;
// Set up render state
SDL.SetRenderViewport(renderer, 0);
SDL.SetRenderClipRect(renderer, nint.Zero);
// Set render state in platform IO
ImGuiPlatformIOPtr platformIo = ImGui.GetPlatformIO();
platformIo.RendererRenderState = (void*)renderer;
Vector2 clipOffset = drawData.DisplayPos;
// Render command lists
for (int n = 0; n < drawData.CmdListsCount; n++)
{
ImDrawListPtr cmdList = drawData.CmdLists[n];
for (int cmdIndex = 0; cmdIndex < cmdList.CmdBuffer.Size; cmdIndex++)
{
ImDrawCmd cmd = cmdList.CmdBuffer[cmdIndex];
if (cmd.UserCallback != null)
{
continue; // User callback not implemented
}
else
{
// 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
{
X = (int)clipMin.X,
Y = (int)clipMin.Y,
W = (int)(clipMax.X - clipMin.X),
H = (int)(clipMax.Y - clipMin.Y)
};
SDL.SetRenderClipRect(renderer, r);
// Get texture
nint texId = ResolveTextureId(cmd.GetTexID(), renderer);
// Convert ImGui vertices to SDL vertices
if (!RenderDrawCommand(cmdList, cmd, renderer, texId, renderScale))
{
Console.WriteLine($"Failed to render ImGui draw command: {SDL.GetError()}");
}
}
}
}
// Reset render state
platformIo.RendererRenderState = null;
// Restore renderer state
SDL.SetRenderViewport(renderer, old.ViewportEnabled ? old.Viewport : new SDL.Rect());
SDL.SetRenderClipRect(renderer, old.ClipEnabled ? old.ClipRect : new SDL.Rect());
}
private static nint ResolveTextureId(ImTextureID texId, nint renderer)
{
if (texId == ImTextureID.Null)
return nint.Zero;
TextureState? state = ImGuiUserData<TextureState>.Get((void*)(nint)texId);
if (state == null)
return (nint)texId;
if (!state.RendererTextures.TryGetValue(renderer, out nint rendererTexture) || rendererTexture == nint.Zero)
{
rendererTexture = CreateRendererTexture(state, renderer);
state.RendererTextures[renderer] = rendererTexture;
}
return rendererTexture;
}
private static nint CreateRendererTexture(TextureState state, nint renderer)
{
ImTextureDataPtr tex = state.Source;
// We keep ARGB8888 here because this project already relies on that upload path.
nint sdlTexture = SDL.CreateTexture(renderer, SDL.PixelFormat.ARGB8888, SDL.TextureAccess.Static, tex.Width, tex.Height);
if (sdlTexture == nint.Zero)
return nint.Zero;
SDL.UpdateTexture(sdlTexture, nint.Zero, (nint)tex.GetPixels(), tex.GetPitch());
SDL.SetTextureBlendMode(sdlTexture, SDL.BlendMode.Blend);
SDL.SetTextureScaleMode(sdlTexture, SDL.ScaleMode.Linear);
return sdlTexture;
}
private static void UploadRendererTexture(ImTextureDataPtr tex, nint renderer, nint sdlTexture)
{
if (tex.Status == ImTextureStatus.WantUpdates)
{
for (int i = 0; i < tex.Updates.Size; i++)
{
var r = tex.Updates[i];
SDL.Rect rect = new SDL.Rect
{
X = r.X,
Y = r.Y,
W = r.W,
H = r.H
};
SDL.UpdateTexture(sdlTexture, rect, (nint)tex.GetPixelsAt(r.X, r.Y), tex.GetPitch());
}
}
else
{
SDL.UpdateTexture(sdlTexture, nint.Zero, (nint)tex.GetPixels(), tex.GetPitch());
}
}
private static void UpdateTexture(ImTextureDataPtr tex, nint renderer)
{
TextureState? state = null;
if ((nint)tex.BackendUserData != nint.Zero)
state = ImGuiUserData<TextureState>.Get(tex.BackendUserData);
if (state == null)
{
state = new TextureState();
tex.BackendUserData = ImGuiUserData<TextureState>.Store(state);
}
state.Source = tex;
tex.SetTexID((nint)tex.BackendUserData);
if (tex.Status == ImTextureStatus.WantDestroy)
{
foreach (nint rendererTexture in state.RendererTextures.Values)
{
if (rendererTexture != nint.Zero)
SDL.DestroyTexture(rendererTexture);
}
state.RendererTextures.Clear();
ImGuiUserData<TextureState>.Free(tex.BackendUserData);
tex.BackendUserData = (void*)nint.Zero;
tex.SetTexID(ImTextureID.Null);
tex.SetStatus(ImTextureStatus.Destroyed);
return;
}
bool hasRendererTexture = state.RendererTextures.TryGetValue(renderer, out nint sdlTexture) && sdlTexture != nint.Zero;
if (!hasRendererTexture)
{
sdlTexture = CreateRendererTexture(state, renderer);
state.RendererTextures[renderer] = sdlTexture;
}
if (sdlTexture != nint.Zero && (tex.Status == ImTextureStatus.WantCreate || tex.Status == ImTextureStatus.WantUpdates || !hasRendererTexture))
{
UploadRendererTexture(tex, renderer, sdlTexture);
tex.SetStatus(ImTextureStatus.Ok);
}
}
private static bool RenderDrawCommand(ImDrawListPtr drawList, ImDrawCmd cmd, nint renderer, nint texId, Vector2 scale)
{
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 * scale.X,
Y = srcVert.Pos.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;
for (int i = 0; i < texures.Size; i++)
{
var texture = texures[i];
texture.Status = ImTextureStatus.WantDestroy;
UpdateTexture(texture, nint.Zero);
}
}
}
+221
View File
@@ -0,0 +1,221 @@
using System;
using System.Diagnostics;
using System.Numerics;
using System.Runtime.InteropServices;
using Hexa.NET.ImGui;
using Hexa.NET.ImGui.Utilities;
using Hexa.NET.ImGuizmo;
using Hexa.NET.ImPlot;
using Hexa.NET.ImPlot3D;
using SDL3;
namespace SDL3_TestingSuite.SDL3;
public sealed unsafe class SDL3Window : IDisposable
{
public readonly nint Window;
public readonly nint Renderer;
internal Action? RenderCallback { get; set; }
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;
public bool Disposed => _disposed;
private bool _disposed;
private FileSystemWatcher? _watcher;
public SDL3Window(string name, int posX, int posY, int width, int height, SDL.WindowFlags flags = SDL.WindowFlags.Resizable | SDL.WindowFlags.HighPixelDensity)
{
if (!SDL.Init(SDL.InitFlags.Events | SDL.InitFlags.Video | SDL.InitFlags.Gamepad))
throw new Exception($"SDL_Init failed: {SDL.GetError()}");
// Create window & renderer
if (!SDL.CreateWindowAndRenderer(name, width, height, flags, out Window, out Renderer))
throw new Exception($"SDL_CreateWindowAndRenderer failed: {SDL.GetError()}");
SDL.SetWindowPosition(Window, posX, posY);
SDL.SetRenderVSync(Renderer, 1);
SDL.ShowWindow(Window);
// Create ImGui context
var context = ImGui.CreateContext();
ImPlot.CreateContext();
ImPlot.SetImGuiContext(context);
ImGuizmo.SetImGuiContext(context);
// ImPlot3D.SetImGuiContext(context);
// ImPlot3D.CreateContext();
ImGuiIOPtr io = ImGui.GetIO();
io.ConfigFlags |= ImGuiConfigFlags.NavEnableKeyboard | ImGuiConfigFlags.NavEnableGamepad | ImGuiConfigFlags.DockingEnable;
// io.ConfigFlags |= ImGuiConfigFlags.ViewportsEnable;
io.Fonts.Flags |= ImFontAtlasFlags.NoBakedLines;
io.Fonts.AddFontDefault();
try
{
string fontsPath = Path.Combine(AppContext.BaseDirectory, "Fonts");
if (!Path.Exists(fontsPath)) Directory.CreateDirectory(fontsPath);
_watcher = new FileSystemWatcher(fontsPath);
_watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.CreationTime;
_watcher.Created += (a, b) =>
{
if (!File.Exists(b.FullPath)) return;
if (Path.GetExtension(b.FullPath) != ".ttf") return;
ImGui.GetIO().AddFont(b.FullPath);
};
_watcher.Deleted += (a, b) =>
{
if (Path.GetExtension(b.FullPath) != ".ttf") return;
ImGui.GetIO().RemoveFont(b.Name);
};
_watcher.IncludeSubdirectories = false;
_watcher.EnableRaisingEvents = true;
string[] fonts = Directory.GetFiles(fontsPath, "*.ttf", SearchOption.AllDirectories);
foreach (string font in fonts)
{
io.AddFont(font);
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
// Init platform and renderer
ImGuiSDL3Platform.Init(Window, Renderer);
ImGuiSDL3Renderer.Init(Renderer);
}
public void Run()
{
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))
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);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (_disposed) return;
_disposed = true;
if (disposing)
{
ImGuiSDL3Renderer.Dispose();
ImGuiSDL3Platform.Dispose();
RenderCallback = null;
}
if (_imGuiContext.Handle != null)
{
ImGui.SetCurrentContext(null);
ImGui.DestroyContext(_imGuiContext);
_imGuiContext = null;
}
if (Renderer != nint.Zero)
{
SDL.DestroyRenderer(Renderer);
}
if (Window != nint.Zero)
{
SDL.DestroyWindow(Window);
}
}
}