From c782115e7f86fd5e763c4e0fbc9d292117c7257d Mon Sep 17 00:00:00 2001 From: Ethan Lee Date: Thu, 21 May 2020 10:24:46 -0400 Subject: [PATCH] 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* */