SFML community forums

Help => Graphics => Topic started by: Flaze07 on July 30, 2017, 02:04:53 pm

Title: removing sf::VertexArray from the middle
Post by: Flaze07 on July 30, 2017, 02:04:53 pm
is it possible ??
Title: Re: removing sf::VertexArray from the middle
Post by: Flaze07 on July 30, 2017, 02:09:30 pm
like sf::VertexArray::Erase(2);
Title: Re: removing sf::VertexArray from the middle
Post by: Hapax on July 30, 2017, 02:20:30 pm
No. (https://www.sfml-dev.org/documentation/2.4.2/classsf_1_1VertexArray.php)

However, there are solutions:
1)
copy all the vertices to the right (assuming the left is the beginning and the right is the end) of the one you want to remove to the one to its left and then resize so that it is one smaller.
2)
use a vector (http://www.cplusplus.com/reference/vector/vector/) of vertices instead of an sf::VertexArray. Remember that you may also need to store the primitive type. You can then, of course, use a vector's erase (http://www.cplusplus.com/reference/vector/vector/erase/) method to remove an element.
e.g. (assumes a drawable class set-up)
// storage
sf::PrimitiveType m_primitiveType;
std::vector<sf::Vertex> m_vertices;

// in draw method
target.draw(m_vertices.data(), m_vertices.size(), m_primitiveType, renderStates);

// erase
m_vertices.erase(m_vertices.begin() + 2);

EDIT: corrected mistake (wrote sf::VectorArray instead of sf::VertexArray)
Title: Re: removing sf::VertexArray from the middle
Post by: Flaze07 on July 30, 2017, 02:27:38 pm
ah, thank you,I will take the first option...that being said...why don't sf::VertexArray has erase option ?
Title: Re: removing sf::VertexArray from the middle
Post by: Hapax on July 30, 2017, 04:01:11 pm
I honestly don't think it needs one. It's a simple container to simplify storing vertices along with its primitive type. However, if you need anything other than that - or, indeed, even if you don't - you can just use a vector of vertices, which I would personally recommend anyway. The draw command may look more confusing at first but it's really not once you've used it a couple of times.

If you think about it, it's called vertex array and you wouldn't expect to be able to remove elements from an array. The fact that you can resize it is a bonus. Also, just so you know, sf::VertexArray actually stores the vertices in a vector (https://github.com/SFML/SFML/blob/5a4b592bbc487b58390065a8dba0d688f7ecd1a8/include/SFML/Graphics/VertexArray.hpp#L190-L191) (as I mentioned above) so there's no reason you can't just do that yourself. ;)
Title: Re: removing sf::VertexArray from the middle
Post by: Flaze07 on July 30, 2017, 04:16:24 pm
yeah...I looked at the implementation it is simple indeed :D
but I think I can just copy the entire code and just add a simple remove function :D
Title: Re: removing sf::VertexArray from the middle
Post by: Hapax on July 30, 2017, 06:19:57 pm
You could fork SFML and modify it to include that functionality for yourself.

However, what's wrong with just using std::vector<sf::Vertex>? :P