diff --git a/Raylib-cs/interop/Raylib.cs b/Raylib-cs/interop/Raylib.cs index 8ccc62c..b29055d 100644 --- a/Raylib-cs/interop/Raylib.cs +++ b/Raylib-cs/interop/Raylib.cs @@ -2531,8 +2531,7 @@ namespace Raylib_cs [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern Music LoadMusicStream(sbyte* fileName); - //TODO: Span/Helper method - /// Load music stream from data + /// Load music stream from memory buffer, fileType refers to extension: i.e. ".wav" [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern Music LoadMusicStreamFromMemory(sbyte* fileType, byte* data, int dataSize); diff --git a/Raylib-cs/types/Raylib.Utils.cs b/Raylib-cs/types/Raylib.Utils.cs index 3eb6612..8c5647a 100644 --- a/Raylib-cs/types/Raylib.Utils.cs +++ b/Raylib-cs/types/Raylib.Utils.cs @@ -115,6 +115,21 @@ namespace Raylib_cs } } + /// + /// Load image from a native copy of managed memory, fileType refers to extension: i.e. "png" + /// + public static Image LoadImageFromMemory(string fileType, byte[] fileData) + { + using var fileTypeNative = fileType.ToUTF8Buffer(); + + byte* fileDataNative = (byte*)MemAlloc(fileData.Length); + Marshal.Copy(fileData, 0, (IntPtr)fileDataNative, fileData.Length); + + Image image = LoadImageFromMemory(fileTypeNative.AsPointer(), fileDataNative, fileData.Length); + + return image; + } + /// Export image data to file public static CBool ExportImage(Image image, string fileName) { @@ -665,6 +680,37 @@ namespace Raylib_cs } } + /// + /// Load font from a native copy of managed memory, fileType refers to extension: i.e. "ttf" + /// + public static Font LoadFontFromMemory( + string fileType, + byte[] fileData, + int fontSize, + int[] fontChars, + int glyphCount + ) + { + using var fileTypeNative = fileType.ToUTF8Buffer(); + + byte* fileDataNative = (byte*)MemAlloc(fileData.Length); + Marshal.Copy(fileData, 0, (IntPtr)fileDataNative, fileData.Length); + + fixed (int* fontCharsNative = fontChars) + { + Font font = LoadFontFromMemory( + fileTypeNative.AsPointer(), + fileDataNative, + fileData.Length, + fontSize, + fontCharsNative, + glyphCount + ); + + return font; + } + } + /// Upload vertex data into GPU and provided VAO/VBO ids public static void UploadMesh(ref Mesh mesh, CBool dynamic) { @@ -936,6 +982,28 @@ namespace Raylib_cs return LoadWave(str1.AsPointer()); } + /// + /// Load wave from a native copy of managed memory, fileType refers to extension: i.e. "wav" + /// + public static Wave LoadWaveFromMemory( + string fileType, + byte[] fileData + ) + { + using var fileTypeNative = fileType.ToUTF8Buffer(); + + byte* fileDataNative = (byte*)MemAlloc(fileData.Length); + Marshal.Copy(fileData, 0, (IntPtr)fileDataNative, fileData.Length); + + Wave wave = LoadWaveFromMemory( + fileTypeNative.AsPointer(), + fileDataNative, + fileData.Length + ); + + return wave; + } + /// Load sound from file public static Sound LoadSound(string fileName) { @@ -964,6 +1032,28 @@ namespace Raylib_cs return LoadMusicStream(str1.AsPointer()); } + /// + /// Load music stream from a native copy of managed memory, fileType refers to extension: i.e. ".wav" + /// + public static Music LoadMusicStreamFromMemory( + string fileType, + byte[] fileData + ) + { + using var fileTypeNative = fileType.ToUTF8Buffer(); + + byte* fileDataNative = (byte*)MemAlloc(fileData.Length); + Marshal.Copy(fileData, 0, (IntPtr)fileDataNative, fileData.Length); + + Music music = LoadMusicStreamFromMemory( + fileTypeNative.AsPointer(), + fileDataNative, + fileData.Length + ); + + return music; + } + public static string SubText(this string input, int position, int length) { return input.Substring(position, Math.Min(length, input.Length));