Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Drawing Sprites to the Window  (Read 3083 times)

0 Members and 1 Guest are viewing this topic.

coolp_jim

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Drawing Sprites to the Window
« 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:
  • Load textures that we'll need (only 6 different textures for now) into std::vector<sf::Texture>.
  • Pull data from a .csv file, 0 is empty space, not 0 is a wall, where {1,2,3,...} specify texture/sprite. I'm drawing a bunch of 10x10px "blocks" on a 10x10 grid.
  • Store map_data in std::vector<std::vector<int>>.
  • Loop through map_data, drawing by creating a sprite for each block from the &loaded_textures, setting it's position, then drawing. Return std::vector<sf::FloatRect> of all bounding boxes.

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();
}
 

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Drawing Sprites to the Window
« Reply #1 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. 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 drawable object in Selba Ward, which can display maps from data stored in a vector of ints  8)
« Last Edit: May 03, 2016, 11:07:03 pm by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

coolp_jim

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Drawing Sprites to the Window
« Reply #2 on: May 05, 2016, 09:43:05 pm »
Thanks for the help. Where did you come up with the name Selba Ward?

select_this

  • Full Member
  • ***
  • Posts: 130
  • Current mood: just ate a pinecone
    • View Profile
    • darrenferrie.com
Re: Drawing Sprites to the Window
« Reply #3 on: May 05, 2016, 10:07:25 pm »
Read the name backwards and you should see it :)
Follow me on Twitter, why don'tcha? @select_this

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Re: Drawing Sprites to the Window
« Reply #4 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

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Drawing Sprites to the Window
« Reply #5 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
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything