From 040d39b4a54c075f9647ef77d889adec3e6cd6c5 Mon Sep 17 00:00:00 2001 From: Chad Yates Date: Thu, 21 May 2020 07:00:19 -0700 Subject: [PATCH 01/17] Avoid string and byte[] allocation when converting to UTF8 native string (#192) --- src/SDL2.cs | 458 ++++++++++++++++++++++++++++++---------------- src/SDL2_image.cs | 75 +++++--- 2 files changed, 348 insertions(+), 185 deletions(-) diff --git a/src/SDL2.cs b/src/SDL2.cs index ab90834..faf9a2d 100644 --- a/src/SDL2.cs +++ b/src/SDL2.cs @@ -28,7 +28,9 @@ #region Using Statements using System; +using System.Diagnostics; using System.Runtime.InteropServices; +using System.Text; #endregion namespace SDL2 @@ -43,19 +45,66 @@ namespace SDL2 #region UTF8 Marshaling - internal static byte[] UTF8_ToNative(string s) + /* Used for stack allocated string marshaling. */ + internal static int Utf8Size(string str) { - if (s == null) + Debug.Assert(str != null); + return (str.Length * 4) + 1; + } + internal static int Utf8SizeNullable(string str) + { + return str != null ? (str.Length * 4) + 1 : 0; + } + internal static unsafe byte* Utf8Encode(string str, byte* buffer, int bufferSize) + { + Debug.Assert(str != null); + fixed (char* strPtr = str) { - return null; + Encoding.UTF8.GetBytes(strPtr, str.Length + 1, buffer, bufferSize); } + return buffer; + } + internal static unsafe byte* Utf8EncodeNullable(string str, byte* buffer, int bufferSize) + { + if (str == null) + { + buffer[0] = 0; + return buffer; + } + fixed (char* strPtr = str) + { + Encoding.UTF8.GetBytes(strPtr, str.Length + 1, buffer, bufferSize); + } + return buffer; + } - // Add a null terminator. That's kind of it... :/ - return System.Text.Encoding.UTF8.GetBytes(s + '\0'); + /* Used for heap allocated string marshaling. + * Returned byte* must be free'd with FreeHGlobal. + */ + internal static unsafe byte* Utf8Encode(string str) + { + Debug.Assert(str != null); + int bufferSize = Utf8Size(str); + byte* buffer = (byte*)Marshal.AllocHGlobal(bufferSize); + fixed (char* strPtr = str) + { + Encoding.UTF8.GetBytes(strPtr, str.Length + 1, buffer, bufferSize); + } + return buffer; + } + internal static unsafe byte* Utf8EncodeNullable(string str) + { + int bufferSize = Utf8SizeNullable(str); + byte* buffer = (byte*)Marshal.AllocHGlobal(bufferSize); + fixed (char* strPtr = str) + { + Encoding.UTF8.GetBytes(strPtr, str != null ? str.Length + 1 : 0, buffer, bufferSize); + } + return buffer; } /* This is public because SDL_DropEvent needs it! */ - public static unsafe string UTF8_ToManaged(IntPtr s, bool freePtr = false) + internal static unsafe string UTF8_ToManaged(IntPtr s, bool freePtr = false) { if (s == IntPtr.Zero) { @@ -205,18 +254,23 @@ namespace SDL2 /* IntPtr refers to an SDL_RWops* */ [DllImport(nativeLibName, EntryPoint = "SDL_RWFromFile", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_SDL_RWFromFile( - byte[] file, - byte[] mode + private static extern unsafe IntPtr INTERNAL_SDL_RWFromFile( + byte* file, + byte* mode ); - public static IntPtr SDL_RWFromFile( + public static unsafe IntPtr SDL_RWFromFile( string file, string mode ) { - return INTERNAL_SDL_RWFromFile( - UTF8_ToNative(file), - UTF8_ToNative(mode) + byte* utf8File = Utf8Encode(file); + byte* utf8Mode = Utf8Encode(mode); + IntPtr rwOps = INTERNAL_SDL_RWFromFile( + utf8File, + utf8Mode ); + Marshal.FreeHGlobal((IntPtr)utf8Mode); + Marshal.FreeHGlobal((IntPtr)utf8File); + return rwOps; } /* IntPtr refers to an SDL_RWops* */ @@ -610,59 +664,75 @@ namespace SDL2 public static extern void SDL_ClearHints(); [DllImport(nativeLibName, EntryPoint = "SDL_GetHint", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_SDL_GetHint(byte[] name); - public static string SDL_GetHint(string name) + private static extern unsafe IntPtr INTERNAL_SDL_GetHint(byte* name); + public static unsafe string SDL_GetHint(string name) { + int utf8NameBufSize = Utf8Size(name); + byte* utf8Name = stackalloc byte[utf8NameBufSize]; return UTF8_ToManaged( INTERNAL_SDL_GetHint( - UTF8_ToNative(name) + Utf8Encode(name, utf8Name, utf8NameBufSize) ) ); } [DllImport(nativeLibName, EntryPoint = "SDL_SetHint", CallingConvention = CallingConvention.Cdecl)] - private static extern SDL_bool INTERNAL_SDL_SetHint( - byte[] name, - byte[] value + private static extern unsafe SDL_bool INTERNAL_SDL_SetHint( + byte* name, + byte* value ); - public static SDL_bool SDL_SetHint(string name, string value) + public static unsafe SDL_bool SDL_SetHint(string name, string value) { + int utf8NameBufSize = Utf8Size(name); + byte* utf8Name = stackalloc byte[utf8NameBufSize]; + + int utf8ValueBufSize = Utf8Size(value); + byte* utf8Value = stackalloc byte[utf8ValueBufSize]; + return INTERNAL_SDL_SetHint( - UTF8_ToNative(name), - UTF8_ToNative(value) + Utf8Encode(name, utf8Name, utf8NameBufSize), + Utf8Encode(value, utf8Value, utf8ValueBufSize) ); } [DllImport(nativeLibName, EntryPoint = "SDL_SetHintWithPriority", CallingConvention = CallingConvention.Cdecl)] - private static extern SDL_bool INTERNAL_SDL_SetHintWithPriority( - byte[] name, - byte[] value, + private static extern unsafe SDL_bool INTERNAL_SDL_SetHintWithPriority( + byte* name, + byte* value, SDL_HintPriority priority ); - public static SDL_bool SDL_SetHintWithPriority( + public static unsafe SDL_bool SDL_SetHintWithPriority( string name, string value, SDL_HintPriority priority ) { + int utf8NameBufSize = Utf8Size(name); + byte* utf8Name = stackalloc byte[utf8NameBufSize]; + + int utf8ValueBufSize = Utf8Size(value); + byte* utf8Value = stackalloc byte[utf8ValueBufSize]; + return INTERNAL_SDL_SetHintWithPriority( - UTF8_ToNative(name), - UTF8_ToNative(value), + Utf8Encode(name, utf8Name, utf8NameBufSize), + Utf8Encode(value, utf8Value, utf8ValueBufSize), priority ); } /* Only available in 2.0.5 or higher. */ [DllImport(nativeLibName, EntryPoint = "SDL_GetHintBoolean", CallingConvention = CallingConvention.Cdecl)] - private static extern SDL_bool INTERNAL_SDL_GetHintBoolean( - byte[] name, + private static extern unsafe SDL_bool INTERNAL_SDL_GetHintBoolean( + byte* name, SDL_bool default_value ); - public static SDL_bool SDL_GetHintBoolean( + public static unsafe SDL_bool SDL_GetHintBoolean( string name, SDL_bool default_value ) { + int utf8NameBufSize = Utf8Size(name); + byte* utf8Name = stackalloc byte[utf8NameBufSize]; return INTERNAL_SDL_GetHintBoolean( - UTF8_ToNative(name), + Utf8Encode(name, utf8Name, utf8NameBufSize), default_value ); } @@ -683,11 +753,13 @@ namespace SDL2 /* Use string.Format for arglists */ [DllImport(nativeLibName, EntryPoint = "SDL_SetError", CallingConvention = CallingConvention.Cdecl)] - private static extern void INTERNAL_SDL_SetError(byte[] fmtAndArglist); - public static void SDL_SetError(string fmtAndArglist) + private static extern unsafe void INTERNAL_SDL_SetError(byte* fmtAndArglist); + public static unsafe void SDL_SetError(string fmtAndArglist) { + int utf8FmtAndArglistBufSize = Utf8Size(fmtAndArglist); + byte* utf8FmtAndArglist = stackalloc byte[utf8FmtAndArglistBufSize]; INTERNAL_SDL_SetError( - UTF8_ToNative(fmtAndArglist) + Utf8Encode(fmtAndArglist, utf8FmtAndArglist, utf8FmtAndArglistBufSize) ); } @@ -752,145 +824,163 @@ namespace SDL2 /* Use string.Format for arglists */ [DllImport(nativeLibName, EntryPoint = "SDL_Log", CallingConvention = CallingConvention.Cdecl)] - private static extern void INTERNAL_SDL_Log(byte[] fmtAndArglist); - public static void SDL_Log(string fmtAndArglist) + private static extern unsafe void INTERNAL_SDL_Log(byte* fmtAndArglist); + public static unsafe void SDL_Log(string fmtAndArglist) { + int utf8FmtAndArglistBufSize = Utf8Size(fmtAndArglist); + byte* utf8FmtAndArglist = stackalloc byte[utf8FmtAndArglistBufSize]; INTERNAL_SDL_Log( - UTF8_ToNative(fmtAndArglist) + Utf8Encode(fmtAndArglist, utf8FmtAndArglist, utf8FmtAndArglistBufSize) ); } /* Use string.Format for arglists */ [DllImport(nativeLibName, EntryPoint = "SDL_LogVerbose", CallingConvention = CallingConvention.Cdecl)] - private static extern void INTERNAL_SDL_LogVerbose( + private static extern unsafe void INTERNAL_SDL_LogVerbose( int category, - byte[] fmtAndArglist + byte* fmtAndArglist ); - public static void SDL_LogVerbose( + public static unsafe void SDL_LogVerbose( int category, string fmtAndArglist ) { + int utf8FmtAndArglistBufSize = Utf8Size(fmtAndArglist); + byte* utf8FmtAndArglist = stackalloc byte[utf8FmtAndArglistBufSize]; INTERNAL_SDL_LogVerbose( category, - UTF8_ToNative(fmtAndArglist) + Utf8Encode(fmtAndArglist, utf8FmtAndArglist, utf8FmtAndArglistBufSize) ); } /* Use string.Format for arglists */ [DllImport(nativeLibName, EntryPoint = "SDL_LogDebug", CallingConvention = CallingConvention.Cdecl)] - private static extern void INTERNAL_SDL_LogDebug( + private static extern unsafe void INTERNAL_SDL_LogDebug( int category, - byte[] fmtAndArglist + byte* fmtAndArglist ); - public static void SDL_LogDebug( + public static unsafe void SDL_LogDebug( int category, string fmtAndArglist ) { + int utf8FmtAndArglistBufSize = Utf8Size(fmtAndArglist); + byte* utf8FmtAndArglist = stackalloc byte[utf8FmtAndArglistBufSize]; INTERNAL_SDL_LogDebug( category, - UTF8_ToNative(fmtAndArglist) + Utf8Encode(fmtAndArglist, utf8FmtAndArglist, utf8FmtAndArglistBufSize) ); } /* Use string.Format for arglists */ [DllImport(nativeLibName, EntryPoint = "SDL_LogInfo", CallingConvention = CallingConvention.Cdecl)] - private static extern void INTERNAL_SDL_LogInfo( + private static extern unsafe void INTERNAL_SDL_LogInfo( int category, - byte[] fmtAndArglist + byte* fmtAndArglist ); - public static void SDL_LogInfo( + public static unsafe void SDL_LogInfo( int category, string fmtAndArglist ) { + int utf8FmtAndArglistBufSize = Utf8Size(fmtAndArglist); + byte* utf8FmtAndArglist = stackalloc byte[utf8FmtAndArglistBufSize]; INTERNAL_SDL_LogInfo( category, - UTF8_ToNative(fmtAndArglist) + Utf8Encode(fmtAndArglist, utf8FmtAndArglist, utf8FmtAndArglistBufSize) ); } /* Use string.Format for arglists */ [DllImport(nativeLibName, EntryPoint = "SDL_LogWarn", CallingConvention = CallingConvention.Cdecl)] - private static extern void INTERNAL_SDL_LogWarn( + private static extern unsafe void INTERNAL_SDL_LogWarn( int category, - byte[] fmtAndArglist + byte* fmtAndArglist ); - public static void SDL_LogWarn( + public static unsafe void SDL_LogWarn( int category, string fmtAndArglist ) { + int utf8FmtAndArglistBufSize = Utf8Size(fmtAndArglist); + byte* utf8FmtAndArglist = stackalloc byte[utf8FmtAndArglistBufSize]; INTERNAL_SDL_LogWarn( category, - UTF8_ToNative(fmtAndArglist) + Utf8Encode(fmtAndArglist, utf8FmtAndArglist, utf8FmtAndArglistBufSize) ); } /* Use string.Format for arglists */ [DllImport(nativeLibName, EntryPoint = "SDL_LogError", CallingConvention = CallingConvention.Cdecl)] - private static extern void INTERNAL_SDL_LogError( + private static extern unsafe void INTERNAL_SDL_LogError( int category, - byte[] fmtAndArglist + byte* fmtAndArglist ); - public static void SDL_LogError( + public static unsafe void SDL_LogError( int category, string fmtAndArglist ) { + int utf8FmtAndArglistBufSize = Utf8Size(fmtAndArglist); + byte* utf8FmtAndArglist = stackalloc byte[utf8FmtAndArglistBufSize]; INTERNAL_SDL_LogError( category, - UTF8_ToNative(fmtAndArglist) + Utf8Encode(fmtAndArglist, utf8FmtAndArglist, utf8FmtAndArglistBufSize) ); } /* Use string.Format for arglists */ [DllImport(nativeLibName, EntryPoint = "SDL_LogCritical", CallingConvention = CallingConvention.Cdecl)] - private static extern void INTERNAL_SDL_LogCritical( + private static extern unsafe void INTERNAL_SDL_LogCritical( int category, - byte[] fmtAndArglist + byte* fmtAndArglist ); - public static void SDL_LogCritical( + public static unsafe void SDL_LogCritical( int category, string fmtAndArglist ) { + int utf8FmtAndArglistBufSize = Utf8Size(fmtAndArglist); + byte* utf8FmtAndArglist = stackalloc byte[utf8FmtAndArglistBufSize]; INTERNAL_SDL_LogCritical( category, - UTF8_ToNative(fmtAndArglist) + Utf8Encode(fmtAndArglist, utf8FmtAndArglist, utf8FmtAndArglistBufSize) ); } /* Use string.Format for arglists */ [DllImport(nativeLibName, EntryPoint = "SDL_LogMessage", CallingConvention = CallingConvention.Cdecl)] - private static extern void INTERNAL_SDL_LogMessage( + private static extern unsafe void INTERNAL_SDL_LogMessage( int category, SDL_LogPriority priority, - byte[] fmtAndArglist + byte* fmtAndArglist ); - public static void SDL_LogMessage( + public static unsafe void SDL_LogMessage( int category, SDL_LogPriority priority, string fmtAndArglist ) { + int utf8FmtAndArglistBufSize = Utf8Size(fmtAndArglist); + byte* utf8FmtAndArglist = stackalloc byte[utf8FmtAndArglistBufSize]; INTERNAL_SDL_LogMessage( category, priority, - UTF8_ToNative(fmtAndArglist) + Utf8Encode(fmtAndArglist, utf8FmtAndArglist, utf8FmtAndArglistBufSize) ); } /* Use string.Format for arglists */ [DllImport(nativeLibName, EntryPoint = "SDL_LogMessageV", CallingConvention = CallingConvention.Cdecl)] - private static extern void INTERNAL_SDL_LogMessageV( + private static extern unsafe void INTERNAL_SDL_LogMessageV( int category, SDL_LogPriority priority, - byte[] fmtAndArglist + byte* fmtAndArglist ); - public static void SDL_LogMessageV( + public static unsafe void SDL_LogMessageV( int category, SDL_LogPriority priority, string fmtAndArglist ) { + int utf8FmtAndArglistBufSize = Utf8Size(fmtAndArglist); + byte* utf8FmtAndArglist = stackalloc byte[utf8FmtAndArglistBufSize]; INTERNAL_SDL_LogMessageV( category, priority, - UTF8_ToNative(fmtAndArglist) + Utf8Encode(fmtAndArglist, utf8FmtAndArglist, utf8FmtAndArglistBufSize) ); } @@ -1094,22 +1184,28 @@ namespace SDL2 /* window refers to an SDL_Window* */ [DllImport(nativeLibName, EntryPoint = "SDL_ShowSimpleMessageBox", CallingConvention = CallingConvention.Cdecl)] - private static extern int INTERNAL_SDL_ShowSimpleMessageBox( + private static extern unsafe int INTERNAL_SDL_ShowSimpleMessageBox( SDL_MessageBoxFlags flags, - byte[] title, - byte[] message, + byte* title, + byte* message, IntPtr window ); - public static int SDL_ShowSimpleMessageBox( + public static unsafe int SDL_ShowSimpleMessageBox( SDL_MessageBoxFlags flags, string title, string message, IntPtr window ) { + int utf8TitleBufSize = Utf8SizeNullable(title); + byte* utf8Title = stackalloc byte[utf8TitleBufSize]; + + int utf8MessageBufSize = Utf8SizeNullable(message); + byte* utf8Message = stackalloc byte[utf8MessageBufSize]; + return INTERNAL_SDL_ShowSimpleMessageBox( flags, - UTF8_ToNative(title), - UTF8_ToNative(message), + Utf8EncodeNullable(title, utf8Title, utf8TitleBufSize), + Utf8EncodeNullable(message, utf8Message, utf8MessageBufSize), window ); } @@ -1344,15 +1440,15 @@ namespace SDL2 /* IntPtr refers to an SDL_Window* */ [DllImport(nativeLibName, EntryPoint = "SDL_CreateWindow", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_SDL_CreateWindow( - byte[] title, + private static extern unsafe IntPtr INTERNAL_SDL_CreateWindow( + byte* title, int x, int y, int w, int h, SDL_WindowFlags flags ); - public static IntPtr SDL_CreateWindow( + public static unsafe IntPtr SDL_CreateWindow( string title, int x, int y, @@ -1360,8 +1456,10 @@ namespace SDL2 int h, SDL_WindowFlags flags ) { + int utf8TitleBufSize = Utf8SizeNullable(title); + byte* utf8Title = stackalloc byte[utf8TitleBufSize]; return INTERNAL_SDL_CreateWindow( - UTF8_ToNative(title), + Utf8EncodeNullable(title, utf8Title, utf8TitleBufSize), x, y, w, h, flags ); @@ -1521,17 +1619,19 @@ namespace SDL2 /* window refers to an SDL_Window*, IntPtr to a void* */ [DllImport(nativeLibName, EntryPoint = "SDL_GetWindowData", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_SDL_GetWindowData( + private static extern unsafe IntPtr INTERNAL_SDL_GetWindowData( IntPtr window, - byte[] name + byte* name ); - public static IntPtr SDL_GetWindowData( + public static unsafe IntPtr SDL_GetWindowData( IntPtr window, string name ) { + int utf8NameBufSize = Utf8Size(name); + byte* utf8Name = stackalloc byte[utf8NameBufSize]; return INTERNAL_SDL_GetWindowData( window, - UTF8_ToNative(name) + Utf8Encode(name, utf8Name, utf8NameBufSize) ); } @@ -1648,23 +1748,28 @@ namespace SDL2 /* IntPtr refers to a function pointer */ [DllImport(nativeLibName, EntryPoint = "SDL_GL_GetProcAddress", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_SDL_GL_GetProcAddress( - byte[] proc + private static extern unsafe IntPtr INTERNAL_SDL_GL_GetProcAddress( + byte* proc ); - public static IntPtr SDL_GL_GetProcAddress(string proc) + public static unsafe IntPtr SDL_GL_GetProcAddress(string proc) { + int utf8ProcBufSize = Utf8Size(proc); + byte* utf8Proc = stackalloc byte[utf8ProcBufSize]; return INTERNAL_SDL_GL_GetProcAddress( - UTF8_ToNative(proc) + Utf8Encode(proc, utf8Proc, utf8ProcBufSize) ); } [DllImport(nativeLibName, EntryPoint = "SDL_GL_LoadLibrary", CallingConvention = CallingConvention.Cdecl)] - private static extern int INTERNAL_SDL_GL_LoadLibrary(byte[] path); - public static int SDL_GL_LoadLibrary(string path) + private static extern unsafe int INTERNAL_SDL_GL_LoadLibrary(byte* path); + public static unsafe int SDL_GL_LoadLibrary(string path) { - return INTERNAL_SDL_GL_LoadLibrary( - UTF8_ToNative(path) + byte* utf8Path = Utf8Encode(path); + int result = INTERNAL_SDL_GL_LoadLibrary( + utf8Path ); + Marshal.FreeHGlobal((IntPtr)utf8Path); + return result; } /* IntPtr refers to a function pointer, proc to a const char* */ @@ -1675,13 +1780,15 @@ namespace SDL2 public static extern void SDL_GL_UnloadLibrary(); [DllImport(nativeLibName, EntryPoint = "SDL_GL_ExtensionSupported", CallingConvention = CallingConvention.Cdecl)] - private static extern SDL_bool INTERNAL_SDL_GL_ExtensionSupported( - byte[] extension + private static extern unsafe SDL_bool INTERNAL_SDL_GL_ExtensionSupported( + byte* extension ); - public static SDL_bool SDL_GL_ExtensionSupported(string extension) + public static unsafe SDL_bool SDL_GL_ExtensionSupported(string extension) { + int utf8ExtensionBufSize = Utf8SizeNullable(extension); + byte* utf8Extension = stackalloc byte[utf8ExtensionBufSize]; return INTERNAL_SDL_GL_ExtensionSupported( - UTF8_ToNative(extension) + Utf8Encode(extension, utf8Extension, utf8ExtensionBufSize) ); } @@ -1779,19 +1886,21 @@ namespace SDL2 /* IntPtr and userdata are void*, window is an SDL_Window* */ [DllImport(nativeLibName, EntryPoint = "SDL_SetWindowData", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_SDL_SetWindowData( + private static extern unsafe IntPtr INTERNAL_SDL_SetWindowData( IntPtr window, - byte[] name, + byte* name, IntPtr userdata ); - public static IntPtr SDL_SetWindowData( + public static unsafe IntPtr SDL_SetWindowData( IntPtr window, string name, IntPtr userdata ) { + int utf8NameBufSize = Utf8Size(name); + byte* utf8Name = stackalloc byte[utf8NameBufSize]; return INTERNAL_SDL_SetWindowData( window, - UTF8_ToNative(name), + Utf8Encode(name, utf8Name, utf8NameBufSize), userdata ); } @@ -1896,17 +2005,19 @@ namespace SDL2 /* window refers to an SDL_Window* */ [DllImport(nativeLibName, EntryPoint = "SDL_SetWindowTitle", CallingConvention = CallingConvention.Cdecl)] - private static extern void INTERNAL_SDL_SetWindowTitle( + private static extern unsafe void INTERNAL_SDL_SetWindowTitle( IntPtr window, - byte[] title + byte* title ); - public static void SDL_SetWindowTitle( + public static unsafe void SDL_SetWindowTitle( IntPtr window, string title ) { + int utf8TitleBufSize = Utf8Size(title); + byte* utf8Title = stackalloc byte[utf8TitleBufSize]; INTERNAL_SDL_SetWindowTitle( window, - UTF8_ToNative(title) + Utf8Encode(title, utf8Title, utf8TitleBufSize) ); } @@ -1927,13 +2038,15 @@ namespace SDL2 ); [DllImport(nativeLibName, EntryPoint = "SDL_VideoInit", CallingConvention = CallingConvention.Cdecl)] - private static extern int INTERNAL_SDL_VideoInit( - byte[] driver_name + private static extern unsafe int INTERNAL_SDL_VideoInit( + byte* driver_name ); - public static int SDL_VideoInit(string driver_name) + public static unsafe int SDL_VideoInit(string driver_name) { + int utf8DriverNameBufSize = Utf8Size(driver_name); + byte* utf8DriverName = stackalloc byte[utf8DriverNameBufSize]; return INTERNAL_SDL_VideoInit( - UTF8_ToNative(driver_name) + Utf8Encode(driver_name, utf8DriverName, utf8DriverNameBufSize) ); } @@ -2011,14 +2124,17 @@ namespace SDL2 /* Only available in 2.0.6 or higher. */ [DllImport(nativeLibName, EntryPoint = "SDL_Vulkan_LoadLibrary", CallingConvention = CallingConvention.Cdecl)] - private static extern int INTERNAL_SDL_Vulkan_LoadLibrary( - byte[] path + private static extern unsafe int INTERNAL_SDL_Vulkan_LoadLibrary( + byte* path ); - public static int SDL_Vulkan_LoadLibrary(string path) + public static unsafe int SDL_Vulkan_LoadLibrary(string path) { - return INTERNAL_SDL_Vulkan_LoadLibrary( - UTF8_ToNative(path) + byte* utf8Path = Utf8Encode(path); + int result = INTERNAL_SDL_Vulkan_LoadLibrary( + utf8Path ); + Marshal.FreeHGlobal((IntPtr)utf8Path); + return result; } /* Only available in 2.0.6 or higher. */ @@ -4196,15 +4312,18 @@ namespace SDL2 } [DllImport(nativeLibName, EntryPoint = "SDL_SetClipboardText", CallingConvention = CallingConvention.Cdecl)] - private static extern int INTERNAL_SDL_SetClipboardText( - byte[] text + private static extern unsafe int INTERNAL_SDL_SetClipboardText( + byte* text ); - public static int SDL_SetClipboardText( + public static unsafe int SDL_SetClipboardText( string text ) { - return INTERNAL_SDL_SetClipboardText( - UTF8_ToNative(text) + byte* utf8Text = Utf8Encode(text); + int result = INTERNAL_SDL_SetClipboardText( + utf8Text ); + Marshal.FreeHGlobal((IntPtr)utf8Text); + return result; } #endregion @@ -5529,13 +5648,15 @@ namespace SDL2 /* Get a scancode from a human-readable name */ [DllImport(nativeLibName, EntryPoint = "SDL_GetScancodeFromName", CallingConvention = CallingConvention.Cdecl)] - private static extern SDL_Scancode INTERNAL_SDL_GetScancodeFromName( - byte[] name + private static extern unsafe SDL_Scancode INTERNAL_SDL_GetScancodeFromName( + byte* name ); - public static SDL_Scancode SDL_GetScancodeFromName(string name) + public static unsafe SDL_Scancode SDL_GetScancodeFromName(string name) { + int utf8NameBufSize = Utf8Size(name); + byte* utf8Name = stackalloc byte[utf8NameBufSize]; return INTERNAL_SDL_GetScancodeFromName( - UTF8_ToNative(name) + Utf8Encode(name, utf8Name, utf8NameBufSize) ); } @@ -5549,12 +5670,16 @@ namespace SDL2 /* Get a key code from a human-readable name */ [DllImport(nativeLibName, EntryPoint = "SDL_GetKeyFromName", CallingConvention = CallingConvention.Cdecl)] - private static extern SDL_Keycode INTERNAL_SDL_GetKeyFromName( - byte[] name + private static extern unsafe SDL_Keycode INTERNAL_SDL_GetKeyFromName( + byte* name ); - public static SDL_Keycode SDL_GetKeyFromName(string name) + public static unsafe SDL_Keycode SDL_GetKeyFromName(string name) { - return INTERNAL_SDL_GetKeyFromName(UTF8_ToNative(name)); + int utf8NameBufSize = Utf8Size(name); + byte* utf8Name = stackalloc byte[utf8NameBufSize]; + return INTERNAL_SDL_GetKeyFromName( + Utf8Encode(name, utf8Name, utf8NameBufSize) + ); } /* Start accepting Unicode text input events, show keyboard */ @@ -5975,13 +6100,15 @@ namespace SDL2 ); [DllImport(nativeLibName, EntryPoint = "SDL_JoystickGetGUIDFromString", CallingConvention = CallingConvention.Cdecl)] - private static extern Guid INTERNAL_SDL_JoystickGetGUIDFromString( - byte[] pchGUID + private static extern unsafe Guid INTERNAL_SDL_JoystickGetGUIDFromString( + byte* pchGUID ); - public static Guid SDL_JoystickGetGUIDFromString(string pchGuid) + public static unsafe Guid SDL_JoystickGetGUIDFromString(string pchGuid) { + int utf8PchGuidBufSize = Utf8Size(pchGuid); + byte* utf8PchGuid = stackalloc byte[utf8PchGuidBufSize]; return INTERNAL_SDL_JoystickGetGUIDFromString( - UTF8_ToNative(pchGuid) + Utf8Encode(pchGuid, utf8PchGuid, utf8PchGuidBufSize) ); } @@ -6169,15 +6296,18 @@ namespace SDL2 } [DllImport(nativeLibName, EntryPoint = "SDL_GameControllerAddMapping", CallingConvention = CallingConvention.Cdecl)] - private static extern int INTERNAL_SDL_GameControllerAddMapping( - byte[] mappingString + private static extern unsafe int INTERNAL_SDL_GameControllerAddMapping( + byte* mappingString ); - public static int SDL_GameControllerAddMapping( + public static unsafe int SDL_GameControllerAddMapping( string mappingString ) { - return INTERNAL_SDL_GameControllerAddMapping( - UTF8_ToNative(mappingString) + byte* utf8MappingString = Utf8Encode(mappingString); + int result = INTERNAL_SDL_GameControllerAddMapping( + utf8MappingString ); + Marshal.FreeHGlobal((IntPtr)utf8MappingString); + return result; } /* Only available in 2.0.6 or higher. */ @@ -6324,14 +6454,16 @@ namespace SDL2 public static extern void SDL_GameControllerUpdate(); [DllImport(nativeLibName, EntryPoint = "SDL_GameControllerGetAxisFromString", CallingConvention = CallingConvention.Cdecl)] - private static extern SDL_GameControllerAxis INTERNAL_SDL_GameControllerGetAxisFromString( - byte[] pchString + private static extern unsafe SDL_GameControllerAxis INTERNAL_SDL_GameControllerGetAxisFromString( + byte* pchString ); - public static SDL_GameControllerAxis SDL_GameControllerGetAxisFromString( + public static unsafe SDL_GameControllerAxis SDL_GameControllerGetAxisFromString( string pchString ) { + int utf8PchStringBufSize = Utf8Size(pchString); + byte* utf8PchString = stackalloc byte[utf8PchStringBufSize]; return INTERNAL_SDL_GameControllerGetAxisFromString( - UTF8_ToNative(pchString) + Utf8Encode(pchString, utf8PchString, utf8PchStringBufSize) ); } @@ -6379,14 +6511,16 @@ namespace SDL2 ); [DllImport(nativeLibName, EntryPoint = "SDL_GameControllerGetButtonFromString", CallingConvention = CallingConvention.Cdecl)] - private static extern SDL_GameControllerButton INTERNAL_SDL_GameControllerGetButtonFromString( - byte[] pchString + private static extern unsafe SDL_GameControllerButton INTERNAL_SDL_GameControllerGetButtonFromString( + byte* pchString ); - public static SDL_GameControllerButton SDL_GameControllerGetButtonFromString( + public static unsafe SDL_GameControllerButton SDL_GameControllerGetButtonFromString( string pchString ) { + int utf8PchStringBufSize = Utf8Size(pchString); + byte* utf8PchString = stackalloc byte[utf8PchStringBufSize]; return INTERNAL_SDL_GameControllerGetButtonFromString( - UTF8_ToNative(pchString) + Utf8Encode(pchString, utf8PchString, utf8PchStringBufSize) ); } @@ -7010,13 +7144,15 @@ namespace SDL2 ); [DllImport(nativeLibName, EntryPoint = "SDL_AudioInit", CallingConvention = CallingConvention.Cdecl)] - private static extern int INTERNAL_SDL_AudioInit( - byte[] driver_name + private static extern unsafe int INTERNAL_SDL_AudioInit( + byte* driver_name ); - public static int SDL_AudioInit(string driver_name) + public static unsafe int SDL_AudioInit(string driver_name) { + int utf8DriverNameBufSize = Utf8Size(driver_name); + byte* utf8DriverName = stackalloc byte[utf8DriverNameBufSize]; return INTERNAL_SDL_AudioInit( - UTF8_ToNative(driver_name) + Utf8Encode(driver_name, utf8DriverName, utf8DriverNameBufSize) ); } @@ -7154,22 +7290,24 @@ namespace SDL2 /* uint refers to an SDL_AudioDeviceID */ [DllImport(nativeLibName, EntryPoint = "SDL_OpenAudioDevice", CallingConvention = CallingConvention.Cdecl)] - private static extern uint INTERNAL_SDL_OpenAudioDevice( - byte[] device, + private static extern unsafe uint INTERNAL_SDL_OpenAudioDevice( + byte* device, int iscapture, ref SDL_AudioSpec desired, out SDL_AudioSpec obtained, int allowed_changes ); - public static uint SDL_OpenAudioDevice( + public static unsafe uint SDL_OpenAudioDevice( string device, int iscapture, ref SDL_AudioSpec desired, out SDL_AudioSpec obtained, int allowed_changes ) { + int utf8DeviceBufSize = Utf8Size(device); + byte* utf8Device = stackalloc byte[utf8DeviceBufSize]; return INTERNAL_SDL_OpenAudioDevice( - UTF8_ToNative(device), + Utf8Encode(device, utf8Device, utf8DeviceBufSize), iscapture, ref desired, out obtained, @@ -7580,16 +7718,22 @@ namespace SDL2 /* Only available in 2.0.1 or higher. */ [DllImport(nativeLibName, EntryPoint = "SDL_GetPrefPath", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_SDL_GetPrefPath( - byte[] org, - byte[] app + private static extern unsafe IntPtr INTERNAL_SDL_GetPrefPath( + byte* org, + byte* app ); - public static string SDL_GetPrefPath(string org, string app) + public static unsafe string SDL_GetPrefPath(string org, string app) { + int utf8OrgBufSize = Utf8SizeNullable(org); + byte* utf8Org = stackalloc byte[utf8OrgBufSize]; + + int utf8AppBufSize = Utf8SizeNullable(app); + byte* utf8App = stackalloc byte[utf8AppBufSize]; + return UTF8_ToManaged( INTERNAL_SDL_GetPrefPath( - UTF8_ToNative(org), - UTF8_ToNative(app) + Utf8EncodeNullable(org, utf8Org, utf8OrgBufSize), + Utf8EncodeNullable(app, utf8App, utf8AppBufSize) ), true ); diff --git a/src/SDL2_image.cs b/src/SDL2_image.cs index ac55fc2..d76b146 100644 --- a/src/SDL2_image.cs +++ b/src/SDL2_image.cs @@ -89,12 +89,17 @@ namespace SDL2 /* IntPtr refers to an SDL_Surface* */ [DllImport(nativeLibName, EntryPoint = "IMG_Load", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_IMG_Load( - byte[] file + private static extern unsafe IntPtr INTERNAL_IMG_Load( + byte* file ); - public static IntPtr IMG_Load(string file) + public static unsafe IntPtr IMG_Load(string file) { - return INTERNAL_IMG_Load(SDL.UTF8_ToNative(file)); + byte* utf8File = SDL.Utf8Encode(file); + IntPtr handle = INTERNAL_IMG_Load( + utf8File + ); + Marshal.FreeHGlobal((IntPtr)utf8File); + return handle; } /* src refers to an SDL_RWops*, IntPtr to an SDL_Surface* */ @@ -108,37 +113,42 @@ namespace SDL2 /* src refers to an SDL_RWops*, IntPtr to an SDL_Surface* */ /* THIS IS A PUBLIC RWops FUNCTION! */ [DllImport(nativeLibName, EntryPoint = "IMG_LoadTyped_RW", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_IMG_LoadTyped_RW( + private static extern unsafe IntPtr INTERNAL_IMG_LoadTyped_RW( IntPtr src, int freesrc, - byte[] type + byte* type ); - public static IntPtr IMG_LoadTyped_RW( + public static unsafe IntPtr IMG_LoadTyped_RW( IntPtr src, int freesrc, string type ) { + int utf8TypeBufSize = SDL.Utf8Size(type); + byte* utf8Type = stackalloc byte[utf8TypeBufSize]; return INTERNAL_IMG_LoadTyped_RW( src, freesrc, - SDL.UTF8_ToNative(type) + SDL.Utf8Encode(type, utf8Type, utf8TypeBufSize) ); } /* IntPtr refers to an SDL_Texture*, renderer to an SDL_Renderer* */ [DllImport(nativeLibName, EntryPoint = "IMG_LoadTexture", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_IMG_LoadTexture( + private static extern unsafe IntPtr INTERNAL_IMG_LoadTexture( IntPtr renderer, - byte[] file + byte* file ); - public static IntPtr IMG_LoadTexture( + public static unsafe IntPtr IMG_LoadTexture( IntPtr renderer, string file ) { - return INTERNAL_IMG_LoadTexture( + byte* utf8File = SDL.Utf8Encode(file); + IntPtr handle = INTERNAL_IMG_LoadTexture( renderer, - SDL.UTF8_ToNative(file) + utf8File ); + Marshal.FreeHGlobal((IntPtr)utf8File); + return handle; } /* renderer refers to an SDL_Renderer*. @@ -159,24 +169,27 @@ namespace SDL2 */ /* THIS IS A PUBLIC RWops FUNCTION! */ [DllImport(nativeLibName, EntryPoint = "IMG_LoadTextureTyped_RW", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_IMG_LoadTextureTyped_RW( + private static extern unsafe IntPtr INTERNAL_IMG_LoadTextureTyped_RW( IntPtr renderer, IntPtr src, int freesrc, - byte[] type + byte* type ); - public static IntPtr IMG_LoadTextureTyped_RW( + public static unsafe IntPtr IMG_LoadTextureTyped_RW( IntPtr renderer, IntPtr src, int freesrc, string type ) { - return INTERNAL_IMG_LoadTextureTyped_RW( + byte* utf8Type = SDL.Utf8Encode(type); + IntPtr handle = INTERNAL_IMG_LoadTextureTyped_RW( renderer, src, freesrc, - SDL.UTF8_ToNative(type) + utf8Type ); + Marshal.FreeHGlobal((IntPtr)utf8Type); + return handle; } /* IntPtr refers to an SDL_Surface* */ @@ -188,16 +201,19 @@ namespace SDL2 /* surface refers to an SDL_Surface* */ [DllImport(nativeLibName, EntryPoint = "IMG_SavePNG", CallingConvention = CallingConvention.Cdecl)] - private static extern int INTERNAL_IMG_SavePNG( + private static extern unsafe int INTERNAL_IMG_SavePNG( IntPtr surface, - byte[] file + byte* file ); - public static int IMG_SavePNG(IntPtr surface, string file) + public static unsafe int IMG_SavePNG(IntPtr surface, string file) { - return INTERNAL_IMG_SavePNG( + byte* utf8File = SDL.Utf8Encode(file); + int result = INTERNAL_IMG_SavePNG( surface, - SDL.UTF8_ToNative(file) + utf8File ); + Marshal.FreeHGlobal((IntPtr)utf8File); + return result; } /* surface refers to an SDL_Surface*, dst to an SDL_RWops* */ @@ -211,18 +227,21 @@ namespace SDL2 /* surface refers to an SDL_Surface* */ [DllImport(nativeLibName, EntryPoint = "IMG_SaveJPG", CallingConvention = CallingConvention.Cdecl)] - private static extern int INTERNAL_IMG_SaveJPG( + private static extern unsafe int INTERNAL_IMG_SaveJPG( IntPtr surface, - byte[] file, + byte* file, int quality ); - public static int IMG_SaveJPG(IntPtr surface, string file, int quality) + public static unsafe int IMG_SaveJPG(IntPtr surface, string file, int quality) { - return INTERNAL_IMG_SaveJPG( + byte* utf8File = SDL.Utf8Encode(file); + int result = INTERNAL_IMG_SaveJPG( surface, - SDL.UTF8_ToNative(file), + utf8File, quality ); + Marshal.FreeHGlobal((IntPtr)utf8File); + return result; } /* surface refers to an SDL_Surface*, dst to an SDL_RWops* */ From c782115e7f86fd5e763c4e0fbc9d292117c7257d Mon Sep 17 00:00:00 2001 From: Ethan Lee Date: Thu, 21 May 2020 10:24:46 -0400 Subject: [PATCH 02/17] Minor style fixes, UTF8 update for mixer/ttf --- src/SDL2.cs | 49 ++++++++--------- src/SDL2_image.cs | 10 ++-- src/SDL2_mixer.cs | 39 +++++++++----- src/SDL2_ttf.cs | 130 ++++++++++++++++++++++++++++------------------ 4 files changed, 135 insertions(+), 93 deletions(-) diff --git a/src/SDL2.cs b/src/SDL2.cs index faf9a2d..b35d6a2 100644 --- a/src/SDL2.cs +++ b/src/SDL2.cs @@ -85,7 +85,7 @@ namespace SDL2 { Debug.Assert(str != null); int bufferSize = Utf8Size(str); - byte* buffer = (byte*)Marshal.AllocHGlobal(bufferSize); + byte* buffer = (byte*) Marshal.AllocHGlobal(bufferSize); fixed (char* strPtr = str) { Encoding.UTF8.GetBytes(strPtr, str.Length + 1, buffer, bufferSize); @@ -95,16 +95,21 @@ namespace SDL2 internal static unsafe byte* Utf8EncodeNullable(string str) { int bufferSize = Utf8SizeNullable(str); - byte* buffer = (byte*)Marshal.AllocHGlobal(bufferSize); + byte* buffer = (byte*) Marshal.AllocHGlobal(bufferSize); fixed (char* strPtr = str) { - Encoding.UTF8.GetBytes(strPtr, str != null ? str.Length + 1 : 0, buffer, bufferSize); + Encoding.UTF8.GetBytes( + strPtr, + (str != null) ? (str.Length + 1) : 0, + buffer, + bufferSize + ); } return buffer; } /* This is public because SDL_DropEvent needs it! */ - internal static unsafe string UTF8_ToManaged(IntPtr s, bool freePtr = false) + public static unsafe string UTF8_ToManaged(IntPtr s, bool freePtr = false) { if (s == IntPtr.Zero) { @@ -268,8 +273,8 @@ namespace SDL2 utf8File, utf8Mode ); - Marshal.FreeHGlobal((IntPtr)utf8Mode); - Marshal.FreeHGlobal((IntPtr)utf8File); + Marshal.FreeHGlobal((IntPtr) utf8Mode); + Marshal.FreeHGlobal((IntPtr) utf8File); return rwOps; } @@ -1746,20 +1751,6 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern void SDL_GL_DeleteContext(IntPtr context); - /* IntPtr refers to a function pointer */ - [DllImport(nativeLibName, EntryPoint = "SDL_GL_GetProcAddress", CallingConvention = CallingConvention.Cdecl)] - private static extern unsafe IntPtr INTERNAL_SDL_GL_GetProcAddress( - byte* proc - ); - public static unsafe IntPtr SDL_GL_GetProcAddress(string proc) - { - int utf8ProcBufSize = Utf8Size(proc); - byte* utf8Proc = stackalloc byte[utf8ProcBufSize]; - return INTERNAL_SDL_GL_GetProcAddress( - Utf8Encode(proc, utf8Proc, utf8ProcBufSize) - ); - } - [DllImport(nativeLibName, EntryPoint = "SDL_GL_LoadLibrary", CallingConvention = CallingConvention.Cdecl)] private static extern unsafe int INTERNAL_SDL_GL_LoadLibrary(byte* path); public static unsafe int SDL_GL_LoadLibrary(string path) @@ -1768,7 +1759,7 @@ namespace SDL2 int result = INTERNAL_SDL_GL_LoadLibrary( utf8Path ); - Marshal.FreeHGlobal((IntPtr)utf8Path); + Marshal.FreeHGlobal((IntPtr) utf8Path); return result; } @@ -1776,6 +1767,16 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr SDL_GL_GetProcAddress(IntPtr proc); + /* IntPtr refers to a function pointer */ + public static unsafe IntPtr SDL_GL_GetProcAddress(string proc) + { + int utf8ProcBufSize = Utf8Size(proc); + byte* utf8Proc = stackalloc byte[utf8ProcBufSize]; + return SDL_GL_GetProcAddress( + (IntPtr) Utf8Encode(proc, utf8Proc, utf8ProcBufSize) + ); + } + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern void SDL_GL_UnloadLibrary(); @@ -2133,7 +2134,7 @@ namespace SDL2 int result = INTERNAL_SDL_Vulkan_LoadLibrary( utf8Path ); - Marshal.FreeHGlobal((IntPtr)utf8Path); + Marshal.FreeHGlobal((IntPtr) utf8Path); return result; } @@ -4322,7 +4323,7 @@ namespace SDL2 int result = INTERNAL_SDL_SetClipboardText( utf8Text ); - Marshal.FreeHGlobal((IntPtr)utf8Text); + Marshal.FreeHGlobal((IntPtr) utf8Text); return result; } @@ -6306,7 +6307,7 @@ namespace SDL2 int result = INTERNAL_SDL_GameControllerAddMapping( utf8MappingString ); - Marshal.FreeHGlobal((IntPtr)utf8MappingString); + Marshal.FreeHGlobal((IntPtr) utf8MappingString); return result; } diff --git a/src/SDL2_image.cs b/src/SDL2_image.cs index d76b146..a7052a4 100644 --- a/src/SDL2_image.cs +++ b/src/SDL2_image.cs @@ -98,7 +98,7 @@ namespace SDL2 IntPtr handle = INTERNAL_IMG_Load( utf8File ); - Marshal.FreeHGlobal((IntPtr)utf8File); + Marshal.FreeHGlobal((IntPtr) utf8File); return handle; } @@ -147,7 +147,7 @@ namespace SDL2 renderer, utf8File ); - Marshal.FreeHGlobal((IntPtr)utf8File); + Marshal.FreeHGlobal((IntPtr) utf8File); return handle; } @@ -188,7 +188,7 @@ namespace SDL2 freesrc, utf8Type ); - Marshal.FreeHGlobal((IntPtr)utf8Type); + Marshal.FreeHGlobal((IntPtr) utf8Type); return handle; } @@ -212,7 +212,7 @@ namespace SDL2 surface, utf8File ); - Marshal.FreeHGlobal((IntPtr)utf8File); + Marshal.FreeHGlobal((IntPtr) utf8File); return result; } @@ -240,7 +240,7 @@ namespace SDL2 utf8File, quality ); - Marshal.FreeHGlobal((IntPtr)utf8File); + Marshal.FreeHGlobal((IntPtr) utf8File); return result; } diff --git a/src/SDL2_mixer.cs b/src/SDL2_mixer.cs index 308aaee..8c59dad 100644 --- a/src/SDL2_mixer.cs +++ b/src/SDL2_mixer.cs @@ -200,12 +200,17 @@ namespace SDL2 /* IntPtr refers to a Mix_Music* */ [DllImport(nativeLibName, EntryPoint = "Mix_LoadMUS", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_Mix_LoadMUS( - byte[] file + private static extern unsafe IntPtr INTERNAL_Mix_LoadMUS( + byte* file ); - public static IntPtr Mix_LoadMUS(string file) + public static unsafe IntPtr Mix_LoadMUS(string file) { - return INTERNAL_Mix_LoadMUS(SDL.UTF8_ToNative(file)); + byte* utf8File = SDL.Utf8Encode(file); + IntPtr handle = INTERNAL_Mix_LoadMUS( + utf8File + ); + Marshal.FreeHGlobal((IntPtr) utf8File); + return handle; } /* IntPtr refers to a Mix_Chunk* */ @@ -569,14 +574,17 @@ namespace SDL2 public static extern int Mix_PlayingMusic(); [DllImport(nativeLibName, EntryPoint = "Mix_SetMusicCMD", CallingConvention = CallingConvention.Cdecl)] - private static extern int INTERNAL_Mix_SetMusicCMD( - byte[] command + private static extern unsafe int INTERNAL_Mix_SetMusicCMD( + byte* command ); - public static int Mix_SetMusicCMD(string command) + public static unsafe int Mix_SetMusicCMD(string command) { - return INTERNAL_Mix_SetMusicCMD( - SDL.UTF8_ToNative(command) + byte* utf8Cmd = SDL.Utf8Encode(command); + int result = INTERNAL_Mix_SetMusicCMD( + utf8Cmd ); + Marshal.FreeHGlobal((IntPtr) utf8Cmd); + return result; } [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] @@ -586,14 +594,17 @@ namespace SDL2 public static extern int Mix_GetSynchroValue(); [DllImport(nativeLibName, EntryPoint = "Mix_SetSoundFonts", CallingConvention = CallingConvention.Cdecl)] - private static extern int INTERNAL_Mix_SetSoundFonts( - byte[] paths + private static extern unsafe int INTERNAL_Mix_SetSoundFonts( + byte* paths ); - public static int Mix_SetSoundFonts(string paths) + public static unsafe int Mix_SetSoundFonts(string paths) { - return INTERNAL_Mix_SetSoundFonts( - SDL.UTF8_ToNative(paths) + byte* utf8Paths = SDL.Utf8Encode(paths); + int result = INTERNAL_Mix_SetSoundFonts( + utf8Paths ); + Marshal.FreeHGlobal((IntPtr) utf8Paths); + return result; } [DllImport(nativeLibName, EntryPoint = "Mix_GetSoundFonts", CallingConvention = CallingConvention.Cdecl)] diff --git a/src/SDL2_ttf.cs b/src/SDL2_ttf.cs index bb58c66..2051b3e 100644 --- a/src/SDL2_ttf.cs +++ b/src/SDL2_ttf.cs @@ -95,16 +95,19 @@ namespace SDL2 /* IntPtr refers to a TTF_Font* */ [DllImport(nativeLibName, EntryPoint = "TTF_OpenFont", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_TTF_OpenFont( - byte[] file, + private static extern unsafe IntPtr INTERNAL_TTF_OpenFont( + byte* file, int ptsize ); - public static IntPtr TTF_OpenFont(string file, int ptsize) + public static unsafe IntPtr TTF_OpenFont(string file, int ptsize) { - return INTERNAL_TTF_OpenFont( - SDL.UTF8_ToNative(file), + byte* utf8File = SDL.Utf8Encode(file); + IntPtr handle = INTERNAL_TTF_OpenFont( + utf8File, ptsize ); + Marshal.FreeHGlobal((IntPtr) utf8File); + return handle; } /* src refers to an SDL_RWops*, IntPtr to a TTF_Font* */ @@ -118,21 +121,24 @@ namespace SDL2 /* IntPtr refers to a TTF_Font* */ [DllImport(nativeLibName, EntryPoint = "TTF_OpenFontIndex", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_TTF_OpenFontIndex( - byte[] file, + private static extern unsafe IntPtr INTERNAL_TTF_OpenFontIndex( + byte* file, int ptsize, long index ); - public static IntPtr TTF_OpenFontIndex( + public static unsafe IntPtr TTF_OpenFontIndex( string file, int ptsize, long index ) { - return INTERNAL_TTF_OpenFontIndex( - SDL.UTF8_ToNative(file), + byte* utf8File = SDL.Utf8Encode(file); + IntPtr handle = INTERNAL_TTF_OpenFontIndex( + utf8File, ptsize, index ); + Marshal.FreeHGlobal((IntPtr) utf8File); + return handle; } /* src refers to an SDL_RWops*, IntPtr to a TTF_Font* */ @@ -282,24 +288,27 @@ namespace SDL2 /* font refers to a TTF_Font* */ [DllImport(nativeLibName, EntryPoint = "TTF_SizeUTF8", CallingConvention = CallingConvention.Cdecl)] - public static extern int INTERNAL_TTF_SizeUTF8( + public static extern unsafe int INTERNAL_TTF_SizeUTF8( IntPtr font, - byte[] text, + byte* text, out int w, out int h ); - public static int TTF_SizeUTF8( + public static unsafe int TTF_SizeUTF8( IntPtr font, string text, out int w, out int h ) { - return INTERNAL_TTF_SizeUTF8( + byte* utf8Text = SDL.Utf8Encode(text); + int result = INTERNAL_TTF_SizeUTF8( font, - SDL.UTF8_ToNative(text), + utf8Text, out w, out h ); + Marshal.FreeHGlobal((IntPtr) utf8Text); + return result; } /* font refers to a TTF_Font* */ @@ -329,27 +338,30 @@ namespace SDL2 * Only available in 2.0.16 or higher. */ [DllImport(nativeLibName, EntryPoint = "TTF_MeasureUTF8", CallingConvention = CallingConvention.Cdecl)] - public static extern int INTERNAL_TTF_MeasureUTF8( + public static extern unsafe int INTERNAL_TTF_MeasureUTF8( IntPtr font, - byte[] text, + byte* text, int measure_width, out int extent, out int count ); - public static int TTF_MeasureUTF8( + public static unsafe int TTF_MeasureUTF8( IntPtr font, string text, int measure_width, out int extent, out int count ) { - return INTERNAL_TTF_MeasureUTF8( + byte* utf8Text = SDL.Utf8Encode(text); + int result = INTERNAL_TTF_MeasureUTF8( font, - SDL.UTF8_ToNative(text), + utf8Text, measure_width, out extent, out count ); + Marshal.FreeHGlobal((IntPtr) utf8Text); + return result; } /* font refers to a TTF_Font* @@ -376,21 +388,24 @@ namespace SDL2 /* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */ [DllImport(nativeLibName, EntryPoint = "TTF_RenderUTF8_Solid", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_TTF_RenderUTF8_Solid( + private static extern unsafe IntPtr INTERNAL_TTF_RenderUTF8_Solid( IntPtr font, - byte[] text, + byte* text, SDL.SDL_Color fg ); - public static IntPtr TTF_RenderUTF8_Solid( + public static unsafe IntPtr TTF_RenderUTF8_Solid( IntPtr font, string text, SDL.SDL_Color fg ) { - return INTERNAL_TTF_RenderUTF8_Solid( + byte* utf8Text = SDL.Utf8Encode(text); + IntPtr result = INTERNAL_TTF_RenderUTF8_Solid( font, - SDL.UTF8_ToNative(text), + utf8Text, fg ); + Marshal.FreeHGlobal((IntPtr) utf8Text); + return result; } /* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */ @@ -418,24 +433,27 @@ namespace SDL2 * Only available in 2.0.16 or higher. */ [DllImport(nativeLibName, EntryPoint = "TTF_RenderUTF8_Solid_Wrapped", CallingConvention = CallingConvention.Cdecl)] - public static extern IntPtr INTERNAL_TTF_RenderUTF8_Solid_Wrapped( + public static extern unsafe IntPtr INTERNAL_TTF_RenderUTF8_Solid_Wrapped( IntPtr font, - byte[] text, + byte* text, SDL.SDL_Color fg, uint wrapLength ); - public static IntPtr TTF_RenderUTF8_Solid_Wrapped( + public static unsafe IntPtr TTF_RenderUTF8_Solid_Wrapped( IntPtr font, string text, SDL.SDL_Color fg, uint wrapLength ) { - return INTERNAL_TTF_RenderUTF8_Solid_Wrapped( + byte* utf8Text = SDL.Utf8Encode(text); + IntPtr result = INTERNAL_TTF_RenderUTF8_Solid_Wrapped( font, - SDL.UTF8_ToNative(text), + utf8Text, fg, wrapLength ); + Marshal.FreeHGlobal((IntPtr) utf8Text); + return result; } /* IntPtr refers to an SDL_Surface*, font to a TTF_Font* @@ -480,24 +498,27 @@ namespace SDL2 /* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */ [DllImport(nativeLibName, EntryPoint = "TTF_RenderUTF8_Shaded", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_TTF_RenderUTF8_Shaded( + private static extern unsafe IntPtr INTERNAL_TTF_RenderUTF8_Shaded( IntPtr font, - byte[] text, + byte* text, SDL.SDL_Color fg, SDL.SDL_Color bg ); - public static IntPtr TTF_RenderUTF8_Shaded( + public static unsafe IntPtr TTF_RenderUTF8_Shaded( IntPtr font, string text, SDL.SDL_Color fg, SDL.SDL_Color bg ) { - return INTERNAL_TTF_RenderUTF8_Shaded( + byte* utf8Text = SDL.Utf8Encode(text); + IntPtr result = INTERNAL_TTF_RenderUTF8_Shaded( font, - SDL.UTF8_ToNative(text), + utf8Text, fg, bg ); + Marshal.FreeHGlobal((IntPtr) utf8Text); + return result; } /* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */ @@ -525,27 +546,30 @@ namespace SDL2 * Only available in 2.0.16 or higher. */ [DllImport(nativeLibName, EntryPoint = "TTF_RenderUTF8_Shaded_Wrapped", CallingConvention = CallingConvention.Cdecl)] - public static extern IntPtr INTERNAL_TTF_RenderUTF8_Shaded_Wrapped( + public static extern unsafe IntPtr INTERNAL_TTF_RenderUTF8_Shaded_Wrapped( IntPtr font, - byte[] text, + byte* text, SDL.SDL_Color fg, SDL.SDL_Color bg, uint wrapLength ); - public static IntPtr TTF_RenderUTF8_Shaded_Wrapped( + public static unsafe IntPtr TTF_RenderUTF8_Shaded_Wrapped( IntPtr font, string text, SDL.SDL_Color fg, SDL.SDL_Color bg, uint wrapLength ) { - return INTERNAL_TTF_RenderUTF8_Shaded_Wrapped( + byte* utf8Text = SDL.Utf8Encode(text); + IntPtr result = INTERNAL_TTF_RenderUTF8_Shaded_Wrapped( font, - SDL.UTF8_ToNative(text), + utf8Text, fg, bg, wrapLength ); + Marshal.FreeHGlobal((IntPtr) utf8Text); + return result; } /* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */ @@ -590,21 +614,24 @@ namespace SDL2 /* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */ [DllImport(nativeLibName, EntryPoint = "TTF_RenderUTF8_Blended", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_TTF_RenderUTF8_Blended( + private static extern unsafe IntPtr INTERNAL_TTF_RenderUTF8_Blended( IntPtr font, - byte[] text, + byte* text, SDL.SDL_Color fg ); - public static IntPtr TTF_RenderUTF8_Blended( + public static unsafe IntPtr TTF_RenderUTF8_Blended( IntPtr font, string text, SDL.SDL_Color fg ) { - return INTERNAL_TTF_RenderUTF8_Blended( + byte* utf8Text = SDL.Utf8Encode(text); + IntPtr result = INTERNAL_TTF_RenderUTF8_Blended( font, - SDL.UTF8_ToNative(text), + utf8Text, fg ); + Marshal.FreeHGlobal((IntPtr) utf8Text); + return result; } /* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */ @@ -628,24 +655,27 @@ namespace SDL2 /* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */ [DllImport(nativeLibName, EntryPoint = "TTF_RenderUTF8_Blended_Wrapped", CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr INTERNAL_TTF_RenderUTF8_Blended_Wrapped( + private static extern unsafe IntPtr INTERNAL_TTF_RenderUTF8_Blended_Wrapped( IntPtr font, - byte[] text, + byte* text, SDL.SDL_Color fg, uint wrapped ); - public static IntPtr TTF_RenderUTF8_Blended_Wrapped( + public static unsafe IntPtr TTF_RenderUTF8_Blended_Wrapped( IntPtr font, string text, SDL.SDL_Color fg, uint wrapped ) { - return INTERNAL_TTF_RenderUTF8_Blended_Wrapped( + byte* utf8Text = SDL.Utf8Encode(text); + IntPtr result = INTERNAL_TTF_RenderUTF8_Blended_Wrapped( font, - SDL.UTF8_ToNative(text), + utf8Text, fg, wrapped ); + Marshal.FreeHGlobal((IntPtr) utf8Text); + return result; } /* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */ From 3e7eaf9d5be29f181507226f626ac61f1e068b6e Mon Sep 17 00:00:00 2001 From: Ethan Lee Date: Thu, 28 May 2020 10:39:25 -0400 Subject: [PATCH 03/17] Added checks for null UTF8 inputs --- src/SDL2.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/SDL2.cs b/src/SDL2.cs index b35d6a2..b92c73c 100644 --- a/src/SDL2.cs +++ b/src/SDL2.cs @@ -68,7 +68,10 @@ namespace SDL2 { if (str == null) { - buffer[0] = 0; + if (bufferSize > 0) + { + buffer[0] = 0; + } return buffer; } fixed (char* strPtr = str) @@ -94,7 +97,11 @@ namespace SDL2 } internal static unsafe byte* Utf8EncodeNullable(string str) { - int bufferSize = Utf8SizeNullable(str); + if (str == null) + { + return (byte*) 0; + } + int bufferSize = Utf8Size(str); byte* buffer = (byte*) Marshal.AllocHGlobal(bufferSize); fixed (char* strPtr = str) { From a31694d145292adefa51c928179d86b408a63db4 Mon Sep 17 00:00:00 2001 From: Ethan Lee Date: Mon, 8 Jun 2020 09:59:03 -0400 Subject: [PATCH 04/17] F# keyword fix --- src/SDL2.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/SDL2.cs b/src/SDL2.cs index b92c73c..d47628e 100644 --- a/src/SDL2.cs +++ b/src/SDL2.cs @@ -4827,6 +4827,8 @@ namespace SDL2 [FieldOffset(0)] public SDL_EventType type; [FieldOffset(0)] + public SDL_EventType typeFSharp; + [FieldOffset(0)] public SDL_DisplayEvent display; [FieldOffset(0)] public SDL_WindowEvent window; From f4d713f651c1cb0e44b3e602c964e95bdace3c13 Mon Sep 17 00:00:00 2001 From: Caleb Cornett Date: Fri, 12 Jun 2020 16:44:05 -0400 Subject: [PATCH 05/17] ExternalStorageState -> ExternalStoragePath --- src/SDL2.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SDL2.cs b/src/SDL2.cs index d47628e..3a1dbef 100644 --- a/src/SDL2.cs +++ b/src/SDL2.cs @@ -7549,7 +7549,7 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern int SDL_AndroidGetExternalStorageState(); - [DllImport(nativeLibName, EntryPoint = "SDL_AndroidGetExternalStorageState", CallingConvention = CallingConvention.Cdecl)] + [DllImport(nativeLibName, EntryPoint = "SDL_AndroidGetExternalStoragePath", CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr INTERNAL_SDL_AndroidGetExternalStoragePath(); public static string SDL_AndroidGetExternalStoragePath() From b253a810e02df97f52bec315da1a5e25517569e0 Mon Sep 17 00:00:00 2001 From: Caleb Cornett Date: Fri, 12 Jun 2020 22:37:18 -0400 Subject: [PATCH 06/17] Add padding to SDL_Event --- src/SDL2.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/SDL2.cs b/src/SDL2.cs index 3a1dbef..95be33f 100644 --- a/src/SDL2.cs +++ b/src/SDL2.cs @@ -4822,7 +4822,7 @@ namespace SDL2 /* General event structure */ // C# doesn't do unions, so we do this ugly thing. */ [StructLayout(LayoutKind.Explicit)] - public struct SDL_Event + public unsafe struct SDL_Event { [FieldOffset(0)] public SDL_EventType type; @@ -4878,6 +4878,8 @@ namespace SDL2 public SDL_DollarGestureEvent dgesture; [FieldOffset(0)] public SDL_DropEvent drop; + [FieldOffset(0)] + private fixed byte padding[56]; } [UnmanagedFunctionPointer(CallingConvention.Cdecl)] From e95ef4e98a596bd76874f2801fbefb570b87f405 Mon Sep 17 00:00:00 2001 From: Chad Yates Date: Mon, 22 Jun 2020 20:06:40 -0700 Subject: [PATCH 07/17] Fix buffer overflow. --- src/SDL2.cs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/SDL2.cs b/src/SDL2.cs index 95be33f..253a176 100644 --- a/src/SDL2.cs +++ b/src/SDL2.cs @@ -68,11 +68,7 @@ namespace SDL2 { if (str == null) { - if (bufferSize > 0) - { - buffer[0] = 0; - } - return buffer; + return (byte*) 0; } fixed (char* strPtr = str) { From 5966578e82557ded39a16a64ee6b0c94ac32089f Mon Sep 17 00:00:00 2001 From: Jeremy Sayers Date: Wed, 28 Oct 2020 22:37:00 -0400 Subject: [PATCH 08/17] Added IMG_GetError and IMG_SetError wrapping calls to SDL_GetError and SDL_SetError to keep SDL2_image.cs consistent with SDL2_image.h --- src/SDL2_image.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/SDL2_image.cs b/src/SDL2_image.cs index a7052a4..9fa193b 100644 --- a/src/SDL2_image.cs +++ b/src/SDL2_image.cs @@ -254,6 +254,16 @@ namespace SDL2 int quality ); + public static string IMG_GetError() + { + return SDL.SDL_GetError(); + } + + public static void IMG_SetError(string fmtAndArglist) + { + SDL.SDL_SetError(fmtAndArglist); + } + #region Animated Image Support /* This region is only available in 2.0.6 or higher. */ From 028745793080dd298e2e3ff78bd77150af549b7c Mon Sep 17 00:00:00 2001 From: Ethan Lee Date: Sun, 15 Nov 2020 10:45:56 -0500 Subject: [PATCH 09/17] Fix my terrible LoadWAV signature --- src/SDL2.cs | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/src/SDL2.cs b/src/SDL2.cs index 253a176..128212a 100644 --- a/src/SDL2.cs +++ b/src/SDL2.cs @@ -7223,36 +7223,30 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern int SDL_GetNumAudioDrivers(); - /* audio_buf will refer to a malloc()'d byte buffer */ + /* audio_buf refers to a malloc()'d buffer, IntPtr to an SDL_AudioSpec* */ /* THIS IS AN RWops FUNCTION! */ [DllImport(nativeLibName, EntryPoint = "SDL_LoadWAV_RW", CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr INTERNAL_SDL_LoadWAV_RW( IntPtr src, int freesrc, - ref SDL_AudioSpec spec, + out SDL_AudioSpec spec, out IntPtr audio_buf, out uint audio_len ); - public static SDL_AudioSpec SDL_LoadWAV( + public static IntPtr SDL_LoadWAV( string file, - ref SDL_AudioSpec spec, + out SDL_AudioSpec spec, out IntPtr audio_buf, out uint audio_len ) { - SDL_AudioSpec result; IntPtr rwops = SDL_RWFromFile(file, "rb"); - IntPtr result_ptr = INTERNAL_SDL_LoadWAV_RW( + return INTERNAL_SDL_LoadWAV_RW( rwops, 1, - ref spec, + out spec, out audio_buf, out audio_len ); - result = (SDL_AudioSpec) Marshal.PtrToStructure( - result_ptr, - typeof(SDL_AudioSpec) - ); - return result; } [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] From 4eab64f274e18d2353c045db2ae4b74596cc4eda Mon Sep 17 00:00:00 2001 From: Caleb Cornett Date: Mon, 21 Dec 2020 23:55:22 -0500 Subject: [PATCH 10/17] Updates for SDL 2.0.14 (#210) --- src/SDL2.cs | 438 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 428 insertions(+), 10 deletions(-) diff --git a/src/SDL2.cs b/src/SDL2.cs index 128212a..d685649 100644 --- a/src/SDL2.cs +++ b/src/SDL2.cs @@ -661,6 +661,36 @@ namespace SDL2 public const string SDL_HINT_GAMECONTROLLERTYPE = "SDL_GAMECONTROLLERTYPE"; + /* Only available in 2.0.14 or higher. */ + public const string SDL_HINT_JOYSTICK_HIDAPI_CORRELATE_XINPUT = + "SDL_JOYSTICK_HIDAPI_CORRELATE_XINPUT"; + public const string SDL_HINT_JOYSTICK_RAWINPUT = + "SDL_JOYSTICK_RAWINPUT"; + public const string SDL_HINT_AUDIO_DEVICE_APP_NAME = + "SDL_AUDIO_DEVICE_APP_NAME"; + public const string SDL_HINT_AUDIO_DEVICE_STREAM_NAME = + "SDL_AUDIO_DEVICE_STREAM_NAME"; + public const string SDL_HINT_PREFERRED_LOCALES = + "SDL_PREFERRED_LOCALES"; + public const string SDL_HINT_THREAD_PRIORITY_POLICY = + "SDL_THREAD_PRIORITY_POLICY"; + public const string SDL_HINT_EMSCRIPTEN_ASYNCIFY = + "SDL_EMSCRIPTEN_ASYNCIFY"; + public const string SDL_HINT_LINUX_JOYSTICK_DEADZONES = + "SDL_LINUX_JOYSTICK_DEADZONES"; + public const string SDL_HINT_ANDROID_BLOCK_ON_PAUSE_PAUSEAUDIO = + "SDL_ANDROID_BLOCK_ON_PAUSE_PAUSEAUDIO"; + public const string SDL_HINT_JOYSTICK_HIDAPI_PS5 = + "SDL_JOYSTICK_HIDAPI_PS5"; + public const string SDL_HINT_THREAD_FORCE_REALTIME_TIME_CRITICAL = + "SDL_THREAD_FORCE_REALTIME_TIME_CRITICAL"; + public const string SDL_HINT_JOYSTICK_THREAD = + "SDL_JOYSTICK_THREAD"; + public const string SDL_HINT_AUTO_UPDATE_JOYSTICKS = + "SDL_AUTO_UPDATE_JOYSTICKS"; + public const string SDL_HINT_AUTO_UPDATE_SENSORS = + "SDL_AUTO_UPDATE_SENSORS"; + public enum SDL_HintPriority { SDL_HINT_DEFAULT, @@ -771,6 +801,12 @@ namespace SDL2 ); } + /* IntPtr refers to a char*. + * Only available in 2.0.14 or higher. + */ + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public static extern IntPtr SDL_GetErrorMsg(IntPtr errstr, int maxlength); + #endregion #region SDL_log.h @@ -1228,7 +1264,7 @@ namespace SDL2 */ public const int SDL_MAJOR_VERSION = 2; public const int SDL_MINOR_VERSION = 0; - public const int SDL_PATCHLEVEL = 12; + public const int SDL_PATCHLEVEL = 14; public static readonly int SDL_COMPILEDVERSION = SDL_VERSIONNUM( SDL_MAJOR_VERSION, @@ -1351,7 +1387,9 @@ namespace SDL2 public enum SDL_DisplayEventID : byte { SDL_DISPLAYEVENT_NONE, - SDL_DISPLAYEVENT_ORIENTATION + SDL_DISPLAYEVENT_ORIENTATION, + SDL_DISPLAYEVENT_CONNECTED, /* Requires >= 2.0.14 */ + SDL_DISPLAYEVENT_DISCONNECTED /* Requires >= 2.0.14 */ } public enum SDL_DisplayOrientation @@ -1388,6 +1426,7 @@ namespace SDL2 SDL_WINDOW_TOOLTIP = 0x00040000, /* Requires >= 2.0.5 */ SDL_WINDOW_POPUP_MENU = 0x00080000, /* Requires >= 2.0.5 */ SDL_WINDOW_VULKAN = 0x10000000, /* Requires >= 2.0.6 */ + SDL_WINDOW_METAL = 0x2000000, /* Requires >= 2.0.14 */ } /* Only available in 2.0.4 or higher. */ @@ -2197,6 +2236,23 @@ namespace SDL2 IntPtr view ); + /* view refers to an SDL_MetalView. + * Only available in 2.0.14 or higher. */ + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public static extern IntPtr SDL_Metal_GetLayer( + IntPtr view + ); + + /* window refers to an SDL_Window*. + * Only available in 2.0.14 or higher. + */ + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public static extern void SDL_Metal_GetDrawableSize( + IntPtr window, + out int w, + out int h + ); + #endregion #region SDL_render.h @@ -3421,34 +3477,42 @@ namespace SDL2 SDL_PackedLayout.SDL_PACKEDLAYOUT_332, 8, 1 ); - public static readonly uint SDL_PIXELFORMAT_RGB444 = + public static readonly uint SDL_PIXELFORMAT_XRGB444 = SDL_DEFINE_PIXELFORMAT( SDL_PixelType.SDL_PIXELTYPE_PACKED16, (uint) SDL_PackedOrder.SDL_PACKEDORDER_XRGB, SDL_PackedLayout.SDL_PACKEDLAYOUT_4444, 12, 2 ); - public static readonly uint SDL_PIXELFORMAT_BGR444 = + public static readonly uint SDL_PIXELFORMAT_RGB444 = + SDL_PIXELFORMAT_XRGB444; + public static readonly uint SDL_PIXELFORMAT_XBGR444 = SDL_DEFINE_PIXELFORMAT( SDL_PixelType.SDL_PIXELTYPE_PACKED16, (uint) SDL_PackedOrder.SDL_PACKEDORDER_XBGR, SDL_PackedLayout.SDL_PACKEDLAYOUT_4444, 12, 2 ); - public static readonly uint SDL_PIXELFORMAT_RGB555 = + public static readonly uint SDL_PIXELFORMAT_BGR444 = + SDL_PIXELFORMAT_XBGR444; + public static readonly uint SDL_PIXELFORMAT_XRGB1555 = SDL_DEFINE_PIXELFORMAT( SDL_PixelType.SDL_PIXELTYPE_PACKED16, (uint) SDL_PackedOrder.SDL_PACKEDORDER_XRGB, SDL_PackedLayout.SDL_PACKEDLAYOUT_1555, 15, 2 ); - public static readonly uint SDL_PIXELFORMAT_BGR555 = + public static readonly uint SDL_PIXELFORMAT_RGB555 = + SDL_PIXELFORMAT_XRGB1555; + public static readonly uint SDL_PIXELFORMAT_XBGR1555 = SDL_DEFINE_PIXELFORMAT( SDL_PixelType.SDL_PIXELTYPE_INDEX1, (uint) SDL_BitmapOrder.SDL_BITMAPORDER_4321, SDL_PackedLayout.SDL_PACKEDLAYOUT_1555, 15, 2 ); + public static readonly uint SDL_PIXELFORMAT_BGR555 = + SDL_PIXELFORMAT_XBGR1555; public static readonly uint SDL_PIXELFORMAT_ARGB4444 = SDL_DEFINE_PIXELFORMAT( SDL_PixelType.SDL_PIXELTYPE_PACKED16, @@ -3533,13 +3597,15 @@ namespace SDL2 0, 24, 3 ); - public static readonly uint SDL_PIXELFORMAT_RGB888 = + public static readonly uint SDL_PIXELFORMAT_XRGB888 = SDL_DEFINE_PIXELFORMAT( SDL_PixelType.SDL_PIXELTYPE_PACKED32, (uint) SDL_PackedOrder.SDL_PACKEDORDER_XRGB, SDL_PackedLayout.SDL_PACKEDLAYOUT_8888, 24, 4 ); + public static readonly uint SDL_PIXELFORMAT_RGB888 = + SDL_PIXELFORMAT_XRGB888; public static readonly uint SDL_PIXELFORMAT_RGBX8888 = SDL_DEFINE_PIXELFORMAT( SDL_PixelType.SDL_PIXELTYPE_PACKED32, @@ -3547,13 +3613,15 @@ namespace SDL2 SDL_PackedLayout.SDL_PACKEDLAYOUT_8888, 24, 4 ); - public static readonly uint SDL_PIXELFORMAT_BGR888 = + public static readonly uint SDL_PIXELFORMAT_XBGR888 = SDL_DEFINE_PIXELFORMAT( SDL_PixelType.SDL_PIXELTYPE_PACKED32, (uint) SDL_PackedOrder.SDL_PACKEDORDER_XBGR, SDL_PackedLayout.SDL_PACKEDLAYOUT_8888, 24, 4 ); + public static readonly uint SDL_PIXELFORMAT_BGR888 = + SDL_PIXELFORMAT_XBGR888; public static readonly uint SDL_PIXELFORMAT_BGRX8888 = SDL_DEFINE_PIXELFORMAT( SDL_PixelType.SDL_PIXELTYPE_PACKED32, @@ -3892,7 +3960,7 @@ namespace SDL2 public IntPtr pixels; // void* public IntPtr userdata; // void* public int locked; - public IntPtr lock_data; // void* + public IntPtr list_blitmap; // void* public SDL_Rect clip_rect; public IntPtr map; // SDL_BlitMap* public int refcount; @@ -4266,6 +4334,14 @@ namespace SDL2 int flag ); + /* surface refers to an SDL_Surface*. + * Only available in 2.0.14 or higher. + */ + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public static extern SDL_bool SDL_HasSurfaceRLE( + IntPtr surface + ); + /* src and dst refer to an SDL_Surface* */ [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern int SDL_SoftStretch( @@ -4358,6 +4434,9 @@ namespace SDL2 SDL_APP_WILLENTERFOREGROUND, SDL_APP_DIDENTERFOREGROUND, + /* Only available in SDL 2.0.14 or higher. */ + SDL_LOCALECHANGED, + /* Display events */ /* Only available in SDL 2.0.9 or higher. */ SDL_DISPLAYEVENT = 0x150, @@ -4395,6 +4474,10 @@ namespace SDL2 SDL_CONTROLLERDEVICEADDED, SDL_CONTROLLERDEVICEREMOVED, SDL_CONTROLLERDEVICEREMAPPED, + SDL_CONTROLLERTOUCHPADDOWN, /* Requires >= 2.0.14 */ + SDL_CONTROLLERTOUCHPADMOTION, /* Requires >= 2.0.14 */ + SDL_CONTROLLERTOUCHPADUP, /* Requires >= 2.0.14 */ + SDL_CONTROLLERSENSORUPDATE, /* Requires >= 2.0.14 */ /* Touch events */ SDL_FINGERDOWN = 0x700, @@ -4702,6 +4785,33 @@ namespace SDL2 */ } + /* Game controller touchpad event structure (event.ctouchpad.*) */ + [StructLayout(LayoutKind.Sequential)] + public struct SDL_ControllerTouchpadEvent + { + public UInt32 type; + public UInt32 timestamp; + public Int32 which; /* SDL_JoystickID */ + public Int32 touchpad; + public Int32 finger; + public float x; + public float y; + public float pressure; + } + + /* Game controller sensor event structure (event.csensor.*) */ + [StructLayout(LayoutKind.Sequential)] + public struct SDL_ControllerSensorEvent + { + public UInt32 type; + public UInt32 timestamp; + public Int32 which; /* SDL_JoystickID */ + public Int32 sensor; + public float data1; + public float data2; + public float data3; + } + // Ignore private members used for padding in this struct #pragma warning disable 0169 /* Audio device event (event.adevice.*) */ @@ -4857,6 +4967,10 @@ namespace SDL2 [FieldOffset(0)] public SDL_ControllerDeviceEvent cdevice; [FieldOffset(0)] + public SDL_ControllerDeviceEvent ctouchpad; + [FieldOffset(0)] + public SDL_ControllerDeviceEvent csensor; + [FieldOffset(0)] public SDL_AudioDeviceEvent adevice; [FieldOffset(0)] public SDL_SensorEvent sensor; @@ -5980,6 +6094,9 @@ namespace SDL2 SDL_JOYSTICK_TYPE_ARCADE_PAD } + /* Only available in 2.0.14 or higher. */ + public const float SDL_IPHONE_MAX_GFORCE = 5.0f; + /* joystick refers to an SDL_Joystick*. * Only available in 2.0.9 or higher. */ @@ -5991,6 +6108,17 @@ namespace SDL2 UInt32 duration_ms ); + /* joystick refers to an SDL_Joystick*. + * Only available in 2.0.14 or higher. + */ + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public static extern int SDL_JoystickRumbleTriggers( + IntPtr joystick, + UInt16 left_rumble, + UInt16 right_rumble, + UInt32 duration_ms + ); + /* joystick refers to an SDL_Joystick* */ [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern void SDL_JoystickClose(IntPtr joystick); @@ -6160,6 +6288,21 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern ushort SDL_JoystickGetProductVersion(IntPtr joystick); + /* joystick refers to an SDL_Joystick*. + * Only available in 2.0.14 or higher. + */ + [DllImport(nativeLibName, EntryPoint = "SDL_JoystickGetSerial", CallingConvention = CallingConvention.Cdecl)] + private static extern IntPtr INTERNAL_SDL_JoystickGetSerial( + IntPtr joystick + ); + public static string SDL_JoystickGetSerial( + IntPtr joystick + ) { + return UTF8_ToManaged( + INTERNAL_SDL_JoystickGetSerial(joystick) + ); + } + /* joystick refers to an SDL_Joystick*. * Only available in 2.0.6 or higher. */ @@ -6211,6 +6354,72 @@ namespace SDL2 int player_index ); + /* Int32 refers to an SDL_JoystickType. + * Only available in 2.0.14 or higher. + */ + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public static extern int SDL_JoystickAttachVirtual( + Int32 type, + int naxes, + int nbuttons, + int nhats + ); + + /* Only available in 2.0.14 or higher. */ + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public static extern int SDL_JoystickDetachVirtual(int device_index); + + /* Only available in 2.0.14 or higher. */ + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public static extern SDL_bool SDL_JoystickIsVirtual(int device_index); + + /* IntPtr refers to an SDL_Joystick*. + * Only available in 2.0.14 or higher. + */ + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public static extern int SDL_JoystickSetVirtualAxis( + IntPtr joystick, + int axis, + Int16 value + ); + + /* IntPtr refers to an SDL_Joystick*. + * Only available in 2.0.14 or higher. + */ + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public static extern int SDL_JoystickSetVirtualButton( + IntPtr joystick, + int button, + byte value + ); + + /* IntPtr refers to an SDL_Joystick*. + * Only available in 2.0.14 or higher. + */ + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public static extern int SDL_JoystickSetVirtualHat( + IntPtr joystick, + int hat, + byte value + ); + + /* IntPtr refers to an SDL_Joystick*. + * Only available in 2.0.14 or higher. + */ + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public static extern SDL_bool SDL_JoystickHasLED(IntPtr joystick); + + /* IntPtr refers to an SDL_Joystick*. + * Only available in 2.0.14 or higher. + */ + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public static extern int SDL_JoystickSetLED( + IntPtr joystick, + byte red, + byte green, + byte blue + ); + #endregion #region SDL_gamecontroller.h @@ -6253,6 +6462,12 @@ namespace SDL2 SDL_CONTROLLER_BUTTON_DPAD_DOWN, SDL_CONTROLLER_BUTTON_DPAD_LEFT, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, + SDL_CONTROLLER_BUTTON_MISC1, + SDL_CONTROLLER_BUTTON_PADDLE1, + SDL_CONTROLLER_BUTTON_PADDLE2, + SDL_CONTROLLER_BUTTON_PADDLE3, + SDL_CONTROLLER_BUTTON_PADDLE4, + SDL_CONTROLLER_BUTTON_TOUCHPAD, SDL_CONTROLLER_BUTTON_MAX, } @@ -6263,7 +6478,9 @@ namespace SDL2 SDL_CONTROLLER_TYPE_XBOXONE, SDL_CONTROLLER_TYPE_PS3, SDL_CONTROLLER_TYPE_PS4, - SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO + SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO, + SDL_CONTROLLER_TYPE_VIRTUAL, /* Requires >= 2.0.14 */ + SDL_CONTROLLER_TYPE_PS5, /* Requires >= 2.0.14 */ } // FIXME: I'd rather this somehow be private... @@ -6441,6 +6658,21 @@ namespace SDL2 IntPtr gamecontroller ); + /* gamecontroller refers to an SDL_GameController*. + * Only available in 2.0.14 or higher. + */ + [DllImport(nativeLibName, EntryPoint = "SDL_GameControllerGetSerial", CallingConvention = CallingConvention.Cdecl)] + private static extern IntPtr INTERNAL_SDL_GameControllerGetSerial( + IntPtr gamecontroller + ); + public static string SDL_GameControllerGetSerial( + IntPtr gamecontroller + ) { + return UTF8_ToManaged( + INTERNAL_SDL_GameControllerGetSerial(gamecontroller) + ); + } + /* gamecontroller refers to an SDL_GameController* */ [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern SDL_bool SDL_GameControllerGetAttached( @@ -6584,6 +6816,17 @@ namespace SDL2 UInt32 duration_ms ); + /* gamecontroller refers to an SDL_GameController*. + * Only available in 2.0.14 or higher. + */ + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public static extern int SDL_GameControllerRumbleTriggers( + IntPtr joystick, + UInt16 left_rumble, + UInt16 right_rumble, + UInt32 duration_ms + ); + /* gamecontroller refers to an SDL_GameController* */ [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern void SDL_GameControllerClose( @@ -6627,6 +6870,114 @@ namespace SDL2 int player_index ); + /* gamecontroller refers to an SDL_GameController*. + * Only available in 2.0.14 or higher. + */ + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public static extern SDL_bool SDL_GameControllerHasLED( + IntPtr gamecontroller + ); + + /* gamecontroller refers to an SDL_GameController*. + * Only available in 2.0.14 or higher. + */ + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public static extern int SDL_GameControllerSetLED( + IntPtr gamecontroller, + byte red, + byte green, + byte blue + ); + + /* gamecontroller refers to an SDL_GameController*. + * Only available in 2.0.14 or higher. + */ + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public static extern SDL_bool SDL_GameControllerHasAxis( + IntPtr gamecontroller, + SDL_GameControllerAxis axis + ); + + /* gamecontroller refers to an SDL_GameController*. + * Only available in 2.0.14 or higher. + */ + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public static extern SDL_bool SDL_GameControllerHasButton( + IntPtr gamecontroller, + SDL_GameControllerButton button + ); + + /* gamecontroller refers to an SDL_GameController*. + * Only available in 2.0.14 or higher. + */ + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public static extern int SDL_GameControllerGetNumTouchpads( + IntPtr gamecontroller + ); + + /* gamecontroller refers to an SDL_GameController*. + * Only available in 2.0.14 or higher. + */ + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public static extern int SDL_GameControllerGetNumTouchpadFingers( + IntPtr gamecontroller, + int touchpad + ); + + /* gamecontroller refers to an SDL_GameController*. + * Only available in 2.0.14 or higher. + */ + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public static extern int SDL_GameControllerGetTouchpadFinger( + IntPtr gamecontroller, + int touchpad, + int finger, + out byte state, + out float x, + out float y, + out float pressure + ); + + /* gamecontroller refers to an SDL_GameController*. + * Only available in 2.0.14 or higher. + */ + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public static extern SDL_bool SDL_GameControllerHasSensor( + IntPtr gamecontroller, + SDL_SensorType type + ); + + /* gamecontroller refers to an SDL_GameController*. + * Only available in 2.0.14 or higher. + */ + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public static extern int SDL_GameControllerSetSensorEnabled( + IntPtr gamecontroller, + SDL_SensorType type, + SDL_bool enabled + ); + + /* gamecontroller refers to an SDL_GameController*. + * Only available in 2.0.14 or higher. + */ + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public static extern SDL_bool SDL_GameControllerIsSensorEnabled( + IntPtr gamecontroller, + SDL_SensorType type + ); + + /* gamecontroller refers to an SDL_GameController*. + * data refers to a float*. + * Only available in 2.0.14 or higher. + */ + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public static extern SDL_bool SDL_GameControllerGetSensorData( + IntPtr gamecontroller, + SDL_SensorType type, + IntPtr data, + int num_values + ); + #endregion #region SDL_haptic.h @@ -6652,6 +7003,7 @@ namespace SDL2 public const byte SDL_HAPTIC_POLAR = 0; public const byte SDL_HAPTIC_CARTESIAN = 1; public const byte SDL_HAPTIC_SPHERICAL = 2; + public const byte SDL_HAPTIC_STEERING_AXIS = 3; /* SDL_HapticRunEffect */ public const uint SDL_HAPTIC_INFINITY = 4294967295U; @@ -7042,6 +7394,14 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern void SDL_SensorUpdate(); + /* Only available in 2.0.14 or higher. */ + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public static extern void SDL_LockSensors(); + + /* Only available in 2.0.14 or higher. */ + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public static extern void SDL_UnlockSensors(); + #endregion #region SDL_audio.h @@ -7554,6 +7914,19 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern int SDL_GetAndroidSDKVersion(); + /* Only available in 2.0.14 or higher. */ + [DllImport(nativeLibName, EntryPoint = "SDL_AndroidRequestPermission", CallingConvention = CallingConvention.Cdecl)] + public static unsafe extern SDL_bool INTERNAL_SDL_AndroidRequestPermission( + byte* permission + ); + public static unsafe SDL_bool SDL_AndroidRequestPermission( + string permission + ) { + return INTERNAL_SDL_AndroidRequestPermission( + Utf8Encode(permission) + ); + } + /* WinRT */ public enum SDL_WinRT_DeviceFamily @@ -7665,6 +8038,14 @@ namespace SDL2 public IntPtr window; // Refers to an EGLNativeWindowType } + /* Only available in 2.0.14 or higher. */ + [StructLayout(LayoutKind.Sequential)] + public struct INTERNAL_os2_wminfo + { + public IntPtr hwnd; /* Refers to an HWND */ + public IntPtr hwndFrame; /* Refers to an HWND */ + } + [StructLayout(LayoutKind.Explicit)] public struct INTERNAL_SysWMDriverUnion { @@ -7688,6 +8069,8 @@ namespace SDL2 public INTERNAL_android_wminfo android; [FieldOffset(0)] public INTERNAL_vivante_wminfo vivante; + [FieldOffset(0)] + public INTERNAL_os2_wminfo os2; // private int dummy; } @@ -7821,6 +8204,10 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr SDL_SIMDAlloc(uint len); + /* Only available in SDL 2.0.14 or higher. */ + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public static extern IntPtr SDL_SIMDRealloc(IntPtr mem, uint len); + /* Only available in SDL 2.0.10 or higher. */ [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern void SDL_SIMDFree(IntPtr ptr); @@ -7830,5 +8217,36 @@ namespace SDL2 public static extern void SDL_HasARMSIMD(); #endregion + + #region SDL_locale.h + + [StructLayout(LayoutKind.Sequential)] + public struct SDL_Locale + { + IntPtr language; + IntPtr country; + } + + /* IntPtr refers to an SDL_Locale*. + * Only available in 2.0.14 or higher. + */ + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + public static extern IntPtr SDL_GetPreferredLocales(); + + #endregion + + #region SDL_misc.h + + /* Only available in 2.0.14 or higher. */ + [DllImport(nativeLibName, EntryPoint = "SDL_OpenURL", CallingConvention = CallingConvention.Cdecl)] + private static unsafe extern int INTERNAL_SDL_OpenURL(byte* url); + public static unsafe int SDL_OpenURL(string url) + { + return INTERNAL_SDL_OpenURL( + Utf8Encode(url) + ); + } + + #endregion } } From 332667f37b6619e6465fe503079253e220d3ffc8 Mon Sep 17 00:00:00 2001 From: Ethan Lee Date: Tue, 22 Dec 2020 02:01:34 -0500 Subject: [PATCH 11/17] Fix return type for GameControllerGetSensorData --- src/SDL2.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SDL2.cs b/src/SDL2.cs index d685649..54e4277 100644 --- a/src/SDL2.cs +++ b/src/SDL2.cs @@ -6971,7 +6971,7 @@ namespace SDL2 * Only available in 2.0.14 or higher. */ [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern SDL_bool SDL_GameControllerGetSensorData( + public static extern int SDL_GameControllerGetSensorData( IntPtr gamecontroller, SDL_SensorType type, IntPtr data, From 1e01bc8eebb501bf6df24ec98784c32843308e0a Mon Sep 17 00:00:00 2001 From: Ethan Lee Date: Fri, 1 Jan 2021 11:32:54 -0500 Subject: [PATCH 12/17] Happy New Year! --- LICENSE | 2 +- gitlab-ci/SDL2-CS.nuspec | 2 +- src/SDL2.cs | 2 +- src/SDL2_image.cs | 2 +- src/SDL2_mixer.cs | 2 +- src/SDL2_ttf.cs | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/LICENSE b/LICENSE index 6f2f39a..7e8803e 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ /* SDL2# - C# Wrapper for SDL2 * - * Copyright (c) 2013-2020 Ethan Lee. + * Copyright (c) 2013-2021 Ethan Lee. * * This software is provided 'as-is', without any express or implied warranty. * In no event will the authors be held liable for any damages arising from diff --git a/gitlab-ci/SDL2-CS.nuspec b/gitlab-ci/SDL2-CS.nuspec index 094a90d..718dc26 100644 --- a/gitlab-ci/SDL2-CS.nuspec +++ b/gitlab-ci/SDL2-CS.nuspec @@ -9,7 +9,7 @@ https://github.com/flibitijibibo/SDL2-CS false SDL2# - C# Wrapper for SDL2 - Copyright 2013-2020 + Copyright 2013-2021 SDL2# SDL2 SDL diff --git a/src/SDL2.cs b/src/SDL2.cs index 54e4277..25ed0ff 100644 --- a/src/SDL2.cs +++ b/src/SDL2.cs @@ -1,7 +1,7 @@ #region License /* SDL2# - C# Wrapper for SDL2 * - * Copyright (c) 2013-2020 Ethan Lee. + * Copyright (c) 2013-2021 Ethan Lee. * * This software is provided 'as-is', without any express or implied warranty. * In no event will the authors be held liable for any damages arising from diff --git a/src/SDL2_image.cs b/src/SDL2_image.cs index 9fa193b..c2b9a56 100644 --- a/src/SDL2_image.cs +++ b/src/SDL2_image.cs @@ -1,7 +1,7 @@ #region License /* SDL2# - C# Wrapper for SDL2 * - * Copyright (c) 2013-2020 Ethan Lee. + * Copyright (c) 2013-2021 Ethan Lee. * * This software is provided 'as-is', without any express or implied warranty. * In no event will the authors be held liable for any damages arising from diff --git a/src/SDL2_mixer.cs b/src/SDL2_mixer.cs index 8c59dad..5b94c1f 100644 --- a/src/SDL2_mixer.cs +++ b/src/SDL2_mixer.cs @@ -1,7 +1,7 @@ #region License /* SDL2# - C# Wrapper for SDL2 * - * Copyright (c) 2013-2020 Ethan Lee. + * Copyright (c) 2013-2021 Ethan Lee. * * This software is provided 'as-is', without any express or implied warranty. * In no event will the authors be held liable for any damages arising from diff --git a/src/SDL2_ttf.cs b/src/SDL2_ttf.cs index 2051b3e..13e3818 100644 --- a/src/SDL2_ttf.cs +++ b/src/SDL2_ttf.cs @@ -1,7 +1,7 @@ #region License /* SDL2# - C# Wrapper for SDL2 * - * Copyright (c) 2013-2020 Ethan Lee. + * Copyright (c) 2013-2021 Ethan Lee. * * This software is provided 'as-is', without any express or implied warranty. * In no event will the authors be held liable for any damages arising from From 21ec9cecad4b668f62216c780107a71188c91ece Mon Sep 17 00:00:00 2001 From: ameliadiedrich Date: Wed, 27 Jan 2021 11:48:18 +0100 Subject: [PATCH 13/17] Added SDL_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR Hint --- src/SDL2.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/SDL2.cs b/src/SDL2.cs index 25ed0ff..489a93b 100644 --- a/src/SDL2.cs +++ b/src/SDL2.cs @@ -604,6 +604,8 @@ namespace SDL2 "SDL_IOS_HIDE_HOME_INDICATOR"; public const string SDL_HINT_TV_REMOTE_AS_JOYSTICK = "SDL_TV_REMOTE_AS_JOYSTICK"; + public const string SDL_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR = + "SDL_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR"; /* Only available in 2.0.9 or higher. */ public const string SDL_HINT_MOUSE_DOUBLE_CLICK_TIME = From 22301b61f4362a57da0598df24e026b017c45d6f Mon Sep 17 00:00:00 2001 From: Ethan Lee Date: Wed, 3 Feb 2021 11:00:31 -0500 Subject: [PATCH 14/17] Fix SDL_LoadFile signature --- src/SDL2.cs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/SDL2.cs b/src/SDL2.cs index 489a93b..8445e3e 100644 --- a/src/SDL2.cs +++ b/src/SDL2.cs @@ -397,12 +397,19 @@ namespace SDL2 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern long SDL_RWclose(IntPtr context); - /* file refers to a const char*, datasize to a size_t* + /* datasize refers to a size_t* * IntPtr refers to a void* * Only available in SDL 2.0.10 or higher. */ - [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern IntPtr SDL_LoadFile(IntPtr file, IntPtr datasize); + [DllImport(nativeLibName, EntryPoint = "SDL_LoadFile", CallingConvention = CallingConvention.Cdecl)] + private static extern unsafe IntPtr INTERNAL_SDL_LoadFile(byte* file, out IntPtr datasize); + public static unsafe IntPtr SDL_LoadFile(string file, out IntPtr datasize) + { + byte* utf8File = Utf8Encode(file); + IntPtr result = INTERNAL_SDL_LoadFile(utf8File, out datasize); + Marshal.FreeHGlobal((IntPtr) utf8File); + return result; + } #endregion From f636c6175de72994d4ebf61d1e03d3442d3db231 Mon Sep 17 00:00:00 2001 From: deccer Date: Mon, 15 Feb 2021 18:30:49 +0400 Subject: [PATCH 15/17] Add support for SDL2-CS.Settings.props, for custom build options. (#214) Based on FNA.Settings.props. --- SDL2-CS.Core.csproj | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/SDL2-CS.Core.csproj b/SDL2-CS.Core.csproj index 6e8e706..8962fde 100644 --- a/SDL2-CS.Core.csproj +++ b/SDL2-CS.Core.csproj @@ -10,6 +10,14 @@ true false + + $(SolutionDir)SDL-CS.Settings.props + + + + + + From 2b8d237fd4585d14ea837764ac247d4cd200158f Mon Sep 17 00:00:00 2001 From: Ethan Lee Date: Mon, 1 Mar 2021 09:20:52 -0500 Subject: [PATCH 16/17] Support SDL2-CS.Settings.props for both project types --- SDL2-CS.Core.csproj | 2 +- SDL2-CS.csproj | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/SDL2-CS.Core.csproj b/SDL2-CS.Core.csproj index 8962fde..0d7a856 100644 --- a/SDL2-CS.Core.csproj +++ b/SDL2-CS.Core.csproj @@ -11,7 +11,7 @@ false - $(SolutionDir)SDL-CS.Settings.props + $(SolutionDir)SDL2-CS.Settings.props diff --git a/SDL2-CS.csproj b/SDL2-CS.csproj index 68da8c8..3575229 100644 --- a/SDL2-CS.csproj +++ b/SDL2-CS.csproj @@ -75,6 +75,14 @@ true + + $(SolutionDir)SDL2-CS.Settings.props + + + + + + From 53026deb22d5fe43bf270dd3c6d0aee573dd488a Mon Sep 17 00:00:00 2001 From: Ethan Lee Date: Sat, 13 Mar 2021 16:13:27 -0500 Subject: [PATCH 17/17] Fix some native memory leaks in the newer functions --- src/SDL2.cs | 33 +++++++++------------------------ 1 file changed, 9 insertions(+), 24 deletions(-) diff --git a/src/SDL2.cs b/src/SDL2.cs index 8445e3e..3c358ff 100644 --- a/src/SDL2.cs +++ b/src/SDL2.cs @@ -91,25 +91,6 @@ namespace SDL2 } return buffer; } - internal static unsafe byte* Utf8EncodeNullable(string str) - { - if (str == null) - { - return (byte*) 0; - } - int bufferSize = Utf8Size(str); - byte* buffer = (byte*) Marshal.AllocHGlobal(bufferSize); - fixed (char* strPtr = str) - { - Encoding.UTF8.GetBytes( - strPtr, - (str != null) ? (str.Length + 1) : 0, - buffer, - bufferSize - ); - } - return buffer; - } /* This is public because SDL_DropEvent needs it! */ public static unsafe string UTF8_ToManaged(IntPtr s, bool freePtr = false) @@ -7931,9 +7912,12 @@ namespace SDL2 public static unsafe SDL_bool SDL_AndroidRequestPermission( string permission ) { - return INTERNAL_SDL_AndroidRequestPermission( - Utf8Encode(permission) + byte* permissionPtr = Utf8Encode(permission); + SDL_bool result = INTERNAL_SDL_AndroidRequestPermission( + permissionPtr ); + Marshal.FreeHGlobal((IntPtr) permissionPtr); + return result; } /* WinRT */ @@ -8251,9 +8235,10 @@ namespace SDL2 private static unsafe extern int INTERNAL_SDL_OpenURL(byte* url); public static unsafe int SDL_OpenURL(string url) { - return INTERNAL_SDL_OpenURL( - Utf8Encode(url) - ); + byte* urlPtr = Utf8Encode(url); + int result = INTERNAL_SDL_OpenURL(urlPtr); + Marshal.FreeHGlobal((IntPtr) urlPtr); + return result; } #endregion