From f880241ee43ae10ea889e5a41cb8ce143a4e688d Mon Sep 17 00:00:00 2001 From: ChrisDill Date: Sun, 23 Sep 2018 12:34:40 +0100 Subject: [PATCH] No longer require unsafe option - Change unsafe/fixed to use IntPtr or MarshallAs instead. - Added methods in physac to marshall IntPtr returns to PhysicsBodyData. --- Bindings/Bindings.csproj | 4 +--- Bindings/Physac.cs | 43 +++++++++++++++++++++++++++++++--------- Bindings/Program.cs | 11 ---------- Bindings/Raylib.cs | 26 ++++++++++++++++++------ Examples/Examples.csproj | 18 +++++++++++------ README.md | 1 - 6 files changed, 67 insertions(+), 36 deletions(-) delete mode 100644 Bindings/Program.cs diff --git a/Bindings/Bindings.csproj b/Bindings/Bindings.csproj index 782bfed..214d8b9 100644 --- a/Bindings/Bindings.csproj +++ b/Bindings/Bindings.csproj @@ -36,7 +36,7 @@ true bin\Debug\ DEBUG;TRACE - true + false full x64 prompt @@ -84,8 +84,6 @@ - - Form diff --git a/Bindings/Physac.cs b/Bindings/Physac.cs index 9358f71..f2636e2 100644 --- a/Bindings/Physac.cs +++ b/Bindings/Physac.cs @@ -135,16 +135,35 @@ namespace Raylib public static extern void SetPhysicsGravity(float x, float y); // Creates a new circle physics body with generic parameters - [DllImport(nativeLibName)] - public static extern PhysicsBodyData CreatePhysicsBodyCircle(Vector2 pos, float radius, float density); + [DllImport(nativeLibName, EntryPoint = "CreatePhysicsBodyCircle")] + // [return: MarshalAs(UnmanagedType.LPStruct)] + private static extern IntPtr CreatePhysicsBodyCircleImport(Vector2 pos, float radius, float density); + public static PhysicsBodyData CreatePhysicsBodyCircle(Vector2 pos, float radius, float density) + { + var body = CreatePhysicsBodyCircleImport(pos, radius, density); + var data = (PhysicsBodyData)Marshal.PtrToStructure(body, typeof(PhysicsBodyData)); + return data; + } // Creates a new rectangle physics body with generic parameters - [DllImport(nativeLibName)] - public static extern PhysicsBodyData CreatePhysicsBodyRectangle(Vector2 pos, float width, float height, float density); + [DllImport(nativeLibName, EntryPoint = "CreatePhysicsBodyRectangle")] + private static extern IntPtr CreatePhysicsBodyRectangleImport(Vector2 pos, float width, float height, float density); + public static PhysicsBodyData CreatePhysicsBodyRectangle(Vector2 pos, float width, float height, float density) + { + var body = CreatePhysicsBodyRectangleImport(pos, width, height, density); + var data = (PhysicsBodyData)Marshal.PtrToStructure(body, typeof(PhysicsBodyData)); + return data; + } // Creates a new polygon physics body with generic parameters - [DllImport(nativeLibName)] - public static extern PhysicsBodyData CreatePhysicsBodyPolygon(Vector2 pos, float radius, int sides, float density); + [DllImport(nativeLibName, EntryPoint = "CreatePhysicsBodyPolygon")] + private static extern IntPtr CreatePhysicsBodyPolygonImport(Vector2 pos, float radius, int sides, float density); + public static PhysicsBodyData CreatePhysicsBodyPolygon(Vector2 pos, float radius, int sides, float density) + { + var body = CreatePhysicsBodyPolygonImport(pos, radius, sides, density); + var data = (PhysicsBodyData)Marshal.PtrToStructure(body, typeof(PhysicsBodyData)); + return data; + } // Adds a force to a physics body [DllImport(nativeLibName)] @@ -163,9 +182,15 @@ namespace Raylib public static extern int GetPhysicsBodiesCount(); // Returns a physics body of the bodies pool at a specific index - [DllImport(nativeLibName)] - public static extern IntPtr GetPhysicsBody(int index); - + [DllImport(nativeLibName, EntryPoint = "GetPhysicsBody")] + public static extern IntPtr GetPhysicsBodyImport(int index); + public static PhysicsBodyData GetPhysicsBody(int index) + { + var body = GetPhysicsBodyImport(index); + var data = (PhysicsBodyData)Marshal.PtrToStructure(body, typeof(PhysicsBodyData)); + return data; + } + // Returns the physics body shape type (PHYSICS_CIRCLE or PHYSICS_POLYGON) [DllImport(nativeLibName)] public static extern int GetPhysicsShapeType(int index); diff --git a/Bindings/Program.cs b/Bindings/Program.cs deleted file mode 100644 index 311d3f2..0000000 --- a/Bindings/Program.cs +++ /dev/null @@ -1,11 +0,0 @@ - -namespace Bindings -{ - static class Program - { - static void Main(string[] args) - { - Examples.core_basic_window(); - } - } -} \ No newline at end of file diff --git a/Bindings/Raylib.cs b/Bindings/Raylib.cs index 3750a06..a4eae5f 100644 --- a/Bindings/Raylib.cs +++ b/Bindings/Raylib.cs @@ -443,7 +443,10 @@ namespace Raylib public int offsetX; public int offsetY; public int advanceX; - public unsafe void* data; + + // [MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.Struct, SizeConst = Raylib.PHYSAC_MAX_VERTICES)] + // public unsafe void* data; + public IntPtr data; } // Font type, includes texture and charSet array data @@ -453,7 +456,11 @@ namespace Raylib public Texture2D texture; public int baseSize; public int charsCount; - public unsafe CharInfo* chars; + + // [MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.Struct, SizeConst = Raylib.PHYSAC_MAX_VERTICES)] + // public unsafe CharInfo* data; + //public CharInfo[] chars; + public IntPtr data; } // Camera type, defines a camera position/orientation in 3d space @@ -514,15 +521,19 @@ namespace Raylib public ushort[] indices; public uint vaoId; - public unsafe fixed uint vboId[7]; + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 7)] // Raylib.PHYSAC_MAX_VERTICES)] + public uint[] vboId; } // Shader type (generic) [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] - public unsafe struct Shader + public struct Shader { public uint id; - public fixed int locs[Raylib.MAX_SHADER_LOCATIONS]; + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = Raylib.MAX_SHADER_LOCATIONS)] + public int[] locs; } // Material texture map @@ -539,7 +550,10 @@ namespace Raylib public struct Material { public Shader shader; - // public MaterialMap[] maps = new MaterialMap[rl.MAX_MATERIAL_MAPS]; + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = Raylib.MAX_MATERIAL_MAPS)] + public MaterialMap[] maps; + public float[] param; } diff --git a/Examples/Examples.csproj b/Examples/Examples.csproj index a875094..563da7d 100644 --- a/Examples/Examples.csproj +++ b/Examples/Examples.csproj @@ -37,7 +37,7 @@ true bin\Debug\ DEBUG;TRACE - true + false full x64 prompt @@ -54,6 +54,9 @@ MinimumRecommendedRules.ruleset true + + raylib-cs.ico + @@ -65,23 +68,26 @@ + + + + + + + {a2b3bbc8-3d48-46dd-b3cf-263f554e4474} Bindings - - - - - + \ No newline at end of file diff --git a/README.md b/README.md index 544802d..2817d92 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,6 @@ static class Program # TODO: - Generator improvements -- Managed bindings(Marhsall etc) - .Net Core support - Windows forms support - Auto select x86/x64 dll