From c859981d0b899c7ff9fee02af1d52e5c1b84dbf4 Mon Sep 17 00:00:00 2001 From: ChrisDill Date: Thu, 16 Dec 2021 17:54:05 +0000 Subject: [PATCH] Move wrapper functions into Raylib.Utils --- Raylib-cs/Raylib.Utils.cs | 200 ++++++++++++++++++++++++++++++++++++ Raylib-cs/Raylib.cs | 210 +------------------------------------- 2 files changed, 202 insertions(+), 208 deletions(-) diff --git a/Raylib-cs/Raylib.Utils.cs b/Raylib-cs/Raylib.Utils.cs index a9536ff..2480646 100644 --- a/Raylib-cs/Raylib.Utils.cs +++ b/Raylib-cs/Raylib.Utils.cs @@ -1,4 +1,5 @@ using System; +using System.Numerics; using System.Runtime.InteropServices; namespace Raylib_cs @@ -8,6 +9,205 @@ namespace Raylib_cs /// public static unsafe partial class Raylib { + public static void InitWindow(int width, int height, Utf8String title) + { + fixed (byte* p = title) + { + InitWindow(width, height, p); + } + } + + public static void SetWindowTitle(Utf8String title) + { + fixed (byte* p = title) + { + SetWindowTitle(p); + } + } + + /// Get the human-readable, UTF-8 encoded name of the primary monitor + public static Utf8String GetMonitorName_(int monitor) + { + return Utf8String.FromPtr(GetMonitorName(monitor)); + } + + /// Get clipboard text content + public static Utf8String GetClipboardText_() + { + return Utf8String.FromPtr(GetClipboardText()); + } + + public static void SetClipboardText(Utf8String text) + { + fixed (byte* p = text) + { + SetClipboardText(p); + } + } + + /// Set custom trace log + public static void SetTraceLogCallback_(TraceLogCallback callback) + { + SetTraceLogCallback(callback); + traceLogCallback = callback; + } + + /// Return gamepad internal name id + public static Utf8String GetGamepadName_(int gamepad) + { + return Utf8String.FromPtr(GetGamepadName(gamepad)); + } + + public static void ImageDrawText(ref Image dst, Utf8String text, int x, int y, int fontSize, Color color) + { + fixed (byte* p = text) + { + fixed (Image* i = &dst) + { + ImageDrawText(i, p, x, y, fontSize, color); + } + } + } + + public static void DrawText(Utf8String text, int posX, int posY, int fontSize, Color color) + { + fixed (byte* p = text) + { + DrawText(p, posX, posY, fontSize, color); + } + } + + public static void DrawTextEx(Font font, Utf8String text, Vector2 position, float fontSize, float spacing, Color tint) + { + fixed (byte* p = text) + { + DrawTextEx(font, p, position, fontSize, spacing, tint); + } + } + + public static void DrawTextPro(Font font, Utf8String text, Vector2 position, float fontSize, float spacing, Color tint) + { + fixed (byte* p = text) + { + DrawTextEx(font, p, position, fontSize, spacing, tint); + } + } + + public static int MeasureText(Utf8String text, int fontSize) + { + fixed (byte* p = text) + { + return MeasureText(p, fontSize); + } + } + + public static Vector2 MeasureTextEx(Font font, Utf8String text, float fontSize, float spacing) + { + fixed (byte* p = text) + { + return MeasureTextEx(font, p, fontSize, spacing); + } + } + + public static void TextAppend(Utf8String text, Utf8String append, int position) + { + fixed (byte* p1 = text) + { + fixed (byte* p2 = append) + { + TextAppend(p1, p2, &position); + } + } + } + + public static Utf8String TextToPascal(Utf8String text) + { + fixed (byte* p1 = text) + { + return Utf8String.FromPtr(TextToPascal(p1)); + } + } + + public static int TextToInteger(Utf8String text) + { + fixed (byte* p1 = text) + { + return TextToInteger(p1); + } + } + + public static int[] LoadCodepoints(Utf8String text, ref int count) + { + fixed (byte* p1 = text) + { + fixed (int* c = &count) + { + var pointsPtr = LoadCodepoints(p1, c); + var codepoints = new ReadOnlySpan(pointsPtr, count).ToArray(); + UnloadCodepoints(pointsPtr); + return codepoints; + } + } + } + + public static int GetCodepointCount(Utf8String text) + { + fixed (byte* p1 = text) + { + return GetCodepointCount(p1); + } + } + + /// Returns next codepoint in a UTF8 encoded string; 0x3f('?') is returned on failure + /// single codepoint / "char" + public static int GetCodepoint(Utf8String text, ref int bytesProcessed) + { + fixed (byte* p1 = text) + { + // this probably wont work + fixed (int* p = &bytesProcessed) + { + return GetCodepoint(p1, p); + } + } + } + + public static Utf8String CodepointToUTF8(int codepoint, ref int byteSize) + { + fixed (int* l1 = &byteSize) + { + var ptr = CodepointToUTF8(codepoint, l1); + return Utf8String.FromPtr(ptr); + } + } + + public static Utf8String TextCodepointsToUTF8(int[] codepoints, int length) + { + fixed (int* c1 = codepoints) + { + var ptr = TextCodepointsToUTF8(c1, length); + return Utf8String.FromPtr(ptr); + } + } + + public static ReadOnlySpan LoadModelAnimations(Utf8String fileName, ref int animsCount) + { + fixed (byte* p1 = fileName) + { + fixed (int* p2 = &animsCount) + { + var model = LoadModelAnimations(p1, p2); + + if ((IntPtr)model == IntPtr.Zero) + { + throw new ApplicationException("Failed to load animation"); + } + + return new ReadOnlySpan(model, animsCount); + } + } + } + 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 5539f2b..5e65cbb 100644 --- a/Raylib-cs/Raylib.cs +++ b/Raylib-cs/Raylib.cs @@ -77,14 +77,6 @@ namespace Raylib_cs [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern void InitWindow(int width, int height, byte* title); - public static void InitWindow(int width, int height, Utf8String title) - { - fixed (byte* p = title) - { - InitWindow(width, height, p); - } - } - /// Check if KEY_ESCAPE pressed or Close icon pressed [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern CBool WindowShouldClose(); @@ -157,14 +149,6 @@ namespace Raylib_cs [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern void SetWindowTitle(byte* title); - public static void SetWindowTitle(Utf8String title) - { - fixed (byte* p = title) - { - SetWindowTitle(p); - } - } - /// Set window position on screen (only PLATFORM_DESKTOP) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern void SetWindowPosition(int x, int y); @@ -237,34 +221,14 @@ namespace Raylib_cs [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern byte* GetMonitorName(int monitor); - /// Get the human-readable, UTF-8 encoded name of the primary monitor - public static Utf8String GetMonitorName_(int monitor) - { - return Utf8String.FromPtr(GetMonitorName(monitor)); - } - /// Get clipboard text content [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern byte* GetClipboardText(); - /// Get clipboard text content - public static Utf8String GetClipboardText_() - { - return Utf8String.FromPtr(GetClipboardText()); - } - /// Set clipboard text content [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern void SetClipboardText(byte* text); - public static void SetClipboardText(Utf8String text) - { - fixed (byte* p = text) - { - SetClipboardText(p); - } - } - // Custom frame control functions // NOTE: Those functions are intended for advance users that want full control over the frame processing // By default EndDrawing() does this job: draws everything + SwapScreenBuffer() + manage frame timming + PollInputEvents() @@ -526,13 +490,6 @@ namespace Raylib_cs [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern void SetTraceLogCallback(TraceLogCallback callback); - /// Set custom trace log - public static void SetTraceLogCallback_(TraceLogCallback callback) - { - SetTraceLogCallback(callback); - traceLogCallback = callback; - } - /// Set custom file binary data loader [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern void SetLoadFileDataCallback(LoadFileDataCallback callback); @@ -652,12 +609,6 @@ namespace Raylib_cs [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern byte* GetGamepadName(int gamepad); - /// Return gamepad internal name id - public static Utf8String GetGamepadName_(int gamepad) - { - return Utf8String.FromPtr(GetGamepadName(gamepad)); - } - /// Detect if a gamepad button has been pressed once [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern CBool IsGamepadButtonPressed(int gamepad, GamepadButton button); @@ -1311,27 +1262,12 @@ namespace Raylib_cs /// Draw text (using default font) within an image (destination) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern void ImageDrawText(ref Image dst, byte* text, int x, int y, int fontSize, Color color); - - public static void ImageDrawText(ref Image dst, Utf8String text, int x, int y, int fontSize, Color color) - { - fixed (byte* p = text) - { - ImageDrawText(ref dst, p, x, y, fontSize, color); - } - } + public static extern void ImageDrawText(Image* dst, byte* 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(ref Image dst, Font font, byte* text, Vector2 position, float fontSize, float spacing, Color tint); + public static extern void ImageDrawTextEx(Image* dst, Font font, byte* text, Vector2 position, float fontSize, float spacing, Color tint); - public static void ImageDrawTextEx(ref Image dst, Font font, Utf8String text, Vector2 position, float fontSize, float spacing, Color tint) - { - fixed (byte* p = text) - { - ImageDrawTextEx(ref dst, font, p, position, fontSize, spacing, tint); - } - } // Texture loading functions // NOTE: These functions require GPU access @@ -1524,41 +1460,14 @@ namespace Raylib_cs [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern void DrawText(byte* text, int posX, int posY, int fontSize, Color color); - public static void DrawText(Utf8String text, int posX, int posY, int fontSize, Color color) - { - fixed (byte* p = text) - { - DrawText(p, posX, posY, fontSize, 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 void DrawTextEx(Font font, Utf8String text, Vector2 position, float fontSize, - float spacing, Color tint) - { - fixed (byte* p = text) - { - DrawTextEx(font, p, position, fontSize, spacing, 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 void DrawTextPro(Font font, Utf8String text, Vector2 position, float fontSize, - float spacing, Color tint) - { - fixed (byte* p = text) - { - DrawTextEx(font, p, position, fontSize, spacing, tint); - } - - } - /// Draw one character (codepoint) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern void DrawTextCodepoint(Font font, int codepoint, Vector2 position, float scale, Color tint); @@ -1570,26 +1479,10 @@ namespace Raylib_cs [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern int MeasureText(byte* text, int fontSize); - public static int MeasureText(Utf8String text, int fontSize) - { - fixed (byte* p = text) - { - return MeasureText(p, 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 Vector2 MeasureTextEx(Font font, Utf8String text, float fontSize, float spacing) - { - fixed (byte* p = text) - { - return MeasureTextEx(font, p, fontSize, spacing); - } - } - /// Get glyph index position in font for a codepoint (unicode character), fallback to '?' if not found [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern int GetGlyphIndex(Font font, int character); @@ -1617,61 +1510,20 @@ namespace Raylib_cs [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern void TextAppend(byte* text, byte* append, int* position); - public static void TextAppend(Utf8String text, Utf8String append, int position) - { - fixed (byte* p1 = text) - { - fixed (byte* p2 = append) - { - TextAppend(p1, p2, &position); - } - } - } - - /// Get Pascal case notation version of provided string [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern byte* TextToPascal(byte* text); - public static Utf8String TextToPascal(Utf8String text) - { - fixed (byte* p1 = text) - { - return Utf8String.FromPtr(TextToPascal(p1)); - } - } /// Get integer value from text (negative values not supported) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern int TextToInteger(byte* text); - public static int TextToInteger(Utf8String text) - { - fixed (byte* p1 = text) - { - return TextToInteger(p1); - } - } - // 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 int[] LoadCodepoints(Utf8String text, ref int count) - { - fixed (byte* p1 = text) - { - fixed (int* c = &count) - { - var pointsPtr = LoadCodepoints(p1, c); - var codepoints = new ReadOnlySpan(pointsPtr, count).ToArray(); - UnloadCodepoints(pointsPtr); - return codepoints; - } - } - } - /// Unload codepoints data from memory [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern void UnloadCodepoints(int* codepoints); @@ -1680,58 +1532,18 @@ namespace Raylib_cs [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern int GetCodepointCount(byte* text); - public static int GetCodepointCount(Utf8String text) - { - fixed (byte* p1 = text) - { - return GetCodepointCount(p1); - } - } - /// 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); - /// Returns next codepoint in a UTF8 encoded string; 0x3f('?') is returned on failure - /// single codepoint / "char" - public static int GetCodepoint(Utf8String text, ref int bytesProcessed) - { - fixed (byte* p1 = text) - { - // this probably wont work - fixed (int* p = &bytesProcessed) - { - return GetCodepoint(p1, p); - } - } - } - /// 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 Utf8String CodepointToUTF8(int codepoint, ref int byteSize) - { - fixed (int* l1 = &byteSize) - { - var ptr = CodepointToUTF8(codepoint, l1); - return Utf8String.FromPtr(ptr); - } - } - /// Encode codepoint into utf8 text (char array length returned as parameter) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern byte* TextCodepointsToUTF8(int* codepoints, int length); - public static Utf8String TextCodepointsToUTF8(int[] codepoints, int length) - { - fixed (int* c1 = codepoints) - { - var ptr = TextCodepointsToUTF8(c1, length); - return Utf8String.FromPtr(ptr); - } - } - //------------------------------------------------------------------------------------ // Basic 3d Shapes Drawing Functions (Module: models) @@ -1999,24 +1811,6 @@ namespace Raylib_cs [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern ModelAnimation* LoadModelAnimations(byte* fileName, int* animsCount); - public static ReadOnlySpan LoadModelAnimations(Utf8String fileName, ref int animsCount) - { - fixed (byte* p1 = fileName) - { - fixed (int* p2 = &animsCount) - { - var model = LoadModelAnimations(p1, p2); - - if ((IntPtr)model == IntPtr.Zero) - { - throw new ApplicationException("Failed to load animation"); - } - - return new ReadOnlySpan(model, animsCount); - } - } - } - /// Update model animation pose [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern void UpdateModelAnimation(Model model, ModelAnimation anim, int frame);