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

Explicit core types and autonomous types. (#304)

* All the "var" variables were changed to explicit types on the raylib class
* "MakeDirectory" overload, and string version of "GetApplicationDirectory" and "GetWorkingDirectory" methods
* Added "Dimensions" property to "Texture2D"
* New overloads for the "LoadFileData" and "SaveFileData" methods
* Raymath's const "NativeLibName" is now referencing to "Raylib.NativeLibName"
* Color constructors are more readable
* Added "Dimensions" property to "Image"
* Changed all the "var" for explicit types on the "Logging" class
* Changed "Int64" for "long" on CBool, and using constructor on the operators instead setting the value manually
* Added indexer to "FilePathLists"
* Changed all "var" for explicit types on "Utf8Buffer"
* New "GetRandomSequence" method overloads in "Raylib.Utils" acepting "int" and "float"
* Added new extension "byte.Lerp"
* Added new extension "Log" for "TraceLogLevel"
* Constructors on Color no longer just truncate the float value, but also round it (+ 0.5f)
* New static method on Color, "Color.Lerp"
* Added static method "FromHSV" to Color
* Added new getter method "GetHSV" on Color
* Added index security on the FilePathList indexer
* Added Rectangle extension
* Added "MoveTowards" extensions to RaylibExtensions
* Added "TranslateLocal" and "TranslateGlobal" to Transform
* Removed unnecessary "partial" declarations
* Updating calls and docs
* Moved the "AudioCallback" delegate into the "AudioMixed.cs" File
* Properties and structs marked as read-only on the "Model.cs" file
* Updated the "README.md" to mentions Raylib-cs targets net6.0 and net8.0 (and also explicitly added that the
"STAThread" attribute comes from the "System" namespace)
* Added a few sizing utils to "Rectangle"
* Added "GetScreenCenter" to "Raylib.Utils"
This commit is contained in:
Matorio 2025-09-21 03:09:38 -05:00 • committed by GitHub
commit 75aabd1cc0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
34 changed files with 602 additions and 196 deletions

View file

@ -10,7 +10,8 @@ C# bindings for raylib, a simple and easy-to-use library to learn videogames pro
[![GitHub stars](https://img.shields.io/github/stars/raylib-cs/raylib-cs?style=social)](https://github.com/raylib-cs/raylib-cs/stargazers)
[![Build](https://github.com/raylib-cs/raylib-cs/workflows/Build/badge.svg)](https://github.com/raylib-cs/raylib-cs/actions?query=workflow%3ABuild)
Raylib-cs targets net6.0 and uses the [official 5.5 release](https://github.com/raysan5/raylib/releases/tag/5.5) to build the native libraries.
Raylib-cs targets net6.0 net8.0 and uses the [official 5.5 release](https://github.com/raysan5/raylib/releases/tag/5.5)
to build the native libraries.
## Status
@ -37,8 +38,8 @@ dotnet add package Raylib-cs
If you need to edit Raylib-cs source then you will need to add the bindings as a project (see below).
If you are new to using NuGet (or you've forgotten) and are trying to run the above command in the command prompt,
remember that you need to be *inside the intended project directory* (not just inside the solution directory) otherwise
the command won't work.
remember that you need to be *inside the intended project directory* (not just inside the solution directory)
otherwise the command won't work.
## Installation - Manual
@ -60,10 +61,10 @@ using Raylib_cs;
namespace HelloWorld;
class Program
internal static class Program
{
// STAThread is required if you deploy using NativeAOT on Windows - See https://github.com/raylib-cs/raylib-cs/issues/301
[STAThread]
[System.STAThread]
public static void Main()
{
Raylib.InitWindow(800, 480, "Hello World");

View file

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<EnableDefaultItems>false</EnableDefaultItems>
@ -49,9 +49,6 @@
<ItemGroup>
<Compile Include="interop\*.cs" />
<Compile Include="types\*.cs" />
<Compile Include="types\native\CBool.cs" />
<Compile Include="types\native\AnsiBuffer.cs" />
<Compile Include="types\native\Utf8Buffer.cs" />
<Compile Include="types\native\FilePathList.cs" />
<Compile Include="types\native\*.cs" />
</ItemGroup>
</Project>

View file

@ -1,7 +1,7 @@
using System;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Numerics;
using System.Security;
using System;
namespace Raylib_cs;

View file

@ -1,5 +1,5 @@
using System.Numerics;
using System.Runtime.InteropServices;
using System.Numerics;
using System.Security;
namespace Raylib_cs;
@ -21,7 +21,7 @@ public static unsafe partial class Raymath
/// <summary>
/// Used by DllImport to load the native library
/// </summary>
public const string NativeLibName = "raylib";
public const string NativeLibName = Raylib.NativeLibName;
/// <summary>Clamp float value</summary>
[DllImport(NativeLibName, CallingConvention = CallingConvention.Cdecl)]
@ -37,12 +37,9 @@ public static unsafe partial class Raymath
/// <summary>Remap input value within input range to output range</summary>
[DllImport(NativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern float Remap(
float value,
float inputStart,
float inputEnd,
float outputStart,
float outputEnd
public static extern float Remap(float value,
float inputStart, float inputEnd,
float outputStart, float outputEnd
);
/// <summary>Wrap input value from min to max</summary>

View file

@ -1,10 +0,0 @@
using System;
namespace Raylib_cs;
/// <summary>
/// Audio stream processor.
/// Used with Raylib.AttachAudioMixedProcessor()
/// and Raylib.DetachAudioMixedProcessor()
/// </summary>
public delegate void AudioCallback<T>(Span<T> buffer);

View file

@ -1,17 +1,22 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System;
namespace Raylib_cs;
/// <summary>
/// Audio stream processor.
/// Used with <see cref="Raylib.AttachAudioMixedProcessor(AudioCallback{float})"/>
/// and <see cref="Raylib.DetachAudioMixedProcessor(AudioCallback{float})"/>
/// </summary>
public delegate void AudioCallback<T>(Span<T> buffer);
internal static unsafe class AudioMixed
{
public static AudioCallback<float> Callback = null;
[UnmanagedCallersOnly(CallConvs = new[]
{
typeof(CallConvCdecl),
})]
[UnmanagedCallersOnly(CallConvs = new Type[]
{ typeof(CallConvCdecl) })]
public static void Processor(void* buffer, uint frames)
{
// The buffer is stereo audio, so we need to double our frame count.

View file

@ -8,9 +8,9 @@ namespace Raylib_cs;
/// NOTE: Useful to create custom audio streams not bound to a specific file
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public partial struct AudioStream
public struct AudioStream
{
//TODO: convert
// TODO: convert
/// <summary>
/// Pointer to internal data(rAudioBuffer *) used by the audio system
/// </summary>

View file

@ -31,5 +31,5 @@ public unsafe struct AutomationEventList
public AutomationEvent* Events;
/// <inheritdoc cref="Events"/>
public ReadOnlySpan<AutomationEvent> EventsAsSpan => new(Events, (int)Count);
public readonly ReadOnlySpan<AutomationEvent> EventsAsSpan => new ReadOnlySpan<AutomationEvent>(Events, (int)Count);
}

View file

@ -7,7 +7,7 @@ namespace Raylib_cs;
/// Camera2D, defines position/orientation in 2d space
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public partial struct Camera2D
public struct Camera2D
{
/// <summary>
/// Camera offset (displacement from target)

View file

@ -28,7 +28,7 @@ public enum CameraProjection
/// Camera3D, defines position/orientation in 3d space
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public partial struct Camera3D
public struct Camera3D
{
/// <summary>
/// Camera position

View file

@ -1,5 +1,5 @@
using System;
using System.Runtime.InteropServices;
using System;
namespace Raylib_cs;
@ -7,7 +7,7 @@ namespace Raylib_cs;
/// Color type, RGBA (32bit)
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public partial struct Color
public struct Color
{
public byte R;
public byte G;
@ -96,28 +96,133 @@ public partial struct Color
/// </summary>
public Color(float r, float g, float b, float a)
{
// X = (byte)Math.Clamp(MathF.Round(x * 255), 0f, 255f);
this.R = (byte)((r < 0) ? 0 : ((r > 1) ? 255 : ((r * 255) + .5f)));
this.G = (byte)((g < 0) ? 0 : ((g > 1) ? 255 : ((g * 255) + .5f)));
this.B = (byte)((b < 0) ? 0 : ((b > 1) ? 255 : ((b * 255) + .5f)));
this.A = (byte)((a < 0) ? 0 : ((a > 1) ? 255 : ((a * 255) + .5f)));
this.R = (byte)(Math.Clamp(r, 0.0f, 1.0f) * 255);
this.G = (byte)(Math.Clamp(g, 0.0f, 1.0f) * 255);
this.B = (byte)(Math.Clamp(b, 0.0f, 1.0f) * 255);
this.A = (byte)(Math.Clamp(a, 0.0f, 1.0f) * 255);
}
/// <summary>
/// <inheritdoc cref="Color(byte, byte, byte)"/>.
/// Accepts <see cref="float"/>'s, upscales and clamps them to the range 0..255.
/// Accepts <see cref="float"/>'s, upscales and clamps them to the range 0...255.
/// Then they are converted to <see cref="byte"/>'s by rounding.
/// </summary>
public Color(float r, float g, float b)
{
// X = (byte)Math.Clamp(MathF.Round(x * 255), 0f, 255f);
this.R = (byte)((r < 0) ? 0 : ((r > 1) ? 255 : ((r * 255) + .5f)));
this.G = (byte)((g < 0) ? 0 : ((g > 1) ? 255 : ((g * 255) + .5f)));
this.B = (byte)((b < 0) ? 0 : ((b > 1) ? 255 : ((b * 255) + .5f)));
this.R = (byte)(Math.Clamp(r, 0.0f, 1.0f) * 255);
this.G = (byte)(Math.Clamp(g, 0.0f, 1.0f) * 255);
this.B = (byte)(Math.Clamp(b, 0.0f, 1.0f) * 255);
this.A = 255;
}
public override string ToString()
public readonly void GetHSV(out float h, out float s, out float v)
{
float rf = this.R / 255f;
float gf = this.G / 255f;
float bf = this.B / 255f;
float max = MathF.Max(rf, MathF.Max(gf, bf));
float min = MathF.Min(rf, MathF.Min(gf, bf));
float delta = max - min;
// Value
v = max;
// Saturation
s = (max == 0f) ? 0f : delta / max;
// Hue
if (delta == 0f)
{
h = 0f;
}
else if (max == rf)
{
h = 60f * (((gf - bf) / delta) % 6f);
}
else if (max == gf)
{
h = 60f * (((bf - rf) / delta) + 2f);
}
else // max == bf
{
h = 60f * (((rf - gf) / delta) + 4f);
}
// Keep h inside 0 and 360
if (h < 0f)
{
h += 360f;
}
}
public static Color FromHSV(float h, float s, float v)
{
float c = v * s;
float x = c * (1f - MathF.Abs((h / 60f) % 2f - 1f));
float m = v - c;
float rf, gf, bf;
if (h < 60f)
{
rf = c;
gf = x;
bf = 0;
}
else if (h < 120f)
{
rf = x;
gf = c;
bf = 0;
}
else if (h < 180f)
{
rf = 0;
gf = c;
bf = x;
}
else if (h < 240f)
{
rf = 0;
gf = x;
bf = c;
}
else if (h < 300f)
{
rf = x;
gf = 0;
bf = c;
}
else
{
rf = c;
gf = 0;
bf = x;
}
byte r = (byte)((rf + m) * 255f + 0.5f);
byte g = (byte)((gf + m) * 255f + 0.5f);
byte b = (byte)((bf + m) * 255f + 0.5f);
return new Color(r, g, b, byte.MaxValue);
}
public static Color Lerp(Color origin, Color target, float t)
{
byte r = LerpB(origin.R, target.R, t);
byte g = LerpB(origin.R, target.G, t);
byte b = LerpB(origin.R, target.B, t);
byte a = LerpB(origin.R, target.A, t);
return new Color(r, g, b, a);
}
// Lerp byte
private static byte LerpB(byte a, byte b, float t)
{
return (byte)(a + (b - a) * t);
}
public readonly override string ToString()
{
return $"{{R:{R} G:{G} B:{B} A:{A}}}";
}

View file

@ -59,7 +59,7 @@ public partial struct GlyphInfo
/// Font, font texture and GlyphInfo array data
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public unsafe partial struct Font
public unsafe struct Font
{
/// <summary>
/// Base size (default chars height)

View file

@ -1,4 +1,5 @@
using System.Runtime.InteropServices;
using System.Numerics;
namespace Raylib_cs;
@ -133,7 +134,7 @@ public enum PixelFormat
/// Image, pixel data stored in CPU memory (RAM)
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public unsafe partial struct Image
public unsafe struct Image
{
/// <summary>
/// Image raw data
@ -159,4 +160,9 @@ public unsafe partial struct Image
/// Data format (PixelFormat type)
/// </summary>
public PixelFormat Format;
/// <summary>
/// Get width and height packed in a Vector2
/// </summary>
public readonly Vector2 Dimensions => new Vector2(Width, Height);
}

View file

@ -29,7 +29,7 @@ internal readonly struct Native
}
[StructLayout(LayoutKind.Sequential, Pack = 4)]
struct VaListLinuxX64
internal struct VaListLinuxX64
{
uint _gpOffset;
uint _fpOffset;
@ -42,10 +42,10 @@ struct VaListLinuxX64
/// </summary>
public static unsafe class Logging
{
[UnmanagedCallersOnly(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })]
[UnmanagedCallersOnly(CallConvs = new Type[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })]
public static unsafe void LogConsole(int msgType, sbyte* text, sbyte* args)
{
var message = GetLogMessage(new IntPtr(text), new IntPtr(args));
string message = GetLogMessage(new IntPtr(text), new IntPtr(args));
Console.WriteLine(message);
}
@ -62,13 +62,13 @@ public static unsafe class Logging
return LinuxX64LogCallback(format, args);
}
var byteLength = VsnPrintf(IntPtr.Zero, UIntPtr.Zero, format, args) + 1;
int byteLength = VsnPrintf(IntPtr.Zero, UIntPtr.Zero, format, args) + 1;
if (byteLength <= 1)
{
return string.Empty;
}
var buffer = Marshal.AllocHGlobal(byteLength);
IntPtr buffer = Marshal.AllocHGlobal(byteLength);
VsPrintf(buffer, format, args);
string result = Marshal.PtrToStringUTF8(buffer);
@ -77,12 +77,12 @@ public static unsafe class Logging
return result;
}
static string AppleLogCallback(IntPtr format, IntPtr args)
private static string AppleLogCallback(IntPtr format, IntPtr args)
{
IntPtr buffer = IntPtr.Zero;
try
{
var count = Native.VasPrintfApple(ref buffer, format, args);
int count = Native.VasPrintfApple(ref buffer, format, args);
if (count == -1)
{
return string.Empty;
@ -95,10 +95,10 @@ public static unsafe class Logging
}
}
static string LinuxX64LogCallback(IntPtr format, IntPtr args)
private static string LinuxX64LogCallback(IntPtr format, IntPtr args)
{
// The args pointer cannot be reused between two calls. We need to make a copy of the underlying structure.
var listStructure = Marshal.PtrToStructure<VaListLinuxX64>(args);
VaListLinuxX64 listStructure = Marshal.PtrToStructure<VaListLinuxX64>(args);
IntPtr listPointer = IntPtr.Zero;
int byteLength = 0;
string result = "";
@ -125,9 +125,9 @@ public static unsafe class Logging
}
// https://github.com/dotnet/runtime/issues/51052
static int VsnPrintf(IntPtr buffer, UIntPtr size, IntPtr format, IntPtr args)
private static int VsnPrintf(IntPtr buffer, UIntPtr size, IntPtr format, IntPtr args)
{
var os = Environment.OSVersion;
OperatingSystem os = Environment.OSVersion;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return Native.VsnPrintfWindows(buffer, size, format, args);
@ -144,7 +144,7 @@ public static unsafe class Logging
}
// https://github.com/dotnet/runtime/issues/51052
static int VsPrintf(IntPtr buffer, IntPtr format, IntPtr args)
private static int VsPrintf(IntPtr buffer, IntPtr format, IntPtr args)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{

View file

@ -48,7 +48,7 @@ public enum MaterialMapIndex
/// Material texture map
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public partial struct MaterialMap
public struct MaterialMap
{
/// <summary>
/// Material map texture
@ -70,14 +70,14 @@ public partial struct MaterialMap
/// Material type (generic)
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public unsafe partial struct Material
public unsafe struct Material
{
/// <summary>
/// Material shader
/// </summary>
public Shader Shader;
//TODO: convert
// TODO: convert
/// <summary>
/// Material maps
/// </summary>

View file

@ -9,7 +9,7 @@ namespace Raylib_cs;
/// NOTE: Data stored in CPU memory (and GPU)
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public unsafe partial struct Mesh
public unsafe struct Mesh
{
/// <summary>
/// Creates a mesh ready for default vertex data allocation
@ -136,7 +136,7 @@ public unsafe partial struct Mesh
/// </summary>
public readonly Span<T> TexCoordsAs<T>() where T : unmanaged
{
return new(TexCoords, 2 * VertexCount * sizeof(float) / sizeof(T));
return new Span<T>(TexCoords, 2 * VertexCount * sizeof(float) / sizeof(T));
}
/// <summary>
@ -144,7 +144,7 @@ public unsafe partial struct Mesh
/// </summary>
public readonly Span<T> TexCoords2As<T>() where T : unmanaged
{
return new(TexCoords2, 2 * VertexCount * sizeof(float) / sizeof(T));
return new Span<T>(TexCoords2, 2 * VertexCount * sizeof(float) / sizeof(T));
}
/// <summary>
@ -152,7 +152,7 @@ public unsafe partial struct Mesh
/// </summary>
public readonly Span<T> NormalsAs<T>() where T : unmanaged
{
return new(Normals, 3 * VertexCount * sizeof(float) / sizeof(T));
return new Span<T>(Normals, 3 * VertexCount * sizeof(float) / sizeof(T));
}
/// <summary>
@ -160,7 +160,7 @@ public unsafe partial struct Mesh
/// </summary>
public readonly Span<T> TangentsAs<T>() where T : unmanaged
{
return new(Tangents, 4 * VertexCount * sizeof(float) / sizeof(T));
return new Span<T>(Tangents, 4 * VertexCount * sizeof(float) / sizeof(T));
}
/// <summary>
@ -168,7 +168,7 @@ public unsafe partial struct Mesh
/// </summary>
public readonly Span<T> ColorsAs<T>() where T : unmanaged
{
return new(Colors, 4 * VertexCount * sizeof(byte) / sizeof(T));
return new Span<T>(Colors, 4 * VertexCount * sizeof(byte) / sizeof(T));
}
/// <summary>
@ -176,7 +176,7 @@ public unsafe partial struct Mesh
/// </summary>
public readonly Span<T> IndicesAs<T>() where T : unmanaged
{
return new(Indices, 3 * TriangleCount * sizeof(ushort) / sizeof(T));
return new Span<T>(Indices, 3 * TriangleCount * sizeof(ushort) / sizeof(T));
}
#endregion

View file

@ -8,7 +8,7 @@ namespace Raylib_cs;
/// Bone information
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public unsafe partial struct BoneInfo
public unsafe struct BoneInfo
{
/// <summary>
/// Bone name (char[32])
@ -25,7 +25,7 @@ public unsafe partial struct BoneInfo
/// Model type
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public unsafe partial struct Model
public unsafe struct Model
{
/// <summary>
/// Local transform matrix
@ -79,7 +79,7 @@ public unsafe partial struct Model
/// Model animation
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public unsafe partial struct ModelAnimation
public unsafe struct ModelAnimation
{
/// <summary>
/// Number of bones
@ -97,7 +97,7 @@ public unsafe partial struct ModelAnimation
public readonly BoneInfo* Bones;
/// <inheritdoc cref="Bones"/>
public ReadOnlySpan<BoneInfo> BoneInfo => new(Bones, BoneCount);
public readonly ReadOnlySpan<BoneInfo> BoneInfo => new ReadOnlySpan<BoneInfo>(Bones, BoneCount);
/// <summary>
/// Poses array by frame (Transform **)
@ -110,9 +110,9 @@ public unsafe partial struct ModelAnimation
public fixed sbyte Name[32];
/// <inheritdoc cref="FramePoses"/>
public FramePosesCollection FramePosesColl => new(FramePoses, FrameCount, BoneCount);
public readonly FramePosesCollection FramePosesColl => new FramePosesCollection(FramePoses, FrameCount, BoneCount);
public struct FramePosesCollection
public readonly struct FramePosesCollection
{
readonly Transform** _framePoses;
@ -120,9 +120,9 @@ public unsafe partial struct ModelAnimation
readonly int _boneCount;
public FramePoses this[int index] => new(_framePoses[index], _boneCount);
public readonly FramePoses this[int index] => new FramePoses(_framePoses[index], _boneCount);
public Transform this[int index1, int index2] => new FramePoses(_framePoses[index1], _boneCount)[index2];
public readonly Transform this[int index1, int index2] => new FramePoses(_framePoses[index1], _boneCount)[index2];
internal FramePosesCollection(Transform** framePoses, int frameCount, int boneCount)
{
@ -133,13 +133,13 @@ public unsafe partial struct ModelAnimation
}
}
public unsafe struct FramePoses
public readonly unsafe struct FramePoses
{
readonly Transform* _poses;
readonly int _count;
public ref Transform this[int index] => ref _poses[index];
public readonly ref Transform this[int index] => ref _poses[index];
internal FramePoses(Transform* poses, int count)
{

View file

@ -7,7 +7,7 @@ namespace Raylib_cs;
/// NOTE: Anything longer than ~10 seconds should be streamed
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public unsafe partial struct Music
public unsafe struct Music
{
/// <summary>
/// Audio stream
@ -29,7 +29,7 @@ public unsafe partial struct Music
/// </summary>
public int CtxType;
//TODO span
// TODO span
/// <summary>
/// Audio context data, depends on type
/// </summary>

View file

@ -27,7 +27,7 @@ public enum NPatchLayout
/// N-Patch layout info
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public partial struct NPatchInfo
public struct NPatchInfo
{
/// <summary>
/// Texture source rectangle

View file

@ -7,7 +7,7 @@ namespace Raylib_cs;
/// Ray, ray for raycasting
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public partial struct Ray
public struct Ray
{
/// <summary>
/// Ray position (origin)
@ -30,7 +30,7 @@ public partial struct Ray
/// Raycast hit information
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public partial struct RayCollision
public struct RayCollision
{
/// <summary>
/// Did the ray hit something?

Some files were not shown because too many files have changed in this diff Show more