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
{
[SuppressUnmanagedCodeSecurity]
public static class Raylib
public static unsafe class Raylib
{
/// <summary>
/// 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);
/// <summary>Set shader uniform value<br/>
/// refers to a const void *</summary>
/// <summary>Set shader uniform value</summary>
[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/>
/// value refers to a const void *</summary>
/// <summary>Set shader uniform value vector</summary>
[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>
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
@@ -546,7 +544,7 @@ namespace Raylib_cs
/// <summary>Get dropped files names (memory should be freed)</summary>
[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>
[DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]

View File

@@ -33,20 +33,20 @@ namespace Raylib_cs
/// <summary>
/// Utility functions for parts of the api that are not easy to interact with via pinvoke.
/// </summary>
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<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);
}
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)
{
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, (IntPtr)valuePtr, uniformType);
}
Raylib.SetShaderValue(shader, uniformLoc, &value, uniformType);
}
public static unsafe void SetShaderValue<T>(Shader shader, int uniformLoc, T value, 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
public static void SetShaderValue<T>(Shader shader, int uniformLoc, T[] values, ShaderUniformDataType uniformType)
where T : unmanaged
{
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)
{
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
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct Wave
public unsafe struct Wave
{
/// <summary>
/// Number of samples
@@ -30,9 +30,9 @@ namespace Raylib_cs
public uint channels;
/// <summary>
/// Buffer data pointer (void *)
/// Buffer data pointer
/// </summary>
public IntPtr data;
public void* data;
}
/// <summary>
@@ -45,7 +45,7 @@ namespace Raylib_cs
/// <summary>
/// Pointer to internal data(rAudioBuffer *) used by the audio system
/// </summary>
public IntPtr audioBuffer;
public IntPtr buffer;
/// <summary>
/// Frequency (samples per second)
@@ -85,7 +85,7 @@ namespace Raylib_cs
/// NOTE: Anything longer than ~10 seconds should be streamed
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct Music
public unsafe struct Music
{
/// <summary>
/// Audio stream
@@ -108,8 +108,8 @@ namespace Raylib_cs
public int ctxType;
/// <summary>
/// Audio context data, depends on type (void *)
/// Audio context data, depends on type
/// </summary>
public IntPtr ctxData;
public void* ctxData;
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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