Skip to content

Commit

Permalink
feat: add imgui extesion
Browse files Browse the repository at this point in the history
  • Loading branch information
Rofli Sanches committed May 15, 2021
1 parent cc7550d commit 0fd2707
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
59 changes: 59 additions & 0 deletions Source/Utils/ImGuiExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using ImGuiNET;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;

namespace UImGui
{
internal static unsafe class ImGuiExtension
{
private static readonly HashSet<IntPtr> _managedAllocations = new HashSet<IntPtr>(); // TODO: Check if yet IntPtr has boxing when comparing equality (see original version)

internal static void SetBackendPlatformName(this ImGuiIOPtr io, string name)
{
if (io.NativePtr->BackendPlatformName != (byte*)0)
{
if (_managedAllocations.Contains((IntPtr)io.NativePtr->BackendPlatformName))
{
Marshal.FreeHGlobal(new IntPtr(io.NativePtr->BackendPlatformName));
}
io.NativePtr->BackendPlatformName = (byte*)0;
}
if (name != null)
{
int byteCount = Encoding.UTF8.GetByteCount(name);
byte* nativeName = (byte*)Marshal.AllocHGlobal(byteCount + 1);
int offset = Utils.GetUtf8(name, nativeName, byteCount);

nativeName[offset] = 0;

io.NativePtr->BackendPlatformName = nativeName;
_managedAllocations.Add((IntPtr)nativeName);
}
}

internal static void SetIniFilename(this ImGuiIOPtr io, string name)
{
if (io.NativePtr->IniFilename != (byte*)0)
{
if (_managedAllocations.Contains((IntPtr)io.NativePtr->IniFilename))
{
Marshal.FreeHGlobal((IntPtr)io.NativePtr->IniFilename);
}
io.NativePtr->IniFilename = (byte*)0;
}
if (name != null)
{
int byteCount = Encoding.UTF8.GetByteCount(name);
byte* nativeName = (byte*)Marshal.AllocHGlobal(byteCount + 1);
int offset = Utils.GetUtf8(name, nativeName, byteCount);

nativeName[offset] = 0;

io.NativePtr->IniFilename = nativeName;
_managedAllocations.Add((IntPtr)nativeName);
}
}
}
}
11 changes: 11 additions & 0 deletions Source/Utils/ImGuiExtension.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0fd2707

Please sign in to comment.