SFML community forums

Help => Graphics => Topic started by: DasOhmoff San on June 01, 2019, 11:28:59 am

Title: Drawing many different shapes? Or one shape changing in a loop and drawing?
Post by: DasOhmoff San on June 01, 2019, 11:28:59 am
Hello, I just wanted to ask which operation would have theoreticly more performance.

Is it better to have for example 1000 different rectangle shapes that are drawn each frame.

Or is it better to have one single rectangle shape that gets changed in a loop and gets drawen 1000 times.
For example something like this:
for(sf::RectangleShape &shape : shapes)
{
    shape.setRotation(other_rotation);
    shape.setPosition(other_pos);
    shape.setFillColor(other_fill_color);
    shape.setOutlineColor(other_outline_color);

    window.draw(shape);
}

Which one is faster?
And what about sprites? Is it better to have 1000 sprites or one single sprite that gets changed every frame before drawing.
Title: Re: Drawing many different shapes? Or one shape changing in a loop and drawing?
Post by: eXpl0it3r on June 01, 2019, 12:13:37 pm
You're asking the wrong question, because instead of sprites and rectangles, you should be using vertex arrays or buffers instead. The most expensive operation overall is the draw call, with a vertex array you reduce that from 1000 calls to a single call. ;)
Title: Re: Drawing many different shapes? Or one shape changing in a loop and drawing?
Post by: DasOhmoff San on June 01, 2019, 11:14:49 pm
You're asking the wrong question, because instead of sprites and rectangles, you should be using vertex arrays or buffers instead. The most expensive operation overall is the draw call, with a vertex array you reduce that from 1000 calls to a single call. ;)

Ah I see, but how can I make a outline or a texture inside the rectangle then?
I think that is not possible with using vertecies only?
Title: Re: Drawing many different shapes? Or one shape changing in a loop and drawing?
Post by: Hapax on June 02, 2019, 12:16:21 am
It is possible (all the SFML shapes and their outlines are pretty much created using a vertex array) but you need to calculate those vertices manually.

This first vertex array tutorial may help you start off with them:
https://www.sfml-dev.org/tutorials/2.5/graphics-vertex-array.php

If you want separetad shapes on one vertex array, you'll need to use the sf::Triangles primitive and just specificy each of the three vertices of each triangle in the array.

You can texture vertex arrays too by specifying the texture coord in each vertex.
Title: Re: Drawing many different shapes? Or one shape changing in a loop and drawing?
Post by: DasOhmoff San on June 02, 2019, 02:41:35 am
Ah ok, thank you!  ;D