SFML community forums

Bindings - other languages => DotNet => Topic started by: phunanon on October 09, 2018, 12:36:29 pm

Title: Feasibility of providing unsafe field-set methods, e.g. for Vertex in VertexArr?
Post by: phunanon on October 09, 2018, 12:36:29 pm
To not necro-bump this previous topic (https://en.sfml-dev.org/forums/index.php?topic=20622), I've created a new topic.

I've bumped into the same issue: needing to recreate a Vertex, and set an index of VertexArray to the new instance.

Would it be feasible to provide methods such as SetPosition(sf::Vector2f position, uint index), SetColor(sf::Color color, uint index), etc, within VertexArray, which would set relevant fields in an unsafe context? 
I can imagine this is not the only array-type within SFML.NET which suffers from this problem.

Unfortunately I only have access to a C# environment at work, otherwise I'd spend time trying to get the source to work and profile it, so I have only theoretical comment: it would probably be better performance; it would definitely mean more maintenance; it would most likely be found through Intellisense and used above recreation.
Title: Re: Feasibility of providing unsafe field-set methods, e.g. for Vertex in VertexArr?
Post by: dabbertorres on October 11, 2018, 08:05:07 pm
Not added to SFML, but you could get the desired behavior with an extension method. And if you're using C# 7 you actually don't need to use unsafe (if not, you'd return Vertex* instead of ref Vertex):
public static class VertexArrayExt
{
    public static ref Vertex getVertex(this VertexArray va, uint index)
    {
        return ref sfVertexArray_getVertex(va.CPointer, index);
    }

    [DllImport("csfml-graphics-2.dll", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
    private static extern ref Vertex sfVertexArray_getVertex(IntPtr CPointer, uint index);
}