From 3e8e4a63192ae8b23b7a4231eb8cb9354ba3034d Mon Sep 17 00:00:00 2001 From: ChrisDill Date: Fri, 17 Dec 2021 12:48:56 +0000 Subject: [PATCH] Multi-target net5.0 --- .github/workflows/build.yml | 4 +- README.md | 2 +- Raylib-cs.Tests/RaylibTests.cs | 4 +- Raylib-cs/Raylib-cs.csproj | 2 +- Raylib-cs/Raylib.Utils.cs | 20 +- Raylib-cs/types/native/CBool.cs | 43 ++-- Raylib-cs/types/native/Utf8String.cs | 369 ++++++++++++++------------- 7 files changed, 232 insertions(+), 212 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index d951004..1abaf9f 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -20,9 +20,9 @@ jobs: with: dotnet-version: 6.0.x - name: Build project - run: dotnet build -c Release + run: dotnet build -c Release -f net6.0 - name: Test project - run: dotnet test + run: dotnet test -c Release -f net6.0 publish: name: Build Release Package runs-on: ubuntu-latest diff --git a/README.md b/README.md index fe908d6..3e2245d 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ C# bindings for raylib 4.0.0, a simple and easy-to-use library to learn videogam [![Build](https://github.com/ChrisDill/Raylib-cs/workflows/Build/badge.svg)](https://github.com/ChrisDill/Raylib-cs/actions?query=workflow%3ABuild) -Raylib-cs targets net6.0. +Raylib-cs targets net5.0 and net6.0. ## Installation - NuGet diff --git a/Raylib-cs.Tests/RaylibTests.cs b/Raylib-cs.Tests/RaylibTests.cs index 8222fc1..61c3390 100644 --- a/Raylib-cs.Tests/RaylibTests.cs +++ b/Raylib-cs.Tests/RaylibTests.cs @@ -62,7 +62,7 @@ namespace Raylib_cs.Tests int count = 0; string input = "aàáâãäāăąȧXǎȁȃeèéêẽëē"; int[] codepoints1 = Raylib.LoadCodepoints(input, ref count); - + for (int i = 0; i < input.Length; i++) { Assert.Equal(input[i], codepoints1[i]); @@ -81,7 +81,7 @@ namespace Raylib_cs.Tests public void TextCodepointsToUTF8() { string input = "aàáâãäāăąȧXǎȁȃeèéêẽëē"; - + int count = 0; int[] codepoints1 = Raylib.LoadCodepoints(input, ref count); diff --git a/Raylib-cs/Raylib-cs.csproj b/Raylib-cs/Raylib-cs.csproj index e1aa0e3..ce99a73 100644 --- a/Raylib-cs/Raylib-cs.csproj +++ b/Raylib-cs/Raylib-cs.csproj @@ -1,6 +1,6 @@ - net6.0 + net5.0;net6.0 false Raylib-cs Raylib_cs diff --git a/Raylib-cs/Raylib.Utils.cs b/Raylib-cs/Raylib.Utils.cs index 66d24da..c67dcc7 100644 --- a/Raylib-cs/Raylib.Utils.cs +++ b/Raylib-cs/Raylib.Utils.cs @@ -97,7 +97,7 @@ namespace Raylib_cs return ImageTextEx(font, p, fontSize, spacing, tint); } } - + public static void ImageToPOT(ref Image image, Color fill) { fixed (Image* p = &image) @@ -274,6 +274,24 @@ namespace Raylib_cs } } + /// + public static void ImageDrawPixel(ref Image dst, int posX, int posY, Color color) + { + fixed (Image* p = &dst) + { + ImageDrawPixel(p, posX, posY, color); + } + } + + /// + public static void ImageDrawPixelV(ref Image dst, Vector2 position, Color color) + { + fixed (Image* p = &dst) + { + ImageDrawPixelV(p, position, color); + } + } + /// public static void ImageDrawText(ref Image dst, Utf8String text, int x, int y, int fontSize, Color color) { diff --git a/Raylib-cs/types/native/CBool.cs b/Raylib-cs/types/native/CBool.cs index 3fc3b06..11dd92f 100644 --- a/Raylib-cs/types/native/CBool.cs +++ b/Raylib-cs/types/native/CBool.cs @@ -1,30 +1,31 @@ using System; using System.Runtime.InteropServices; -namespace Raylib_cs; - -[StructLayout(LayoutKind.Sequential)] -public readonly struct CBool +namespace Raylib_cs { - private readonly byte value; - - private CBool(bool value) + [StructLayout(LayoutKind.Sequential)] + public readonly struct CBool { - this.value = Convert.ToByte(value); - } + private readonly byte value; - public static implicit operator CBool(bool value) - { - return new CBool(value); - } + private CBool(bool value) + { + this.value = Convert.ToByte(value); + } - public static implicit operator bool(CBool x) - { - return Convert.ToBoolean(x.value); - } + public static implicit operator CBool(bool value) + { + return new CBool(value); + } - public override string ToString() - { - return Convert.ToBoolean(value).ToString(); + public static implicit operator bool(CBool x) + { + return Convert.ToBoolean(x.value); + } + + public override string ToString() + { + return Convert.ToBoolean(value).ToString(); + } } -} \ No newline at end of file +} diff --git a/Raylib-cs/types/native/Utf8String.cs b/Raylib-cs/types/native/Utf8String.cs index cad8f5d..b50c8e6 100644 --- a/Raylib-cs/types/native/Utf8String.cs +++ b/Raylib-cs/types/native/Utf8String.cs @@ -1,209 +1,210 @@ using System; using System.Text; -namespace Raylib_cs; - -#region LICENSE - -// ** -- TAKEN FROM SQLitePCL.raw -- ** \\ -/* https://github.com/ericsink/SQLitePCL.raw */ -/* - Copyright 2014-2021 SourceGear, LLC - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -// CHANGES: -// - Update Method Names -// - Implicit Conversion From string - -#endregion - -/// -/// A raw string representation suitable for passing into many core SQLite apis. These will normally be pointers to -/// utf8 encoded bytes, with a trailing \0 terminator. strings can be represented as -/// well as empty strings. -/// -public readonly ref struct Utf8String +namespace Raylib_cs { - // this span will contain a zero terminator byte - // if sp.Length is 0, it represents a null string - // if sp.Length is 1, the only byte must be zero, and it is an empty string - readonly ReadOnlySpan sp; + #region LICENSE - public ref readonly byte GetPinnableReference() - { - return ref sp.GetPinnableReference(); - } + // ** -- TAKEN FROM SQLitePCL.raw -- ** \\ + /* https://github.com/ericsink/SQLitePCL.raw */ + /* + Copyright 2014-2021 SourceGear, LLC + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + */ - private Utf8String(ReadOnlySpan a) - { - // no check here. anything that calls this - // constructor must make assurances about the - // zero terminator. - sp = a; - } + // CHANGES: + // - Update Method Names + // - Implicit Conversion From string + + #endregion /// - /// Creates a new instance of which directly points at the memory pointed to by . The span must contain a valid encoded block of memory that - /// terminates with a \0 byte. The span passed in must include the \0 terminator. - /// - /// Both and empty strings can be created here. To create a string, - /// pass in an empty . To create an empty string, pass in a span with length 1, that - /// only contains a \0 + /// A raw string representation suitable for passing into many core SQLite apis. These will normally be pointers to + /// utf8 encoded bytes, with a trailing \0 terminator. strings can be represented as + /// well as empty strings. /// - /// - /// Thrown if span.Length > 0 and span[^1] is not \0. - /// - public static Utf8String FromSpan(ReadOnlySpan span) + public readonly ref struct Utf8String { - if ( - span.Length > 0 - && span[^1] != 0 - ) + // this span will contain a zero terminator byte + // if sp.Length is 0, it represents a null string + // if sp.Length is 1, the only byte must be zero, and it is an empty string + readonly ReadOnlySpan sp; + + public ref readonly byte GetPinnableReference() { - throw new ArgumentException("zero terminator required"); + return ref sp.GetPinnableReference(); } - return new Utf8String(span); - } - - public static Utf8String FromString(string s) - { - if (s == null) + private Utf8String(ReadOnlySpan a) { - return new Utf8String(ReadOnlySpan.Empty); + // no check here. anything that calls this + // constructor must make assurances about the + // zero terminator. + sp = a; } - return new Utf8String(s.ToUtf8String()); - } - - static unsafe long GetLength(byte* p) - { - var q = p; - while (*q != 0) + /// + /// Creates a new instance of which directly points at the memory pointed to by . The span must contain a valid encoded block of memory that + /// terminates with a \0 byte. The span passed in must include the \0 terminator. + /// + /// Both and empty strings can be created here. To create a string, + /// pass in an empty . To create an empty string, pass in a span with length 1, that + /// only contains a \0 + /// + /// + /// Thrown if span.Length > 0 and span[^1] is not \0. + /// + public static Utf8String FromSpan(ReadOnlySpan span) { - q++; - } - - return q - p; - } - - static unsafe ReadOnlySpan FindZeroTerminator(byte* p) - { - var len = (int)GetLength(p); - return new ReadOnlySpan(p, len + 1); - } - - /// - /// Creates a new instance of which directly points at the memory pointed to by . The pointer must either be or point to a valid encoded block of memory that terminates with a \0 byte. - /// - public static unsafe Utf8String FromPtr(byte* p) - { - return p == null ? new Utf8String(ReadOnlySpan.Empty) : new Utf8String(FindZeroTerminator(p)); - } - - // TODO maybe remove this and just use FromSpan? - - /// - /// Creates a new instance of which directly points at the memory pointed to by with length . The pointer must be to a valid - /// encoded block of memory that terminates with a \0 byte. The value refers to - /// the number of bytes in the utf8 encoded value not including the \0 byte terminator. - /// - /// can be , in which case is ignored - /// and a new instance is created that represents . Note that this - /// different from a pointer to a single \0 byte and a length of one. That would represent an empty string. - /// - /// - /// Thrown if is not and p[len] is not \0. - /// - public static unsafe Utf8String FromPtrLen(byte* p, int len) - { - if (p == null) - { - return new Utf8String(ReadOnlySpan.Empty); - } - - // the given len does NOT include the zero terminator - var sp = new ReadOnlySpan(p, len + 1); - return FromSpan(sp); - } - - public static unsafe Utf8String FromIntPtr(IntPtr p) - { - return p == IntPtr.Zero ? new Utf8String(ReadOnlySpan.Empty) : new Utf8String(FindZeroTerminator((byte*)p.ToPointer())); - } - - /// - /// UTF16 String representation - /// Returns "NUL" on Null native type - /// - /// - public override string ToString() - { - if (sp.Length == 0) - { - // object.ToString() should never thrown - // throw new NullReferenceException(); - return "NUL"; - } - - unsafe - { - fixed (byte* q = sp) + if ( + span.Length > 0 + && span[^1] != 0 + ) { - return Encoding.UTF8.GetString(q, sp.Length - 1); + throw new ArgumentException("zero terminator required"); + } + + return new Utf8String(span); + } + + public static Utf8String FromString(string s) + { + if (s == null) + { + return new Utf8String(ReadOnlySpan.Empty); + } + + return new Utf8String(s.ToUtf8String()); + } + + static unsafe long GetLength(byte* p) + { + var q = p; + while (*q != 0) + { + q++; + } + + return q - p; + } + + static unsafe ReadOnlySpan FindZeroTerminator(byte* p) + { + var len = (int)GetLength(p); + return new ReadOnlySpan(p, len + 1); + } + + /// + /// Creates a new instance of which directly points at the memory pointed to by . The pointer must either be or point to a valid encoded block of memory that terminates with a \0 byte. + /// + public static unsafe Utf8String FromPtr(byte* p) + { + return p == null ? new Utf8String(ReadOnlySpan.Empty) : new Utf8String(FindZeroTerminator(p)); + } + + // TODO maybe remove this and just use FromSpan? + + /// + /// Creates a new instance of which directly points at the memory pointed to by with length . The pointer must be to a valid + /// encoded block of memory that terminates with a \0 byte. The value refers to + /// the number of bytes in the utf8 encoded value not including the \0 byte terminator. + /// + /// can be , in which case is ignored + /// and a new instance is created that represents . Note that this + /// different from a pointer to a single \0 byte and a length of one. That would represent an empty string. + /// + /// + /// Thrown if is not and p[len] is not \0. + /// + public static unsafe Utf8String FromPtrLen(byte* p, int len) + { + if (p == null) + { + return new Utf8String(ReadOnlySpan.Empty); + } + + // the given len does NOT include the zero terminator + var sp = new ReadOnlySpan(p, len + 1); + return FromSpan(sp); + } + + public static unsafe Utf8String FromIntPtr(IntPtr p) + { + return p == IntPtr.Zero ? new Utf8String(ReadOnlySpan.Empty) : new Utf8String(FindZeroTerminator((byte*)p.ToPointer())); + } + + /// + /// UTF16 String representation + /// Returns "NUL" on Null native type + /// + /// + public override string ToString() + { + if (sp.Length == 0) + { + // object.ToString() should never thrown + // throw new NullReferenceException(); + return "NUL"; + } + + unsafe + { + fixed (byte* q = sp) + { + return Encoding.UTF8.GetString(q, sp.Length - 1); + } } } - } - /// - /// Gets the encoded bytes for the provided . The array - /// will include a trailing \0 character. The length of the array will 's - /// +1 (for the trailing \0 byte). These bytes are - /// suitable to use with using the extension or or . Note that for the length provided should not include the - /// trailing \0 terminator. - /// - public static byte[] GetZeroTerminatedUTF8Bytes(string value) - { - return value.ToUtf8String(); - } - - public static implicit operator Utf8String(string s) - { - return FromString(s); - } -} - -public static class Utf8StringUtils -{ - public static byte[] ToUtf8String(this string sourceText) - { - if (sourceText == null) + /// + /// Gets the encoded bytes for the provided . The array + /// will include a trailing \0 character. The length of the array will 's + /// +1 (for the trailing \0 byte). These bytes are + /// suitable to use with using the extension or or . Note that for the length provided should not include the + /// trailing \0 terminator. + /// + public static byte[] GetZeroTerminatedUTF8Bytes(string value) { - return null; + return value.ToUtf8String(); } - var length = Encoding.UTF8.GetByteCount(sourceText); - - var byteArray = new byte[length + 1]; - var wrote = Encoding.UTF8.GetBytes(sourceText, 0, sourceText.Length, byteArray, 0); - byteArray[wrote] = 0; - - return byteArray; + public static implicit operator Utf8String(string s) + { + return FromString(s); + } } -} \ No newline at end of file + + public static class Utf8StringUtils + { + public static byte[] ToUtf8String(this string sourceText) + { + if (sourceText == null) + { + return null; + } + + var length = Encoding.UTF8.GetByteCount(sourceText); + + var byteArray = new byte[length + 1]; + var wrote = Encoding.UTF8.GetBytes(sourceText, 0, sourceText.Length, byteArray, 0); + byteArray[wrote] = 0; + + return byteArray; + } + } +}