From 43bf3d04deae477973a29587ebacc7f7e66e68e5 Mon Sep 17 00:00:00 2001 From: Ben Parsons <9parsonsb@gmail.com> Date: Sun, 5 Dec 2021 21:12:01 +1100 Subject: [PATCH] Implement safe ModelAnimation --- Raylib-cs/Raylib.cs | 21 ++++++++++++++++++++- Raylib-cs/types/Model.cs | 30 +++++++++++++++++++----------- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/Raylib-cs/Raylib.cs b/Raylib-cs/Raylib.cs index 2924d67..0e00751 100644 --- a/Raylib-cs/Raylib.cs +++ b/Raylib-cs/Raylib.cs @@ -6,6 +6,7 @@ using System.Security; using System.Text; using System.Text.Unicode; using System.Xml; +using Microsoft.Toolkit.HighPerformance; namespace Raylib_cs { @@ -2001,7 +2002,25 @@ namespace Raylib_cs /// Load model animations from file [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern ModelAnimation* LoadModelAnimations(string fileName, ref int animsCount); + public static extern ModelAnimation* LoadModelAnimations(byte* fileName, int* animsCount); + + public static ReadOnlySpan LoadModelAnimations(Utf8String fileName, ref int animsCount) + { + fixed (byte* p1 = fileName) + { + fixed (int* p2 = &animsCount) + { + var model = LoadModelAnimations(p1, p2); + + if ((IntPtr)model == IntPtr.Zero) + { + throw new ApplicationException("Failed to load animation"); + } + + return new ReadOnlySpan(model, animsCount); + } + } + } /// Update model animation pose [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] diff --git a/Raylib-cs/types/Model.cs b/Raylib-cs/types/Model.cs index 76c795d..8997f24 100644 --- a/Raylib-cs/types/Model.cs +++ b/Raylib-cs/types/Model.cs @@ -1,6 +1,8 @@ using System; +using System.Collections.Generic; using System.Numerics; using System.Runtime.InteropServices; +using Microsoft.Toolkit.HighPerformance; namespace Raylib_cs { @@ -45,17 +47,17 @@ namespace Raylib_cs /// /// Meshes array (Mesh *) /// - public Mesh *meshes; + public Mesh* meshes; /// /// Materials array (Material *) /// - public Material *materials; + public Material* materials; /// /// Mesh material number (int *) /// - public int *meshMaterial; + public int* meshMaterial; /// /// Number of bones @@ -65,38 +67,44 @@ namespace Raylib_cs /// /// Bones information (skeleton, BoneInfo *) /// - public BoneInfo *bones; + public BoneInfo* bones; /// /// Bones base transformation (pose, Transform *) /// - public Transform *bindPose; + public Transform* bindPose; } /// /// Model animation /// [StructLayout(LayoutKind.Sequential)] - public unsafe struct ModelAnimation + public readonly unsafe struct ModelAnimation { /// /// Number of bones /// - public int boneCount; + public readonly int boneCount; /// /// Number of animation frames /// - public int frameCount; + public readonly int frameCount; /// /// Bones information (skeleton, BoneInfo *) /// - public BoneInfo *bones; + public readonly BoneInfo* bones; + + /// + public ReadOnlySpan BoneInfo => new(bones, boneCount); /// /// Poses array by frame (Transform **) /// - public Transform *framePoses; + public readonly Transform** framePoses; + + /// + public ReadOnlySpan2D FramePoses => new(framePoses, frameCount,boneCount,sizeof(Transform)); } -} +} \ No newline at end of file