From 8ecc4bded93c847002c1a9fb93f1896f27e834a4 Mon Sep 17 00:00:00 2001 From: ChrisDill Date: Wed, 12 Jan 2022 20:12:55 +0000 Subject: [PATCH] Yet more string changes - Use sbyte/byte correctly in Raylib.cs - Using Marshal.StringToCoTaskMemUTF8 - Update utils string usage --- Raylib-cs/Raylib.Utils.cs | 310 +++++++++++++++++---------- Raylib-cs/Raylib.cs | 120 +++++------ Raylib-cs/types/native/UTF8Buffer.cs | 30 +-- 3 files changed, 278 insertions(+), 182 deletions(-) diff --git a/Raylib-cs/Raylib.Utils.cs b/Raylib-cs/Raylib.Utils.cs index 8264744..f562cf9 100644 --- a/Raylib-cs/Raylib.Utils.cs +++ b/Raylib-cs/Raylib.Utils.cs @@ -9,21 +9,15 @@ namespace Raylib_cs /// Initialize window and OpenGL context public static void InitWindow(int width, int height, string title) { - using var str = new UTF8Buffer(title); - fixed (byte* b = str.buffer) - { - InitWindow(width, height, b); - } + using var str1 = title.ToUTF8Buffer(); + InitWindow(width, height, str1.AsPointer()); } /// Set title for window (only PLATFORM_DESKTOP) public static void SetWindowTitle(string title) { - using var str = new UTF8Buffer(title); - fixed (byte* b = str.buffer) - { - SetWindowTitle(b); - } + using var str1 = title.ToUTF8Buffer(); + SetWindowTitle(str1.AsPointer()); } /// Get the human-readable, UTF-8 encoded name of the primary monitor @@ -41,13 +35,101 @@ namespace Raylib_cs /// Set clipboard text content public static void SetClipboardText(string text) { - using var str = new UTF8Buffer(text); - fixed (byte* b = str.buffer) + using var str1 = text.ToUTF8Buffer(); + SetClipboardText(str1.AsPointer()); + } + + /// Load shader from files and bind default locations + public static Shader LoadShader(string vsFileName, string fsFileName) + { + using var str1 = vsFileName.ToUTF8Buffer(); + using var str2 = fsFileName.ToUTF8Buffer(); + return LoadShader(str1.AsPointer(), str2.AsPointer()); + } + + /// Load shader from code string and bind default locations + public static Shader LoadShaderFromMemory(string vsCode, string fsCode) + { + using var str1 = vsCode.ToUTF8Buffer(); + using var str2 = fsCode.ToUTF8Buffer(); + return LoadShaderFromMemory(str1.AsPointer(), str2.AsPointer()); + } + + /// Get shader uniform location + public static int GetShaderLocation(Shader shader, string uniformName) + { + using var str1 = uniformName.ToUTF8Buffer(); + return GetShaderLocation(shader, str1.AsPointer()); + } + + /// Get shader attribute location + public static int GetShaderLocationAttrib(Shader shader, string attribName) + { + using var str1 = attribName.ToUTF8Buffer(); + return GetShaderLocation(shader, str1.AsPointer()); + } + + /// Takes a screenshot of current screen (saved a .png) + public static void TakeScreenshot(string fileName) + { + using var str1 = fileName.ToUTF8Buffer(); + TakeScreenshot(str1.AsPointer()); + } + + /// Check file extension + public static CBool IsFileExtension(string fileName, string ext) + { + using var str1 = fileName.ToUTF8Buffer(); + using var str2 = ext.ToUTF8Buffer(); + return IsFileExtension(str1.AsPointer(), str2.AsPointer()); + } + + /// Get file modification time (last write time) + public static long GetFileModTime(string fileName) + { + using var str1 = fileName.ToUTF8Buffer(); + return GetFileModTime(str1.AsPointer()); + } + + /// Load image from file into CPU memory (RAM) + public static Image LoadImage(string fileName) + { + using var str1 = fileName.ToUTF8Buffer(); + return LoadImage(str1.AsPointer()); + } + + /// Load image from RAW file data + public static Image LoadImageRaw(string fileName, int width, int height, PixelFormat format, int headerSize) + { + using var str1 = fileName.ToUTF8Buffer(); + return LoadImageRaw(str1.AsPointer(), width, height, format, headerSize); + } + + /// Load image sequence from file (frames appended to image.data) + public static Image LoadImageAnim(string fileName, int[] frames) + { + using var str1 = fileName.ToUTF8Buffer(); + fixed (int* p = frames) { - SetClipboardText(b); + return LoadImageAnim(str1.AsPointer(), p); } } + /// Export image data to file + public static void ExportImage(Image image, string fileName) + { + using var str1 = fileName.ToUTF8Buffer(); + ExportImage(image, str1.AsPointer()); + } + + /// Show trace log messages (LOG_DEBUG, LOG_INFO, LOG_WARNING, LOG_ERROR) + public static void TraceLog(TraceLogLevel logLevel, string text) + { + using var str1 = text.ToUTF8Buffer(); + TraceLog(logLevel, str1.AsPointer()); + } + + /// Set shader uniform value vector public static void SetShaderValueV(Shader shader, int uniformLoc, T[] values, ShaderUniformDataType uniformType, int count) where T : unmanaged @@ -92,9 +174,10 @@ namespace Raylib_cs /// Load file data as byte array (read) public static byte* LoadFileData(string fileName, ref uint bytesRead) { + using var str1 = fileName.ToUTF8Buffer(); fixed (uint* p = &bytesRead) { - return LoadFileData(fileName, p); + return LoadFileData(str1.AsPointer(), p); } } @@ -140,21 +223,15 @@ namespace Raylib_cs /// Create an image from text (default font) public static Image ImageText(string text, int fontSize, Color color) { - using var str = new UTF8Buffer(text); - fixed (byte* b = str.buffer) - { - return ImageText(b, fontSize, color); - } + using var str1 = text.ToUTF8Buffer(); + return ImageText(str1.AsPointer(), fontSize, color); } /// Create an image from text (custom sprite font) public static Image ImageTextEx(Font font, string text, float fontSize, float spacing, Color tint) { - using var str = new UTF8Buffer(text); - fixed (byte* b = str.buffer) - { - return ImageTextEx(font, b, fontSize, spacing, tint); - } + using var str1 = text.ToUTF8Buffer(); + return ImageTextEx(font, str1.AsPointer(), fontSize, spacing, tint); } /// Convert image to POT (power-of-two) @@ -466,29 +543,30 @@ namespace Raylib_cs /// Draw text (using default font) within an image (destination) public static void ImageDrawText(ref Image dst, string text, int x, int y, int fontSize, Color color) { + using var str1 = text.ToUTF8Buffer(); fixed (Image* p = &dst) { - using var str = new UTF8Buffer(text); - fixed (byte* b = str.buffer) - { - ImageDrawText(p, b, x, y, fontSize, color); - } + ImageDrawText(p, str1.AsPointer(), x, y, fontSize, color); } } /// Draw text (custom sprite font) within an image (destination) public static void ImageDrawTextEx(ref Image dst, Font font, string text, Vector2 position, int fontSize, float spacing, Color color) { + using var str1 = text.ToUTF8Buffer(); fixed (Image* p = &dst) { - using var str = new UTF8Buffer(text); - fixed (byte* b = str.buffer) - { - ImageDrawTextEx(p, font, b, position, fontSize, spacing, color); - } + ImageDrawTextEx(p, font, str1.AsPointer(), position, fontSize, spacing, color); } } + /// Load texture from file into GPU memory (VRAM) + public static Texture2D LoadTexture(string fileName) + { + using var str1 = fileName.ToUTF8Buffer(); + return LoadTexture(str1.AsPointer()); + } + /// Generate GPU mipmaps for a texture public static void GenTextureMipmaps(ref Texture2D texture) { @@ -497,6 +575,20 @@ namespace Raylib_cs GenTextureMipmaps(p); } } + + /// Load font from file into GPU memory (VRAM) + public static Font LoadFont(string fileName) + { + using var str1 = fileName.ToUTF8Buffer(); + return LoadFont(str1.AsPointer()); + } + + /// Load font from file with extended parameters + public static Font LoadFontEx(string fileName, int fontSize, int[] fontChars, int charsCount) + { + using var str1 = fileName.ToUTF8Buffer(); + return LoadFontEx(str1.AsPointer(), fontSize, fontChars, charsCount); + } /// Upload vertex data into GPU and provided VAO/VBO ids public static void UploadMesh(ref Mesh mesh, CBool dynamic) @@ -537,20 +629,17 @@ namespace Raylib_cs /// Load model animations from file public static ReadOnlySpan LoadModelAnimations(string fileName, ref uint animsCount) { - using var str = new UTF8Buffer(fileName); - fixed (byte* b = str.buffer) + using var str1 = fileName.ToUTF8Buffer(); + fixed (uint* p = &animsCount) { - fixed (uint* p = &animsCount) + var model = LoadModelAnimations(str1.AsPointer(), p); + + if ((IntPtr)model == IntPtr.Zero) { - var model = LoadModelAnimations(b, p); - - if ((IntPtr)model == IntPtr.Zero) - { - throw new ApplicationException("Failed to load animation"); - } - - return new ReadOnlySpan(model, (int)animsCount); + throw new ApplicationException("Failed to load animation"); } + + return new ReadOnlySpan(model, (int)animsCount); } } @@ -629,118 +718,91 @@ namespace Raylib_cs } } + /// Draw text (using default font) public static void DrawText(string text, int posX, int posY, int fontSize, Color color) { - using var str = new UTF8Buffer(text); - fixed (byte* b = str.buffer) - { - DrawText(b, posX, posY, fontSize, color); - } + using var str1 = text.ToUTF8Buffer(); + DrawText(str1.AsPointer(), posX, posY, fontSize, color); } + /// Draw text using font and additional parameters public static void DrawTextEx(Font font, string text, Vector2 position, float fontSize, float spacing, Color tint) { - using var str = new UTF8Buffer(text); - fixed (byte* b = str.buffer) - { - DrawTextEx(font, b, position, fontSize, spacing, tint); - } + using var str1 = text.ToUTF8Buffer(); + DrawTextEx(font, str1.AsPointer(), position, fontSize, spacing, tint); } + /// Draw text using Font and pro parameters (rotation) public static void DrawTextPro(Font font, string text, Vector2 position, float fontSize, float spacing, Color tint) { - using var str = new UTF8Buffer(text); - fixed (byte* b = str.buffer) - { - DrawTextEx(font, b, position, fontSize, spacing, tint); - } + using var str1 = text.ToUTF8Buffer(); + DrawTextEx(font, str1.AsPointer(), position, fontSize, spacing, tint); } + /// Measure string width for default font public static int MeasureText(string text, int fontSize) { - using var str = new UTF8Buffer(text); - fixed (byte* b = str.buffer) - { - return MeasureText(b, fontSize); - } + using var str1 = text.ToUTF8Buffer(); + return MeasureText(str1.AsPointer(), fontSize); } + /// Measure string size for Font public static Vector2 MeasureTextEx(Font font, string text, float fontSize, float spacing) { - using var str = new UTF8Buffer(text); - fixed (byte* b = str.buffer) - { - return MeasureTextEx(font, b, fontSize, spacing); - } + using var str1 = text.ToUTF8Buffer(); + return MeasureTextEx(font, str1.AsPointer(), fontSize, spacing); } + /// Append text at specific position and move cursor! public static void TextAppend(string text, string append, int position) { - using var str = new UTF8Buffer(text); - using var str1 = new UTF8Buffer(append); - fixed (byte* p1 = str.buffer) - { - fixed (byte* p2 = str1.buffer) - { - TextAppend(p1, p2, &position); - } - } + using var str1 = text.ToUTF8Buffer(); + using var str2 = append.ToUTF8Buffer(); + TextAppend(str1.AsPointer(), str2.AsPointer(), &position); } + /// Get Pascal case notation version of provided string public static string TextToPascal(string text) { - using var str = new UTF8Buffer(text); - fixed (byte* b = str.buffer) - { - return Utf8StringUtils.GetUTF8String(TextToPascal(b)); - } + using var str1 = text.ToUTF8Buffer(); + return Utf8StringUtils.GetUTF8String(TextToPascal(str1.AsPointer())); } + /// Get integer value from text (negative values not supported) public static int TextToInteger(string text) { - using var str = new UTF8Buffer(text); - fixed (byte* b = str.buffer) - { - return TextToInteger(b); - } + using var str1 = text.ToUTF8Buffer(); + return TextToInteger(str1.AsPointer()); } + /// Get all codepoints in a string, codepoints count returned by parameters public static int[] LoadCodepoints(string text, ref int count) { - using var str = new UTF8Buffer(text); - fixed (byte* b = str.buffer) + using var str1 = text.ToUTF8Buffer(); + fixed (int* c = &count) { - fixed (int* c = &count) - { - var pointsPtr = LoadCodepoints(b, c); - var codepoints = new ReadOnlySpan(pointsPtr, count).ToArray(); - UnloadCodepoints(pointsPtr); - return codepoints; - } + var pointsPtr = LoadCodepoints(str1.AsPointer(), c); + var codepoints = new ReadOnlySpan(pointsPtr, count).ToArray(); + UnloadCodepoints(pointsPtr); + return codepoints; } } + /// Get total number of characters (codepoints) in a UTF8 encoded string public static int GetCodepointCount(string text) { - using var str = new UTF8Buffer(text); - fixed (byte* b = str.buffer) - { - return GetCodepointCount(b); - } + using var str1 = text.ToUTF8Buffer(); + return GetCodepointCount(str1.AsPointer()); } /// Returns next codepoint in a UTF8 encoded string; 0x3f('?') is returned on failure /// single codepoint / "char" public static int GetCodepoint(string text, ref int bytesProcessed) { - using var str = new UTF8Buffer(text); - fixed (byte* b = str.buffer) + using var str1 = text.ToUTF8Buffer(); + fixed (int* p = &bytesProcessed) { - // this probably wont work - fixed (int* p = &bytesProcessed) - { - return GetCodepoint(b, p); - } + return GetCodepoint(str1.AsPointer(), p); } } @@ -766,6 +828,13 @@ namespace Raylib_cs } } + /// Draw a model (with texture if set) + public static Model LoadModel(string fileName) + { + using var str1 = fileName.ToUTF8Buffer(); + return LoadModel(str1.AsPointer()); + } + /// Draw a triangle strip defined by points public static void DrawTriangleStrip3D(Vector3[] points, int pointsCount, Color color) { @@ -784,6 +853,27 @@ namespace Raylib_cs } } + /// Load wave data from file + public static Wave LoadWave(string fileName) + { + using var str1 = fileName.ToUTF8Buffer(); + return LoadWave(str1.AsPointer()); + } + + /// Load sound from file + public static Sound LoadSound(string fileName) + { + using var str1 = fileName.ToUTF8Buffer(); + return LoadSound(str1.AsPointer()); + } + + /// Load music stream from file + public static Music LoadMusicStream(string fileName) + { + using var str1 = fileName.ToUTF8Buffer(); + return LoadMusicStream(str1.AsPointer()); + } + public static string SubText(this string input, int position, int length) { return input.Substring(position, Math.Min(length, input.Length)); diff --git a/Raylib-cs/Raylib.cs b/Raylib-cs/Raylib.cs index 41f663a..f215dd1 100644 --- a/Raylib-cs/Raylib.cs +++ b/Raylib-cs/Raylib.cs @@ -32,7 +32,7 @@ namespace Raylib_cs /// Initialize window and OpenGL context [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern void InitWindow(int width, int height, byte* title); + public static extern void InitWindow(int width, int height, sbyte* title); /// Check if KEY_ESCAPE pressed or Close icon pressed [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] @@ -104,7 +104,7 @@ namespace Raylib_cs /// Set title for window (only PLATFORM_DESKTOP) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern void SetWindowTitle(byte* title); + public static extern void SetWindowTitle(sbyte* title); /// Set window position on screen (only PLATFORM_DESKTOP) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] @@ -176,15 +176,15 @@ namespace Raylib_cs /// Get the human-readable, UTF-8 encoded name of the primary monitor [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern byte* GetMonitorName(int monitor); + public static extern sbyte* GetMonitorName(int monitor); /// Get clipboard text content [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern byte* GetClipboardText(); + public static extern sbyte* GetClipboardText(); /// Set clipboard text content [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern void SetClipboardText(byte* text); + public static extern void SetClipboardText(sbyte* text); // Custom frame control functions // NOTE: Those functions are intended for advance users that want full control over the frame processing @@ -316,19 +316,19 @@ namespace Raylib_cs /// Load shader from files and bind default locations [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Shader LoadShader(string vsFileName, string fsFileName); + public static extern Shader LoadShader(sbyte* vsFileName, sbyte* fsFileName); /// Load shader from code strings and bind default locations [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Shader LoadShaderFromMemory(string vsCode, string fsCode); + public static extern Shader LoadShaderFromMemory(sbyte* vsCode, sbyte* fsCode); /// Get shader uniform location [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern int GetShaderLocation(Shader shader, string uniformName); + public static extern int GetShaderLocation(Shader shader, sbyte* uniformName); /// Get shader attribute location [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern int GetShaderLocationAttrib(Shader shader, string attribName); + public static extern int GetShaderLocationAttrib(Shader shader, sbyte* attribName); /// Set shader uniform value [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] @@ -413,7 +413,7 @@ namespace Raylib_cs /// Takes a screenshot of current screen (saved a .png) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern void TakeScreenshot(string fileName); + public static extern void TakeScreenshot(sbyte* fileName); /// Setup window configuration flags (view FLAGS) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] @@ -421,7 +421,7 @@ namespace Raylib_cs /// Show trace log messages (LOG_DEBUG, LOG_INFO, LOG_WARNING, LOG_ERROR) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern void TraceLog(TraceLogLevel logLevel, string text); + public static extern void TraceLog(TraceLogLevel logLevel, sbyte* text); /// Set the current threshold (minimum) log level [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] @@ -468,7 +468,7 @@ namespace Raylib_cs /// Load file data as byte array (read) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern byte* LoadFileData(string fileName, uint* bytesRead); + public static extern byte* LoadFileData(sbyte* fileName, uint* bytesRead); /// Unload file data allocated by LoadFileData() [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] @@ -476,11 +476,11 @@ namespace Raylib_cs /// Save data to file from byte array (write) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern CBool SaveFileData(string fileName, void* data, uint bytesToWrite); + public static extern CBool SaveFileData(sbyte* fileName, void* data, uint bytesToWrite); /// Check file extension [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern CBool IsFileExtension(string fileName, string ext); + public static extern CBool IsFileExtension(sbyte* fileName, sbyte* ext); /// Check if a file has been dropped into window [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] @@ -488,7 +488,7 @@ namespace Raylib_cs /// Get dropped files names (memory should be freed) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern byte** GetDroppedFiles(int* count); + public static extern sbyte** GetDroppedFiles(int* count); /// Clear dropped files paths buffer (free memory) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] @@ -496,7 +496,7 @@ namespace Raylib_cs /// Get file modification time (last write time) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern int GetFileModTime(string fileName); + public static extern int GetFileModTime(sbyte* fileName); /// Compress data (DEFLATE algorithm) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] @@ -508,7 +508,7 @@ namespace Raylib_cs /// Encode data to Base64 string [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern byte* EncodeDataBase64(byte* data, int dataLength, int* outputLength); + public static extern sbyte* EncodeDataBase64(byte* data, int dataLength, int* outputLength); /// Decode Base64 string data [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] @@ -527,7 +527,7 @@ namespace Raylib_cs /// Open URL with default system browser (if available) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern void OpenURL(string url); + public static extern void OpenURL(sbyte* url); //------------------------------------------------------------------------------------ // Input Handling Functions (Module: core) @@ -572,7 +572,7 @@ namespace Raylib_cs /// Return gamepad internal name id [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern byte* GetGamepadName(int gamepad); + public static extern sbyte* GetGamepadName(int gamepad); /// Detect if a gamepad button has been pressed once [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] @@ -604,7 +604,7 @@ namespace Raylib_cs /// Set internal gamepad mappings (SDL_GameControllerDB) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern int SetGamepadMappings(string mappings); + public static extern int SetGamepadMappings(sbyte* mappings); // Input-related functions: mouse @@ -962,19 +962,19 @@ namespace Raylib_cs /// Load image from file into CPU memory (RAM) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Image LoadImage(string fileName); + public static extern Image LoadImage(sbyte* fileName); /// Load image from RAW file data [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Image LoadImageRaw(string fileName, int width, int height, PixelFormat format, int headerSize); + public static extern Image LoadImageRaw(sbyte* fileName, int width, int height, PixelFormat format, int headerSize); /// Load image sequence from file (frames appended to image.data) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Image LoadImageAnim(string fileName, int* frames); + public static extern Image LoadImageAnim(sbyte* fileName, int* frames); /// Load image from memory buffer, fileType refers to extension: i.e. "png" [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Image LoadImageFromMemory(string fileType, byte* fileData, int dataSize); + public static extern Image LoadImageFromMemory(sbyte* fileType, byte* fileData, int dataSize); /// Load image from GPU texture data [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] @@ -990,11 +990,11 @@ namespace Raylib_cs /// Export image data to file [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern void ExportImage(Image image, string fileName); + public static extern void ExportImage(Image image, sbyte* fileName); /// Export image as code file defining an array of bytes [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern void ExportImageAsCode(Image image, string fileName); + public static extern void ExportImageAsCode(Image image, sbyte* fileName); // Image generation functions @@ -1040,11 +1040,11 @@ namespace Raylib_cs /// Create an image from text (default font) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Image ImageText(byte* text, int fontSize, Color color); + public static extern Image ImageText(sbyte* text, int fontSize, Color color); /// Create an image from text (custom sprite font) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Image ImageTextEx(Font font, byte* text, float fontSize, float spacing, Color tint); + public static extern Image ImageTextEx(Font font, sbyte* text, float fontSize, float spacing, Color tint); /// Convert image to POT (power-of-two) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] @@ -1212,11 +1212,11 @@ namespace Raylib_cs /// Draw text (using default font) within an image (destination) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern void ImageDrawText(Image* dst, byte* text, int x, int y, int fontSize, Color color); + public static extern void ImageDrawText(Image* dst, sbyte* text, int x, int y, int fontSize, Color color); /// Draw text (custom sprite font) within an image (destination) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern void ImageDrawTextEx(Image* dst, Font font, byte* text, Vector2 position, float fontSize, float spacing, Color tint); + public static extern void ImageDrawTextEx(Image* dst, Font font, sbyte* text, Vector2 position, float fontSize, float spacing, Color tint); // Texture loading functions @@ -1224,7 +1224,7 @@ namespace Raylib_cs /// Load texture from file into GPU memory (VRAM) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Texture2D LoadTexture(string fileName); + public static extern Texture2D LoadTexture(sbyte* fileName); /// Load texture from image data [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] @@ -1368,11 +1368,11 @@ namespace Raylib_cs /// Load font from file into GPU memory (VRAM) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Font LoadFont(string fileName); + public static extern Font LoadFont(sbyte* fileName); /// Load font from file with extended parameters [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Font LoadFontEx(string fileName, int fontSize, int[] fontChars, int charsCount); + public static extern Font LoadFontEx(sbyte* fileName, int fontSize, int[] fontChars, int charsCount); /// Load font from Image (XNA style) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] @@ -1381,7 +1381,7 @@ namespace Raylib_cs /// Load font from memory buffer, fileType refers to extension: i.e. "ttf"
/// fileData refers to const unsigned char *
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Font LoadFontFromMemory(string fileType, byte* fileData, int dataSize, int fontSize, int[] fontChars, int charsCount); + public static extern Font LoadFontFromMemory(sbyte* fileType, byte* fileData, int dataSize, int fontSize, int[] fontChars, int charsCount); /// Load font data for further use [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] @@ -1408,15 +1408,15 @@ namespace Raylib_cs /// Draw text (using default font) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern void DrawText(byte* text, int posX, int posY, int fontSize, Color color); + public static extern void DrawText(sbyte* text, int posX, int posY, int fontSize, Color color); /// Draw text using font and additional parameters [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern void DrawTextEx(Font font, byte* text, Vector2 position, float fontSize, float spacing, Color tint); + public static extern void DrawTextEx(Font font, sbyte* text, Vector2 position, float fontSize, float spacing, Color tint); /// Draw text using Font and pro parameters (rotation) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern void DrawTextPro(Font font, byte* text, Vector2 position, float fontSize, float spacing, Color tint); + public static extern void DrawTextPro(Font font, sbyte* text, Vector2 position, float fontSize, float spacing, Color tint); /// Draw one character (codepoint) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] @@ -1427,11 +1427,11 @@ namespace Raylib_cs /// Measure string width for default font [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern int MeasureText(byte* text, int fontSize); + public static extern int MeasureText(sbyte* text, int fontSize); /// Measure string size for Font [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Vector2 MeasureTextEx(Font font, byte* text, float fontSize, float spacing); + public static extern Vector2 MeasureTextEx(Font font, sbyte* text, float fontSize, float spacing); /// Get glyph index position in font for a codepoint (unicode character), fallback to '?' if not found [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] @@ -1450,29 +1450,29 @@ namespace Raylib_cs /// Text formatting with variables (sprintf style) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern string TextFormat(string text); + public static extern sbyte* TextFormat(sbyte* text); /// Get a piece of a text string [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern string TextSubtext(string text, int position, int length); + public static extern sbyte* TextSubtext(sbyte* text, int position, int length); /// Append text at specific position and move cursor! [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern void TextAppend(byte* text, byte* append, int* position); + public static extern void TextAppend(sbyte* text, sbyte* append, int* position); /// Get Pascal case notation version of provided string [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern byte* TextToPascal(byte* text); + public static extern sbyte* TextToPascal(sbyte* text); /// Get integer value from text (negative values not supported) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern int TextToInteger(byte* text); + public static extern int TextToInteger(sbyte* text); // UTF8 text strings management functions /// Get all codepoints in a string, codepoints count returned by parameters [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern int* LoadCodepoints(byte* text, int* count); + public static extern int* LoadCodepoints(sbyte* text, int* count); /// Unload codepoints data from memory [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] @@ -1480,19 +1480,19 @@ namespace Raylib_cs /// Get total number of characters (codepoints) in a UTF8 encoded string [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern int GetCodepointCount(byte* text); + public static extern int GetCodepointCount(sbyte* text); /// Returns next codepoint in a UTF8 encoded string; 0x3f('?') is returned on failure [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern int GetCodepoint(byte* text, int* bytesProcessed); + public static extern int GetCodepoint(sbyte* text, int* bytesProcessed); /// Encode codepoint into utf8 text (char array length returned as parameter) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern byte* CodepointToUTF8(int codepoint, int* byteSize); + public static extern sbyte* CodepointToUTF8(int codepoint, int* byteSize); /// Encode text as codepoints array into UTF-8 text string (WARNING: memory must be freed!) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern byte* TextCodepointsToUTF8(int* codepoints, int length); + public static extern sbyte* TextCodepointsToUTF8(int* codepoints, int length); //------------------------------------------------------------------------------------ @@ -1594,7 +1594,7 @@ namespace Raylib_cs /// Load model from files (meshes and materials) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Model LoadModel(string fileName); + public static extern Model LoadModel(sbyte* fileName); /// Load model from generated mesh (default material) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] @@ -1636,7 +1636,7 @@ namespace Raylib_cs /// Export mesh data to file, returns true on success [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern CBool ExportMesh(Mesh mesh, string fileName); + public static extern CBool ExportMesh(Mesh mesh, sbyte* fileName); /// Compute mesh bounding box limits [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] @@ -1656,7 +1656,7 @@ namespace Raylib_cs //TODO: safe Helper method /// Load materials from model file [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Material* LoadMaterials(string fileName, int* materialCount); + public static extern Material* LoadMaterials(sbyte* fileName, int* materialCount); /// Load default material (Supports: DIFFUSE, SPECULAR, NORMAL maps) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] @@ -1759,7 +1759,7 @@ namespace Raylib_cs /// Load model animations from file [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern ModelAnimation* LoadModelAnimations(byte* fileName, uint* animsCount); + public static extern ModelAnimation* LoadModelAnimations(sbyte* fileName, uint* animsCount); /// Update model animation pose [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] @@ -1843,16 +1843,16 @@ namespace Raylib_cs /// Load wave data from file [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Wave LoadWave(string fileName); + public static extern Wave LoadWave(sbyte* fileName); /// Load wave from memory buffer, fileType refers to extension: i.e. "wav"
/// fileData refers to a const unsigned char *
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Wave LoadWaveFromMemory(string fileType, byte* fileData, int dataSize); + public static extern Wave LoadWaveFromMemory(sbyte* fileType, byte* fileData, int dataSize); /// Load sound from file [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Sound LoadSound(string fileName); + public static extern Sound LoadSound(sbyte* fileName); /// Load sound from wave data [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] @@ -1872,11 +1872,11 @@ namespace Raylib_cs /// Export wave data to file [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern void ExportWave(Wave wave, string fileName); + public static extern void ExportWave(Wave wave, sbyte* fileName); /// Export wave sample data to code (.h) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern void ExportWaveAsCode(Wave wave, string fileName); + public static extern void ExportWaveAsCode(Wave wave, sbyte* fileName); // Wave/Sound management functions @@ -1947,12 +1947,12 @@ namespace Raylib_cs /// Load music stream from file [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Music LoadMusicStream(string fileName); + public static extern Music LoadMusicStream(sbyte* fileName); //TODO: Span/Helper method /// Load music stream from data [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern Music LoadMusicStreamFromMemory(string fileType, byte* data, int dataSize); + public static extern Music LoadMusicStreamFromMemory(sbyte* fileType, byte* data, int dataSize); /// Unload music stream [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] diff --git a/Raylib-cs/types/native/UTF8Buffer.cs b/Raylib-cs/types/native/UTF8Buffer.cs index 92eb61e..49cf205 100644 --- a/Raylib-cs/types/native/UTF8Buffer.cs +++ b/Raylib-cs/types/native/UTF8Buffer.cs @@ -1,5 +1,4 @@ using System; -using System.Buffers; using System.Text; using System.Runtime.InteropServices; @@ -11,41 +10,48 @@ namespace Raylib_cs /// public ref struct UTF8Buffer { - public byte[] buffer; + private IntPtr data; public UTF8Buffer(string text) { - int length = (text.Length * 4) + 1; - buffer = ArrayPool.Shared.Rent(length); - int count = Encoding.UTF8.GetBytes(text.AsSpan(), buffer.AsSpan()); - buffer[count] = 0; + data = Marshal.StringToCoTaskMemUTF8(text); + } + + public unsafe sbyte* AsPointer() + { + return (sbyte*)data.ToPointer(); } public void Dispose() { - ArrayPool.Shared.Return(buffer); + Marshal.FreeCoTaskMem(data); } } public static class Utf8StringUtils { - public static byte[] ToUtf8String(this string sourceText) + public static UTF8Buffer ToUTF8Buffer(this string text) { - if (sourceText == null) + return new UTF8Buffer(text); + } + + public static byte[] ToUtf8String(this string text) + { + if (text == null) { return null; } - var length = Encoding.UTF8.GetByteCount(sourceText); + var length = Encoding.UTF8.GetByteCount(text); var byteArray = new byte[length + 1]; - var wrote = Encoding.UTF8.GetBytes(sourceText, 0, sourceText.Length, byteArray, 0); + var wrote = Encoding.UTF8.GetBytes(text, 0, text.Length, byteArray, 0); byteArray[wrote] = 0; return byteArray; } - public static unsafe string GetUTF8String(byte* bytes) + public static unsafe string GetUTF8String(sbyte* bytes) { return Marshal.PtrToStringUTF8((IntPtr)bytes); }