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.Text;
using System.Runtime.InteropServices;
namespace SDL2 {
internal unsafe class LPUtf8StrMarshaler : ICustomMarshaler {
static LPUtf8StrMarshaler _instance = new LPUtf8StrMarshaler();
namespace SDL2
{
internal unsafe class LPUtf8StrMarshaler : ICustomMarshaler
{
private static LPUtf8StrMarshaler _instance = new LPUtf8StrMarshaler();
static ICustomMarshaler GetInstance (string cookie) {
private static ICustomMarshaler GetInstance(string cookie)
{
return _instance;
}
public object MarshalNativeToManaged (IntPtr pNativeData) {
public object MarshalNativeToManaged(IntPtr pNativeData)
{
var ptr = (byte*)pNativeData;
while (*ptr != 0)
{
ptr++;
}
var bytes = new byte[ptr - (byte*)pNativeData];
Marshal.Copy(pNativeData, bytes, 0, bytes.Length);
return Encoding.UTF8.GetString(bytes);
}
public IntPtr MarshalManagedToNative (object ManagedObj) {
public IntPtr MarshalManagedToNative(object ManagedObj)
{
var str = ManagedObj as string;
if (str == null)
{
throw new ArgumentException("ManagedObj must be a string.", "ManagedObj");
}
var bytes = Encoding.UTF8.GetBytes(str);
var mem = Marshal.AllocHGlobal(bytes.Length + 1);
Marshal.Copy(bytes, 0, mem, bytes.Length);
@ -30,14 +65,17 @@ namespace SDL2 {
return mem;
}
public void CleanUpNativeData (IntPtr pNativeData) {
public void CleanUpNativeData(IntPtr pNativeData)
{
Marshal.FreeHGlobal(pNativeData);
}
public void CleanUpManagedData (object ManagedObj) {
public void CleanUpManagedData(object ManagedObj)
{
}
public int GetNativeDataSize () {
public int GetNativeDataSize()
{
return -1;
}
}

View file

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

View file

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

View file

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