SFML community forums

Help => Graphics => Topic started by: Aiden_Cohen on March 31, 2018, 09:46:59 pm

Title: Bundling multiple drawable objects into a returnable object
Post by: Aiden_Cohen on March 31, 2018, 09:46:59 pm
I am interested in creating a separate function to draw my menu screen in a game I'm making, how would I accomplish this in sfml and what would i need the function to return?
Title: Re: Bundling multiple drawable objects into a returnable object
Post by: Hapax on April 01, 2018, 06:57:23 pm
You could create a new class that inherits from sf::Drawable and then override the draw function so that it draws all the objects you need. Then, when you draw your new object, it draws everything at once.

Here's a basic example snippet:
class NewDrawable public sf::Drawable
{
public:
    // ...

private:
    virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
    sf::RectangleShape m_rectangle;
    sf::CircleShape m_circle;
    sf::Sprite m_sprite;
}

void NewDrawable::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
        target.draw(m_rectangle);
        target.draw(m_circle);
        target.draw(m_sprite);
}