SFML community forums
Help => Graphics => Topic started by: Irksome Ninja on February 06, 2015, 10:18:15 pm
-
For a long time, I have been using static tilemaps, where I only once create and fill my vertexarray with tile information, by using sf::Quads, but say I want to add/remove certain tiles now and then, do I throw away the old array and refills a new? Or should I just append to the vertexarray?
I guess rebuilding the vertexarray often, is not so optimal?
-
My guess would be - it doesn't actually matter.
But, have you tried and benchmarked it?
-
The awnser to that is creating a sf::Vertex*. Something like this:
sf::Vertex* v = vertexarray[4 * xCoords + (yCoords * mapHeight)];
Then you have a pointer to the first vertex of the quad that you wan't to edit and can access the folliing with:
v[0].pos....
v[1].pos....
etc. etc.
The code might defer a bit depending on how you map is laid out.
-
Rebuilding the vertex array often doesn't cause any particular performance hit. Resizing the vertex array each frame and then setting each vertex can be done rather efficiently as resizing the vertex array doesn't change the actual memory allocation. Only if it resizes larger than it was does this occur. You can avoid this by setting it to the maximum size required in advance but occasional increasing of size shouldn't be noticable.
This is the technique I used in my Faux Car (http://en.sfml-dev.org/forums/index.php?topic=16912.0) tests. The track is over 250 segments, each requiring 4 triangles, plus any number of road marks, road-side objects, and opponents (each requiring two triangles). It's all one vertex array that is rebuilt from scratch every frame. Some optimisations are in place so that back-facing road triangles are not added to the vertex array so the vertex array changes size constantly.