-
-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use Span for SFML.Net API #263
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace SFML.Audio | ||
|
@@ -57,9 +58,14 @@ protected override bool OnStart() | |
/// <param name="samples">Array of samples to process</param> | ||
/// <returns>False to stop recording audio data, true to continue</returns> | ||
//////////////////////////////////////////////////////////// | ||
protected override bool OnProcessSamples(short[] samples) | ||
protected override bool OnProcessSamples(Span<short> samples) | ||
{ | ||
mySamplesArray.AddRange(samples); | ||
if (mySamplesArray.Capacity < mySamplesArray.Count + samples.Length) | ||
mySamplesArray.Capacity = mySamplesArray.Count + samples.Length; | ||
|
||
for (int i = 0; i < samples.Length; i++) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there not a way that we can use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no such overload for |
||
mySamplesArray.Add(samples[i]); | ||
|
||
return true; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,18 +84,17 @@ public Image(Stream stream) : | |
/// <param name="bytes">Byte array containing the file contents</param> | ||
/// <exception cref="LoadingFailedException" /> | ||
//////////////////////////////////////////////////////////// | ||
public Image(byte[] bytes) : | ||
public Image(Span<byte> bytes) : | ||
base(IntPtr.Zero) | ||
{ | ||
GCHandle pin = GCHandle.Alloc(bytes, GCHandleType.Pinned); | ||
try | ||
{ | ||
CPointer = sfImage_createFromMemory(pin.AddrOfPinnedObject(), (UIntPtr)bytes.Length); | ||
} | ||
finally | ||
unsafe | ||
{ | ||
pin.Free(); | ||
fixed (byte* ptr = bytes) | ||
{ | ||
CPointer = sfImage_createFromMemory((IntPtr)ptr, (UIntPtr)bytes.Length); | ||
} | ||
} | ||
|
||
if (CPointer == IntPtr.Zero) | ||
{ | ||
throw new LoadingFailedException("image"); | ||
|
@@ -148,14 +147,14 @@ public Image(Color[,] pixels) : | |
/// <param name="pixels">array containing the pixels</param> | ||
/// <exception cref="LoadingFailedException" /> | ||
//////////////////////////////////////////////////////////// | ||
public Image(uint width, uint height, byte[] pixels) : | ||
public Image(uint width, uint height, Span<byte> pixels) : | ||
base(IntPtr.Zero) | ||
{ | ||
unsafe | ||
{ | ||
fixed (byte* PixelsPtr = pixels) | ||
fixed (byte* ptr = pixels) | ||
{ | ||
CPointer = sfImage_createFromPixels(width, height, PixelsPtr); | ||
CPointer = sfImage_createFromPixels(width, height, ptr); | ||
} | ||
} | ||
|
||
|
@@ -310,14 +309,17 @@ public void SetPixel(uint x, uint y, Color color) | |
/// </summary> | ||
/// <returns>Array of pixels</returns> | ||
//////////////////////////////////////////////////////////// | ||
public byte[] Pixels | ||
public Span<byte> Pixels | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it reasonable to add a |
||
{ | ||
get | ||
{ | ||
Vector2u size = Size; | ||
byte[] PixelsPtr = new byte[size.X * size.Y * 4]; | ||
Marshal.Copy(sfImage_getPixelsPtr(CPointer), PixelsPtr, 0, PixelsPtr.Length); | ||
return PixelsPtr; | ||
unsafe | ||
{ | ||
Vector2u size = Size; | ||
IntPtr ptr = sfImage_getPixelsPtr(CPointer); | ||
uint length = size.X * size.Y * 4; | ||
return new Span<byte>((void*)ptr, (int)length); | ||
} | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -360,7 +360,7 @@ public void Draw(Drawable drawable, RenderStates states) | |
/// <param name="vertices">Pointer to the vertices</param> | ||
/// <param name="type">Type of primitives to draw</param> | ||
//////////////////////////////////////////////////////////// | ||
public void Draw(Vertex[] vertices, PrimitiveType type) | ||
public void Draw(Span<Vertex> vertices, PrimitiveType type) | ||
{ | ||
Draw(vertices, type, RenderStates.Default); | ||
} | ||
|
@@ -373,44 +373,15 @@ public void Draw(Vertex[] vertices, PrimitiveType type) | |
/// <param name="type">Type of primitives to draw</param> | ||
/// <param name="states">Render states to use for drawing</param> | ||
//////////////////////////////////////////////////////////// | ||
public void Draw(Vertex[] vertices, PrimitiveType type, RenderStates states) | ||
{ | ||
Draw(vertices, 0, (uint)vertices.Length, type, states); | ||
} | ||
|
||
//////////////////////////////////////////////////////////// | ||
/// <summary> | ||
/// Draw primitives defined by a sub-array of vertices, with default render states | ||
/// </summary> | ||
/// <param name="vertices">Array of vertices to draw</param> | ||
/// <param name="start">Index of the first vertex to draw in the array</param> | ||
/// <param name="count">Number of vertices to draw</param> | ||
/// <param name="type">Type of primitives to draw</param> | ||
//////////////////////////////////////////////////////////// | ||
public void Draw(Vertex[] vertices, uint start, uint count, PrimitiveType type) | ||
{ | ||
Draw(vertices, start, count, type, RenderStates.Default); | ||
} | ||
|
||
//////////////////////////////////////////////////////////// | ||
/// <summary> | ||
/// Draw primitives defined by a sub-array of vertices | ||
/// </summary> | ||
/// <param name="vertices">Pointer to the vertices</param> | ||
/// <param name="start">Index of the first vertex to use in the array</param> | ||
/// <param name="count">Number of vertices to draw</param> | ||
/// <param name="type">Type of primitives to draw</param> | ||
/// <param name="states">Render states to use for drawing</param> | ||
//////////////////////////////////////////////////////////// | ||
public void Draw(Vertex[] vertices, uint start, uint count, PrimitiveType type, RenderStates states) | ||
public void Draw(Span<Vertex> vertices, PrimitiveType type, RenderStates states) | ||
{ | ||
RenderStates.MarshalData marshaledStates = states.Marshal(); | ||
|
||
unsafe | ||
{ | ||
fixed (Vertex* vertexPtr = vertices) | ||
{ | ||
sfRenderTexture_drawPrimitives(CPointer, vertexPtr + start, (UIntPtr)count, type, ref marshaledStates); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to FORCE using slices? This removes the ability to specify offsets and lengths during There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The way I see it, the second overload is just a span-like version of the first one, so users could just do |
||
sfRenderTexture_drawPrimitives(CPointer, vertexPtr, (UIntPtr)vertices.Length, type, ref marshaledStates); | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we .NET doesn't own the memory for samples, should we be handling this this way? Our
Span
could be invalidated if CSFML moves things IIRCThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will be safe for the lifetime of the
SoundBuffer
object instance for as long as all public members onSoundBuffer
class don't produce iterator invalidation on the underlyingstd::vector
containing the samples onsf::SoundBuffer
(m_samples).