Not Important
Not Important

Reputation: 13

SFML combine drawable objects

I want to combine let's say sf:RectangleShape and sf::Text into one drawable object so that I only have to call the function mWindow.draw() once for the two objects. Is it possible?

Upvotes: 1

Views: 846

Answers (1)

Benjamin Lindley
Benjamin Lindley

Reputation: 103713

What you can do is implement a class which inherits from sf::Drawable, and override the draw function to draw both your rectangle and your text. Your render window will take care of the rest.

class BoxAndText : public sf::Drawable
{
public:
    sf::RectangleShape rect;
    sf::Text text;
private:
    void draw(sf::RenderTarget& target, sf::RenderStates states) const override
    {
        target.draw(rect, states);
        target.draw(text, states);
    }
};

Upvotes: 1

Related Questions