using System.Reflection; using System.Runtime.InteropServices; namespace GamePort.Platform; internal static unsafe class Sdl { private const string Library="SDL3"; static Sdl() { NativeLibrary.SetDllImportResolver(typeof(Sdl).Assembly, (name, assembly, path) => { if(name!=Library)return 0; var filename=OperatingSystem.IsWindows()?"SDL3.dll":"libSDL3.so.0"; var localDirectory = OperatingSystem.IsWindows() ? ".tools/sdl-win" : ".tools/sdl/lib"; foreach(var candidate in new[]{Path.Combine(AppContext.BaseDirectory,filename),Path.Combine(Directory.GetCurrentDirectory(),localDirectory,filename),filename}) if(NativeLibrary.TryLoad(candidate,out var handle))return handle; return 0; }); } public const uint InitVideo=0x20, InitAudio=0x10; public const ulong WindowVulkan=0x10000000, WindowResizable=0x20, WindowHighPixelDensity=0x2000; public const uint Quit=0x100, KeyDown=0x300, KeyUp=0x301, WindowResized=0x206, WindowPixelSizeChanged=0x207, WindowFocusLost=0x20F; public const uint WindowFocusGained=0x20E, WindowMinimized=0x209, WindowRestored=0x20B, MouseButtonDown=0x401, MouseButtonUp=0x402; [StructLayout(LayoutKind.Explicit,Size=128)] public struct Event { [FieldOffset(0)]public uint Type; [FieldOffset(16)]public uint WindowId; [FieldOffset(24)]public uint Scancode; [FieldOffset(24)]public byte MouseButton; [FieldOffset(28)]public uint Key; [FieldOffset(36)]public byte Down; [FieldOffset(37)]public byte Repeat; } [DllImport(Library,CallingConvention=CallingConvention.Cdecl)] [return:MarshalAs(UnmanagedType.I1)] public static extern bool SDL_Init(uint flags); [DllImport(Library,CallingConvention=CallingConvention.Cdecl)] public static extern void SDL_Quit(); [DllImport(Library,CallingConvention=CallingConvention.Cdecl)] private static extern nint SDL_GetError(); public static string Error=>Marshal.PtrToStringUTF8(SDL_GetError())??"SDL error"; [DllImport(Library,CallingConvention=CallingConvention.Cdecl)] public static extern nint SDL_CreateWindow([MarshalAs(UnmanagedType.LPUTF8Str)]string title,int w,int h,ulong flags); [DllImport(Library,CallingConvention=CallingConvention.Cdecl)]public static extern void SDL_DestroyWindow(nint window); [DllImport(Library,CallingConvention=CallingConvention.Cdecl)] [return:MarshalAs(UnmanagedType.I1)]public static extern bool SDL_SetWindowTitle(nint window,[MarshalAs(UnmanagedType.LPUTF8Str)]string title); [DllImport(Library,CallingConvention=CallingConvention.Cdecl)] [return:MarshalAs(UnmanagedType.I1)]public static extern bool SDL_PollEvent(out Event e); [DllImport(Library,CallingConvention=CallingConvention.Cdecl)]public static extern byte** SDL_Vulkan_GetInstanceExtensions(out uint count); [DllImport(Library,CallingConvention=CallingConvention.Cdecl)] [return:MarshalAs(UnmanagedType.I1)]public static extern bool SDL_Vulkan_CreateSurface(nint window,nint instance,void* allocator,out ulong surface); [DllImport(Library,CallingConvention=CallingConvention.Cdecl)] [return:MarshalAs(UnmanagedType.I1)]public static extern bool SDL_GetWindowSizeInPixels(nint window,out int w,out int h); [DllImport(Library,CallingConvention=CallingConvention.Cdecl)] [return:MarshalAs(UnmanagedType.I1)]public static extern bool SDL_GetWindowSize(nint window,out int w,out int h); [DllImport(Library,CallingConvention=CallingConvention.Cdecl)]public static extern uint SDL_GetMouseState(out float x,out float y); [DllImport(Library,CallingConvention=CallingConvention.Cdecl)] [return:MarshalAs(UnmanagedType.I1)]public static extern bool SDL_SetWindowFullscreen(nint window,[MarshalAs(UnmanagedType.I1)]bool fullscreen); [DllImport(Library,CallingConvention=CallingConvention.Cdecl)] [return:MarshalAs(UnmanagedType.I1)]public static extern bool SDL_SetWindowRelativeMouseMode(nint window,[MarshalAs(UnmanagedType.I1)]bool enabled); [DllImport(Library,CallingConvention=CallingConvention.Cdecl)] [return:MarshalAs(UnmanagedType.I1)]public static extern bool SDL_SetWindowSize(nint window,int width,int height); [DllImport(Library,CallingConvention=CallingConvention.Cdecl)] [return:MarshalAs(UnmanagedType.I1)]public static extern bool SDL_PushEvent(ref Event e); [StructLayout(LayoutKind.Sequential)]public struct AudioSpec {public uint Format;public int Channels;public int Frequency;} [DllImport(Library,CallingConvention=CallingConvention.Cdecl)] [return:MarshalAs(UnmanagedType.I1)]public static extern bool SDL_InitSubSystem(uint flags); [DllImport(Library,CallingConvention=CallingConvention.Cdecl)]public static extern void SDL_QuitSubSystem(uint flags); [DllImport(Library,CallingConvention=CallingConvention.Cdecl)] [return:MarshalAs(UnmanagedType.I1)]public static extern bool SDL_LoadWAV([MarshalAs(UnmanagedType.LPUTF8Str)]string path,out AudioSpec spec,out nint buffer,out uint length); [DllImport(Library,CallingConvention=CallingConvention.Cdecl)]public static extern void SDL_free(nint buffer); [DllImport(Library,CallingConvention=CallingConvention.Cdecl)]public static extern nint SDL_OpenAudioDeviceStream(uint device,ref AudioSpec spec,nint callback,nint user); [DllImport(Library,CallingConvention=CallingConvention.Cdecl)] [return:MarshalAs(UnmanagedType.I1)]public static extern bool SDL_ResumeAudioStreamDevice(nint stream); [DllImport(Library,CallingConvention=CallingConvention.Cdecl)] [return:MarshalAs(UnmanagedType.I1)]public static extern bool SDL_PauseAudioStreamDevice(nint stream); [DllImport(Library,CallingConvention=CallingConvention.Cdecl)] [return:MarshalAs(UnmanagedType.I1)]public static extern bool SDL_PutAudioStreamData(nint stream,nint buffer,int length); [DllImport(Library,CallingConvention=CallingConvention.Cdecl)]public static extern int SDL_GetAudioStreamQueued(nint stream); [DllImport(Library,CallingConvention=CallingConvention.Cdecl)]public static extern void SDL_DestroyAudioStream(nint stream); [DllImport(Library,CallingConvention=CallingConvention.Cdecl)]public static extern void SDL_Delay(uint ms); }