SFML community forums

Help => General => Topic started by: coolp_jim on May 03, 2016, 10:09:28 pm

Title: Drawing Sprites to the Window
Post by: coolp_jim on May 03, 2016, 10:09:28 pm
When drawing to the window, my application runs fine (at 60fps is what I'd like) with small window/view sizes. When I increase view size to show more of the drawn map, it then starts to slow. Large window, small map size helps a little bit. I'd like to bounce my design choices off the community here, if that's OK.

Here's the flow of the program:

Having run gprof, I'm seeing a lot of time spent in that final point there, mainly creating the sf::Sprite and drawing, which can be seen below. I've tried creating a std::vector<sf::Sprite> to pull from in the same manner as I'm pulling from needed_textures right now, but didn't see any speed increase. Map size is 35x29 right now, so say ~500 10x10px sprites are being drawn per frame.

sf::FloatRect drawBlock(std::vector<sf::Texture> &needed_textures, int block_type, int left, int top, sf::RenderWindow &window)
{
    sf::Sprite block_sprite;

    block_sprite.setTexture(needed_textures.at(block_type - 1));

    block_sprite.setPosition(left, top);
    window.draw(block_sprite);

    return block_sprite.getGlobalBounds();
}
 
Title: Re: Drawing Sprites to the Window
Post by: Hapax on May 03, 2016, 11:04:23 pm
Each drawing of a sprite "costs" a draw call. That is, each one is treated individually and the graphics card needs to set up everything (including texture) each time. This can slow down the frame considerably if there are many.

What you are probably looking for is drawing all of the blocks in one go (one draw call) or at least only a few.
This can be achieved using sf::VertexArray (http://www.sfml-dev.org/tutorials/2.3/graphics-vertex-array.php). You can display multiple (connected or unconnected) triangles (and quads) using just one draw call. By "multiple", I mean "a lot"! One caveat is that a single vertex array can only use a single texture. The best option is often to compound textures into a single image and use that image as the texture for the vertex array which would display all of those objects. If you must have separate textures, you can use more than one vertex array.

Since it sounds like you might be attempting to display a grid-based tile map, you may be interested in the Tile Map (https://github.com/Hapaxia/SelbaWard/wiki/Tile-Map) drawable object in Selba Ward (https://github.com/Hapaxia/SelbaWard/wiki), which can display maps from data stored in a vector of ints  8)
Title: Re: Drawing Sprites to the Window
Post by: coolp_jim on May 05, 2016, 09:43:05 pm
Thanks for the help. Where did you come up with the name Selba Ward?
Title: Re: Drawing Sprites to the Window
Post by: select_this on May 05, 2016, 10:07:25 pm
Read the name backwards and you should see it :)
Title: Re: Drawing Sprites to the Window
Post by: Mortal on May 05, 2016, 10:51:10 pm
Read the name backwards and you should see it :)

OMG, thats cool, i like the hidden name behind it. ;D
Title: Re: Drawing Sprites to the Window
Post by: Hapax on May 09, 2016, 12:38:04 am
Thanks for the help.
You're welcome. I'm glad I could be of some assistance.

Read the name backwards and you should see it
Don't just give it away :P