-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Rofli Sanches
committed
May 15, 2021
1 parent
cc7550d
commit 0fd2707
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.