2
0
mirror of https://github.com/raylib-cs/raylib-cs synced 2025-09-09 03:01:41 -04:00

Big unsafe update 1

This commit is contained in:
2021-11-11 12:00:42 +00:00
parent bb7ceb2102
commit e5400d0bae
10 changed files with 97 additions and 107 deletions

View File

@@ -6,7 +6,7 @@ using System.Security;
namespace Raylib_cs namespace Raylib_cs
{ {
[SuppressUnmanagedCodeSecurity] [SuppressUnmanagedCodeSecurity]
public static class Raylib public static unsafe class Raylib
{ {
/// <summary> /// <summary>
/// Used by DllImport to load the native library. /// Used by DllImport to load the native library.
@@ -384,15 +384,13 @@ namespace Raylib_cs
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern int GetShaderLocationAttrib(Shader shader, string attribName); public static extern int GetShaderLocationAttrib(Shader shader, string attribName);
/// <summary>Set shader uniform value<br/> /// <summary>Set shader uniform value</summary>
/// refers to a const void *</summary>
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] [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);
/// <summary>Set shader uniform value vector<br/> /// <summary>Set shader uniform value vector</summary>
/// value refers to a const void *</summary>
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] [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);
/// <summary>Set shader uniform value (matrix 4x4)</summary> /// <summary>Set shader uniform value (matrix 4x4)</summary>
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
@@ -546,7 +544,7 @@ namespace Raylib_cs
/// <summary>Get dropped files names (memory should be freed)</summary> /// <summary>Get dropped files names (memory should be freed)</summary>
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
public static extern unsafe byte** GetDroppedFiles(int* count); public static extern byte** GetDroppedFiles(int* count);
/// <summary>Clear dropped files paths buffer (free memory)</summary> /// <summary>Clear dropped files paths buffer (free memory)</summary>
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]

View File

@@ -33,20 +33,20 @@ namespace Raylib_cs
/// <summary> /// <summary>
/// Utility functions for parts of the api that are not easy to interact with via pinvoke. /// Utility functions for parts of the api that are not easy to interact with via pinvoke.
/// </summary> /// </summary>
public static class Utils public static unsafe class Utils
{ {
public static string SubText(this string input, int position, int length) public static string SubText(this string input, int position, int length)
{ {
return input.Substring(position, Math.Min(length, input.Length)); return input.Substring(position, Math.Min(length, input.Length));
} }
public static unsafe string[] GetDroppedFiles() public static string[] GetDroppedFiles()
{ {
int count; int count;
var buffer = Raylib.GetDroppedFiles(&count); var buffer = Raylib.GetDroppedFiles(&count);
var files = new string[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]); files[i] = Marshal.PtrToStringUTF8((IntPtr)buffer[i]);
} }
@@ -56,67 +56,59 @@ namespace Raylib_cs
return files; 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 model.materials[materialIndex];
return *materials;
} }
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(); return model.materials[materialIndex].maps[(int)mapIndex].texture;
MaterialMap* maps = (MaterialMap*)materials[0].maps.ToPointer();
return 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 model.materials[materialIndex], (int)mapIndex, texture);
Raylib.SetMaterialTexture(ref 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(); model.materials[materialIndex].shader = shader;
materials[materialIndex].shader = shader;
} }
public static unsafe void SetShaderValueV<T>(Shader shader, int uniformLoc, T[] values, ShaderUniformDataType uniformType, int count) where T : unmanaged public static void SetShaderValueV<T>(Shader shader, int uniformLoc, T[] values, ShaderUniformDataType uniformType, int count)
where T : unmanaged
{ {
SetShaderValueV(shader, uniformLoc, (Span<T>)values, uniformType, count); SetShaderValueV(shader, uniformLoc, (Span<T>)values, uniformType, count);
} }
public static unsafe void SetShaderValueV<T>(Shader shader, int uniformLoc, Span<T> values, ShaderUniformDataType uniformType, int count) where T : unmanaged public static void SetShaderValueV<T>(Shader shader, int uniformLoc, Span<T> values, ShaderUniformDataType uniformType, int count)
where T : unmanaged
{ {
fixed (T* valuePtr = values) fixed (T* valuePtr = values)
{ {
Raylib.SetShaderValueV(shader, uniformLoc, (IntPtr)valuePtr, uniformType, count); Raylib.SetShaderValueV(shader, uniformLoc, valuePtr, uniformType, count);
} }
} }
public static unsafe void SetShaderValue<T>(Shader shader, int uniformLoc, ref T value, ShaderUniformDataType uniformType, int count = 0) where T : unmanaged public static void SetShaderValue<T>(Shader shader, int uniformLoc, T value, ShaderUniformDataType uniformType)
where T : unmanaged
{ {
fixed (T* valuePtr = &value) Raylib.SetShaderValue(shader, uniformLoc, &value, uniformType);
{
Raylib.SetShaderValue(shader, uniformLoc, (IntPtr)valuePtr, uniformType);
}
} }
public static unsafe void SetShaderValue<T>(Shader shader, int uniformLoc, T value, ShaderUniformDataType uniformType) where T : unmanaged public static void SetShaderValue<T>(Shader shader, int uniformLoc, T[] values, ShaderUniformDataType uniformType)
{ where T : unmanaged
Raylib.SetShaderValue(shader, uniformLoc, (IntPtr)(&value), uniformType);
}
public static unsafe void SetShaderValue<T>(Shader shader, int uniformLoc, T[] values, ShaderUniformDataType uniformType) where T : unmanaged
{ {
SetShaderValue(shader, uniformLoc, (Span<T>)values, uniformType); SetShaderValue(shader, uniformLoc, (Span<T>)values, uniformType);
} }
public static unsafe void SetShaderValue<T>(Shader shader, int uniformLoc, Span<T> values, ShaderUniformDataType uniformType) where T : unmanaged public static void SetShaderValue<T>(Shader shader, int uniformLoc, Span<T> values, ShaderUniformDataType uniformType)
where T : unmanaged
{ {
fixed (T* valuePtr = values) fixed (T* valuePtr = values)
{ {
Raylib.SetShaderValue(shader, uniformLoc, (IntPtr)valuePtr, uniformType); Raylib.SetShaderValue(shader, uniformLoc, valuePtr, uniformType);
} }
} }
} }

View File

@@ -7,7 +7,7 @@ namespace Raylib_cs
/// Wave type, defines audio wave data /// Wave type, defines audio wave data
/// </summary> /// </summary>
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
public struct Wave public unsafe struct Wave
{ {
/// <summary> /// <summary>
/// Number of samples /// Number of samples
@@ -30,9 +30,9 @@ namespace Raylib_cs
public uint channels; public uint channels;
/// <summary> /// <summary>
/// Buffer data pointer (void *) /// Buffer data pointer
/// </summary> /// </summary>
public IntPtr data; public void* data;
} }
/// <summary> /// <summary>
@@ -45,7 +45,7 @@ namespace Raylib_cs
/// <summary> /// <summary>
/// Pointer to internal data(rAudioBuffer *) used by the audio system /// Pointer to internal data(rAudioBuffer *) used by the audio system
/// </summary> /// </summary>
public IntPtr audioBuffer; public IntPtr buffer;
/// <summary> /// <summary>
/// Frequency (samples per second) /// Frequency (samples per second)
@@ -85,7 +85,7 @@ namespace Raylib_cs
/// NOTE: Anything longer than ~10 seconds should be streamed /// NOTE: Anything longer than ~10 seconds should be streamed
/// </summary> /// </summary>
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
public struct Music public unsafe struct Music
{ {
/// <summary> /// <summary>
/// Audio stream /// Audio stream
@@ -108,8 +108,8 @@ namespace Raylib_cs
public int ctxType; public int ctxType;
/// <summary> /// <summary>
/// Audio context data, depends on type (void *) /// Audio context data, depends on type
/// </summary> /// </summary>
public IntPtr ctxData; public void* ctxData;
} }
} }

View File

@@ -60,7 +60,7 @@ namespace Raylib_cs
/// Font, font texture and GlyphInfo array data /// Font, font texture and GlyphInfo array data
/// </summary> /// </summary>
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
public struct Font public unsafe struct Font
{ {
/// <summary> /// <summary>
/// Base size (default chars height) /// Base size (default chars height)
@@ -85,11 +85,11 @@ namespace Raylib_cs
/// <summary> /// <summary>
/// Rectangles in texture for the glyphs /// Rectangles in texture for the glyphs
/// </summary> /// </summary>
public IntPtr recs; public Rectangle* recs;
/// <summary> /// <summary>
/// Glyphs info data /// Glyphs info data
/// </summary> /// </summary>
public IntPtr glyphs; public GlyphInfo* glyphs;
} }
} }

View File

@@ -119,12 +119,12 @@ namespace Raylib_cs
/// Image, pixel data stored in CPU memory (RAM) /// Image, pixel data stored in CPU memory (RAM)
/// </summary> /// </summary>
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
public struct Image public unsafe struct Image
{ {
/// <summary> /// <summary>
/// Image raw data (void *) /// Image raw data
/// </summary> /// </summary>
public IntPtr data; public void* data;
/// <summary> /// <summary>
/// Image base width /// Image base width

View File

@@ -71,7 +71,7 @@ namespace Raylib_cs
/// Material type (generic) /// Material type (generic)
/// </summary> /// </summary>
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
public struct Material public unsafe struct Material
{ {
/// <summary> /// <summary>
/// Material shader /// Material shader
@@ -79,13 +79,13 @@ namespace Raylib_cs
public Shader shader; public Shader shader;
/// <summary> /// <summary>
/// Material maps (MaterialMap *) /// Material maps
/// </summary> /// </summary>
public IntPtr maps; public MaterialMap *maps;
/// <summary> /// <summary>
/// Material generic parameters (if required, float *) /// Material generic parameters (if required)
/// </summary> /// </summary>
public IntPtr param; public float *param;
} }
} }

View File

@@ -31,7 +31,7 @@ namespace Raylib_cs
/// NOTE: Data stored in CPU memory (and GPU) /// NOTE: Data stored in CPU memory (and GPU)
/// </summary> /// </summary>
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
public struct Mesh public unsafe struct Mesh
{ {
/// <summary> /// <summary>
/// Number of vertices stored in arrays /// Number of vertices stored in arrays
@@ -46,63 +46,63 @@ namespace Raylib_cs
#region Default vertex data #region Default vertex data
/// <summary> /// <summary>
/// Vertex position (XYZ - 3 components per vertex) (shader-location = 0, float *) /// Vertex position (XYZ - 3 components per vertex) (shader-location = 0)
/// </summary> /// </summary>
public IntPtr vertices; public float* vertices;
/// <summary> /// <summary>
/// Vertex texture coordinates (UV - 2 components per vertex) (shader-location = 1, float *) /// Vertex texture coordinates (UV - 2 components per vertex) (shader-location = 1)
/// </summary> /// </summary>
public IntPtr texcoords; public float* texcoords;
/// <summary> /// <summary>
/// Vertex second texture coordinates (useful for lightmaps) (shader-location = 5, float *) /// Vertex second texture coordinates (useful for lightmaps) (shader-location = 5)
/// </summary> /// </summary>
public IntPtr texcoords2; public float* texcoords2;
/// <summary> /// <summary>
/// Vertex normals (XYZ - 3 components per vertex) (shader-location = 2, float *) /// Vertex normals (XYZ - 3 components per vertex) (shader-location = 2)
/// </summary> /// </summary>
public IntPtr normals; public float* normals;
/// <summary> /// <summary>
/// Vertex tangents (XYZW - 4 components per vertex) (shader-location = 4, float *) /// Vertex tangents (XYZW - 4 components per vertex) (shader-location = 4)
/// </summary> /// </summary>
public IntPtr tangents; public float* tangents;
/// <summary> /// <summary>
/// Vertex colors (RGBA - 4 components per vertex) (shader-location = 3, unsigned char *) /// Vertex colors (RGBA - 4 components per vertex) (shader-location = 3)
/// </summary> /// </summary>
public IntPtr colors; public byte* colors;
/// <summary> /// <summary>
/// Vertex indices (in case vertex data comes indexed, unsigned short *) /// Vertex indices (in case vertex data comes indexed)
/// </summary> /// </summary>
public IntPtr indices; public ushort* indices;
#endregion #endregion
#region Animation vertex data #region Animation vertex data
/// <summary> /// <summary>
/// Animated vertex positions (after bones transformations, float *) /// Animated vertex positions (after bones transformations)
/// </summary> /// </summary>
public IntPtr animVertices; public float* animVertices;
/// <summary> /// <summary>
/// Animated normals (after bones transformations, float *) /// Animated normals (after bones transformations)
/// </summary> /// </summary>
public IntPtr animNormals; public float* animNormals;
/// <summary> /// <summary>
/// Vertex bone ids, up to 4 bones influence by vertex (skinning, int *) /// Vertex bone ids, up to 4 bones influence by vertex (skinning)
/// </summary> /// </summary>
public IntPtr boneIds; public byte* boneIds;
/// <summary> /// <summary>
/// Vertex bone weight, up to 4 bones influence by vertex (skinning, float *) /// Vertex bone weight, up to 4 bones influence by vertex (skinning)
/// </summary> /// </summary>
public IntPtr boneWeights; public float* boneWeights;
#endregion #endregion
@@ -116,7 +116,7 @@ namespace Raylib_cs
/// <summary> /// <summary>
/// OpenGL Vertex Buffer Objects id (default vertex data, uint[]) /// OpenGL Vertex Buffer Objects id (default vertex data, uint[])
/// </summary> /// </summary>
public IntPtr vboId; public uint* vboId;
#endregion #endregion
} }

View File

@@ -8,12 +8,12 @@ namespace Raylib_cs
/// Bone information /// Bone information
/// </summary> /// </summary>
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
public struct BoneInfo public unsafe struct BoneInfo
{ {
/// <summary> /// <summary>
/// Bone name (char[32]) /// Bone name (char[32])
/// </summary> /// </summary>
public IntPtr name; public fixed sbyte name[32];
/// <summary> /// <summary>
/// Bone parent /// Bone parent
@@ -25,7 +25,7 @@ namespace Raylib_cs
/// Model type /// Model type
/// </summary> /// </summary>
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
public struct Model public unsafe struct Model
{ {
/// <summary> /// <summary>
/// Local transform matrix /// Local transform matrix
@@ -45,17 +45,17 @@ namespace Raylib_cs
/// <summary> /// <summary>
/// Meshes array (Mesh *) /// Meshes array (Mesh *)
/// </summary> /// </summary>
public IntPtr meshes; public Mesh *meshes;
/// <summary> /// <summary>
/// Materials array (Material *) /// Materials array (Material *)
/// </summary> /// </summary>
public IntPtr materials; public Material *materials;
/// <summary> /// <summary>
/// Mesh material number (int *) /// Mesh material number (int *)
/// </summary> /// </summary>
public IntPtr meshMaterial; public int *meshMaterial;
/// <summary> /// <summary>
/// Number of bones /// Number of bones
@@ -65,19 +65,19 @@ namespace Raylib_cs
/// <summary> /// <summary>
/// Bones information (skeleton, BoneInfo *) /// Bones information (skeleton, BoneInfo *)
/// </summary> /// </summary>
public IntPtr bones; public BoneInfo *bones;
/// <summary> /// <summary>
/// Bones base transformation (pose, Transform *) /// Bones base transformation (pose, Transform *)
/// </summary> /// </summary>
public IntPtr bindPose; public Transform *bindPose;
} }
/// <summary> /// <summary>
/// Model animation /// Model animation
/// </summary> /// </summary>
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
public struct ModelAnimation public unsafe struct ModelAnimation
{ {
/// <summary> /// <summary>
/// Number of bones /// Number of bones
@@ -92,11 +92,11 @@ namespace Raylib_cs
/// <summary> /// <summary>
/// Bones information (skeleton, BoneInfo *) /// Bones information (skeleton, BoneInfo *)
/// </summary> /// </summary>
public IntPtr bones; public BoneInfo *bones;
/// <summary> /// <summary>
/// Poses array by frame (Transform **) /// Poses array by frame (Transform **)
/// </summary> /// </summary>
public IntPtr framePoses; public Transform *framePoses;
} }
} }

View File

@@ -7,7 +7,7 @@ namespace Raylib_cs
/// RenderBatch type /// RenderBatch type
/// </summary> /// </summary>
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
public struct RenderBatch public unsafe struct RenderBatch
{ {
/// <summary> /// <summary>
/// Number of vertex buffers (multi-buffering support) /// Number of vertex buffers (multi-buffering support)
@@ -22,12 +22,12 @@ namespace Raylib_cs
/// <summary> /// <summary>
/// Dynamic buffer(s) for vertex data /// Dynamic buffer(s) for vertex data
/// </summary> /// </summary>
IntPtr vertexBuffer; VertexBuffer* vertexBuffer;
/// <summary> /// <summary>
/// Draw calls array, depends on textureId /// Draw calls array, depends on textureId
/// </summary> /// </summary>
IntPtr draws; DrawCall* draws;
/// <summary> /// <summary>
/// Draw calls counter /// Draw calls counter
@@ -52,26 +52,26 @@ namespace Raylib_cs
public int elementCount; public int elementCount;
/// <summary> /// <summary>
/// Vertex position (XYZ - 3 components per vertex) (shader-location = 0, float *) /// Vertex position (XYZ - 3 components per vertex) (shader-location = 0)
/// </summary> /// </summary>
public IntPtr vertices; public float* vertices;
/// <summary> /// <summary>
/// Vertex texture coordinates (UV - 2 components per vertex) (shader-location = 1, float *) /// Vertex texture coordinates (UV - 2 components per vertex) (shader-location = 1)
/// </summary> /// </summary>
public IntPtr texcoords; public float* texcoords;
/// <summary> /// <summary>
/// Vertex colors (RGBA - 4 components per vertex) (shader-location = 3, unsigned char *) /// Vertex colors (RGBA - 4 components per vertex) (shader-location = 3)
/// </summary> /// </summary>
public IntPtr colors; public byte* colors;
/// <summary> /// <summary>
/// Vertex indices (in case vertex data comes indexed) (6 indices per quad)<br/> /// Vertex indices (in case vertex data comes indexed) (6 indices per quad)<br/>
/// unsigned int * for GRAPHICS_API_OPENGL_11 or GRAPHICS_API_OPENGL_33<br/> /// unsigned int* for GRAPHICS_API_OPENGL_11 or GRAPHICS_API_OPENGL_33<br/>
/// unsigned short * for GRAPHICS_API_OPENGL_ES2 /// unsigned short* for GRAPHICS_API_OPENGL_ES2
/// </summary> /// </summary>
public IntPtr indices; public void* indices;
/// <summary> /// <summary>
/// OpenGL Vertex Array Object id /// OpenGL Vertex Array Object id

View File

@@ -70,7 +70,7 @@ namespace Raylib_cs
/// Shader type (generic) /// Shader type (generic)
/// </summary> /// </summary>
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
public struct Shader public unsafe struct Shader
{ {
/// <summary> /// <summary>
/// Shader program id /// Shader program id
@@ -80,6 +80,6 @@ namespace Raylib_cs
/// <summary> /// <summary>
/// Shader locations array (MAX_SHADER_LOCATIONS, int *) /// Shader locations array (MAX_SHADER_LOCATIONS, int *)
/// </summary> /// </summary>
public IntPtr locs; public int* locs;
} }
} }