From e5400d0baea4560537472ccc9876b432a6bce909 Mon Sep 17 00:00:00 2001 From: ChrisDill Date: Thu, 11 Nov 2021 12:00:42 +0000 Subject: [PATCH] Big unsafe update 1 --- Raylib-cs/Raylib.cs | 14 ++++----- Raylib-cs/RaylibUtils.cs | 56 +++++++++++++++------------------- Raylib-cs/types/Audio.cs | 14 ++++----- Raylib-cs/types/Font.cs | 6 ++-- Raylib-cs/types/Image.cs | 6 ++-- Raylib-cs/types/Material.cs | 10 +++--- Raylib-cs/types/Mesh.cs | 48 ++++++++++++++--------------- Raylib-cs/types/Model.cs | 22 ++++++------- Raylib-cs/types/RenderBatch.cs | 24 +++++++-------- Raylib-cs/types/Shader.cs | 4 +-- 10 files changed, 97 insertions(+), 107 deletions(-) diff --git a/Raylib-cs/Raylib.cs b/Raylib-cs/Raylib.cs index 1c5094e..14f0306 100644 --- a/Raylib-cs/Raylib.cs +++ b/Raylib-cs/Raylib.cs @@ -6,7 +6,7 @@ using System.Security; namespace Raylib_cs { [SuppressUnmanagedCodeSecurity] - public static class Raylib + public static unsafe class Raylib { /// /// Used by DllImport to load the native library. @@ -384,15 +384,13 @@ namespace Raylib_cs [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern int GetShaderLocationAttrib(Shader shader, string attribName); - /// Set shader uniform value
- /// refers to a const void *
+ /// Set shader uniform value [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern void SetShaderValue(Shader shader, int uniformLoc, IntPtr value, ShaderUniformDataType uniformType); + public static extern void SetShaderValue(Shader shader, int uniformLoc, void* value, ShaderUniformDataType uniformType); - /// Set shader uniform value vector
- /// value refers to a const void *
+ /// Set shader uniform value vector [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern void SetShaderValueV(Shader shader, int uniformLoc, IntPtr value, ShaderUniformDataType uniformType, int count); + public static extern void SetShaderValueV(Shader shader, int uniformLoc, void* value, ShaderUniformDataType uniformType, int count); /// Set shader uniform value (matrix 4x4) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] @@ -546,7 +544,7 @@ namespace Raylib_cs /// Get dropped files names (memory should be freed) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern unsafe byte** GetDroppedFiles(int* count); + public static extern byte** GetDroppedFiles(int* count); /// Clear dropped files paths buffer (free memory) [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] diff --git a/Raylib-cs/RaylibUtils.cs b/Raylib-cs/RaylibUtils.cs index 741b7a2..e90e44c 100644 --- a/Raylib-cs/RaylibUtils.cs +++ b/Raylib-cs/RaylibUtils.cs @@ -33,20 +33,20 @@ namespace Raylib_cs /// /// Utility functions for parts of the api that are not easy to interact with via pinvoke. /// - public static class Utils + public static unsafe class Utils { public static string SubText(this string input, int position, int length) { return input.Substring(position, Math.Min(length, input.Length)); } - public static unsafe string[] GetDroppedFiles() + public static string[] GetDroppedFiles() { int count; var buffer = Raylib.GetDroppedFiles(&count); var files = new string[count]; - for (int i = 0; i < count; i++) + for (var i = 0; i < count; i++) { files[i] = Marshal.PtrToStringUTF8((IntPtr)buffer[i]); } @@ -56,67 +56,59 @@ namespace Raylib_cs return files; } - public unsafe static Material GetMaterial(ref Model model, int materialIndex) + public static Material GetMaterial(ref Model model, int materialIndex) { - Material* materials = (Material*)model.materials.ToPointer(); - return *materials; + return model.materials[materialIndex]; } - public unsafe static Texture2D GetMaterialTexture(ref Model model, int materialIndex, MaterialMapIndex mapIndex) + public static Texture2D GetMaterialTexture(ref Model model, int materialIndex, MaterialMapIndex mapIndex) { - Material* materials = (Material*)model.materials.ToPointer(); - MaterialMap* maps = (MaterialMap*)materials[0].maps.ToPointer(); - return maps[(int)mapIndex].texture; + return model.materials[materialIndex].maps[(int)mapIndex].texture; } - public unsafe static void SetMaterialTexture(ref Model model, int materialIndex, MaterialMapIndex mapIndex, ref Texture2D texture) + public static void SetMaterialTexture(ref Model model, int materialIndex, MaterialMapIndex mapIndex, ref Texture2D texture) { - Material* materials = (Material*)model.materials.ToPointer(); - Raylib.SetMaterialTexture(ref materials[materialIndex], (int)mapIndex, texture); + Raylib.SetMaterialTexture(ref model.materials[materialIndex], (int)mapIndex, texture); } - public unsafe static void SetMaterialShader(ref Model model, int materialIndex, ref Shader shader) + public static void SetMaterialShader(ref Model model, int materialIndex, ref Shader shader) { - Material* materials = (Material*)model.materials.ToPointer(); - materials[materialIndex].shader = shader; + model.materials[materialIndex].shader = shader; } - public static unsafe void SetShaderValueV(Shader shader, int uniformLoc, T[] values, ShaderUniformDataType uniformType, int count) where T : unmanaged + public static void SetShaderValueV(Shader shader, int uniformLoc, T[] values, ShaderUniformDataType uniformType, int count) + where T : unmanaged { SetShaderValueV(shader, uniformLoc, (Span)values, uniformType, count); } - public static unsafe void SetShaderValueV(Shader shader, int uniformLoc, Span values, ShaderUniformDataType uniformType, int count) where T : unmanaged + public static void SetShaderValueV(Shader shader, int uniformLoc, Span values, ShaderUniformDataType uniformType, int count) + where T : unmanaged { fixed (T* valuePtr = values) { - Raylib.SetShaderValueV(shader, uniformLoc, (IntPtr)valuePtr, uniformType, count); + Raylib.SetShaderValueV(shader, uniformLoc, valuePtr, uniformType, count); } } - public static unsafe void SetShaderValue(Shader shader, int uniformLoc, ref T value, ShaderUniformDataType uniformType, int count = 0) where T : unmanaged + public static void SetShaderValue(Shader shader, int uniformLoc, T value, ShaderUniformDataType uniformType) + where T : unmanaged { - fixed (T* valuePtr = &value) - { - Raylib.SetShaderValue(shader, uniformLoc, (IntPtr)valuePtr, uniformType); - } + Raylib.SetShaderValue(shader, uniformLoc, &value, uniformType); } - public static unsafe void SetShaderValue(Shader shader, int uniformLoc, T value, ShaderUniformDataType uniformType) where T : unmanaged - { - Raylib.SetShaderValue(shader, uniformLoc, (IntPtr)(&value), uniformType); - } - - public static unsafe void SetShaderValue(Shader shader, int uniformLoc, T[] values, ShaderUniformDataType uniformType) where T : unmanaged + public static void SetShaderValue(Shader shader, int uniformLoc, T[] values, ShaderUniformDataType uniformType) + where T : unmanaged { SetShaderValue(shader, uniformLoc, (Span)values, uniformType); } - public static unsafe void SetShaderValue(Shader shader, int uniformLoc, Span values, ShaderUniformDataType uniformType) where T : unmanaged + public static void SetShaderValue(Shader shader, int uniformLoc, Span values, ShaderUniformDataType uniformType) + where T : unmanaged { fixed (T* valuePtr = values) { - Raylib.SetShaderValue(shader, uniformLoc, (IntPtr)valuePtr, uniformType); + Raylib.SetShaderValue(shader, uniformLoc, valuePtr, uniformType); } } } diff --git a/Raylib-cs/types/Audio.cs b/Raylib-cs/types/Audio.cs index 2f68020..83a601c 100644 --- a/Raylib-cs/types/Audio.cs +++ b/Raylib-cs/types/Audio.cs @@ -7,7 +7,7 @@ namespace Raylib_cs /// Wave type, defines audio wave data ///
[StructLayout(LayoutKind.Sequential)] - public struct Wave + public unsafe struct Wave { /// /// Number of samples @@ -30,9 +30,9 @@ namespace Raylib_cs public uint channels; /// - /// Buffer data pointer (void *) + /// Buffer data pointer /// - public IntPtr data; + public void* data; } /// @@ -45,7 +45,7 @@ namespace Raylib_cs /// /// Pointer to internal data(rAudioBuffer *) used by the audio system /// - public IntPtr audioBuffer; + public IntPtr buffer; /// /// Frequency (samples per second) @@ -85,7 +85,7 @@ namespace Raylib_cs /// NOTE: Anything longer than ~10 seconds should be streamed /// [StructLayout(LayoutKind.Sequential)] - public struct Music + public unsafe struct Music { /// /// Audio stream @@ -108,8 +108,8 @@ namespace Raylib_cs public int ctxType; /// - /// Audio context data, depends on type (void *) + /// Audio context data, depends on type /// - public IntPtr ctxData; + public void* ctxData; } } diff --git a/Raylib-cs/types/Font.cs b/Raylib-cs/types/Font.cs index b559315..f9cc4c9 100644 --- a/Raylib-cs/types/Font.cs +++ b/Raylib-cs/types/Font.cs @@ -60,7 +60,7 @@ namespace Raylib_cs /// Font, font texture and GlyphInfo array data /// [StructLayout(LayoutKind.Sequential)] - public struct Font + public unsafe struct Font { /// /// Base size (default chars height) @@ -85,11 +85,11 @@ namespace Raylib_cs /// /// Rectangles in texture for the glyphs /// - public IntPtr recs; + public Rectangle* recs; /// /// Glyphs info data /// - public IntPtr glyphs; + public GlyphInfo* glyphs; } } diff --git a/Raylib-cs/types/Image.cs b/Raylib-cs/types/Image.cs index 271cc55..f60c114 100644 --- a/Raylib-cs/types/Image.cs +++ b/Raylib-cs/types/Image.cs @@ -119,12 +119,12 @@ namespace Raylib_cs /// Image, pixel data stored in CPU memory (RAM) /// [StructLayout(LayoutKind.Sequential)] - public struct Image + public unsafe struct Image { /// - /// Image raw data (void *) + /// Image raw data /// - public IntPtr data; + public void* data; /// /// Image base width diff --git a/Raylib-cs/types/Material.cs b/Raylib-cs/types/Material.cs index 3c07e81..1d82983 100644 --- a/Raylib-cs/types/Material.cs +++ b/Raylib-cs/types/Material.cs @@ -71,7 +71,7 @@ namespace Raylib_cs /// Material type (generic) /// [StructLayout(LayoutKind.Sequential)] - public struct Material + public unsafe struct Material { /// /// Material shader @@ -79,13 +79,13 @@ namespace Raylib_cs public Shader shader; /// - /// Material maps (MaterialMap *) + /// Material maps /// - public IntPtr maps; + public MaterialMap *maps; /// - /// Material generic parameters (if required, float *) + /// Material generic parameters (if required) /// - public IntPtr param; + public float *param; } } diff --git a/Raylib-cs/types/Mesh.cs b/Raylib-cs/types/Mesh.cs index 6c29141..910dad7 100644 --- a/Raylib-cs/types/Mesh.cs +++ b/Raylib-cs/types/Mesh.cs @@ -31,7 +31,7 @@ namespace Raylib_cs /// NOTE: Data stored in CPU memory (and GPU) /// [StructLayout(LayoutKind.Sequential)] - public struct Mesh + public unsafe struct Mesh { /// /// Number of vertices stored in arrays @@ -46,63 +46,63 @@ namespace Raylib_cs #region Default vertex data /// - /// Vertex position (XYZ - 3 components per vertex) (shader-location = 0, float *) + /// Vertex position (XYZ - 3 components per vertex) (shader-location = 0) /// - public IntPtr vertices; + public float* vertices; /// - /// Vertex texture coordinates (UV - 2 components per vertex) (shader-location = 1, float *) + /// Vertex texture coordinates (UV - 2 components per vertex) (shader-location = 1) /// - public IntPtr texcoords; + public float* texcoords; /// - /// Vertex second texture coordinates (useful for lightmaps) (shader-location = 5, float *) + /// Vertex second texture coordinates (useful for lightmaps) (shader-location = 5) /// - public IntPtr texcoords2; + public float* texcoords2; /// - /// Vertex normals (XYZ - 3 components per vertex) (shader-location = 2, float *) + /// Vertex normals (XYZ - 3 components per vertex) (shader-location = 2) /// - public IntPtr normals; + public float* normals; /// - /// Vertex tangents (XYZW - 4 components per vertex) (shader-location = 4, float *) + /// Vertex tangents (XYZW - 4 components per vertex) (shader-location = 4) /// - public IntPtr tangents; + public float* tangents; /// - /// Vertex colors (RGBA - 4 components per vertex) (shader-location = 3, unsigned char *) + /// Vertex colors (RGBA - 4 components per vertex) (shader-location = 3) /// - public IntPtr colors; + public byte* colors; /// - /// Vertex indices (in case vertex data comes indexed, unsigned short *) + /// Vertex indices (in case vertex data comes indexed) /// - public IntPtr indices; + public ushort* indices; #endregion #region Animation vertex data /// - /// Animated vertex positions (after bones transformations, float *) + /// Animated vertex positions (after bones transformations) /// - public IntPtr animVertices; + public float* animVertices; /// - /// Animated normals (after bones transformations, float *) + /// Animated normals (after bones transformations) /// - public IntPtr animNormals; + public float* animNormals; /// - /// Vertex bone ids, up to 4 bones influence by vertex (skinning, int *) + /// Vertex bone ids, up to 4 bones influence by vertex (skinning) /// - public IntPtr boneIds; + public byte* boneIds; /// - /// Vertex bone weight, up to 4 bones influence by vertex (skinning, float *) + /// Vertex bone weight, up to 4 bones influence by vertex (skinning) /// - public IntPtr boneWeights; + public float* boneWeights; #endregion @@ -116,7 +116,7 @@ namespace Raylib_cs /// /// OpenGL Vertex Buffer Objects id (default vertex data, uint[]) /// - public IntPtr vboId; + public uint* vboId; #endregion } diff --git a/Raylib-cs/types/Model.cs b/Raylib-cs/types/Model.cs index 91638d3..76c795d 100644 --- a/Raylib-cs/types/Model.cs +++ b/Raylib-cs/types/Model.cs @@ -8,12 +8,12 @@ namespace Raylib_cs /// Bone information /// [StructLayout(LayoutKind.Sequential)] - public struct BoneInfo + public unsafe struct BoneInfo { /// /// Bone name (char[32]) /// - public IntPtr name; + public fixed sbyte name[32]; /// /// Bone parent @@ -25,7 +25,7 @@ namespace Raylib_cs /// Model type /// [StructLayout(LayoutKind.Sequential)] - public struct Model + public unsafe struct Model { /// /// Local transform matrix @@ -45,17 +45,17 @@ namespace Raylib_cs /// /// Meshes array (Mesh *) /// - public IntPtr meshes; + public Mesh *meshes; /// /// Materials array (Material *) /// - public IntPtr materials; + public Material *materials; /// /// Mesh material number (int *) /// - public IntPtr meshMaterial; + public int *meshMaterial; /// /// Number of bones @@ -65,19 +65,19 @@ namespace Raylib_cs /// /// Bones information (skeleton, BoneInfo *) /// - public IntPtr bones; + public BoneInfo *bones; /// /// Bones base transformation (pose, Transform *) /// - public IntPtr bindPose; + public Transform *bindPose; } /// /// Model animation /// [StructLayout(LayoutKind.Sequential)] - public struct ModelAnimation + public unsafe struct ModelAnimation { /// /// Number of bones @@ -92,11 +92,11 @@ namespace Raylib_cs /// /// Bones information (skeleton, BoneInfo *) /// - public IntPtr bones; + public BoneInfo *bones; /// /// Poses array by frame (Transform **) /// - public IntPtr framePoses; + public Transform *framePoses; } } diff --git a/Raylib-cs/types/RenderBatch.cs b/Raylib-cs/types/RenderBatch.cs index e3e3c05..f4abc16 100644 --- a/Raylib-cs/types/RenderBatch.cs +++ b/Raylib-cs/types/RenderBatch.cs @@ -7,7 +7,7 @@ namespace Raylib_cs /// RenderBatch type /// [StructLayout(LayoutKind.Sequential)] - public struct RenderBatch + public unsafe struct RenderBatch { /// /// Number of vertex buffers (multi-buffering support) @@ -22,12 +22,12 @@ namespace Raylib_cs /// /// Dynamic buffer(s) for vertex data /// - IntPtr vertexBuffer; + VertexBuffer* vertexBuffer; /// /// Draw calls array, depends on textureId /// - IntPtr draws; + DrawCall* draws; /// /// Draw calls counter @@ -52,26 +52,26 @@ namespace Raylib_cs public int elementCount; /// - /// Vertex position (XYZ - 3 components per vertex) (shader-location = 0, float *) + /// Vertex position (XYZ - 3 components per vertex) (shader-location = 0) /// - public IntPtr vertices; + public float* vertices; /// - /// Vertex texture coordinates (UV - 2 components per vertex) (shader-location = 1, float *) + /// Vertex texture coordinates (UV - 2 components per vertex) (shader-location = 1) /// - public IntPtr texcoords; + public float* texcoords; /// - /// Vertex colors (RGBA - 4 components per vertex) (shader-location = 3, unsigned char *) + /// Vertex colors (RGBA - 4 components per vertex) (shader-location = 3) /// - public IntPtr colors; + public byte* colors; /// /// Vertex indices (in case vertex data comes indexed) (6 indices per quad)
- /// unsigned int * for GRAPHICS_API_OPENGL_11 or GRAPHICS_API_OPENGL_33
- /// unsigned short * for GRAPHICS_API_OPENGL_ES2 + /// unsigned int* for GRAPHICS_API_OPENGL_11 or GRAPHICS_API_OPENGL_33
+ /// unsigned short* for GRAPHICS_API_OPENGL_ES2 ///
- public IntPtr indices; + public void* indices; /// /// OpenGL Vertex Array Object id diff --git a/Raylib-cs/types/Shader.cs b/Raylib-cs/types/Shader.cs index f82192a..e79e9a7 100644 --- a/Raylib-cs/types/Shader.cs +++ b/Raylib-cs/types/Shader.cs @@ -70,7 +70,7 @@ namespace Raylib_cs /// Shader type (generic) /// [StructLayout(LayoutKind.Sequential)] - public struct Shader + public unsafe struct Shader { /// /// Shader program id @@ -80,6 +80,6 @@ namespace Raylib_cs /// /// Shader locations array (MAX_SHADER_LOCATIONS, int *) /// - public IntPtr locs; + public int* locs; } }