From e3c8fd6586e68b8108c474308657ad01cd66ae72 Mon Sep 17 00:00:00 2001 From: Laurent Gomila Date: Mon, 9 May 2011 23:23:08 +0200 Subject: [PATCH] Fixed crash in Text.DisplayedString (get) in SFML.Net (fixes issue #16) --- bindings/dotnet/src/Graphics/Text.cs | 41 ++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/bindings/dotnet/src/Graphics/Text.cs b/bindings/dotnet/src/Graphics/Text.cs index 16d12987..1524e0e0 100644 --- a/bindings/dotnet/src/Graphics/Text.cs +++ b/bindings/dotnet/src/Graphics/Text.cs @@ -201,10 +201,32 @@ namespace SFML //////////////////////////////////////////////////////////// public string DisplayedString { - // TODO : use unicode functions - // (convert from UTF-16 to UTF-32, and find how to marshal System.String as sfUint32*...) - get {return sfText_GetString(This);} - set {sfText_SetString(This, value);} + get + { + // Get the number of characters + // This is probably not the most optimized way; if anyone has a better solution... + int length = Marshal.PtrToStringAnsi(sfText_GetString(This)).Length; + + // Copy the characters + byte[] characters = new byte[length * 4]; + Marshal.Copy(sfText_GetUnicodeString(This), characters, 0, characters.Length); + + // Convert from UTF-32 to String (UTF-16) + return System.Text.Encoding.UTF32.GetString(characters); + } + + set + { + // Convert from String (UTF-16) to UTF-32 + int[] characters = new int[value.Length]; + for (int i = 0; i < value.Length; ++i) + characters[i] = Char.ConvertToUtf32(value, i); + + // Transform to raw and pass to the C API + GCHandle handle = GCHandle.Alloc(characters, GCHandleType.Pinned); + sfText_SetUnicodeString(This, handle.AddrOfPinnedObject()); + handle.Free(); + } } //////////////////////////////////////////////////////////// @@ -406,8 +428,8 @@ namespace SFML [DllImport("csfml-graphics-2", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] static extern void sfRenderImage_DrawTextWithShader(IntPtr This, IntPtr String, IntPtr Shader); - [DllImport("csfml-graphics-2", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi), SuppressUnmanagedCodeSecurity] - static extern void sfText_SetString(IntPtr This, string Text); + [DllImport("csfml-graphics-2", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + static extern void sfText_SetUnicodeString(IntPtr This, IntPtr Text); [DllImport("csfml-graphics-2", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] static extern void sfText_SetFont(IntPtr This, IntPtr Font); @@ -418,8 +440,11 @@ namespace SFML [DllImport("csfml-graphics-2", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] static extern void sfText_SetStyle(IntPtr This, Styles Style); - [DllImport("csfml-graphics-2", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi), SuppressUnmanagedCodeSecurity] - static extern string sfText_GetString(IntPtr This); + [DllImport("csfml-graphics-2", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + static extern IntPtr sfText_GetString(IntPtr This); + + [DllImport("csfml-graphics-2", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + static extern IntPtr sfText_GetUnicodeString(IntPtr This); [DllImport("csfml-graphics-2", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] static extern uint sfText_GetCharacterSize(IntPtr This);