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

Author Topic: A couple of questions regarding sf::VertexArray and sf::Sprite  (Read 3202 times)

0 Members and 1 Guest are viewing this topic.

The Terminator

  • Full Member
  • ***
  • Posts: 224
  • Windows and Mac C++ Developer
    • View Profile
Hi there,

I have a couple questions on how to approach an issue I'm having. I have a tilemap that I just designed that draws ~3000 tiles, but slows to a crawl (~35 fps from 200 fps earlier). What I'm doing right now is having a "tile" be represented as a drawable sprite, and in its constructor the sprite is given a pointer to a texture in my resource manager. Because of this, only 1 texture has to be created per type of tile.

Now sf::Sprite is a very lightweight class, but I'm pretty confused as to why it's really bringing down fps, especially since there's an extremely low number of sf::Texture 's being used (Exactly 1, lol).

This brings me to my first question: why is this approach slowing down my program so significantly?

Second question: Would a sf::VertexArray be a much better approach? I'm pretty confused on how to define the vertices because my tile images are not packed into one sprite sheet, they're individual .png files.

Thanks!
Current Projects:
Technoport

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
AW: A couple of questions regarding sf::VertexArray and sf::Sprite
« Reply #1 on: April 05, 2014, 09:28:16 pm »
The issue is the number of draw calls that are being made (window.draw(x)). Each of them have a bit of overhead, thus if you do everything in one draw call, i.e. by using a vertex array, then you'll gain quite a bit of performance. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

The Terminator

  • Full Member
  • ***
  • Posts: 224
  • Windows and Mac C++ Developer
    • View Profile
Re: A couple of questions regarding sf::VertexArray and sf::Sprite
« Reply #2 on: April 05, 2014, 09:45:05 pm »
Thanks for the response. Do you think you could give me an example of using the vertex array the way that I want it? I looked at the vertex array tutorial but I'm sort of confused on how the vertex array actually gets the pixel data and how to draw it.

Thanks again!
Current Projects:
Technoport

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: A couple of questions regarding sf::VertexArray and sf::Sprite
« Reply #3 on: April 05, 2014, 10:29:09 pm »
A single vertex array can only use a single texture, but it is not a problem because this is exactly what you should do to get the best performances. Using one image per tile type is the worst strategy.
Laurent Gomila - SFML developer

The Terminator

  • Full Member
  • ***
  • Posts: 224
  • Windows and Mac C++ Developer
    • View Profile
Re: A couple of questions regarding sf::VertexArray and sf::Sprite
« Reply #4 on: April 05, 2014, 11:06:14 pm »
How do you pack the textures into one image in the tutorials Laurent?
Current Projects:
Technoport

coderx0x

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: A couple of questions regarding sf::VertexArray and sf::Sprite
« Reply #5 on: April 05, 2014, 11:13:06 pm »
How do you pack the textures into one image in the tutorials Laurent?

any picture editor

dabbertorres

  • Hero Member
  • *****
  • Posts: 506
    • View Profile
    • website/blog
Re: A couple of questions regarding sf::VertexArray and sf::Sprite
« Reply #6 on: April 06, 2014, 12:40:02 am »
How do you pack the textures into one image in the tutorials Laurent?
You could even write a program using SFML that takes multiple files, and packs them together into a tilemap. I did that for making frame animated sprites.

Mutoh

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: A couple of questions regarding sf::VertexArray and sf::Sprite
« Reply #7 on: April 06, 2014, 01:12:06 am »
You could pre-render your level with a sf::RenderTexture and sf::Sprite combo, Terminator.  :) Like so:

class Level : public sf::Drawable {
        public:
                std::vector<Tile> m_tiles;
               
                sf::RenderTexture m_renderer;
                sf::Sprite m_preRendered;
               
                // ---
               
                sf::Vector2u getSize() const;
               
                Level::Level() {
                        // insert tiles into m_tiles
                       
                        m_renderer.create(getSize());
                        m_preRendered.setTexture(m_renderer.getTexture());
                       
                        for (Tile& tile: m_tiles) {
                                m_renderer.draw(tile);
                        }
                }
               
                void draw (sf::RenderTarget& tar, sf::RenderStates states) const {
                        tar.draw(m_preRendered, states);
                }
};

If you want your level to have, for example, moving platforms, or tiles that fall upon being stepped on, make them separate entities. If you want something almost Worms-like and want any of your tiles to be destructible, you could make it so that fixed portions of your level (with the altered Tile) are re-rendered, or something else.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: A couple of questions regarding sf::VertexArray and sf::Sprite
« Reply #8 on: April 06, 2014, 01:46:23 am »
You could pre-render your level with a sf::RenderTexture and sf::Sprite combo, Terminator.  :) Like so:

.....

That works, but with 3000 tiles I am pretty sure that is going to exceed the maximum texture size  ;)
« Last Edit: April 06, 2014, 01:48:14 am by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

The Terminator

  • Full Member
  • ***
  • Posts: 224
  • Windows and Mac C++ Developer
    • View Profile
Re: A couple of questions regarding sf::VertexArray and sf::Sprite
« Reply #9 on: April 06, 2014, 01:52:36 am »
I did several optimizations in the rendering and updating of the tiles, and I brought my fps back up to about ~130 fps. I think I will stick with the approach I have because 3000 tiles will be like the max amount I will be drawing.

Thanks for all the responses!
Current Projects:
Technoport

 

anything