Skip to content

Commit

Permalink
Use UIntPtr for size_t arguments and return values from CSFML
Browse files Browse the repository at this point in the history
  • Loading branch information
Marioalexsan authored and eXpl0it3r committed Jun 3, 2024
1 parent 1efcfdc commit 1dfc5db
Show file tree
Hide file tree
Showing 16 changed files with 85 additions and 66 deletions.
4 changes: 2 additions & 2 deletions src/SFML.Audio/Music.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public class Music : ObjectBase
GCHandle pin = GCHandle.Alloc(bytes, GCHandleType.Pinned);
try
{
CPointer = sfMusic_createFromMemory(pin.AddrOfPinnedObject(), Convert.ToUInt64(bytes.Length));
CPointer = sfMusic_createFromMemory(pin.AddrOfPinnedObject(), (UIntPtr)bytes.Length);
}
finally
{
Expand Down Expand Up @@ -408,7 +408,7 @@ public TimeSpan(Time offset, Time length)
private unsafe static extern IntPtr sfMusic_createFromStream(IntPtr stream);

[DllImport(CSFML.audio, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
private static extern IntPtr sfMusic_createFromMemory(IntPtr data, ulong size);
private static extern IntPtr sfMusic_createFromMemory(IntPtr data, UIntPtr size);

[DllImport(CSFML.audio, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
private static extern void sfMusic_destroy(IntPtr MusicStream);
Expand Down
4 changes: 2 additions & 2 deletions src/SFML.Audio/SoundBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public class SoundBuffer : ObjectBase
GCHandle pin = GCHandle.Alloc(bytes, GCHandleType.Pinned);
try
{
CPointer = sfSoundBuffer_createFromMemory(pin.AddrOfPinnedObject(), Convert.ToUInt64(bytes.Length));
CPointer = sfSoundBuffer_createFromMemory(pin.AddrOfPinnedObject(), (UIntPtr)bytes.Length);
}
finally
{
Expand Down Expand Up @@ -224,7 +224,7 @@ protected override void Destroy(bool disposing)
private unsafe static extern IntPtr sfSoundBuffer_createFromStream(IntPtr stream);

[DllImport(CSFML.audio, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
private unsafe static extern IntPtr sfSoundBuffer_createFromMemory(IntPtr data, ulong size);
private unsafe static extern IntPtr sfSoundBuffer_createFromMemory(IntPtr data, UIntPtr size);

[DllImport(CSFML.audio, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
private unsafe static extern IntPtr sfSoundBuffer_createFromSamples(short* Samples, ulong SampleCount, uint ChannelsCount, uint SampleRate);
Expand Down
14 changes: 7 additions & 7 deletions src/SFML.Audio/SoundRecorder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,10 @@ public static string[] AvailableDevices
{
unsafe
{
uint Count;
UIntPtr Count;
IntPtr* DevicesPtr = sfSoundRecorder_getAvailableDevices(out Count);
string[] Devices = new string[Count];
for (uint i = 0; i < Count; ++i)
string[] Devices = new string[(int)Count];
for (int i = 0; i < (int)Count; ++i)
{
Devices[i] = Marshal.PtrToStringAnsi(DevicesPtr[i]);
}
Expand Down Expand Up @@ -284,9 +284,9 @@ private bool StartRecording(IntPtr userData)
/// <param name="userData">User data -- unused</param>
/// <returns>False to stop recording audio data, true to continue</returns>
////////////////////////////////////////////////////////////
private bool ProcessSamples(IntPtr samples, uint nbSamples, IntPtr userData)
private bool ProcessSamples(IntPtr samples, UIntPtr nbSamples, IntPtr userData)
{
short[] samplesArray = new short[nbSamples];
short[] samplesArray = new short[(int)nbSamples];
Marshal.Copy(samples, samplesArray, 0, samplesArray.Length);

return OnProcessSamples(samplesArray);
Expand All @@ -308,7 +308,7 @@ private void StopRecording(IntPtr userData)
private delegate bool StartCallback(IntPtr userData);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate bool ProcessCallback(IntPtr samples, uint nbSamples, IntPtr userData);
private delegate bool ProcessCallback(IntPtr samples, UIntPtr nbSamples, IntPtr userData);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate void StopCallback(IntPtr userData);
Expand Down Expand Up @@ -340,7 +340,7 @@ private void StopRecording(IntPtr userData)
private static extern void sfSoundRecorder_setProcessingInterval(IntPtr SoundRecorder, Time Interval);

[DllImport(CSFML.audio, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
private unsafe static extern IntPtr* sfSoundRecorder_getAvailableDevices(out uint Count);
private unsafe static extern IntPtr* sfSoundRecorder_getAvailableDevices(out UIntPtr Count);

[DllImport(CSFML.audio, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
private static extern IntPtr sfSoundRecorder_getDefaultDevice();
Expand Down
26 changes: 22 additions & 4 deletions src/SFML.Graphics/Font.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ public Font(string filename) : base(sfFont_createFromFile(filename))
////////////////////////////////////////////////////////////
public Font(Stream stream) : base(IntPtr.Zero)
{
myStream = new StreamAdaptor(stream);
CPointer = sfFont_createFromStream(myStream.InputStreamPtr);
using (var adaptor = new StreamAdaptor(stream))
{
CPointer = sfFont_createFromStream(adaptor.InputStreamPtr);
}

if (CPointer == IntPtr.Zero)
{
Expand All @@ -57,7 +59,23 @@ public Font(Stream stream) : base(IntPtr.Zero)
/// <param name="bytes">Byte array containing the file contents</param>
/// <exception cref="LoadingFailedException" />
////////////////////////////////////////////////////////////
public Font(byte[] bytes) : this(new MemoryStream(bytes)) { }
public Font(byte[] bytes) :
base(IntPtr.Zero)
{
GCHandle pin = GCHandle.Alloc(bytes, GCHandleType.Pinned);
try
{
CPointer = sfFont_createFromMemory(pin.AddrOfPinnedObject(), (UIntPtr)bytes.Length);
}
finally
{
pin.Free();
}
if (CPointer == IntPtr.Zero)
{
throw new LoadingFailedException("font");
}
}

////////////////////////////////////////////////////////////
/// <summary>
Expand Down Expand Up @@ -265,7 +283,7 @@ internal struct InfoMarshalData
private static extern IntPtr sfFont_createFromStream(IntPtr stream);

[DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
private static extern IntPtr sfFont_createFromMemory(IntPtr data, ulong size);
private static extern IntPtr sfFont_createFromMemory(IntPtr data, UIntPtr size);

[DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
private static extern IntPtr sfFont_copy(IntPtr Font);
Expand Down
4 changes: 2 additions & 2 deletions src/SFML.Graphics/Image.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public Image(string filename) : base(sfImage_createFromFile(filename))
GCHandle pin = GCHandle.Alloc(bytes, GCHandleType.Pinned);
try
{
CPointer = sfImage_createFromMemory(pin.AddrOfPinnedObject(), Convert.ToUInt64(bytes.Length));
CPointer = sfImage_createFromMemory(pin.AddrOfPinnedObject(), (UIntPtr)bytes.Length);
}
finally
{
Expand Down Expand Up @@ -389,7 +389,7 @@ protected override void Destroy(bool disposing)
private unsafe static extern IntPtr sfImage_createFromStream(IntPtr stream);

[DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
private unsafe static extern IntPtr sfImage_createFromMemory(IntPtr data, ulong size);
private unsafe static extern IntPtr sfImage_createFromMemory(IntPtr data, UIntPtr size);

[DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
private static extern IntPtr sfImage_copy(IntPtr Image);
Expand Down
4 changes: 2 additions & 2 deletions src/SFML.Graphics/RenderTexture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ public void Draw(Vertex[] vertices, uint start, uint count, PrimitiveType type,
{
fixed (Vertex* vertexPtr = vertices)
{
sfRenderTexture_drawPrimitives(CPointer, vertexPtr + start, count, type, ref marshaledStates);
sfRenderTexture_drawPrimitives(CPointer, vertexPtr + start, (UIntPtr)count, type, ref marshaledStates);
}
}
}
Expand Down Expand Up @@ -599,7 +599,7 @@ protected override void Destroy(bool disposing)
private static extern bool sfRenderTexture_generateMipmap(IntPtr CPointer);

[DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
private unsafe static extern void sfRenderTexture_drawPrimitives(IntPtr CPointer, Vertex* vertexPtr, uint vertexCount, PrimitiveType type, ref RenderStates.MarshalData renderStates);
private unsafe static extern void sfRenderTexture_drawPrimitives(IntPtr CPointer, Vertex* vertexPtr, UIntPtr vertexCount, PrimitiveType type, ref RenderStates.MarshalData renderStates);

[DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
private static extern void sfRenderTexture_pushGLStates(IntPtr CPointer);
Expand Down
4 changes: 2 additions & 2 deletions src/SFML.Graphics/RenderWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ public void Draw(Vertex[] vertices, uint start, uint count, PrimitiveType type,
{
fixed (Vertex* vertexPtr = vertices)
{
sfRenderWindow_drawPrimitives(CPointer, vertexPtr + start, count, type, ref marshaledStates);
sfRenderWindow_drawPrimitives(CPointer, vertexPtr + start, (UIntPtr)count, type, ref marshaledStates);
}
}
}
Expand Down Expand Up @@ -930,7 +930,7 @@ private void Initialize()
private static extern Vector2i sfRenderWindow_mapCoordsToPixel(IntPtr CPointer, Vector2f point, IntPtr View);

[DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
private unsafe static extern void sfRenderWindow_drawPrimitives(IntPtr CPointer, Vertex* vertexPtr, uint vertexCount, PrimitiveType type, ref RenderStates.MarshalData renderStates);
private unsafe static extern void sfRenderWindow_drawPrimitives(IntPtr CPointer, Vertex* vertexPtr, UIntPtr vertexCount, PrimitiveType type, ref RenderStates.MarshalData renderStates);

[DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
private static extern void sfRenderWindow_pushGLStates(IntPtr CPointer);
Expand Down
24 changes: 12 additions & 12 deletions src/SFML.Graphics/Shader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ public unsafe void SetUniformArray(string name, float[] array)
{
fixed (float* data = array)
{
sfShader_setFloatUniformArray(CPointer, name, data, (uint)array.Length);
sfShader_setFloatUniformArray(CPointer, name, data, (UIntPtr)array.Length);
}
}

Expand All @@ -421,7 +421,7 @@ public unsafe void SetUniformArray(string name, Glsl.Vec2[] array)
{
fixed (Glsl.Vec2* data = array)
{
sfShader_setVec2UniformArray(CPointer, name, data, (uint)array.Length);
sfShader_setVec2UniformArray(CPointer, name, data, (UIntPtr)array.Length);
}
}

Expand All @@ -436,7 +436,7 @@ public unsafe void SetUniformArray(string name, Glsl.Vec3[] array)
{
fixed (Glsl.Vec3* data = array)
{
sfShader_setVec3UniformArray(CPointer, name, data, (uint)array.Length);
sfShader_setVec3UniformArray(CPointer, name, data, (UIntPtr)array.Length);
}
}

Expand All @@ -451,7 +451,7 @@ public unsafe void SetUniformArray(string name, Glsl.Vec4[] array)
{
fixed (Glsl.Vec4* data = array)
{
sfShader_setVec4UniformArray(CPointer, name, data, (uint)array.Length);
sfShader_setVec4UniformArray(CPointer, name, data, (UIntPtr)array.Length);
}
}

Expand All @@ -466,7 +466,7 @@ public unsafe void SetUniformArray(string name, Glsl.Mat3[] array)
{
fixed (Glsl.Mat3* data = array)
{
sfShader_setMat3UniformArray(CPointer, name, data, (uint)array.Length);
sfShader_setMat3UniformArray(CPointer, name, data, (UIntPtr)array.Length);
}
}

Expand All @@ -481,7 +481,7 @@ public unsafe void SetUniformArray(string name, Glsl.Mat4[] array)
{
fixed (Glsl.Mat4* data = array)
{
sfShader_setMat4UniformArray(CPointer, name, data, (uint)array.Length);
sfShader_setMat4UniformArray(CPointer, name, data, (UIntPtr)array.Length);
}
}

Expand Down Expand Up @@ -816,22 +816,22 @@ protected override void Destroy(bool disposing)
private static extern void sfShader_setCurrentTextureUniform(IntPtr shader, string name);

[DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
private static extern unsafe void sfShader_setFloatUniformArray(IntPtr shader, string name, float* data, uint length);
private static extern unsafe void sfShader_setFloatUniformArray(IntPtr shader, string name, float* data, UIntPtr length);

[DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
private static extern unsafe void sfShader_setVec2UniformArray(IntPtr shader, string name, Glsl.Vec2* data, uint length);
private static extern unsafe void sfShader_setVec2UniformArray(IntPtr shader, string name, Glsl.Vec2* data, UIntPtr length);

[DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
private static extern unsafe void sfShader_setVec3UniformArray(IntPtr shader, string name, Glsl.Vec3* data, uint length);
private static extern unsafe void sfShader_setVec3UniformArray(IntPtr shader, string name, Glsl.Vec3* data, UIntPtr length);

[DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
private static extern unsafe void sfShader_setVec4UniformArray(IntPtr shader, string name, Glsl.Vec4* data, uint length);
private static extern unsafe void sfShader_setVec4UniformArray(IntPtr shader, string name, Glsl.Vec4* data, UIntPtr length);

[DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
private static extern unsafe void sfShader_setMat3UniformArray(IntPtr shader, string name, Glsl.Mat3* data, uint length);
private static extern unsafe void sfShader_setMat3UniformArray(IntPtr shader, string name, Glsl.Mat3* data, UIntPtr length);

[DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
private static extern unsafe void sfShader_setMat4UniformArray(IntPtr shader, string name, Glsl.Mat4* data, uint length);
private static extern unsafe void sfShader_setMat4UniformArray(IntPtr shader, string name, Glsl.Mat4* data, UIntPtr length);

[DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity, Obsolete]
private static extern void sfShader_setFloatParameter(IntPtr shader, string name, float x);
Expand Down
12 changes: 6 additions & 6 deletions src/SFML.Graphics/Shape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,26 +215,26 @@ protected override void Destroy(bool disposing)
/// Callback passed to the C API
/// </summary>
////////////////////////////////////////////////////////////
private uint InternalGetPointCount(IntPtr userData)
private UIntPtr InternalGetPointCount(IntPtr userData)
{
return GetPointCount();
return (UIntPtr)GetPointCount();
}

////////////////////////////////////////////////////////////
/// <summary>
/// Callback passed to the C API
/// </summary>
////////////////////////////////////////////////////////////
private Vector2f InternalGetPoint(uint index, IntPtr userData)
private Vector2f InternalGetPoint(UIntPtr index, IntPtr userData)
{
return GetPoint(index);
return GetPoint((uint)index);
}

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate uint GetPointCountCallbackType(IntPtr UserData);
private delegate UIntPtr GetPointCountCallbackType(IntPtr UserData);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate Vector2f GetPointCallbackType(uint index, IntPtr UserData);
private delegate Vector2f GetPointCallbackType(UIntPtr index, IntPtr UserData);

private readonly GetPointCountCallbackType myGetPointCountCallback;
private readonly GetPointCallbackType myGetPointCallback;
Expand Down
4 changes: 2 additions & 2 deletions src/SFML.Graphics/Text.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ public Styles Style
////////////////////////////////////////////////////////////
public Vector2f FindCharacterPos(uint index)
{
return sfText_findCharacterPos(CPointer, index);
return sfText_findCharacterPos(CPointer, (UIntPtr)index);
}

////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -451,7 +451,7 @@ protected override void Destroy(bool disposing)
private static extern Styles sfText_getStyle(IntPtr CPointer);

[DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
private static extern Vector2f sfText_findCharacterPos(IntPtr CPointer, uint Index);
private static extern Vector2f sfText_findCharacterPos(IntPtr CPointer, UIntPtr Index);

[DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
private static extern FloatRect sfText_getLocalBounds(IntPtr CPointer);
Expand Down
10 changes: 5 additions & 5 deletions src/SFML.Graphics/Texture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,12 @@ public enum TextureCoordinateType
{
if (srgb)
{
CPointer = sfTexture_createSrgbFromMemory(pin.AddrOfPinnedObject(), Convert.ToUInt64(bytes.Length), ref area);
CPointer = sfTexture_createSrgbFromMemory(pin.AddrOfPinnedObject(), (UIntPtr)bytes.Length, ref area);
}
else
{
CPointer = sfTexture_createFromMemory(pin.AddrOfPinnedObject(), Convert.ToUInt64(bytes.Length), ref area);
}
CPointer = sfTexture_createFromMemory(pin.AddrOfPinnedObject(), (UIntPtr)bytes.Length, ref area);
}
}
finally
{
Expand Down Expand Up @@ -582,10 +582,10 @@ protected override void Destroy(bool disposing)
private static extern IntPtr sfTexture_createSrgbFromImage(IntPtr image, ref IntRect area);

[DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
private static extern IntPtr sfTexture_createFromMemory(IntPtr data, ulong size, ref IntRect area);
private static extern IntPtr sfTexture_createFromMemory(IntPtr data, UIntPtr size, ref IntRect area);

[DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
private static extern IntPtr sfTexture_createSrgbFromMemory(IntPtr data, ulong size, ref IntRect area);
private static extern IntPtr sfTexture_createSrgbFromMemory(IntPtr data, UIntPtr size, ref IntRect area);

[DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
private static extern IntPtr sfTexture_copy(IntPtr texture);
Expand Down
Loading

0 comments on commit 1dfc5db

Please sign in to comment.