using NativeLibraryLoader; using System; using System.Diagnostics; using System.Runtime.InteropServices; namespace Veldrid.Sdl2 { public static unsafe partial class Sdl2Native { private static readonly NativeLibraryLoader.NativeLoader s_sdl2Lib = LoadSdl2(); private static NativeLoader LoadSdl2() { string[] names; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { names = new[] { "SDL2.dll" }; } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { names = new[] { "libSDL2-2.0.so", "libSDL2-2.0.so.0", "libSDL2-2.0.so.1", }; } else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { names = new[] { "libsdl2.dylib" }; } else { Debug.WriteLine("Unknown SDL platform. Attempting to load \"SDL2\""); names = new[] { "SDL2.dll" }; } NativeLoader lib = new NativeLoader("sdl2"); return lib; } /// /// Loads an SDL2 function by the given name. /// /// The delegate type of the function to load. /// The name of the exported native function. /// A delegate which can be used to invoke the native function. /// Thrown when no function with the given name is exported by SDL2. /// public static T LoadFunction(string name) { try { return s_sdl2Lib.LoadFunction(name); } catch { Debug.WriteLine( $"Unable to load SDL2 function \"{name}\". " + $"Attempting to call this function will cause an exception to be thrown."); return default(T); } } [UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate byte* SDL_GetError_t(); private static SDL_GetError_t s_sdl_getError = LoadFunction("SDL_GetError"); public static byte* SDL_GetError() => s_sdl_getError(); [UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate void SDL_ClearError_t(); private static SDL_ClearError_t s_sdl_clearError = LoadFunction("SDL_ClearError"); public static byte* SDL_ClearError() { s_sdl_clearError(); return null; } [UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate void SDL_free_t(void* ptr); private static SDL_free_t s_sdl_free = LoadFunction("SDL_free"); public static void SDL_free(void* ptr) { s_sdl_free(ptr); } } }