Watch
2
0
Fork
You've already forked raylib-cs
0

Preserve raylib types

Removed "Cricle" and "Line" structs (and their extension methods) to conserve only raylib types.

Added "Dimensions" property to "Texture2D"
Added another "Load" overload for the "Texture2D" extensions (load from memory)

New overloads for the "LoadFileData" and "SaveFileData" methods
This commit is contained in:
Matorio 2025-07-11 21:48:06 -05:00
commit c0a9549454
11 changed files with 121 additions and 299 deletions

View file

@ -1,69 +0,0 @@
using System.Numerics;
namespace Raylib_cs;
public struct Circle
{
public Vector2 position;
public float radius;
public Color color;
public float X
{
get
{
return position.X;
}
set
{
position.Y = value;
}
}
public float Y
{
get
{
return position.Y;
}
set
{
position.Y = value;
}
}
public Circle(Vector2 position)
{
this.position = position;
}
public Circle(float x, float y)
{
position = new Vector2(x, y);
}
public Circle(float x, float y, Color color)
{
position = new Vector2(x, y);
this.color = color;
}
public Circle(Vector2 position, float radius)
{
this.position = position;
this.radius = radius;
}
public Circle(float x, float y, float radius)
{
position = new Vector2(x, y);
this.radius = radius;
}
public Circle(Vector2 position, float radius, Color color)
{
this.position = position;
this.radius = radius;
this.color = color;
}
}

View file

@ -1,39 +0,0 @@
using System.Numerics;
namespace Raylib_cs;
public struct Line
{
public Vector2 pointA;
public Vector2 pointB;
public Line(Vector2 p1, Vector2 p2)
{
pointA = p1;
pointB = p2;
}
public Line(Vector2 startPos, float endPosX, float endPosY)
{
pointA = startPos;
pointB = new Vector2(endPosX, endPosY);
}
public Line(float startPosX, float startPosY, Vector2 endPos)
{
pointA = new Vector2(startPosX, startPosY);
pointB = endPos;
}
public Line(float startPosX, float startPosY, float endPosX, float endPosY)
{
pointA = new Vector2(startPosX, startPosY);
pointB = new Vector2(endPosX, endPosY);
}
public readonly override string ToString()
{
return $"P1: {pointA}, P2: {pointB}";
}
}

View file

@ -228,6 +228,14 @@ public static unsafe partial class Raylib
return (T*)MemAlloc(count * (uint)sizeof(T));
}
/// <summary>Save data to file from an unmanaged type</summary>
/// <returns>True if the operation was successfully</returns>
public static CBool SaveFileData<T>(T data, string fileName) where T : unmanaged
{
using AnsiBuffer ansiBuffer = fileName.ToAnsiBuffer();
return SaveFileData(ansiBuffer.AsPointer(), &data, sizeof(T));
}
/// <summary>Load file data as byte array (read)</summary>
public static byte* LoadFileData(string fileName, ref int bytesRead)
{
@ -238,6 +246,29 @@ public static unsafe partial class Raylib
}
}
public static byte[] LoadFileData(string fileName)
{
int length = 0;
byte* data = LoadFileData(fileName, ref length);
byte[] arr = new byte[length];
Marshal.Copy((IntPtr)data, arr, 0, length);
UnloadFileData(data);
return arr;
}
/// <summary>
/// Load file data as an array of unmanaged types
/// </summary>
public static T[] LoadFileData<T>(string fileName) where T : unmanaged
{
int length = 0;
byte* data = LoadFileData(fileName, ref length);
Span<T> values = new Span<T>(data, length / sizeof(T));
T[] arr = values.ToArray();
UnloadFileData(data);
return arr;
}
/// <summary>Get dropped files names (memory should be freed)</summary>
public static string[] GetDroppedFiles()
{

View file

@ -1,4 +1,5 @@
using System.Runtime.InteropServices;
using System.Numerics;
namespace Raylib_cs;
@ -128,4 +129,15 @@ public partial struct Texture2D
/// Data format (PixelFormat type)
/// </summary>
public PixelFormat Format;
/// <summary>
/// Get width and height packed in a Vector2
/// </summary>
public Vector2 Dimensions
{
get
{
return new Vector2(Width, Height);
}
}
}