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

Extensions and explicit types

Added a new "RaylibExtensions" static class that gives overloaded extension methods to types so you can use them directly instead of calling the raylib class for everything.

Two new types: "Circle" and "Line"

All the "var" variables were changed to explicit types on the raylib class.

"MakeDirectory" overload, and string version of "GetApplicationDirectory" and "GetWorkingDirectory" methods.
This commit is contained in:
Matorio 2025-06-20 18:41:16 -05:00
commit d723905f5c
5 changed files with 751 additions and 77 deletions

View file

@ -0,0 +1,545 @@
using System.Collections.Generic;
using static Raylib_cs.Raylib;
using System.Numerics;
namespace Raylib_cs;
public static class RaylibExtensions
{
public static unsafe void SaveFileText(string fileName, string text)
{
using AnsiBuffer fileBuffer = fileName.ToAnsiBuffer();
using AnsiBuffer textBuffer = text.ToAnsiBuffer();
Raylib.SaveFileText(fileBuffer.AsPointer(), textBuffer.AsPointer());
}
public static unsafe string LoadFileText(string fileName)
{
using AnsiBuffer nameBuffer = fileName.ToAnsiBuffer();
sbyte* data = Raylib.LoadFileText(nameBuffer.AsPointer());
return new string(data);
}
#region Rectangle
public static void Draw(this Rectangle rectangle, Color color)
{
DrawRectangleRec(rectangle, color);
}
public static void Draw(this Rectangle rec, Vector2 origin, Color color)
{
rec.Draw(origin, 0.0f, color);
}
public static void Draw(this Rectangle rec, Vector2 origin, float rotation, Color color)
{
DrawRectanglePro(rec, origin, rotation, color);
}
public static void GetIntegerPosition(this Rectangle rec, out int x, out int y)
{
x = (int)rec.X;
y = (int)rec.Y;
}
public static void GetIntegerDimentions(this Rectangle rec, out int width, out int height)
{
width = (int)rec.Width;
height = (int)rec.Height;
}
public static void GetIntegerValues(this Rectangle rec, out int x, out int y, out int width, out int height)
{
x = (int)rec.X;
y = (int)rec.Y;
width = (int)rec.Width;
height = (int)rec.Height;
}
public static void DrawLines(this Rectangle rectangle, Color color)
{
rectangle.GetIntegerValues(out int x, out int y, out int w, out int h);
DrawRectangleLines(x, y, w, h, color);
}
public static void DrawLines(this Rectangle rectangle, float thickness, Color color)
{
DrawRectangleLinesEx(rectangle, thickness, color);
}
public static void DrawGradient(this Rectangle rec, Color topLeft, Color bottomLeft, Color topRight, Color bottomRight)
{
DrawRectangleGradientEx(rec, topLeft, bottomLeft, topRight, bottomRight);
}
public static void DrawGradientV(this Rectangle rectangle, Color top, Color bottom)
{
rectangle.GetIntegerValues(out int x, out int y, out int w, out int h);
DrawRectangleGradientV(x, y, w, h, top, bottom);
}
public static void DrawGradientH(this Rectangle rec, Color left, Color right)
{
rec.GetIntegerValues(out int x, out int y, out int w, out int h);
DrawRectangleGradientH(x, y, w, h, left, right);
}
public static void DrawRounded(this Rectangle rec, float roundness, Color color)
{
DrawRectangleRounded(rec, roundness, 10, color);
}
public static void DrawRounded(this Rectangle rec, float roundness, int segments, Color color)
{
DrawRectangleRounded(rec, roundness, segments, color);
}
public static void DrawRoundedLines(this Rectangle rec, float roundness, Color color)
{
DrawRectangleRoundedLines(rec, roundness, 10, color);
}
public static void DrawRoundedLines(this Rectangle rec, float roundness, int segments, Color color)
{
DrawRectangleRoundedLines(rec, roundness, segments, color);
}
public static void DrawRoundedLines(this Rectangle rec, float roundness, float thickness, Color color)
{
DrawRectangleRoundedLinesEx(rec, roundness, 10, thickness, color);
}
public static void DrawRoundedLines(this Rectangle rec, float roundness, int segments, float thickness, Color color)
{
DrawRectangleRoundedLinesEx(rec, roundness, segments, thickness, color);
}
public static CBool CheckCollision(this Rectangle rec, Rectangle rectangle)
{
return CheckCollisionRecs(rec, rectangle);
}
public static CBool CheckCollision(this Rectangle rec, Vector2 point)
{
return CheckCollisionPointRec(point, rec);
}
public static CBool CheckCircleCollision(this Rectangle rec, Vector2 center, float radius)
{
return CheckCollisionCircleRec(center, radius, rec);
}
public static CBool CheckColllision(this Rectangle rec, Circle circle)
{
return CheckCollisionCircleRec(circle.position, circle.radius, rec);
}
public static Rectangle GetCollisionRectangle(this Rectangle rectangle, Rectangle rectangle2)
{
return GetCollisionRec(rectangle, rectangle2);
}
#endregion
#region Circle
public static void Draw(this Circle circle)
{
DrawCircleV(circle.position, circle.radius, circle.color);
}
public static void Draw(this Circle circle, Color colorOverride)
{
DrawCircleV(circle.position, circle.radius, colorOverride);
}
public static void DrawLines(this Circle circle)
{
DrawCircleLinesV(circle.position, circle.radius, circle.color);
}
public static void DrawLines(this Circle circle, Color colorOverride)
{
DrawCircleLinesV(circle.position, circle.radius, colorOverride);
}
public static void DrawGradient(this Circle circle, Color innerColor, Color outerColor)
{
int x = (int)circle.position.X;
int y = (int)circle.position.Y;
DrawCircleGradient(x, y, circle.radius, innerColor, outerColor);
}
public static void DrawSector(this Circle circle, float startAngle, float endAngle)
{
circle.DrawSector(startAngle, endAngle, 36);
}
public static void DrawSector(this Circle circle, float startAngle, float endAngle, int segments)
{
circle.DrawSector(startAngle, endAngle, segments, circle.color);
}
public static void DrawSector(this Circle circle, float startAngle, float endAngle, int segments, Color colorOverride)
{
DrawCircleSector(circle.position, circle.radius, startAngle, endAngle, segments, colorOverride);
}
public static void DrawSectorLines(this Circle circle, float startAngle, float endAngle)
{
circle.DrawSectorLines(startAngle, endAngle, 36);
}
public static void DrawSectorLines(this Circle circle, float startAngle, float endAngle, int segments)
{
circle.DrawSectorLines(startAngle, endAngle, segments, circle.color);
}
public static void DrawSectorLines(this Circle circle, float startAngle, float endAngle, int segments, Color colorOverride)
{
DrawCircleSectorLines(circle.position, circle.radius, startAngle, endAngle, segments, colorOverride);
}
public static CBool CheckCollision(this Circle circle, Circle circle2)
{
return CheckCollisionCircles(circle.position, circle.radius, circle2.position, circle2.radius);
}
public static CBool CheckCollision(this Circle circle, Rectangle rec)
{
return CheckCollisionCircleRec(circle.position, circle.radius, rec);
}
public static CBool CheckCollision(this Circle circle, Vector2 point)
{
return CheckCollisionPointCircle(point, circle.position, circle.radius);
}
public static CBool CheckCollision(this Circle circle, Line line)
{
return CheckCollisionCircleLine(circle.position, circle.radius, line.pointA, line.pointB);
}
#endregion
#region Line
public static CBool CheckCollision(this Line line1, Line line2, ref Vector2 collisionPoint)
{
return CheckCollisionLines(line1.pointA, line1.pointB, line2.pointA, line2.pointB, ref collisionPoint);
}
public static CBool CheckCollision(this Line line, Circle circle)
{
return CheckCollisionCircleLine(circle.position, circle.radius, line.pointA, line.pointB);
}
public static CBool CheckCollision(this Line line, Vector2 point, int threshold)
{
return CheckCollisionPointLine(point, line.pointA, line.pointB, threshold);
}
public static void Draw(this Line line, Color color)
{
DrawLineV(line.pointA, line.pointB, color);
}
public static void DrawBezier(this Line line, Color color)
{
line.DrawBezier(1, color);
}
public static void DrawBezier(this Line line, float thick, Color color)
{
DrawLineBezier(line.pointA, line.pointB, thick, color);
}
public static void DrawBezierCubic(this Line line, Line control, Color color)
{
line.DrawBezierCubic(control, 1, color);
}
public static void DrawBezierCubic(this Line line, Line control, float thick, Color color)
{
DrawLineBezierCubic(line.pointA, line.pointB, control.pointA, control.pointB, thick, color);
}
public static void DrawBezierQuad(this Line line, Vector2 control, Color color)
{
line.DrawBezierQuad(control, 1, color);
}
public static void DrawBezierQuad(this Line line, Vector2 control, float thick, Color color)
{
DrawLineBezierQuad(line.pointA, line.pointB, control, thick, color);
}
public static float GetDistance(this Line line)
{
return Vector2.Distance(line.pointA, line.pointB);
}
public static void SwapPoints(this Line line)
{
Vector2 temp = line.pointA;
line.pointA = line.pointB;
line.pointB = temp;
}
public static void DrawLines(this IEnumerable<Line> lines, Color color)
{
Line[] linesArray = System.Linq.Enumerable.ToArray<Line>(lines);
Vector2[] points = new Vector2[linesArray.Length * 2];
for (int i = 0; i < linesArray.Length; i++)
{
Line l = linesArray[i];
points[i * 2] = l.pointA;
points[i * 2 + 1] = l.pointB;
}
DrawLineStrip(points, points.Length, color);
}
public static void DrawLines(this IEnumerable<Vector2> points, Color color)
{
Vector2[] pointArray = System.Linq.Enumerable.ToArray<Vector2>(points);
DrawLineStrip(pointArray, pointArray.Length, color);
}
#endregion
#region Image
public static unsafe Image* GetPointer(this ref Image image)
{
fixed (Image* ptr = &image)
{
return ptr;
}
}
public static void Load(this ref Image image, string fileName)
{
image = LoadImage(fileName);
}
public static void Load(this ref Image image, string fileType, byte[] data)
{
image = LoadImageFromMemory(fileType, data);
}
public static void Load(this ref Image image, Texture2D texture)
{
image = LoadImageFromTexture(texture);
}
public static void Load(this ref Image image, string fileName, int width, int height, PixelFormat format, int headerSize)
{
image = LoadImageRaw(fileName, width, height, format, headerSize);
}
public static void Unload(this Image image)
{
UnloadImage(image);
}
public static unsafe Color[] GetPalette(this Image image, int maxPaletteSize)
{
int colorCount = 0;
Color* colors = LoadImagePalette(image, maxPaletteSize, &colorCount);
Color[] palette = new Color[colorCount];
for (int i = 0; i < colorCount; i++)
{
palette[i] = colors[i];
}
UnloadImagePalette(colors);
return palette;
}
public static void LoadFromScreen(this ref Image image)
{
image = LoadImageFromScreen();
}
public static void Draw(this ref Image image, Line line, Color color)
{
ImageDrawLineV(ref image, line.pointA, line.pointB, color);
}
public static void Draw(this ref Image image, Line line, int thickness, Color color)
{
ImageDrawLineEx(ref image, line.pointA, line.pointB, thickness, color);
}
public static void Draw(this ref Image image, Circle circle)
{
ImageDrawCircle(ref image, (int)circle.X, (int)circle.Y, (int)circle.radius, circle.color);
}
public static void Draw(this ref Image image, Rectangle rectangle, Color color)
{
ImageDrawRectangleRec(ref image, rectangle, color);
}
public static void Draw(this ref Image image, string text, Vector2 position, int fontSize, Color color)
{
ImageDrawText(ref image, text, (int)position.X, (int)position.Y, fontSize, color);
}
public static void Draw(this ref Image image, Font font, string text, Vector2 position, int fontSize, float spacing, Color color)
{
ImageDrawTextEx(ref image, font, text, position, fontSize, spacing, color);
}
public static unsafe void DrawLines(this ref Image image, Circle circle)
{
ImageDrawCircleLinesV(ref image, circle.position, (int)circle.radius, circle.color);
}
public static void DrawTriangle(this ref Image image, Vector2 p1, Vector2 p2, Vector2 p3, Color color)
{
ImageDrawTriangle(ref image, p1, p2, p3, color);
}
public static void DrawTriangle(this ref Image image, Vector2 p1, Vector2 p2, Vector2 p3, Color c1, Color c2, Color c3)
{
ImageDrawTriangleEx(ref image, p1, p2, p3, c1, c2, c3);
}
public static void DrawLines(this ref Image image, Rectangle rectangle, Color color)
{
ImageDrawRectangleLines(ref image, rectangle, 1, color);
}
public static void DrawLines(this ref Image image, Rectangle rectangle, int thickness, Color color)
{
ImageDrawRectangleLines(ref image, rectangle, thickness, color);
}
#endregion
#region Texture2D
public static void Load(this ref Texture2D texture, string fileName)
{
texture = LoadTexture(fileName);
}
public static void Load(this ref Texture2D texture, Image image)
{
texture = LoadTextureFromImage(image);
}
public static void Unload(this Texture2D texture)
{
UnloadTexture(texture);
}
public static void SetFilter(this Texture2D texture, TextureFilter filter)
{
SetTextureFilter(texture, filter);
}
public static void SetWrap(this Texture2D texture, TextureWrap wrap)
{
SetTextureWrap(texture, wrap);
}
public static void Update<T>(this Texture2D texture, T[] pixels) where T : unmanaged
{
UpdateTexture(texture, pixels);
}
public static Rectangle GetSourceRectangle(this Texture2D texture)
{
return new Rectangle(Vector2.Zero, texture.Width, texture.Height);
}
public static Vector2 GetDimensions(this Texture2D texture)
{
return new Vector2(texture.Width, texture.Height);
}
public static void Draw(this Texture2D texture, int x, int y)
{
DrawTexture(texture, x, y, Color.White);
}
public static void Draw(this Texture2D texture, int x, int y, Color color)
{
DrawTexture(texture, x, y, color);
}
public static void Draw(this Texture2D texture, Vector2 position)
{
DrawTexture(texture, (int)position.X, (int)position.Y, Color.White);
}
public static void Draw(this Texture2D texture, Vector2 position, Vector2 origin)
{
Vector2 size = texture.GetDimensions();
Rectangle source = new Rectangle(Vector2.Zero, size);
Rectangle target = new Rectangle(position, size);
DrawTexturePro(texture, source, target, origin, 0, Color.White);
}
public static void Draw(this Texture2D texture, Vector2 position, Vector2 origin, Color color)
{
Vector2 size = texture.GetDimensions();
Rectangle source = new Rectangle(Vector2.Zero, size);
Rectangle target = new Rectangle(position, size);
DrawTexturePro(texture, source, target, origin, 0, color);
}
public static void Draw(this Texture2D texture, Vector2 position, Color color)
{
DrawTexture(texture, (int)position.X, (int)position.Y, color);
}
public static void Draw(this Texture2D texture, Vector2 position, float rotation, float scale)
{
DrawTextureEx(texture, position, rotation, scale, Color.White);
}
public static void Draw(this Texture2D texture, Vector2 position, float rotation, float scale, Color color)
{
DrawTextureEx(texture, position, rotation, scale, color);
}
public static void Draw(this Texture2D texture, Vector2 position, float rotation, Vector2 scale)
{
Draw(texture, position, rotation, scale, Color.White);
}
public static void Draw(this Texture2D texture, Vector2 position, float rotation, Vector2 scale, Color color)
{
Vector2 size = new Vector2(texture.Width, texture.Height);
Rectangle source = new Rectangle(Vector2.Zero, size);
Rectangle target = new Rectangle(position, size);
target.Size *= scale;
DrawTexturePro(texture, source, target, Vector2.Zero, rotation, color);
}
public static void Draw(this Texture2D texture, Rectangle source, Rectangle dest)
{
DrawTexturePro(texture, source, dest, Vector2.Zero, 0, Color.White);
}
public static void Draw(this Texture2D texture, Rectangle source, Rectangle dest, Vector2 origin)
{
DrawTexturePro(texture, source, dest, origin, 0, Color.White);
}
public static void Draw(this Texture2D texture, Rectangle source, Rectangle dest, Vector2 origin, float rotation)
{
DrawTexturePro(texture, source, dest, origin, rotation, Color.White);
}
public static void Draw(this Texture2D texture, Rectangle source, Rectangle dest, Vector2 origin, float rotation, Color color)
{
DrawTexturePro(texture, source, dest, origin, rotation, color);
}
#endregion
}

69
Raylib-cs/types/Circle.cs Normal file
View file

@ -0,0 +1,69 @@
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;
}
}

39
Raylib-cs/types/Line.cs Normal file
View file

@ -0,0 +1,39 @@
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

@ -1,6 +1,6 @@
using System;
using System.Numerics;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Numerics;
using System;
namespace Raylib_cs; namespace Raylib_cs;
@ -9,14 +9,14 @@ public static unsafe partial class Raylib
/// <summary>Initialize window and OpenGL context</summary> /// <summary>Initialize window and OpenGL context</summary>
public static void InitWindow(int width, int height, string title) public static void InitWindow(int width, int height, string title)
{ {
using var str1 = title.ToUtf8Buffer(); using Utf8Buffer str1 = title.ToUtf8Buffer();
InitWindow(width, height, str1.AsPointer()); InitWindow(width, height, str1.AsPointer());
} }
/// <summary>Set title for window (only PLATFORM_DESKTOP)</summary> /// <summary>Set title for window (only PLATFORM_DESKTOP)</summary>
public static void SetWindowTitle(string title) public static void SetWindowTitle(string title)
{ {
using var str1 = title.ToUtf8Buffer(); using Utf8Buffer str1 = title.ToUtf8Buffer();
SetWindowTitle(str1.AsPointer()); SetWindowTitle(str1.AsPointer());
} }
@ -35,94 +35,94 @@ public static unsafe partial class Raylib
/// <summary>Set clipboard text content</summary> /// <summary>Set clipboard text content</summary>
public static void SetClipboardText(string text) public static void SetClipboardText(string text)
{ {
using var str1 = text.ToUtf8Buffer(); using Utf8Buffer str1 = text.ToUtf8Buffer();
SetClipboardText(str1.AsPointer()); SetClipboardText(str1.AsPointer());
} }
/// <summary>Open URL with default system browser (if available)</summary> /// <summary>Open URL with default system browser (if available)</summary>
public static void OpenURL(string url) public static void OpenURL(string url)
{ {
using var str1 = url.ToUtf8Buffer(); using Utf8Buffer str1 = url.ToUtf8Buffer();
OpenURL(str1.AsPointer()); OpenURL(str1.AsPointer());
} }
/// <summary>Set internal gamepad mappings (SDL_GameControllerDB)</summary> /// <summary>Set internal gamepad mappings (SDL_GameControllerDB)</summary>
public static int SetGamepadMappings(string mappings) public static int SetGamepadMappings(string mappings)
{ {
using var str1 = mappings.ToUtf8Buffer(); using Utf8Buffer str1 = mappings.ToUtf8Buffer();
return SetGamepadMappings(str1.AsPointer()); return SetGamepadMappings(str1.AsPointer());
} }
/// <summary>Load shader from files and bind default locations</summary> /// <summary>Load shader from files and bind default locations</summary>
public static Shader LoadShader(string vsFileName, string fsFileName) public static Shader LoadShader(string vsFileName, string fsFileName)
{ {
using var str1 = vsFileName.ToAnsiBuffer(); using AnsiBuffer str1 = vsFileName.ToAnsiBuffer();
using var str2 = fsFileName.ToAnsiBuffer(); using AnsiBuffer str2 = fsFileName.ToAnsiBuffer();
return LoadShader(str1.AsPointer(), str2.AsPointer()); return LoadShader(str1.AsPointer(), str2.AsPointer());
} }
/// <summary>Load shader from code string and bind default locations</summary> /// <summary>Load shader from code string and bind default locations</summary>
public static Shader LoadShaderFromMemory(string vsCode, string fsCode) public static Shader LoadShaderFromMemory(string vsCode, string fsCode)
{ {
using var str1 = vsCode.ToUtf8Buffer(); using Utf8Buffer str1 = vsCode.ToUtf8Buffer();
using var str2 = fsCode.ToUtf8Buffer(); using Utf8Buffer str2 = fsCode.ToUtf8Buffer();
return LoadShaderFromMemory(str1.AsPointer(), str2.AsPointer()); return LoadShaderFromMemory(str1.AsPointer(), str2.AsPointer());
} }
/// <summary>Get shader uniform location</summary> /// <summary>Get shader uniform location</summary>
public static int GetShaderLocation(Shader shader, string uniformName) public static int GetShaderLocation(Shader shader, string uniformName)
{ {
using var str1 = uniformName.ToUtf8Buffer(); using Utf8Buffer str1 = uniformName.ToUtf8Buffer();
return GetShaderLocation(shader, str1.AsPointer()); return GetShaderLocation(shader, str1.AsPointer());
} }
/// <summary>Get shader attribute location</summary> /// <summary>Get shader attribute location</summary>
public static int GetShaderLocationAttrib(Shader shader, string attribName) public static int GetShaderLocationAttrib(Shader shader, string attribName)
{ {
using var str1 = attribName.ToUtf8Buffer(); using Utf8Buffer str1 = attribName.ToUtf8Buffer();
return GetShaderLocationAttrib(shader, str1.AsPointer()); return GetShaderLocationAttrib(shader, str1.AsPointer());
} }
/// <summary>Takes a screenshot of current screen (saved a .png)</summary> /// <summary>Takes a screenshot of current screen (saved a .png)</summary>
public static void TakeScreenshot(string fileName) public static void TakeScreenshot(string fileName)
{ {
using var str1 = fileName.ToAnsiBuffer(); using AnsiBuffer str1 = fileName.ToAnsiBuffer();
TakeScreenshot(str1.AsPointer()); TakeScreenshot(str1.AsPointer());
} }
/// <summary>Check file extension</summary> /// <summary>Check file extension</summary>
public static CBool IsFileExtension(string fileName, string ext) public static CBool IsFileExtension(string fileName, string ext)
{ {
using var str1 = fileName.ToAnsiBuffer(); using AnsiBuffer str1 = fileName.ToAnsiBuffer();
using var str2 = ext.ToAnsiBuffer(); using AnsiBuffer str2 = ext.ToAnsiBuffer();
return IsFileExtension(str1.AsPointer(), str2.AsPointer()); return IsFileExtension(str1.AsPointer(), str2.AsPointer());
} }
/// <summary>Get file modification time (last write time)</summary> /// <summary>Get file modification time (last write time)</summary>
public static long GetFileModTime(string fileName) public static long GetFileModTime(string fileName)
{ {
using var str1 = fileName.ToAnsiBuffer(); using AnsiBuffer str1 = fileName.ToAnsiBuffer();
return GetFileModTime(str1.AsPointer()); return GetFileModTime(str1.AsPointer());
} }
/// <summary>Load image from file into CPU memory (RAM)</summary> /// <summary>Load image from file into CPU memory (RAM)</summary>
public static Image LoadImage(string fileName) public static Image LoadImage(string fileName)
{ {
using var str1 = fileName.ToAnsiBuffer(); using AnsiBuffer str1 = fileName.ToAnsiBuffer();
return LoadImage(str1.AsPointer()); return LoadImage(str1.AsPointer());
} }
/// <summary>Load image from RAW file data</summary> /// <summary>Load image from RAW file data</summary>
public static Image LoadImageRaw(string fileName, int width, int height, PixelFormat format, int headerSize) public static Image LoadImageRaw(string fileName, int width, int height, PixelFormat format, int headerSize)
{ {
using var str1 = fileName.ToAnsiBuffer(); using AnsiBuffer str1 = fileName.ToAnsiBuffer();
return LoadImageRaw(str1.AsPointer(), width, height, format, headerSize); return LoadImageRaw(str1.AsPointer(), width, height, format, headerSize);
} }
/// <summary>Load image sequence from file (frames appended to image.data)</summary> /// <summary>Load image sequence from file (frames appended to image.data)</summary>
public static Image LoadImageAnim(string fileName, out int frames) public static Image LoadImageAnim(string fileName, out int frames)
{ {
using var str1 = fileName.ToAnsiBuffer(); using AnsiBuffer str1 = fileName.ToAnsiBuffer();
fixed (int* p = &frames) fixed (int* p = &frames)
{ {
return LoadImageAnim(str1.AsPointer(), p); return LoadImageAnim(str1.AsPointer(), p);
@ -134,7 +134,7 @@ public static unsafe partial class Raylib
/// </summary> /// </summary>
public static Image LoadImageFromMemory(string fileType, byte[] fileData) public static Image LoadImageFromMemory(string fileType, byte[] fileData)
{ {
using var fileTypeNative = fileType.ToAnsiBuffer(); using AnsiBuffer fileTypeNative = fileType.ToAnsiBuffer();
fixed (byte* fileDataNative = fileData) fixed (byte* fileDataNative = fileData)
{ {
Image image = LoadImageFromMemory(fileTypeNative.AsPointer(), fileDataNative, fileData.Length); Image image = LoadImageFromMemory(fileTypeNative.AsPointer(), fileDataNative, fileData.Length);
@ -145,21 +145,21 @@ public static unsafe partial class Raylib
/// <summary>Export image data to file</summary> /// <summary>Export image data to file</summary>
public static CBool ExportImage(Image image, string fileName) public static CBool ExportImage(Image image, string fileName)
{ {
using var str1 = fileName.ToAnsiBuffer(); using AnsiBuffer str1 = fileName.ToAnsiBuffer();
return ExportImage(image, str1.AsPointer()); return ExportImage(image, str1.AsPointer());
} }
/// <summary>Export image as code file defining an array of bytes</summary> /// <summary>Export image as code file defining an array of bytes</summary>
public static CBool ExportImageAsCode(Image image, string fileName) public static CBool ExportImageAsCode(Image image, string fileName)
{ {
using var str1 = fileName.ToAnsiBuffer(); using AnsiBuffer str1 = fileName.ToAnsiBuffer();
return ExportImageAsCode(image, str1.AsPointer()); return ExportImageAsCode(image, str1.AsPointer());
} }
/// <summary>Show trace log messages (LOG_DEBUG, LOG_INFO, LOG_WARNING, LOG_ERROR)</summary> /// <summary>Show trace log messages (LOG_DEBUG, LOG_INFO, LOG_WARNING, LOG_ERROR)</summary>
public static void TraceLog(TraceLogLevel logLevel, string text) public static void TraceLog(TraceLogLevel logLevel, string text)
{ {
using var str1 = text.ToUtf8Buffer(); using Utf8Buffer str1 = text.ToUtf8Buffer();
TraceLog(logLevel, str1.AsPointer()); TraceLog(logLevel, str1.AsPointer());
} }
@ -231,7 +231,7 @@ public static unsafe partial class Raylib
/// <summary>Load file data as byte array (read)</summary> /// <summary>Load file data as byte array (read)</summary>
public static byte* LoadFileData(string fileName, ref int bytesRead) public static byte* LoadFileData(string fileName, ref int bytesRead)
{ {
using var str1 = fileName.ToAnsiBuffer(); using AnsiBuffer str1 = fileName.ToAnsiBuffer();
fixed (int* p = &bytesRead) fixed (int* p = &bytesRead)
{ {
return LoadFileData(str1.AsPointer(), p); return LoadFileData(str1.AsPointer(), p);
@ -241,10 +241,10 @@ public static unsafe partial class Raylib
/// <summary>Get dropped files names (memory should be freed)</summary> /// <summary>Get dropped files names (memory should be freed)</summary>
public static string[] GetDroppedFiles() public static string[] GetDroppedFiles()
{ {
var filePathList = LoadDroppedFiles(); FilePathList filePathList = LoadDroppedFiles();
var files = new string[filePathList.Count]; string[] files = new string[filePathList.Count];
for (var i = 0; i < filePathList.Count; i++) for (int i = 0; i < filePathList.Count; i++)
{ {
files[i] = Marshal.PtrToStringUTF8((IntPtr)filePathList.Paths[i]); files[i] = Marshal.PtrToStringUTF8((IntPtr)filePathList.Paths[i]);
} }
@ -426,21 +426,21 @@ public static unsafe partial class Raylib
/// <summary>Generate image: grayscale image from text data</summary> /// <summary>Generate image: grayscale image from text data</summary>
public static Image GenImageText(int width, int height, string text) public static Image GenImageText(int width, int height, string text)
{ {
using var str1 = text.ToUtf8Buffer(); using Utf8Buffer str1 = text.ToUtf8Buffer();
return GenImageText(width, height, str1.AsPointer()); return GenImageText(width, height, str1.AsPointer());
} }
/// <summary>Create an image from text (default font)</summary> /// <summary>Create an image from text (default font)</summary>
public static Image ImageText(string text, int fontSize, Color color) public static Image ImageText(string text, int fontSize, Color color)
{ {
using var str1 = text.ToUtf8Buffer(); using Utf8Buffer str1 = text.ToUtf8Buffer();
return ImageText(str1.AsPointer(), fontSize, color); return ImageText(str1.AsPointer(), fontSize, color);
} }
/// <summary>Create an image from text (custom sprite font)</summary> /// <summary>Create an image from text (custom sprite font)</summary>
public static Image ImageTextEx(Font font, string text, float fontSize, float spacing, Color tint) public static Image ImageTextEx(Font font, string text, float fontSize, float spacing, Color tint)
{ {
using var str1 = text.ToUtf8Buffer(); using Utf8Buffer str1 = text.ToUtf8Buffer();
return ImageTextEx(font, str1.AsPointer(), fontSize, spacing, tint); return ImageTextEx(font, str1.AsPointer(), fontSize, spacing, tint);
} }
@ -749,6 +749,22 @@ public static unsafe partial class Raylib
} }
} }
public static void ImageDrawCircleLines(ref Image dst, int centerX, int centerY, int radius, Color color)
{
fixed (Image* p = &dst)
{
ImageDrawCircleLines(p, centerX, centerY, radius, color);
}
}
public static void ImageDrawCircleLinesV(ref Image dst, Vector2 center, int radius, Color color)
{
fixed (Image* p = &dst)
{
ImageDrawCircleLinesV(p, center, radius, color);
}
}
/// <summary>Draw circle within an image (Vector version)</summary> /// <summary>Draw circle within an image (Vector version)</summary>
public static void ImageDrawCircleV(ref Image dst, Vector2 center, int radius, Color color) public static void ImageDrawCircleV(ref Image dst, Vector2 center, int radius, Color color)
{ {
@ -857,7 +873,7 @@ public static unsafe partial class Raylib
/// <summary>Draw text (using default font) within an image (destination)</summary> /// <summary>Draw text (using default font) within an image (destination)</summary>
public static void ImageDrawText(ref Image dst, string text, int x, int y, int fontSize, Color color) public static void ImageDrawText(ref Image dst, string text, int x, int y, int fontSize, Color color)
{ {
using var str1 = text.ToUtf8Buffer(); using Utf8Buffer str1 = text.ToUtf8Buffer();
fixed (Image* p = &dst) fixed (Image* p = &dst)
{ {
ImageDrawText(p, str1.AsPointer(), x, y, fontSize, color); ImageDrawText(p, str1.AsPointer(), x, y, fontSize, color);
@ -875,7 +891,7 @@ public static unsafe partial class Raylib
Color color Color color
) )
{ {
using var str1 = text.ToUtf8Buffer(); using Utf8Buffer str1 = text.ToUtf8Buffer();
fixed (Image* p = &dst) fixed (Image* p = &dst)
{ {
ImageDrawTextEx(p, font, str1.AsPointer(), position, fontSize, spacing, color); ImageDrawTextEx(p, font, str1.AsPointer(), position, fontSize, spacing, color);
@ -885,7 +901,7 @@ public static unsafe partial class Raylib
/// <summary>Load texture from file into GPU memory (VRAM)</summary> /// <summary>Load texture from file into GPU memory (VRAM)</summary>
public static Texture2D LoadTexture(string fileName) public static Texture2D LoadTexture(string fileName)
{ {
using var str1 = fileName.ToAnsiBuffer(); using AnsiBuffer str1 = fileName.ToAnsiBuffer();
return LoadTexture(str1.AsPointer()); return LoadTexture(str1.AsPointer());
} }
@ -931,7 +947,7 @@ public static unsafe partial class Raylib
/// <summary>Load font from file into GPU memory (VRAM)</summary> /// <summary>Load font from file into GPU memory (VRAM)</summary>
public static Font LoadFont(string fileName) public static Font LoadFont(string fileName)
{ {
using var str1 = fileName.ToAnsiBuffer(); using AnsiBuffer str1 = fileName.ToAnsiBuffer();
return LoadFont(str1.AsPointer()); return LoadFont(str1.AsPointer());
} }
@ -941,7 +957,7 @@ public static unsafe partial class Raylib
/// </summary> /// </summary>
public static Font LoadFontEx(string fileName, int fontSize, int[] codepoints, int codepointCount) public static Font LoadFontEx(string fileName, int fontSize, int[] codepoints, int codepointCount)
{ {
using var str1 = fileName.ToAnsiBuffer(); using AnsiBuffer str1 = fileName.ToAnsiBuffer();
fixed (int* p = codepoints) fixed (int* p = codepoints)
{ {
return LoadFontEx(str1.AsPointer(), fontSize, p, codepointCount); return LoadFontEx(str1.AsPointer(), fontSize, p, codepointCount);
@ -959,7 +975,7 @@ public static unsafe partial class Raylib
int codepointCount int codepointCount
) )
{ {
using var fileTypeNative = fileType.ToAnsiBuffer(); using AnsiBuffer fileTypeNative = fileType.ToAnsiBuffer();
fixed (byte* fileDataNative = fileData) fixed (byte* fileDataNative = fileData)
{ {
fixed (int* fontCharsNative = codepoints) fixed (int* fontCharsNative = codepoints)
@ -972,7 +988,6 @@ public static unsafe partial class Raylib
fontCharsNative, fontCharsNative,
codepointCount codepointCount
); );
return font; return font;
} }
} }
@ -1017,7 +1032,7 @@ public static unsafe partial class Raylib
/// <summary>Load model animations from file</summary> /// <summary>Load model animations from file</summary>
public static ModelAnimation* LoadModelAnimations(string fileName, ref int animCount) public static ModelAnimation* LoadModelAnimations(string fileName, ref int animCount)
{ {
using var str1 = fileName.ToAnsiBuffer(); using AnsiBuffer str1 = fileName.ToAnsiBuffer();
fixed (int* p = &animCount) fixed (int* p = &animCount)
{ {
return LoadModelAnimations(str1.AsPointer(), p); return LoadModelAnimations(str1.AsPointer(), p);
@ -1126,21 +1141,14 @@ public static unsafe partial class Raylib
/// <summary>Draw text (using default font)</summary> /// <summary>Draw text (using default font)</summary>
public static void DrawText(string text, int posX, int posY, int fontSize, Color color) public static void DrawText(string text, int posX, int posY, int fontSize, Color color)
{ {
using var str1 = text.ToUtf8Buffer(); using Utf8Buffer str1 = text.ToUtf8Buffer();
DrawText(str1.AsPointer(), posX, posY, fontSize, color); DrawText(str1.AsPointer(), posX, posY, fontSize, color);
} }
/// <summary>Draw text using font and additional parameters</summary> /// <summary>Draw text using font and additional parameters</summary>
public static void DrawTextEx( public static void DrawTextEx(Font font, string text, Vector2 position, float fontSize, float spacing, Color tint)
Font font,
string text,
Vector2 position,
float fontSize,
float spacing,
Color tint
)
{ {
using var str1 = text.ToUtf8Buffer(); using Utf8Buffer str1 = text.ToUtf8Buffer();
DrawTextEx(font, str1.AsPointer(), position, fontSize, spacing, tint); DrawTextEx(font, str1.AsPointer(), position, fontSize, spacing, tint);
} }
@ -1156,32 +1164,32 @@ public static unsafe partial class Raylib
Color tint Color tint
) )
{ {
using var str1 = text.ToUtf8Buffer(); using Utf8Buffer str1 = text.ToUtf8Buffer();
DrawTextPro(font, str1.AsPointer(), position, origin, rotation, fontSize, spacing, tint); DrawTextPro(font, str1.AsPointer(), position, origin, rotation, fontSize, spacing, tint);
} }
/// <summary>Measure string width for default font</summary> /// <summary>Measure string width for default font</summary>
public static int MeasureText(string text, int fontSize) public static int MeasureText(string text, int fontSize)
{ {
using var str1 = text.ToUtf8Buffer(); using Utf8Buffer str1 = text.ToUtf8Buffer();
return MeasureText(str1.AsPointer(), fontSize); return MeasureText(str1.AsPointer(), fontSize);
} }
/// <summary>Measure string size for Font</summary> /// <summary>Measure string size for Font</summary>
public static Vector2 MeasureTextEx(Font font, string text, float fontSize, float spacing) public static Vector2 MeasureTextEx(Font font, string text, float fontSize, float spacing)
{ {
using var str1 = text.ToUtf8Buffer(); using Utf8Buffer str1 = text.ToUtf8Buffer();
return MeasureTextEx(font, str1.AsPointer(), fontSize, spacing); return MeasureTextEx(font, str1.AsPointer(), fontSize, spacing);
} }
/// <summary>Get all codepoints in a string, codepoints count returned by parameters</summary> /// <summary>Get all codepoints in a string, codepoints count returned by parameters</summary>
public static int[] LoadCodepoints(string text, ref int count) public static int[] LoadCodepoints(string text, ref int count)
{ {
using var str1 = text.ToUtf8Buffer(); using Utf8Buffer str1 = text.ToUtf8Buffer();
fixed (int* c = &count) fixed (int* c = &count)
{ {
var pointsPtr = LoadCodepoints(str1.AsPointer(), c); int* pointsPtr = LoadCodepoints(str1.AsPointer(), c);
var codepoints = new ReadOnlySpan<int>(pointsPtr, count).ToArray(); int[] codepoints = new ReadOnlySpan<int>(pointsPtr, count).ToArray();
UnloadCodepoints(pointsPtr); UnloadCodepoints(pointsPtr);
return codepoints; return codepoints;
} }
@ -1190,14 +1198,14 @@ public static unsafe partial class Raylib
/// <summary>Get total number of codepoints in a UTF8 encoded string</summary> /// <summary>Get total number of codepoints in a UTF8 encoded string</summary>
public static int GetCodepointCount(string text) public static int GetCodepointCount(string text)
{ {
using var str1 = text.ToUtf8Buffer(); using Utf8Buffer str1 = text.ToUtf8Buffer();
return GetCodepointCount(str1.AsPointer()); return GetCodepointCount(str1.AsPointer());
} }
/// <summary>Get next codepoint in a UTF-8 encoded string, 0x3f('?') is returned on failure</summary> /// <summary>Get next codepoint in a UTF-8 encoded string, 0x3f('?') is returned on failure</summary>
public static int GetCodepoint(string text, ref int codepointSize) public static int GetCodepoint(string text, ref int codepointSize)
{ {
using var str1 = text.ToUtf8Buffer(); using Utf8Buffer str1 = text.ToUtf8Buffer();
fixed (int* p = &codepointSize) fixed (int* p = &codepointSize)
{ {
return GetCodepoint(str1.AsPointer(), p); return GetCodepoint(str1.AsPointer(), p);
@ -1209,7 +1217,7 @@ public static unsafe partial class Raylib
{ {
fixed (int* l1 = &utf8Size) fixed (int* l1 = &utf8Size)
{ {
var ptr = CodepointToUTF8(codepoint, l1); sbyte* ptr = CodepointToUTF8(codepoint, l1);
return Utf8StringUtils.GetUTF8String(ptr); return Utf8StringUtils.GetUTF8String(ptr);
} }
} }
@ -1219,8 +1227,8 @@ public static unsafe partial class Raylib
{ {
fixed (int* c1 = codepoints) fixed (int* c1 = codepoints)
{ {
var ptr = LoadUTF8(c1, length); sbyte* ptr = LoadUTF8(c1, length);
var text = Utf8StringUtils.GetUTF8String(ptr); string text = Utf8StringUtils.GetUTF8String(ptr);
MemFree(ptr); MemFree(ptr);
return text; return text;
} }
@ -1229,21 +1237,21 @@ public static unsafe partial class Raylib
/// <summary>Draw a model (with texture if set)</summary> /// <summary>Draw a model (with texture if set)</summary>
public static Model LoadModel(string fileName) public static Model LoadModel(string fileName)
{ {
using var str1 = fileName.ToAnsiBuffer(); using AnsiBuffer str1 = fileName.ToAnsiBuffer();
return LoadModel(str1.AsPointer()); return LoadModel(str1.AsPointer());
} }
/// <summary>Export mesh data to file, returns true on success</summary> /// <summary>Export mesh data to file, returns true on success</summary>
public static CBool ExportMesh(Mesh mesh, string fileName) public static CBool ExportMesh(Mesh mesh, string fileName)
{ {
using var str1 = fileName.ToAnsiBuffer(); using AnsiBuffer str1 = fileName.ToAnsiBuffer();
return ExportMesh(mesh, str1.AsPointer()); return ExportMesh(mesh, str1.AsPointer());
} }
/// <summary>Export mesh as code file (.h) defining multiple arrays of vertex attributes</summary> /// <summary>Export mesh as code file (.h) defining multiple arrays of vertex attributes</summary>
public static CBool ExportMeshAsCode(Mesh mesh, string fileName) public static CBool ExportMeshAsCode(Mesh mesh, string fileName)
{ {
using var str1 = fileName.ToAnsiBuffer(); using AnsiBuffer str1 = fileName.ToAnsiBuffer();
return ExportMeshAsCode(mesh, str1.AsPointer()); return ExportMeshAsCode(mesh, str1.AsPointer());
} }
@ -1268,19 +1276,16 @@ public static unsafe partial class Raylib
/// <summary>Load wave data from file</summary> /// <summary>Load wave data from file</summary>
public static Wave LoadWave(string fileName) public static Wave LoadWave(string fileName)
{ {
using var str1 = fileName.ToAnsiBuffer(); using AnsiBuffer str1 = fileName.ToAnsiBuffer();
return LoadWave(str1.AsPointer()); return LoadWave(str1.AsPointer());
} }
/// <summary> /// <summary>
/// Load wave from 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, byte[] fileData)
string fileType,
byte[] fileData
)
{ {
using var fileTypeNative = fileType.ToAnsiBuffer(); using AnsiBuffer fileTypeNative = fileType.ToAnsiBuffer();
fixed (byte* fileDataNative = fileData) fixed (byte* fileDataNative = fileData)
{ {
@ -1297,28 +1302,28 @@ public static unsafe partial class Raylib
/// <summary>Load sound from file</summary> /// <summary>Load sound from file</summary>
public static Sound LoadSound(string fileName) public static Sound LoadSound(string fileName)
{ {
using var str1 = fileName.ToAnsiBuffer(); using AnsiBuffer str1 = fileName.ToAnsiBuffer();
return LoadSound(str1.AsPointer()); return LoadSound(str1.AsPointer());
} }
/// <summary>Export wave data to file</summary> /// <summary>Export wave data to file</summary>
public static CBool ExportWave(Wave wave, string fileName) public static CBool ExportWave(Wave wave, string fileName)
{ {
using var str1 = fileName.ToAnsiBuffer(); using AnsiBuffer str1 = fileName.ToAnsiBuffer();
return ExportWave(wave, str1.AsPointer()); return ExportWave(wave, str1.AsPointer());
} }
/// <summary>Export wave sample data to code (.h)</summary> /// <summary>Export wave sample data to code (.h)</summary>
public static CBool ExportWaveAsCode(Wave wave, string fileName) public static CBool ExportWaveAsCode(Wave wave, string fileName)
{ {
using var str1 = fileName.ToAnsiBuffer(); using AnsiBuffer str1 = fileName.ToAnsiBuffer();
return ExportWaveAsCode(wave, str1.AsPointer()); return ExportWaveAsCode(wave, str1.AsPointer());
} }
/// <summary>Load music stream from file</summary> /// <summary>Load music stream from file</summary>
public static Music LoadMusicStream(string fileName) public static Music LoadMusicStream(string fileName)
{ {
using var str1 = fileName.ToAnsiBuffer(); using AnsiBuffer str1 = fileName.ToAnsiBuffer();
return LoadMusicStream(str1.AsPointer()); return LoadMusicStream(str1.AsPointer());
} }
@ -1330,7 +1335,7 @@ public static unsafe partial class Raylib
byte[] fileData byte[] fileData
) )
{ {
using var fileTypeNative = fileType.ToAnsiBuffer(); using AnsiBuffer fileTypeNative = fileType.ToAnsiBuffer();
fixed (byte* fileDataNative = fileData) fixed (byte* fileDataNative = fileData)
{ {
@ -1408,14 +1413,14 @@ public static unsafe partial class Raylib
/// <summary>Load automation events list from file, NULL for empty list, capacity = MAX_AUTOMATION_EVENTS</summary> /// <summary>Load automation events list from file, NULL for empty list, capacity = MAX_AUTOMATION_EVENTS</summary>
public static AutomationEventList LoadAutomationEventList(string fileName) public static AutomationEventList LoadAutomationEventList(string fileName)
{ {
using var str1 = fileName.ToUtf8Buffer(); using Utf8Buffer str1 = fileName.ToUtf8Buffer();
return LoadAutomationEventList(str1.AsPointer()); return LoadAutomationEventList(str1.AsPointer());
} }
/// <summary>Export automation events list as text file</summary> /// <summary>Export automation events list as text file</summary>
public static CBool ExportAutomationEventList(AutomationEventList list, string fileName) public static CBool ExportAutomationEventList(AutomationEventList list, string fileName)
{ {
using var str1 = fileName.ToUtf8Buffer(); using Utf8Buffer str1 = fileName.ToUtf8Buffer();
return ExportAutomationEventList(list, str1.AsPointer()); return ExportAutomationEventList(list, str1.AsPointer());
} }
@ -1427,4 +1432,20 @@ public static unsafe partial class Raylib
SetAutomationEventList(p); SetAutomationEventList(p);
} }
} }
public static int MakeDirectory(string path)
{
using AnsiBuffer pathBuffer = path.ToAnsiBuffer();
return MakeDirectory(pathBuffer.AsPointer());
}
public static string GetApplicationDirectoryString()
{
return new string(GetApplicationDirectory());
}
public static string GetWorkingDirectoryString()
{
return new string(GetWorkingDirectory());
}
} }

View file

@ -1,5 +1,5 @@
using System;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System;
namespace Raylib_cs; namespace Raylib_cs;