SFML community forums

Help => Graphics => Topic started by: fralexxelarf on March 14, 2017, 02:46:17 pm

Title: Entity/quad drawing order and VertexArrays
Post by: fralexxelarf on March 14, 2017, 02:46:17 pm
Hello!

I have a RenderingSystem that loops through a vector of SpriteBatches, one for each texture atlas/spritesheet. The SpriteBatch has a VertexArray that has quads for each instance of X-type entity that uses that texture.

Spritebatch
class SpriteBatch : public sf::Drawable, public sf::Transformable
{
public:
        unsigned int m_VerticesCount;
        sf::VertexArray m_Vertices;
        sf::Texture* m_pTexture;
public:
    SpriteBatch(sf::Texture* texture)
        m_pTexture(texture), m_VerticesCount(0)
    {
            m_Vertices.setPrimitiveType(sf::Quads);
    }

    void draw(sf::RenderTarget& target, sf::RenderStates states) const
    {
            // Apply the transform
            states.transform *= getTransform();

            // Apply the tileset texture
            states.texture = m_pTexture;

            // Draw the vertex array
            target.draw(m_Vertices, states);
    }
};

RenderingSystem render method.
// Loops through all spritebatches and draws them.
void RenderingSystem::render(sf::RenderWindow& window)
{
        for (auto spritebatch : m_SpriteBatches) window.draw(*spritebatch);
}

Do you have any tips on how I would go about ordering the individual quads above/below eachother?

I've come up with this idea about moving the quads(where Z != 0) to a new, temporary SpriteBatch, then loop through all new spritebatches and draw them in order, from lowest to highest Z. (Z values would be stored relative to quad positions and also in each new SpriteBatch.) Does that sound plausible? Or would it be too slow and better to just use some list of entities?

I hope your understand my question, otherwise tell me and I will try to clarify as well as I can. :)


Thanks!
Title: Drawing order and spritebatches
Post by: eXpl0it3r on March 14, 2017, 03:10:48 pm
Whenever you add a sprite to the batcher, make it take a Z position value and then correctly order the vertices in the vertex array.
I also suggest to just use std::vector<sf::Vertex> directly, that way you're way more flexible.
Title: Re: Entity/quad drawing order and VertexArrays
Post by: fralexxelarf on March 14, 2017, 03:35:10 pm
Ahh ok! Then it basically becomes like ordering a vector of sf::Sprites, but vertices instead? But this assumes all sprites that could have different Z values use the same texture, right? (and then static Z value-Sprites/Vertices, like GUI and tiles in another texture & batch?)

Thanks for the fast reply.

Edit: clarification
Title: Re: Entity/quad drawing order and VertexArrays
Post by: eXpl0it3r on March 14, 2017, 04:01:46 pm
Yes, if you want to mix the order of different texture objects, then you'll have to use multiple vertex array for the same texture.
However, many such situations can be prevented by packing textures in a smart way.
Title: Re: Entity/quad drawing order and VertexArrays
Post by: fralexxelarf on March 14, 2017, 04:16:53 pm
Ok nice, I think I got it then!

Thanks a lot! :)