Skip to content

Commit

Permalink
Changing internal logging delegate to work with Emscripten/WASM
Browse files Browse the repository at this point in the history
  • Loading branch information
NoelFB committed Feb 12, 2024
1 parent 72c4506 commit 71cd134
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions Framework/Platform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public enum FosterEventType : int
ControllerAxis
}

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void FosterLogFn(IntPtr msg, int type);
// [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
// public delegate void FosterLogFn(IntPtr msg, int type);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void FosterWriteFn(IntPtr context, IntPtr data, int size);
Expand Down Expand Up @@ -191,6 +191,7 @@ public static void FreeUTF8(IntPtr ptr)
Marshal.FreeHGlobal(ptr);
}

[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
private static void HandleLog(IntPtr msg, int type)
{
switch (type)
Expand All @@ -202,18 +203,18 @@ private static void HandleLog(IntPtr msg, int type)
}
}

// need to store static references otherwise the delegates will get collected
private static readonly FosterLogFn handleLog = HandleLog;

static Platform()
static unsafe Platform()
{
FosterSetLogCallback(handleLog, 0);
// This is done in this way so that the delegate is compatible with WASM/Emscripten,
// which do not accept normal delegates (only unmanaged function pointer ones like this)
delegate* unmanaged[Cdecl]<IntPtr, int, void> fn = &HandleLog;
FosterSetLogCallback((IntPtr)fn, 0);
}

[DllImport(DLL)]
public static extern void FosterStartup(FosterDesc desc);
public static extern void FosterSetLogCallback(IntPtr logFn, int level);
[DllImport(DLL)]
public static extern void FosterSetLogCallback(FosterLogFn logFn, int level);
public static extern void FosterStartup(FosterDesc desc);
[DllImport(DLL)]
public static extern void FosterBeginFrame();
[DllImport(DLL)]
Expand Down Expand Up @@ -312,4 +313,7 @@ static Platform()
public static extern void FosterDraw(ref FosterDrawCommand command);
[DllImport(DLL)]
public static extern void FosterClear(ref FosterClearCommand command);

// [DllImport(DLL)]
// public static extern void emscripten_set_main_loop(IntPtr action, int fps, bool simulateInfiniteLoop);
}

0 comments on commit 71cd134

Please sign in to comment.