2
0
mirror of https://github.com/raylib-cs/raylib-cs synced 2025-04-03 11:09:40 -04:00

Update FromMemory utils to use fixed instead

This commit is contained in:
ChrisDill 2023-06-09 20:12:30 +01:00
parent 074ba78d09
commit ccf31c38e1

View File

@ -116,18 +116,16 @@ namespace Raylib_cs
} }
/// <summary> /// <summary>
/// Load image from a native copy of managed memory, fileType refers to extension: i.e. "png" /// Load image from managed memory, fileType refers to extension: i.e. "png"
/// </summary> /// </summary>
public static Image LoadImageFromMemory(string fileType, byte[] fileData) public static Image LoadImageFromMemory(string fileType, byte[] fileData)
{ {
using var fileTypeNative = fileType.ToUTF8Buffer(); using var fileTypeNative = fileType.ToUTF8Buffer();
fixed (byte* fileDataNative = fileData)
byte* fileDataNative = (byte*)MemAlloc(fileData.Length); {
Marshal.Copy(fileData, 0, (IntPtr)fileDataNative, fileData.Length); Image image = LoadImageFromMemory(fileTypeNative.AsPointer(), fileDataNative, fileData.Length);
return image;
Image image = LoadImageFromMemory(fileTypeNative.AsPointer(), fileDataNative, fileData.Length); }
return image;
} }
/// <summary>Export image data to file</summary> /// <summary>Export image data to file</summary>
@ -681,7 +679,7 @@ namespace Raylib_cs
} }
/// <summary> /// <summary>
/// Load font from a native copy of managed memory, fileType refers to extension: i.e. "ttf" /// Load font from managed memory, fileType refers to extension: i.e. "ttf"
/// </summary> /// </summary>
public static Font LoadFontFromMemory( public static Font LoadFontFromMemory(
string fileType, string fileType,
@ -692,22 +690,21 @@ namespace Raylib_cs
) )
{ {
using var fileTypeNative = fileType.ToUTF8Buffer(); using var fileTypeNative = fileType.ToUTF8Buffer();
fixed (byte* fileDataNative = fileData)
byte* fileDataNative = (byte*)MemAlloc(fileData.Length);
Marshal.Copy(fileData, 0, (IntPtr)fileDataNative, fileData.Length);
fixed (int* fontCharsNative = fontChars)
{ {
Font font = LoadFontFromMemory( fixed (int* fontCharsNative = fontChars)
fileTypeNative.AsPointer(), {
fileDataNative, Font font = LoadFontFromMemory(
fileData.Length, fileTypeNative.AsPointer(),
fontSize, fileDataNative,
fontCharsNative, fileData.Length,
glyphCount fontSize,
); fontCharsNative,
glyphCount
);
return font; return font;
}
} }
} }
@ -961,7 +958,7 @@ namespace Raylib_cs
} }
/// <summary> /// <summary>
/// Load wave from a native copy of managed memory, fileType refers to extension: i.e. "wav" /// Load wave from managed memory, fileType refers to extension: i.e. "wav"
/// </summary> /// </summary>
public static Wave LoadWaveFromMemory( public static Wave LoadWaveFromMemory(
string fileType, string fileType,
@ -970,16 +967,16 @@ namespace Raylib_cs
{ {
using var fileTypeNative = fileType.ToUTF8Buffer(); using var fileTypeNative = fileType.ToUTF8Buffer();
byte* fileDataNative = (byte*)MemAlloc(fileData.Length); fixed (byte* fileDataNative = fileData)
Marshal.Copy(fileData, 0, (IntPtr)fileDataNative, fileData.Length); {
Wave wave = LoadWaveFromMemory(
fileTypeNative.AsPointer(),
fileDataNative,
fileData.Length
);
Wave wave = LoadWaveFromMemory( return wave;
fileTypeNative.AsPointer(), }
fileDataNative,
fileData.Length
);
return wave;
} }
/// <summary>Load sound from file</summary> /// <summary>Load sound from file</summary>
@ -1011,7 +1008,7 @@ namespace Raylib_cs
} }
/// <summary> /// <summary>
/// Load music stream from a native copy of managed memory, fileType refers to extension: i.e. ".wav" /// Load music stream from managed memory, fileType refers to extension: i.e. ".wav"
/// </summary> /// </summary>
public static Music LoadMusicStreamFromMemory( public static Music LoadMusicStreamFromMemory(
string fileType, string fileType,
@ -1020,16 +1017,16 @@ namespace Raylib_cs
{ {
using var fileTypeNative = fileType.ToUTF8Buffer(); using var fileTypeNative = fileType.ToUTF8Buffer();
byte* fileDataNative = (byte*)MemAlloc(fileData.Length); fixed (byte* fileDataNative = fileData)
Marshal.Copy(fileData, 0, (IntPtr)fileDataNative, fileData.Length); {
Music music = LoadMusicStreamFromMemory(
fileTypeNative.AsPointer(),
fileDataNative,
fileData.Length
);
Music music = LoadMusicStreamFromMemory( return music;
fileTypeNative.AsPointer(), }
fileDataNative,
fileData.Length
);
return music;
} }
public static string SubText(this string input, int position, int length) public static string SubText(this string input, int position, int length)