Minor style fixes, UTF8 update for mixer/ttf

This commit is contained in:
Ethan Lee 2020-05-21 10:24:46 -04:00
parent 040d39b4a5
commit c782115e7f
4 changed files with 135 additions and 93 deletions

View file

@ -98,13 +98,18 @@ namespace SDL2
byte* buffer = (byte*) Marshal.AllocHGlobal(bufferSize); byte* buffer = (byte*) Marshal.AllocHGlobal(bufferSize);
fixed (char* strPtr = str) 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; return buffer;
} }
/* This is public because SDL_DropEvent needs it! */ /* 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) if (s == IntPtr.Zero)
{ {
@ -1746,20 +1751,6 @@ namespace SDL2
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern void SDL_GL_DeleteContext(IntPtr context); 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)] [DllImport(nativeLibName, EntryPoint = "SDL_GL_LoadLibrary", CallingConvention = CallingConvention.Cdecl)]
private static extern unsafe int INTERNAL_SDL_GL_LoadLibrary(byte* path); private static extern unsafe int INTERNAL_SDL_GL_LoadLibrary(byte* path);
public static unsafe int SDL_GL_LoadLibrary(string path) public static unsafe int SDL_GL_LoadLibrary(string path)
@ -1776,6 +1767,16 @@ namespace SDL2
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr SDL_GL_GetProcAddress(IntPtr proc); 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)] [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern void SDL_GL_UnloadLibrary(); public static extern void SDL_GL_UnloadLibrary();

View file

@ -200,12 +200,17 @@ namespace SDL2
/* IntPtr refers to a Mix_Music* */ /* IntPtr refers to a Mix_Music* */
[DllImport(nativeLibName, EntryPoint = "Mix_LoadMUS", CallingConvention = CallingConvention.Cdecl)] [DllImport(nativeLibName, EntryPoint = "Mix_LoadMUS", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr INTERNAL_Mix_LoadMUS( private static extern unsafe IntPtr INTERNAL_Mix_LoadMUS(
byte[] file 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* */ /* IntPtr refers to a Mix_Chunk* */
@ -569,14 +574,17 @@ namespace SDL2
public static extern int Mix_PlayingMusic(); public static extern int Mix_PlayingMusic();
[DllImport(nativeLibName, EntryPoint = "Mix_SetMusicCMD", CallingConvention = CallingConvention.Cdecl)] [DllImport(nativeLibName, EntryPoint = "Mix_SetMusicCMD", CallingConvention = CallingConvention.Cdecl)]
private static extern int INTERNAL_Mix_SetMusicCMD( private static extern unsafe int INTERNAL_Mix_SetMusicCMD(
byte[] command byte* command
); );
public static int Mix_SetMusicCMD(string command) public static unsafe int Mix_SetMusicCMD(string command)
{ {
return INTERNAL_Mix_SetMusicCMD( byte* utf8Cmd = SDL.Utf8Encode(command);
SDL.UTF8_ToNative(command) int result = INTERNAL_Mix_SetMusicCMD(
utf8Cmd
); );
Marshal.FreeHGlobal((IntPtr) utf8Cmd);
return result;
} }
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
@ -586,14 +594,17 @@ namespace SDL2
public static extern int Mix_GetSynchroValue(); public static extern int Mix_GetSynchroValue();
[DllImport(nativeLibName, EntryPoint = "Mix_SetSoundFonts", CallingConvention = CallingConvention.Cdecl)] [DllImport(nativeLibName, EntryPoint = "Mix_SetSoundFonts", CallingConvention = CallingConvention.Cdecl)]
private static extern int INTERNAL_Mix_SetSoundFonts( private static extern unsafe int INTERNAL_Mix_SetSoundFonts(
byte[] paths byte* paths
); );
public static int Mix_SetSoundFonts(string paths) public static unsafe int Mix_SetSoundFonts(string paths)
{ {
return INTERNAL_Mix_SetSoundFonts( byte* utf8Paths = SDL.Utf8Encode(paths);
SDL.UTF8_ToNative(paths) int result = INTERNAL_Mix_SetSoundFonts(
utf8Paths
); );
Marshal.FreeHGlobal((IntPtr) utf8Paths);
return result;
} }
[DllImport(nativeLibName, EntryPoint = "Mix_GetSoundFonts", CallingConvention = CallingConvention.Cdecl)] [DllImport(nativeLibName, EntryPoint = "Mix_GetSoundFonts", CallingConvention = CallingConvention.Cdecl)]

View file

@ -95,16 +95,19 @@ namespace SDL2
/* IntPtr refers to a TTF_Font* */ /* IntPtr refers to a TTF_Font* */
[DllImport(nativeLibName, EntryPoint = "TTF_OpenFont", CallingConvention = CallingConvention.Cdecl)] [DllImport(nativeLibName, EntryPoint = "TTF_OpenFont", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr INTERNAL_TTF_OpenFont( private static extern unsafe IntPtr INTERNAL_TTF_OpenFont(
byte[] file, byte* file,
int ptsize 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( byte* utf8File = SDL.Utf8Encode(file);
SDL.UTF8_ToNative(file), IntPtr handle = INTERNAL_TTF_OpenFont(
utf8File,
ptsize ptsize
); );
Marshal.FreeHGlobal((IntPtr) utf8File);
return handle;
} }
/* src refers to an SDL_RWops*, IntPtr to a TTF_Font* */ /* src refers to an SDL_RWops*, IntPtr to a TTF_Font* */
@ -118,21 +121,24 @@ namespace SDL2
/* IntPtr refers to a TTF_Font* */ /* IntPtr refers to a TTF_Font* */
[DllImport(nativeLibName, EntryPoint = "TTF_OpenFontIndex", CallingConvention = CallingConvention.Cdecl)] [DllImport(nativeLibName, EntryPoint = "TTF_OpenFontIndex", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr INTERNAL_TTF_OpenFontIndex( private static extern unsafe IntPtr INTERNAL_TTF_OpenFontIndex(
byte[] file, byte* file,
int ptsize, int ptsize,
long index long index
); );
public static IntPtr TTF_OpenFontIndex( public static unsafe IntPtr TTF_OpenFontIndex(
string file, string file,
int ptsize, int ptsize,
long index long index
) { ) {
return INTERNAL_TTF_OpenFontIndex( byte* utf8File = SDL.Utf8Encode(file);
SDL.UTF8_ToNative(file), IntPtr handle = INTERNAL_TTF_OpenFontIndex(
utf8File,
ptsize, ptsize,
index index
); );
Marshal.FreeHGlobal((IntPtr) utf8File);
return handle;
} }
/* src refers to an SDL_RWops*, IntPtr to a TTF_Font* */ /* src refers to an SDL_RWops*, IntPtr to a TTF_Font* */
@ -282,24 +288,27 @@ namespace SDL2
/* font refers to a TTF_Font* */ /* font refers to a TTF_Font* */
[DllImport(nativeLibName, EntryPoint = "TTF_SizeUTF8", CallingConvention = CallingConvention.Cdecl)] [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, IntPtr font,
byte[] text, byte* text,
out int w, out int w,
out int h out int h
); );
public static int TTF_SizeUTF8( public static unsafe int TTF_SizeUTF8(
IntPtr font, IntPtr font,
string text, string text,
out int w, out int w,
out int h out int h
) { ) {
return INTERNAL_TTF_SizeUTF8( byte* utf8Text = SDL.Utf8Encode(text);
int result = INTERNAL_TTF_SizeUTF8(
font, font,
SDL.UTF8_ToNative(text), utf8Text,
out w, out w,
out h out h
); );
Marshal.FreeHGlobal((IntPtr) utf8Text);
return result;
} }
/* font refers to a TTF_Font* */ /* font refers to a TTF_Font* */
@ -329,27 +338,30 @@ namespace SDL2
* Only available in 2.0.16 or higher. * Only available in 2.0.16 or higher.
*/ */
[DllImport(nativeLibName, EntryPoint = "TTF_MeasureUTF8", CallingConvention = CallingConvention.Cdecl)] [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, IntPtr font,
byte[] text, byte* text,
int measure_width, int measure_width,
out int extent, out int extent,
out int count out int count
); );
public static int TTF_MeasureUTF8( public static unsafe int TTF_MeasureUTF8(
IntPtr font, IntPtr font,
string text, string text,
int measure_width, int measure_width,
out int extent, out int extent,
out int count out int count
) { ) {
return INTERNAL_TTF_MeasureUTF8( byte* utf8Text = SDL.Utf8Encode(text);
int result = INTERNAL_TTF_MeasureUTF8(
font, font,
SDL.UTF8_ToNative(text), utf8Text,
measure_width, measure_width,
out extent, out extent,
out count out count
); );
Marshal.FreeHGlobal((IntPtr) utf8Text);
return result;
} }
/* font refers to a TTF_Font* /* font refers to a TTF_Font*
@ -376,21 +388,24 @@ namespace SDL2
/* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */ /* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */
[DllImport(nativeLibName, EntryPoint = "TTF_RenderUTF8_Solid", CallingConvention = CallingConvention.Cdecl)] [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, IntPtr font,
byte[] text, byte* text,
SDL.SDL_Color fg SDL.SDL_Color fg
); );
public static IntPtr TTF_RenderUTF8_Solid( public static unsafe IntPtr TTF_RenderUTF8_Solid(
IntPtr font, IntPtr font,
string text, string text,
SDL.SDL_Color fg SDL.SDL_Color fg
) { ) {
return INTERNAL_TTF_RenderUTF8_Solid( byte* utf8Text = SDL.Utf8Encode(text);
IntPtr result = INTERNAL_TTF_RenderUTF8_Solid(
font, font,
SDL.UTF8_ToNative(text), utf8Text,
fg fg
); );
Marshal.FreeHGlobal((IntPtr) utf8Text);
return result;
} }
/* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */ /* 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. * Only available in 2.0.16 or higher.
*/ */
[DllImport(nativeLibName, EntryPoint = "TTF_RenderUTF8_Solid_Wrapped", CallingConvention = CallingConvention.Cdecl)] [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, IntPtr font,
byte[] text, byte* text,
SDL.SDL_Color fg, SDL.SDL_Color fg,
uint wrapLength uint wrapLength
); );
public static IntPtr TTF_RenderUTF8_Solid_Wrapped( public static unsafe IntPtr TTF_RenderUTF8_Solid_Wrapped(
IntPtr font, IntPtr font,
string text, string text,
SDL.SDL_Color fg, SDL.SDL_Color fg,
uint wrapLength uint wrapLength
) { ) {
return INTERNAL_TTF_RenderUTF8_Solid_Wrapped( byte* utf8Text = SDL.Utf8Encode(text);
IntPtr result = INTERNAL_TTF_RenderUTF8_Solid_Wrapped(
font, font,
SDL.UTF8_ToNative(text), utf8Text,
fg, fg,
wrapLength wrapLength
); );
Marshal.FreeHGlobal((IntPtr) utf8Text);
return result;
} }
/* IntPtr refers to an SDL_Surface*, font to a TTF_Font* /* 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* */ /* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */
[DllImport(nativeLibName, EntryPoint = "TTF_RenderUTF8_Shaded", CallingConvention = CallingConvention.Cdecl)] [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, IntPtr font,
byte[] text, byte* text,
SDL.SDL_Color fg, SDL.SDL_Color fg,
SDL.SDL_Color bg SDL.SDL_Color bg
); );
public static IntPtr TTF_RenderUTF8_Shaded( public static unsafe IntPtr TTF_RenderUTF8_Shaded(
IntPtr font, IntPtr font,
string text, string text,
SDL.SDL_Color fg, SDL.SDL_Color fg,
SDL.SDL_Color bg SDL.SDL_Color bg
) { ) {
return INTERNAL_TTF_RenderUTF8_Shaded( byte* utf8Text = SDL.Utf8Encode(text);
IntPtr result = INTERNAL_TTF_RenderUTF8_Shaded(
font, font,
SDL.UTF8_ToNative(text), utf8Text,
fg, fg,
bg bg
); );
Marshal.FreeHGlobal((IntPtr) utf8Text);
return result;
} }
/* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */ /* 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. * Only available in 2.0.16 or higher.
*/ */
[DllImport(nativeLibName, EntryPoint = "TTF_RenderUTF8_Shaded_Wrapped", CallingConvention = CallingConvention.Cdecl)] [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, IntPtr font,
byte[] text, byte* text,
SDL.SDL_Color fg, SDL.SDL_Color fg,
SDL.SDL_Color bg, SDL.SDL_Color bg,
uint wrapLength uint wrapLength
); );
public static IntPtr TTF_RenderUTF8_Shaded_Wrapped( public static unsafe IntPtr TTF_RenderUTF8_Shaded_Wrapped(
IntPtr font, IntPtr font,
string text, string text,
SDL.SDL_Color fg, SDL.SDL_Color fg,
SDL.SDL_Color bg, SDL.SDL_Color bg,
uint wrapLength uint wrapLength
) { ) {
return INTERNAL_TTF_RenderUTF8_Shaded_Wrapped( byte* utf8Text = SDL.Utf8Encode(text);
IntPtr result = INTERNAL_TTF_RenderUTF8_Shaded_Wrapped(
font, font,
SDL.UTF8_ToNative(text), utf8Text,
fg, fg,
bg, bg,
wrapLength wrapLength
); );
Marshal.FreeHGlobal((IntPtr) utf8Text);
return result;
} }
/* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */ /* 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* */ /* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */
[DllImport(nativeLibName, EntryPoint = "TTF_RenderUTF8_Blended", CallingConvention = CallingConvention.Cdecl)] [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, IntPtr font,
byte[] text, byte* text,
SDL.SDL_Color fg SDL.SDL_Color fg
); );
public static IntPtr TTF_RenderUTF8_Blended( public static unsafe IntPtr TTF_RenderUTF8_Blended(
IntPtr font, IntPtr font,
string text, string text,
SDL.SDL_Color fg SDL.SDL_Color fg
) { ) {
return INTERNAL_TTF_RenderUTF8_Blended( byte* utf8Text = SDL.Utf8Encode(text);
IntPtr result = INTERNAL_TTF_RenderUTF8_Blended(
font, font,
SDL.UTF8_ToNative(text), utf8Text,
fg fg
); );
Marshal.FreeHGlobal((IntPtr) utf8Text);
return result;
} }
/* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */ /* 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* */ /* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */
[DllImport(nativeLibName, EntryPoint = "TTF_RenderUTF8_Blended_Wrapped", CallingConvention = CallingConvention.Cdecl)] [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, IntPtr font,
byte[] text, byte* text,
SDL.SDL_Color fg, SDL.SDL_Color fg,
uint wrapped uint wrapped
); );
public static IntPtr TTF_RenderUTF8_Blended_Wrapped( public static unsafe IntPtr TTF_RenderUTF8_Blended_Wrapped(
IntPtr font, IntPtr font,
string text, string text,
SDL.SDL_Color fg, SDL.SDL_Color fg,
uint wrapped uint wrapped
) { ) {
return INTERNAL_TTF_RenderUTF8_Blended_Wrapped( byte* utf8Text = SDL.Utf8Encode(text);
IntPtr result = INTERNAL_TTF_RenderUTF8_Blended_Wrapped(
font, font,
SDL.UTF8_ToNative(text), utf8Text,
fg, fg,
wrapped wrapped
); );
Marshal.FreeHGlobal((IntPtr) utf8Text);
return result;
} }
/* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */ /* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */