Terrance8D
Terrance8D

Reputation: 193

Making custom types drawable with SFML

I picked up using SFML recently, and I decided as a learning experience I would make a pong clone using it. I've defined a class called Ball which draws uses SFML to draw a RectangleShape. When I try to draw this custom type to the screen with the window.draw() function however, I get errors because Ball isn't an sf::Drawable. I would appreciate help with this, being new to SFML.

Upvotes: 3

Views: 4448

Answers (1)

Conduit
Conduit

Reputation: 2735

To use window.draw(object) object's class must inherit the drawable interface and implement the abstract sf::Drawable::draw function.

It sounds like the sf::RectangleShape is a member of Ball. SFML knows how to render the shape, but not Ball itself. Ball's class declaration should look like this:

class Ball : public sf::Drawable //,...
{
    //...
    private:
    virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
    //...
};

And draw should be implemented like this:

void Ball::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
    //assuming m_shape is the sf::RectangleShape
    target.draw(m_shape, states);
}

Upvotes: 5

Related Questions