Completed manual marshalling of strings.

Commented out code using __arglist because at the moment CoreFX reference assemblies do not have a definition for RuntimeArgumentHandle.
This commit is contained in:
Emmanuel Stapf 2015-10-15 18:09:08 +09:00
Родитель bb471e5de9
Коммит cfd6665846
7 изменённых файлов: 758 добавлений и 571 удалений

Просмотреть файл

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@ -9,6 +9,8 @@
<OutputType>Library</OutputType>
<RootNamespace>SDL2</RootNamespace>
<AssemblyName>SDL2-CS</AssemblyName>
<TargetFrameworkRootPath>$(MSBuildThisFileDirectory)\CoreFX</TargetFrameworkRootPath>
<FrameworkPathOverride>$(TargetFrameworkRootPath)</FrameworkPathOverride>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<DebugSymbols>true</DebugSymbols>
@ -74,22 +76,19 @@
<ConsolePause>false</ConsolePause>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
<Compile Include="src\SDL2.cs" />
<Compile Include="src\SDL2_image.cs" />
<Compile Include="src\SDL2_mixer.cs" />
<Compile Include="src\SDL2_ttf.cs" />
<Compile Include="src\LPUtf8StrMarshaler.cs" />
<Compile Include="src\UTF8String.cs" />
</ItemGroup>
<ItemGroup>
<None Include="SDL2-CS.dll.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<Folder Include="src\" />
</ItemGroup>
<ItemGroup />
<ProjectExtensions>
<MonoDevelop>
<Properties>
@ -102,4 +101,5 @@
<ItemGroup>
<Reference Include="System" />
</ItemGroup>
<Import Project="$(MSBuildThisFileDirectory)\CoreFX\configs\CoreCLR.CSharp.targets" />
</Project>

Просмотреть файл

@ -1,106 +0,0 @@
/* SDL2# - C# Wrapper for SDL2
*
* Copyright (c) 2013-2015 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
{
public const string LeaveAllocated = "LeaveAllocated";
private static ICustomMarshaler
_leaveAllocatedInstance = new LPUtf8StrMarshaler(true),
_defaultInstance = new LPUtf8StrMarshaler(false);
public static ICustomMarshaler GetInstance(string cookie)
{
switch (cookie)
{
case "LeaveAllocated":
return _leaveAllocatedInstance;
default:
return _defaultInstance;
}
}
private bool _leaveAllocated;
public LPUtf8StrMarshaler(bool leaveAllocated)
{
_leaveAllocated = leaveAllocated;
}
public object MarshalNativeToManaged(IntPtr pNativeData)
{
if (pNativeData == IntPtr.Zero)
return null;
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)
{
if (ManagedObj == null)
return IntPtr.Zero;
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 = SDL.SDL_malloc((IntPtr) bytes.Length + 1);
Marshal.Copy(bytes, 0, mem, bytes.Length);
((byte*)mem)[bytes.Length] = 0;
return mem;
}
public void CleanUpManagedData(object ManagedObj)
{
}
public void CleanUpNativeData(IntPtr pNativeData)
{
if (!_leaveAllocated)
{
SDL.SDL_free(pNativeData);
}
}
public int GetNativeDataSize ()
{
return -1;
}
}
}

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -88,11 +88,13 @@ namespace SDL2
public static extern void IMG_Quit();
/* IntPtr refers to an SDL_Surface* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr IMG_Load(
[In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string file
);
public static IntPtr IMG_Load( string file)
{
UTF8String fileUTF8 = new UTF8String(file);
return IMG_LoadNative(fileUTF8.Handle);
}
[DllImport(nativeLibName, EntryPoint = "IMG_LoadNative", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr IMG_LoadNative( IntPtr file);
/* src refers to an SDL_RWops*, IntPtr to an SDL_Surface* */
/* THIS IS A PUBLIC RWops FUNCTION! */
@ -104,21 +106,22 @@ namespace SDL2
/* src refers to an SDL_RWops*, IntPtr to an SDL_Surface* */
/* THIS IS A PUBLIC RWops FUNCTION! */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr IMG_LoadTyped_RW(
IntPtr src,
int freesrc,
[In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string type
);
public static IntPtr IMG_LoadTyped_RW(IntPtr src, int freesrc, string type)
{
UTF8String typeUTF8 = new UTF8String(type);
return IMG_LoadTyped_RWNative(src, freesrc, typeUTF8.Handle);
}
[DllImport(nativeLibName, EntryPoint = "IMG_LoadTypedRW", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr IMG_LoadTyped_RWNative( IntPtr src, int freesrc, IntPtr type );
/* IntPtr refers to an SDL_Texture*, renderer to an SDL_Renderer* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr IMG_LoadTexture(
IntPtr renderer,
[In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string file
);
public static IntPtr IMG_LoadTexture(IntPtr renderer, string file)
{
UTF8String fileUTF8 = new UTF8String(file);
return IMG_LoadTextureNative(renderer, fileUTF8.Handle);
}
[DllImport(nativeLibName, EntryPoint = "IMG_loadTexture", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr IMG_LoadTextureNative( IntPtr renderer, IntPtr file );
/* renderer refers to an SDL_Renderer*.
* src refers to an SDL_RWops*.
@ -137,14 +140,13 @@ namespace SDL2
* IntPtr to an SDL_Texture*.
*/
/* THIS IS A PUBLIC RWops FUNCTION! */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr IMG_LoadTextureTyped_RW(
IntPtr renderer,
IntPtr src,
int freesrc,
[In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string type
);
public static IntPtr IMG_LoadTextureTyped_RW(IntPtr renderer, IntPtr src, int freesrc, string type)
{
UTF8String typeUTF8 = new UTF8String(type);
return IMG_LoadTextureTyped_RWNative(renderer, src, freesrc, typeUTF8.Handle);
}
[DllImport(nativeLibName, EntryPoint = "IMG_LoadTextureTyped_RW", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr IMG_LoadTextureTyped_RWNative( IntPtr renderer, IntPtr src, int freesrc, IntPtr type );
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern int IMG_InvertAlpha(int on);
@ -157,12 +159,13 @@ namespace SDL2
);
/* surface refers to an SDL_Surface* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern int IMG_SavePNG(
IntPtr surface,
[In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string file
);
public static int IMG_SavePNG(IntPtr surface, string file)
{
UTF8String fileUTF8 = new UTF8String(file);
return IMG_SavePNGNative(surface, fileUTF8.Handle);
}
[DllImport(nativeLibName, EntryPoint = "IMG_SavePNG", CallingConvention = CallingConvention.Cdecl)]
public static extern int IMG_SavePNGNative( IntPtr surface, IntPtr file );
/* surface refers to an SDL_Surface*, dst to an SDL_RWops* */
/* THIS IS A PUBLIC RWops FUNCTION! */

Просмотреть файл

@ -184,16 +184,18 @@ namespace SDL2
/* This is an RWops macro in the C header. */
public static IntPtr Mix_LoadWAV(string file)
{
IntPtr rwops = SDL.INTERNAL_SDL_RWFromFile(file, "rb");
IntPtr rwops = SDL.SDL_RWFromFile(file, "rb");
return Mix_LoadWAV_RW(rwops, 1);
}
/* IntPtr refers to a Mix_Music* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr Mix_LoadMUS(
[In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string file
);
public static IntPtr Mix_LoadMUS( string file)
{
UTF8String fileUTF8 = new UTF8String(file);
return Mix_LoadMUSNative(fileUTF8.Handle);
}
[DllImport(nativeLibName, EntryPoint = "Mix_LoadMUS", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr Mix_LoadMUSNative( IntPtr file);
/* IntPtr refers to a Mix_Chunk* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
@ -221,16 +223,22 @@ namespace SDL2
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern int Mix_GetNumChunkDecoders();
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
[return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler), MarshalCookie = LPUtf8StrMarshaler.LeaveAllocated)]
public static extern string Mix_GetChunkDecoder(int index);
public static string Mix_GetChunkDecoder( int index)
{
return (new UTF8String(Mix_GetChunkDecoderNative(index))).String();
}
[DllImport(nativeLibName, EntryPoint = "Mix_GetChunkDecoder", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr Mix_GetChunkDecoderNative( int index);
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern int Mix_GetNumMusicDecoders();
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
[return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler), MarshalCookie = LPUtf8StrMarshaler.LeaveAllocated)]
public static extern string Mix_GetMusicDecoder(int index);
public static string Mix_GetMusicDecoder( int index)
{
return (new UTF8String(Mix_GetMusicDecoderNative(index))).String();
}
[DllImport(nativeLibName, EntryPoint = "Mix_GetMusicDecoder", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr Mix_GetMusicDecoderNative( int index);
/* music refers to a Mix_Music* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
@ -449,11 +457,13 @@ namespace SDL2
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern int Mix_PlayingMusic();
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern int Mix_SetMusicCMD(
[In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string command
);
public static int Mix_SetMusicCMD( string command)
{
UTF8String commandUTF8 = new UTF8String(command);
return Mix_SetMusicCMDNative(commandUTF8.Handle);
}
[DllImport(nativeLibName, EntryPoint = "Mix_SetMusicCMD", CallingConvention = CallingConvention.Cdecl)]
public static extern int Mix_SetMusicCMDNative( IntPtr command);
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern int Mix_SetSynchroValue(int value);
@ -461,15 +471,20 @@ namespace SDL2
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern int Mix_GetSynchroValue();
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern int Mix_SetSoundFonts(
[In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string paths
);
public static int Mix_SetSoundFonts( string paths)
{
UTF8String pathsUTF8 = new UTF8String(paths);
return Mix_SetSoundFontsNative(pathsUTF8.Handle);
}
[DllImport(nativeLibName, EntryPoint = "Mix_SetSoundFonts", CallingConvention = CallingConvention.Cdecl)]
public static extern int Mix_SetSoundFontsNative( IntPtr paths);
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
[return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler), MarshalCookie = LPUtf8StrMarshaler.LeaveAllocated)]
public static extern string Mix_GetSoundFonts();
public static string Mix_GetSoundFonts()
{
return (new UTF8String(Mix_GetSoundFontsNative())).String();
}
[DllImport(nativeLibName, EntryPoint = "Mix_GetSoundFonts", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr Mix_GetSoundFontsNative();
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern int Mix_EachSoundFont(

Просмотреть файл

@ -92,13 +92,14 @@ namespace SDL2
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern int TTF_Init();
/* IntPtr refers to a TTF_Font* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr TTF_OpenFont(
[In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string file,
int ptsize
);
/* IntPtr refers to a TTF_Font */
public static IntPtr TTF_OpenFont(string file, int ptsize)
{
UTF8String fileUTF8 = new UTF8String(file);
return TTF_OpenFontNative(fileUTF8.Handle, ptsize);
}
[DllImport(nativeLibName, EntryPoint = "TTF_OpenFont", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr TTF_OpenFontNative( IntPtr file, int ptsize );
/* src refers to an SDL_RWops*, IntPtr to a TTF_Font* */
/* THIS IS A PUBLIC RWops FUNCTION! */
@ -110,13 +111,13 @@ namespace SDL2
);
/* IntPtr refers to a TTF_Font* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr TTF_OpenFontIndex(
[In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string file,
int ptsize,
long index
);
public static IntPtr TTF_OpenFontIndex( string file, int ptsize, long index)
{
UTF8String fileUTF8 = new UTF8String(file);
return TTF_OpenFontIndexNative(fileUTF8.Handle, ptsize, index);
}
[DllImport(nativeLibName, EntryPoint = "TTF_OpenFontIndex", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr TTF_OpenFontIndexNative( IntPtr file, int ptsize, long index);
/* src refers to an SDL_RWops*, IntPtr to a TTF_Font* */
/* THIS IS A PUBLIC RWops FUNCTION! */
@ -185,18 +186,20 @@ namespace SDL2
public static extern int TTF_FontFaceIsFixedWidth(IntPtr font);
/* font refers to a TTF_Font* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
[return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler), MarshalCookie = LPUtf8StrMarshaler.LeaveAllocated)]
public static extern string TTF_FontFaceFamilyName(
IntPtr font
);
public static string TTF_FontFaceFamilyName( IntPtr font)
{
return (new UTF8String(TTF_FontFaceFamilyNameNative(font))).String();
}
[DllImport(nativeLibName, EntryPoint = "TTF_FontFaceFamilyName", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr TTF_FontFaceFamilyNameNative( IntPtr font);
/* font refers to a TTF_Font* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
[return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler), MarshalCookie = LPUtf8StrMarshaler.LeaveAllocated)]
public static extern string TTF_FontFaceStyleName(
IntPtr font
);
public static string TTF_FontFaceStyleName( IntPtr font)
{
return (new UTF8String(TTF_FontFaceStyleNameNative(font))).String();
}
[DllImport(nativeLibName, EntryPoint = "TTF_FontFaceStyleName", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr TTF_FontFaceStyleNameNative( IntPtr font);
/* font refers to a TTF_Font* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
@ -215,24 +218,22 @@ namespace SDL2
);
/* font refers to a TTF_Font* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern int TTF_SizeText(
IntPtr font,
[In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string text,
out int w,
out int h
);
public static int TTF_SizeText( IntPtr font, string text, out int w, out int h)
{
UTF8String textUTF8 = new UTF8String(text);
return TTF_SizeTextNative(font, textUTF8.Handle, out w, out h);
}
[DllImport(nativeLibName, EntryPoint="TTF_SizeText", CallingConvention = CallingConvention.Cdecl)]
public static extern int TTF_SizeTextNative( IntPtr font, IntPtr text, out int w, out int h);
/* font refers to a TTF_Font* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern int TTF_SizeUTF8(
IntPtr font,
[In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string text,
out int w,
out int h
);
public static int TTF_SizeUTF8( IntPtr font, string text, out int w, out int h)
{
UTF8String textUTF8 = new UTF8String(text);
return TTF_SizeUTF8Native(font, textUTF8.Handle, out w, out h);
}
[DllImport(nativeLibName, EntryPoint="TTF_SizeUTF8", CallingConvention = CallingConvention.Cdecl)]
public static extern int TTF_SizeUTF8Native( IntPtr font, IntPtr text, out int w, out int h);
/* font refers to a TTF_Font* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
@ -254,13 +255,13 @@ namespace SDL2
);
/* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr TTF_RenderUTF8_Solid(
IntPtr font,
[In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string text,
SDL.SDL_Color fg
);
public static IntPtr TTF_RenderUTF8_Solid( IntPtr font, string text, SDL.SDL_Color fg)
{
UTF8String textUTF8 = new UTF8String(text);
return TTF_RenderUTF8_SolidNative(font, textUTF8.Handle, fg);
}
[DllImport(nativeLibName, EntryPoint="CalTTF_RenderUTF8_Solid", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr TTF_RenderUTF8_SolidNative( IntPtr font, IntPtr text, SDL.SDL_Color fg);
/* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
@ -290,14 +291,13 @@ namespace SDL2
);
/* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr TTF_RenderUTF8_Shaded(
IntPtr font,
[In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string text,
SDL.SDL_Color fg,
SDL.SDL_Color bg
);
public static IntPtr TTF_RenderUTF8_Shaded( IntPtr font, string text, SDL.SDL_Color fg, SDL.SDL_Color bg)
{
UTF8String textUTF8 = new UTF8String(text);
return TTF_RenderUTF8_ShadedNative(font, textUTF8.Handle, fg, bg);
}
[DllImport(nativeLibName, EntryPoint="CalTTF_RenderUTF8_Shaded", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr TTF_RenderUTF8_ShadedNative( IntPtr font, IntPtr text, SDL.SDL_Color fg, SDL.SDL_Color bg);
/* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
@ -328,13 +328,13 @@ namespace SDL2
);
/* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr TTF_RenderUTF8_Blended(
IntPtr font,
[In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string text,
SDL.SDL_Color fg
);
public static IntPtr TTF_RenderUTF8_Blended( IntPtr font, string text, SDL.SDL_Color fg, int toto)
{
UTF8String textUTF8 = new UTF8String(text);
return TTF_RenderUTF8_BlendedNative(font, textUTF8.Handle, fg);
}
[DllImport(nativeLibName, EntryPoint="CalTTF_RenderUTF8_Blended", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr TTF_RenderUTF8_BlendedNative( IntPtr font, IntPtr text, SDL.SDL_Color fg);
/* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
@ -356,14 +356,13 @@ namespace SDL2
);
/* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr TTF_RenderUTF8_Blended_Wrapped(
IntPtr font,
[In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))]
string text,
SDL.SDL_Color fg,
uint wrapped
);
public static IntPtr TTF_RenderUTF8_Blended_Wrapped( IntPtr font, string text, SDL.SDL_Color fg, uint wrapped)
{
UTF8String textUTF8 = new UTF8String(text);
return TTF_RenderUTF8_Blended_WrappedNative(font, textUTF8.Handle, fg, wrapped);
}
[DllImport(nativeLibName, EntryPoint = "TTF_RenderUTF8_Blended_Wrapped", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr TTF_RenderUTF8_Blended_WrappedNative( IntPtr font, IntPtr text, SDL.SDL_Color fg, uint wrapped);
/* IntPtr refers to an SDL_Surface*, font to a TTF_Font* */
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]

190
src/UTF8String.cs Normal file
Просмотреть файл

@ -0,0 +1,190 @@
/* UTF-8 string wrapper
*
* Copyright (c) 2015 Silicon Studio Corp. (http://siliconstudio.co.jp)
*
* 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.
*
*/
using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics.Contracts;
namespace SDL2
{
/// <summary>
/// .NET representation of a UTF8 string. Mostly used for marshalling between .NET and UTF-8.
/// </summary>
public unsafe struct UTF8String : IDisposable
{
#region Initialization
/// <summary>
/// Initialize instance with .NET string <see cref="s"/>
/// </summary>
/// <param name="s">.NET string to wrap into a UTF8 sequence of unmanaged bytes.</param>
public UTF8String(string s)
{
if (s != null)
{
byte[] bytes = Encoding.UTF8.GetBytes(s);
int nb = bytes.Length;
IntPtr lPtr = SDL.SDL_malloc((IntPtr) (nb + 1));
if (lPtr == IntPtr.Zero)
{
throw new OutOfMemoryException("Cannot Allocate UTF8String");
}
else
{
Marshal.Copy(bytes, 0, lPtr, bytes.Length);
((byte*) lPtr)[bytes.Length] = 0;
}
_handle = lPtr;
_capacity = bytes.Length;
}
else
{
_handle = IntPtr.Zero;
_capacity = 0;
}
IsShared = false;
Contract.Ensures((s == null) || (_handle != IntPtr.Zero), "handle set");
Contract.Ensures((s == null) || (_capacity >= s.Length), "capacity_greater_than_input");
Contract.Ensures(ReferenceEquals(s, String()) || (String().Equals(s)), "string_set");
Contract.Ensures(!IsShared, "not shared.");
}
/// <summary>
/// Initialize instance with a sequence of unmanaged bytes without owning the associated pointer, i.e. we won't free the resource.
/// </summary>
/// <param name="o">Unmanaged pointer from which data will be read.</param>
public UTF8String(IntPtr o)
{
if (o != IntPtr.Zero)
{
byte* ptr = (byte*) o;
// Count number of available bytes.
while (*ptr != 0) { ptr++; }
long nb = ptr - (byte*) o + 1;
if (nb > int.MaxValue)
{
throw new ArgumentOutOfRangeException("UTF-8 string too large.");
}
_capacity = (int) nb;
_handle = o;
}
else
{
_handle = IntPtr.Zero;
_capacity = 0;
}
IsShared = true;
Contract.Ensures(_handle == o, "handle set");
Contract.Ensures(_capacity >= 0, "capacity_non_negative");
Contract.Ensures(IsShared, "shared");
}
#endregion
#region Access
/// <summary>
/// Access to the underlying unmanaged memory.
/// </summary>
public IntPtr Handle { get { return _handle; } }
/// <summary>
/// .NET string representation of current UTF-8 encoding string.
/// </summary>
/// <returns></returns>
public string String()
{
return (_handle == IntPtr.Zero ? null : Encoding.UTF8.GetString((byte*) _handle, _capacity - 1));
}
#endregion
#region Measurements
/// <summary>
/// Number of bytes of allocated unmanaged data.
/// </summary>
public int Capacity { get { return _capacity; } }
public bool IsShared { get; set; }
#endregion
#region Element change
public void SetString(string s)
{
if (s != null)
{
byte[] bytes = Encoding.UTF8.GetBytes(s);
int nb = bytes.Length;
if (nb + 1 > Capacity)
{
IntPtr lPtr = SDL.SDL_realloc(_handle, (IntPtr) (nb + 1));
if (lPtr == IntPtr.Zero)
{
Dispose();
throw new OutOfMemoryException("Cannot reallocate UTF8String");
}
_handle = lPtr;
_capacity = nb + 1;
}
Marshal.Copy(bytes, 0, _handle, nb);
((byte*) _handle)[nb] = 0;
}
else
{
Dispose();
}
Contract.Ensures(String().Equals(s), "string_set");
}
#endregion
#region Dispose
/// <summary>
/// Free allocated memory.
/// </summary>
public void Dispose()
{
if (_handle != IntPtr.Zero)
{
SDL.SDL_free(_handle);
_handle = IntPtr.Zero;
_capacity = 0;
}
}
#endregion
#region Implementation: Accesss
private int _capacity;
private IntPtr _handle;
#endregion
[ContractInvariantMethod]
private void ClassInvariant()
{
// Contract.Invariant (_handle != IntPtr.Zero, "internal pointer not null");
// Contract.Invariant (_capacity >= 0, "capacity non-negative");
}
}
}