mirror of
https://github.com/raylib-cs/raylib-cs
synced 2025-06-30 19:03:42 -04:00
Add .editorconfig and apply formatting
This commit is contained in:
@ -1,36 +1,35 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Raylib_cs
|
||||
namespace Raylib_cs;
|
||||
|
||||
/// <summary>
|
||||
/// Converts text to a Ansi buffer for passing to native code
|
||||
/// </summary>
|
||||
public readonly ref struct AnsiBuffer
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts text to a Ansi buffer for passing to native code
|
||||
/// </summary>
|
||||
public readonly ref struct AnsiBuffer
|
||||
private readonly IntPtr _data;
|
||||
|
||||
public AnsiBuffer(string text)
|
||||
{
|
||||
private readonly IntPtr _data;
|
||||
|
||||
public AnsiBuffer(string text)
|
||||
{
|
||||
_data = Marshal.StringToHGlobalAnsi(text);
|
||||
}
|
||||
|
||||
public unsafe sbyte* AsPointer()
|
||||
{
|
||||
return (sbyte*)_data.ToPointer();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Marshal.FreeHGlobal(_data);
|
||||
}
|
||||
_data = Marshal.StringToHGlobalAnsi(text);
|
||||
}
|
||||
|
||||
public static class AnsiStringUtils
|
||||
public unsafe sbyte* AsPointer()
|
||||
{
|
||||
public static AnsiBuffer ToAnsiBuffer(this string text)
|
||||
{
|
||||
return new AnsiBuffer(text);
|
||||
}
|
||||
return (sbyte*)_data.ToPointer();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Marshal.FreeHGlobal(_data);
|
||||
}
|
||||
}
|
||||
|
||||
public static class AnsiStringUtils
|
||||
{
|
||||
public static AnsiBuffer ToAnsiBuffer(this string text)
|
||||
{
|
||||
return new AnsiBuffer(text);
|
||||
}
|
||||
}
|
||||
|
@ -1,87 +1,89 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Raylib_cs
|
||||
namespace Raylib_cs;
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public readonly struct CBool
|
||||
{
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public readonly struct CBool
|
||||
/* The values of booleans in C++ are stored in a single byte, which means it
|
||||
* only supports values from -128 to 127. It is possible to argument that
|
||||
* they only support a single bit, yes, but the minimum storage unit of a
|
||||
* computer is a sbyte, so that's what we'll be using when doing operations,
|
||||
* which can be later implicitely cast onto any type during usage.
|
||||
*
|
||||
* It is wise to note that C booleans are any numeric value, but allocating an
|
||||
* Int64 for every CBool instance is.. well, wildly memory-inefficient. Yes, the
|
||||
* process is basically treated as a 0-cost instantiation, but it's better to rely
|
||||
* on explicit motivation than to blindly trust the runtime judgement on its memory
|
||||
* management.
|
||||
*
|
||||
* 'value' is visible and constructable (if necessary), but impossible to modify or access.
|
||||
*/
|
||||
public sbyte Value
|
||||
{
|
||||
/* The values of booleans in C++ are stored in a single byte, which means it
|
||||
* only supports values from -128 to 127. It is possible to argument that
|
||||
* they only support a single bit, yes, but the minimum storage unit of a
|
||||
* computer is a sbyte, so that's what we'll be using when doing operations,
|
||||
* which can be later implicitely cast onto any type during usage.
|
||||
*
|
||||
* It is wise to note that C booleans are any numeric value, but allocating an
|
||||
* Int64 for every CBool instance is.. well, wildly memory-inefficient. Yes, the
|
||||
* process is basically treated as a 0-cost instantiation, but it's better to rely
|
||||
* on explicit motivation than to blindly trust the runtime judgement on its memory
|
||||
* management.
|
||||
*
|
||||
* 'value' is visible and constructable (if necessary), but impossible to modify or access.
|
||||
*/
|
||||
public sbyte Value { init; private get; }
|
||||
init; private get;
|
||||
}
|
||||
|
||||
// Constructors for easier usage.
|
||||
public CBool(bool value)
|
||||
{
|
||||
this.Value = (sbyte)(value ? 1 : 0);
|
||||
}
|
||||
public CBool(Int64 value)
|
||||
{
|
||||
this.Value = (sbyte)(value != 0 ? 1 : 0);
|
||||
}
|
||||
// Constructors for easier usage.
|
||||
public CBool(bool value)
|
||||
{
|
||||
this.Value = (sbyte)(value ? 1 : 0);
|
||||
}
|
||||
public CBool(Int64 value)
|
||||
{
|
||||
this.Value = (sbyte)(value != 0 ? 1 : 0);
|
||||
}
|
||||
|
||||
// CBool -> Native
|
||||
// Allows for arithmetic between CBools and for assignment to greater integer variables.
|
||||
public static implicit operator sbyte(CBool x)
|
||||
{
|
||||
return x.Value;
|
||||
}
|
||||
// CBool -> Native
|
||||
// Allows for arithmetic between CBools and for assignment to greater integer variables.
|
||||
public static implicit operator sbyte(CBool x)
|
||||
{
|
||||
return x.Value;
|
||||
}
|
||||
|
||||
// Allows for CBools to be implicitely assigned to a native boolean variable.
|
||||
public static implicit operator bool(CBool x)
|
||||
{
|
||||
return x.Value != 0 ? true : false;
|
||||
}
|
||||
// Allows for CBools to be implicitely assigned to a native boolean variable.
|
||||
public static implicit operator bool(CBool x)
|
||||
{
|
||||
return x.Value != 0 ? true : false;
|
||||
}
|
||||
|
||||
// Native -> CBool
|
||||
// Allows native booleans to be implicitely constructed into CBools while passing parameters.
|
||||
public static implicit operator CBool(bool x)
|
||||
{
|
||||
return new CBool { Value = (sbyte)(x ? 1 : 0) };
|
||||
}
|
||||
// Native -> CBool
|
||||
// Allows native booleans to be implicitely constructed into CBools while passing parameters.
|
||||
public static implicit operator CBool(bool x)
|
||||
{
|
||||
return new CBool { Value = (sbyte)(x ? 1 : 0) };
|
||||
}
|
||||
|
||||
// Same goes for integer numeric values (any value, so an Int64 is used).
|
||||
public static implicit operator CBool(Int64 x)
|
||||
{
|
||||
return new CBool { Value = (sbyte)(x != 0 ? 1 : 0) };
|
||||
}
|
||||
// Same goes for integer numeric values (any value, so an Int64 is used).
|
||||
public static implicit operator CBool(Int64 x)
|
||||
{
|
||||
return new CBool { Value = (sbyte)(x != 0 ? 1 : 0) };
|
||||
}
|
||||
|
||||
/* Arithmetic overloads
|
||||
* Operations between CBools and integers are already covered by the implicit
|
||||
* sbyte cast. So no need to worry about those.
|
||||
*
|
||||
* All casts return CBool, since there is no way to know if the assignment is
|
||||
* to a native boolean or integer, or a CBool.
|
||||
*/
|
||||
/* Arithmetic overloads
|
||||
* Operations between CBools and integers are already covered by the implicit
|
||||
* sbyte cast. So no need to worry about those.
|
||||
*
|
||||
* All casts return CBool, since there is no way to know if the assignment is
|
||||
* to a native boolean or integer, or a CBool.
|
||||
*/
|
||||
|
||||
// Addition
|
||||
public static CBool operator +(CBool left, CBool right)
|
||||
{
|
||||
return new CBool { Value = (sbyte)(left.Value + right.Value) };
|
||||
}
|
||||
// Addition
|
||||
public static CBool operator +(CBool left, CBool right)
|
||||
{
|
||||
return new CBool { Value = (sbyte)(left.Value + right.Value) };
|
||||
}
|
||||
|
||||
// Subtraction
|
||||
public static CBool operator -(CBool left, CBool right)
|
||||
{
|
||||
return new CBool { Value = (sbyte)(left.Value - right.Value) };
|
||||
}
|
||||
// Subtraction
|
||||
public static CBool operator -(CBool left, CBool right)
|
||||
{
|
||||
return new CBool { Value = (sbyte)(left.Value - right.Value) };
|
||||
}
|
||||
|
||||
// ToString override
|
||||
public override string ToString()
|
||||
{
|
||||
return ((bool)this).ToString();
|
||||
}
|
||||
// ToString override
|
||||
public override string ToString()
|
||||
{
|
||||
return ((bool)this).ToString();
|
||||
}
|
||||
}
|
||||
|
@ -1,26 +1,25 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Raylib_cs
|
||||
namespace Raylib_cs;
|
||||
|
||||
/// <summary>
|
||||
/// File path list
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public unsafe struct FilePathList
|
||||
{
|
||||
/// <summary>
|
||||
/// File path list
|
||||
/// Filepaths max entries
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public unsafe struct FilePathList
|
||||
{
|
||||
/// <summary>
|
||||
/// Filepaths max entries
|
||||
/// </summary>
|
||||
public uint Capacity;
|
||||
public uint Capacity;
|
||||
|
||||
/// <summary>
|
||||
/// Filepaths entries count
|
||||
/// </summary>
|
||||
public uint Count;
|
||||
/// <summary>
|
||||
/// Filepaths entries count
|
||||
/// </summary>
|
||||
public uint Count;
|
||||
|
||||
/// <summary>
|
||||
/// Filepaths entries
|
||||
/// </summary>
|
||||
public byte** Paths;
|
||||
}
|
||||
/// <summary>
|
||||
/// Filepaths entries
|
||||
/// </summary>
|
||||
public byte** Paths;
|
||||
}
|
||||
|
@ -1,63 +1,62 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace Raylib_cs
|
||||
namespace Raylib_cs;
|
||||
|
||||
/// <summary>
|
||||
/// Converts text to a UTF8 buffer for passing to native code
|
||||
/// </summary>
|
||||
public readonly ref struct Utf8Buffer
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts text to a UTF8 buffer for passing to native code
|
||||
/// </summary>
|
||||
public readonly ref struct Utf8Buffer
|
||||
private readonly IntPtr _data;
|
||||
|
||||
public Utf8Buffer(string text)
|
||||
{
|
||||
private readonly IntPtr _data;
|
||||
|
||||
public Utf8Buffer(string text)
|
||||
{
|
||||
_data = Marshal.StringToCoTaskMemUTF8(text);
|
||||
}
|
||||
|
||||
public unsafe sbyte* AsPointer()
|
||||
{
|
||||
return (sbyte*)_data.ToPointer();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Marshal.ZeroFreeCoTaskMemUTF8(_data);
|
||||
}
|
||||
_data = Marshal.StringToCoTaskMemUTF8(text);
|
||||
}
|
||||
|
||||
public static class Utf8StringUtils
|
||||
public unsafe sbyte* AsPointer()
|
||||
{
|
||||
public static Utf8Buffer ToUtf8Buffer(this string text)
|
||||
{
|
||||
return new Utf8Buffer(text);
|
||||
}
|
||||
return (sbyte*)_data.ToPointer();
|
||||
}
|
||||
|
||||
public static byte[] ToUtf8String(this string text)
|
||||
{
|
||||
if (text == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var length = Encoding.UTF8.GetByteCount(text);
|
||||
|
||||
var byteArray = new byte[length + 1];
|
||||
var wrote = Encoding.UTF8.GetBytes(text, 0, text.Length, byteArray, 0);
|
||||
byteArray[wrote] = 0;
|
||||
|
||||
return byteArray;
|
||||
}
|
||||
|
||||
public static unsafe string GetUTF8String(sbyte* bytes)
|
||||
{
|
||||
return Marshal.PtrToStringUTF8((IntPtr)bytes);
|
||||
}
|
||||
|
||||
public static byte[] GetUTF8Bytes(this string text)
|
||||
{
|
||||
return Encoding.UTF8.GetBytes(text);
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
Marshal.ZeroFreeCoTaskMemUTF8(_data);
|
||||
}
|
||||
}
|
||||
|
||||
public static class Utf8StringUtils
|
||||
{
|
||||
public static Utf8Buffer ToUtf8Buffer(this string text)
|
||||
{
|
||||
return new Utf8Buffer(text);
|
||||
}
|
||||
|
||||
public static byte[] ToUtf8String(this string text)
|
||||
{
|
||||
if (text == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var length = Encoding.UTF8.GetByteCount(text);
|
||||
|
||||
var byteArray = new byte[length + 1];
|
||||
var wrote = Encoding.UTF8.GetBytes(text, 0, text.Length, byteArray, 0);
|
||||
byteArray[wrote] = 0;
|
||||
|
||||
return byteArray;
|
||||
}
|
||||
|
||||
public static unsafe string GetUTF8String(sbyte* bytes)
|
||||
{
|
||||
return Marshal.PtrToStringUTF8((IntPtr)bytes);
|
||||
}
|
||||
|
||||
public static byte[] GetUTF8Bytes(this string text)
|
||||
{
|
||||
return Encoding.UTF8.GetBytes(text);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user