VertexArray#[](index) should not return a copy of the Vertex
alanwillms opened this issue · comments
It seems VertexArray#[](index)
returns a copy of Vertex at index
instead of the original instance.
Look:
vertex_array = SF::VertexArray.new(SF::Quads, 4)
puts vertex_array[0].position.inspect
# SF::Vector2(Float32)(@x=0, @y=0)
vertex_array[0].position = SF.vector2(64, 128)
puts vertex_array[0].position.inspect
# SF::Vector2(Float32)(@x=0, @y=0)
The workaround is creating a new vertex and overwriting it:
vertex_array = SF::VertexArray.new(SF::Quads, 4)
puts vertex_array[0].position.inspect
# SF::Vector2(Float32)(@x=0, @y=0)
vertex = SF::Vertex.new
vertex.position = SF.vector2(64, 128)
vertex_array[0] = vertex
puts vertex_array[0].position.inspect
# SF::Vector2(Float32)(@x=64, @y=128)
That is true. This is how structs work in Crystal. And it's not the only place where this problem is present.
I gave this a lot of thought today, and there is nothing I can do to change this. Making a class-based wrapper would be a huge performance hit.
The only way to not copy a struct is to use pointers, and that's just not an option.
C++ code relies a lot on the mutability of these structs. The mindset here should be different to avoid the need to edit structs.
Compare: original (broken), remade examples