Added UTF-8 support to supplementary libs

Style changes for consistency
This commit is contained in:
Jameson Ernst 2013-06-25 23:16:43 -07:00
parent 1c6c04f2e2
commit 4868a53683
4 changed files with 84 additions and 69 deletions

View File

@ -1,28 +1,63 @@
/* SDL2# - C# Wrapper for SDL2
*
* Copyright (c) 2013 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
* the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software in a
* product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*
* Ethan "flibitijibibo" Lee <flibitijibibo@flibitijibibo.com>
*
*/
using System; using System;
using System.Text; using System.Text;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
namespace SDL2 { namespace SDL2
internal unsafe class LPUtf8StrMarshaler : ICustomMarshaler { {
static LPUtf8StrMarshaler _instance = new LPUtf8StrMarshaler(); internal unsafe class LPUtf8StrMarshaler : ICustomMarshaler
{
private static LPUtf8StrMarshaler _instance = new LPUtf8StrMarshaler();
static ICustomMarshaler GetInstance (string cookie) { private static ICustomMarshaler GetInstance(string cookie)
{
return _instance; return _instance;
} }
public object MarshalNativeToManaged (IntPtr pNativeData) { public object MarshalNativeToManaged(IntPtr pNativeData)
{
var ptr = (byte*)pNativeData; var ptr = (byte*)pNativeData;
while (*ptr != 0) while (*ptr != 0)
{
ptr++; ptr++;
}
var bytes = new byte[ptr - (byte*)pNativeData]; var bytes = new byte[ptr - (byte*)pNativeData];
Marshal.Copy(pNativeData, bytes, 0, bytes.Length); Marshal.Copy(pNativeData, bytes, 0, bytes.Length);
return Encoding.UTF8.GetString(bytes); return Encoding.UTF8.GetString(bytes);
} }
public IntPtr MarshalManagedToNative (object ManagedObj) { public IntPtr MarshalManagedToNative(object ManagedObj)
{
var str = ManagedObj as string; var str = ManagedObj as string;
if (str == null) if (str == null)
{
throw new ArgumentException("ManagedObj must be a string.", "ManagedObj"); throw new ArgumentException("ManagedObj must be a string.", "ManagedObj");
}
var bytes = Encoding.UTF8.GetBytes(str); var bytes = Encoding.UTF8.GetBytes(str);
var mem = Marshal.AllocHGlobal(bytes.Length + 1); var mem = Marshal.AllocHGlobal(bytes.Length + 1);
Marshal.Copy(bytes, 0, mem, bytes.Length); Marshal.Copy(bytes, 0, mem, bytes.Length);
@ -30,14 +65,17 @@ namespace SDL2 {
return mem; return mem;
} }
public void CleanUpNativeData (IntPtr pNativeData) { public void CleanUpNativeData(IntPtr pNativeData)
{
Marshal.FreeHGlobal(pNativeData); Marshal.FreeHGlobal(pNativeData);
} }
public void CleanUpManagedData (object ManagedObj) { public void CleanUpManagedData(object ManagedObj)
{
} }
public int GetNativeDataSize () { public int GetNativeDataSize()
{
return -1; return -1;
} }
} }

View File

@ -90,7 +90,7 @@ namespace SDL2
/* IntPtr refers to an SDL_Surface* */ /* IntPtr refers to an SDL_Surface* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr IMG_Load( public static extern IntPtr IMG_Load(
[In()] [MarshalAs(UnmanagedType.LPStr)] [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string file string file
); );
@ -98,7 +98,7 @@ namespace SDL2
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr IMG_LoadTexture( public static extern IntPtr IMG_LoadTexture(
IntPtr renderer, IntPtr renderer,
[In()] [MarshalAs(UnmanagedType.LPStr)] [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string file string file
); );

View File

@ -199,7 +199,7 @@ namespace SDL2
/* IntPtr refers to a Mix_Music* */ /* IntPtr refers to a Mix_Music* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr Mix_LoadMUS( public static extern IntPtr Mix_LoadMUS(
[In()] [MarshalAs(UnmanagedType.LPStr)] [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string file string file
); );
@ -222,27 +222,17 @@ namespace SDL2
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern int Mix_GetNumChunkDecoders(); public static extern int Mix_GetNumChunkDecoders();
[DllImport(nativeLibName, EntryPoint = "Mix_GetChunkDecoder", CallingConvention = CallingConvention.Cdecl)] [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr INTERNAL_Mix_GetChunkDecoder(int index); [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
public static string Mix_GetChunkDecoder(int index) public static extern string Mix_GetChunkDecoder(int index);
{
return Marshal.PtrToStringAnsi(
INTERNAL_Mix_GetChunkDecoder(index)
);
}
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern int Mix_GetNumMusicDecoders(); public static extern int Mix_GetNumMusicDecoders();
[DllImport(nativeLibName, EntryPoint = "Mix_GetMusicDecoder", CallingConvention = CallingConvention.Cdecl)] [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr INTERNAL_Mix_GetMusicDecoder(int index); [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
public static string Mix_GetMusicDecoder(int index) public static extern string Mix_GetMusicDecoder(int index);
{
return Marshal.PtrToStringAnsi(
INTERNAL_Mix_GetMusicDecoder(index)
);
}
/* music refers to a Mix_Music* */ /* music refers to a Mix_Music* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern Mix_MusicType Mix_GetMusicType(IntPtr music); public static extern Mix_MusicType Mix_GetMusicType(IntPtr music);
@ -462,7 +452,7 @@ namespace SDL2
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern int Mix_SetMusicCMD( public static extern int Mix_SetMusicCMD(
[In()] [MarshalAs(UnmanagedType.LPStr)] [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string command string command
); );
@ -474,17 +464,14 @@ namespace SDL2
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern int Mix_SetSoundFonts( public static extern int Mix_SetSoundFonts(
[In()] [MarshalAs(UnmanagedType.LPStr)] [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string paths string paths
); );
[DllImport(nativeLibName, EntryPoint = "Mix_GetSoundFonts", CallingConvention = CallingConvention.Cdecl)] [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr INTERNAL_Mix_GetSoundFonts(); [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
public static string Mix_GetSoundFonts() public static extern string Mix_GetSoundFonts();
{
return Marshal.PtrToStringAnsi(INTERNAL_Mix_GetSoundFonts());
}
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern int Mix_EachSoundFont( public static extern int Mix_EachSoundFont(
SoundFontDelegate function, SoundFontDelegate function,

View File

@ -95,7 +95,7 @@ namespace SDL2
/* IntPtr refers to a TTF_Font* */ /* IntPtr refers to a TTF_Font* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr TTF_OpenFont( public static extern IntPtr TTF_OpenFont(
[In()] [MarshalAs(UnmanagedType.LPStr)] [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string file, string file,
int ptsize int ptsize
); );
@ -103,7 +103,7 @@ namespace SDL2
/* IntPtr refers to a TTF_Font* */ /* IntPtr refers to a TTF_Font* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr TTF_OpenFontIndex( public static extern IntPtr TTF_OpenFontIndex(
[In()] [MarshalAs(UnmanagedType.LPStr)] [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string file, string file,
int ptsize, int ptsize,
long index long index
@ -166,29 +166,19 @@ namespace SDL2
public static extern int TTF_FontFaceIsFixedWidth(IntPtr font); public static extern int TTF_FontFaceIsFixedWidth(IntPtr font);
/* font refers to a TTF_Font* */ /* font refers to a TTF_Font* */
[DllImport(nativeLibName, EntryPoint = "TTF_FontFaceFamilyName", CallingConvention = CallingConvention.Cdecl)] [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr INTERNAL_TTF_FontFaceFamilyName( [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
public static extern string TTF_FontFaceFamilyName(
IntPtr font IntPtr font
); );
public static string TTF_FontFaceFamily(IntPtr font)
{
return Marshal.PtrToStringAnsi(
INTERNAL_TTF_FontFaceFamilyName(font)
);
}
/* font refers to a TTF_Font* */ /* font refers to a TTF_Font* */
[DllImport(nativeLibName, EntryPoint = "TTF_FontFaceStyleName", CallingConvention = CallingConvention.Cdecl)] [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr INTERNAL_TTF_FontFaceStyleName( [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
public static extern string TTF_FontFaceStyleName(
IntPtr font IntPtr font
); );
public static string TTF_FontFaceStyleName(IntPtr font)
{
return Marshal.PtrToStringAnsi(
INTERNAL_TTF_FontFaceStyleName(font)
);
}
/* font refers to a TTF_Font* */ /* font refers to a TTF_Font* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern int TTF_GlyphIsProvided(IntPtr font, ushort ch); public static extern int TTF_GlyphIsProvided(IntPtr font, ushort ch);
@ -209,7 +199,7 @@ namespace SDL2
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern int TTF_SizeText( public static extern int TTF_SizeText(
IntPtr font, IntPtr font,
[In()] [MarshalAs(UnmanagedType.LPStr)] [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string text, string text,
ref int w, ref int w,
ref int h ref int h
@ -219,7 +209,7 @@ namespace SDL2
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern int TTF_SizeUTF8( public static extern int TTF_SizeUTF8(
IntPtr font, IntPtr font,
[In()] [MarshalAs(UnmanagedType.LPStr)] [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string text, string text,
ref int w, ref int w,
ref int h ref int h
@ -238,7 +228,7 @@ namespace SDL2
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr TTF_RenderText_Solid( public static extern IntPtr TTF_RenderText_Solid(
IntPtr font, IntPtr font,
[In()] [MarshalAs(UnmanagedType.LPStr)] [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string text, string text,
SDL.SDL_Color fg SDL.SDL_Color fg
); );
@ -247,7 +237,7 @@ namespace SDL2
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr TTF_RenderUTF8_Solid( public static extern IntPtr TTF_RenderUTF8_Solid(
IntPtr font, IntPtr font,
[In()] [MarshalAs(UnmanagedType.LPStr)] [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string text, string text,
SDL.SDL_Color fg SDL.SDL_Color fg
); );
@ -272,7 +262,7 @@ namespace SDL2
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr TTF_RenderText_Shaded( public static extern IntPtr TTF_RenderText_Shaded(
IntPtr font, IntPtr font,
[In()] [MarshalAs(UnmanagedType.LPStr)] [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string text, string text,
SDL.SDL_Color fg, SDL.SDL_Color fg,
SDL.SDL_Color bg SDL.SDL_Color bg
@ -282,7 +272,7 @@ namespace SDL2
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr TTF_RenderUTF8_Shaded( public static extern IntPtr TTF_RenderUTF8_Shaded(
IntPtr font, IntPtr font,
[In()] [MarshalAs(UnmanagedType.LPStr)] [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string text, string text,
SDL.SDL_Color fg, SDL.SDL_Color fg,
SDL.SDL_Color bg SDL.SDL_Color bg
@ -310,7 +300,7 @@ namespace SDL2
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr TTF_RenderText_Blended( public static extern IntPtr TTF_RenderText_Blended(
IntPtr font, IntPtr font,
[In()] [MarshalAs(UnmanagedType.LPStr)] [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string text, string text,
SDL.SDL_Color fg SDL.SDL_Color fg
); );
@ -319,7 +309,7 @@ namespace SDL2
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr TTF_RenderUTF8_Blended( public static extern IntPtr TTF_RenderUTF8_Blended(
IntPtr font, IntPtr font,
[In()] [MarshalAs(UnmanagedType.LPStr)] [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string text, string text,
SDL.SDL_Color fg SDL.SDL_Color fg
); );
@ -336,7 +326,7 @@ namespace SDL2
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr TTF_RenderText_Blended_Wrapped( public static extern IntPtr TTF_RenderText_Blended_Wrapped(
IntPtr font, IntPtr font,
[In()] [MarshalAs(UnmanagedType.LPStr)] [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string text, string text,
SDL.SDL_Color fg, SDL.SDL_Color fg,
uint wrapped uint wrapped
@ -346,7 +336,7 @@ namespace SDL2
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr TTF_RenderUTF8_Blended_Wrapped( public static extern IntPtr TTF_RenderUTF8_Blended_Wrapped(
IntPtr font, IntPtr font,
[In()] [MarshalAs(UnmanagedType.LPStr)] [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string text, string text,
SDL.SDL_Color fg, SDL.SDL_Color fg,
uint wrapped uint wrapped