SFML community forums

Help => Graphics => Topic started by: efeman on July 18, 2013, 02:29:15 am

Title: Extending RectangleShape and Rendering
Post by: efeman on July 18, 2013, 02:29:15 am
I think this problem is from my lack of C++ experience, so forgive me if it's silly. I'm creating a Tile class for the usual reasons, and wanted to just extend RectangleShape so that I could keep all the nice things already available and just add extras for my implementation. So I have it defined as:

class Tile : public sf::RectangleShape {
public:
        Tile(sf::Vector2f pos, const sf::Color color);
        void render(sf::RenderWindow& window);
};
 

The confusion I'm having is with how to render something like this. With a RectangleShape, you can just call window.draw(rectshape). I was hoping to define it something like

void Tile::render(sf::RenderWindow& window) {
        window.draw(this);
}
 

but alas it doesn't work that way.
Title: Re: Extending RectangleShape and Rendering
Post by: eXpl0it3r on July 18, 2013, 03:06:18 am
I advise against the additional inheritance from sf::RectangleShape, you can instead add the rectangle shape as a member.
Then you could inherit from sf::Drawable and implement the draw function accordingly. That way you'll be able to call window.draw(myTile)
Title: Re: Extending RectangleShape and Rendering
Post by: efeman on July 18, 2013, 03:42:15 am
Ah, that makes sense. So, in this scheme, all the positional and visual data of the tile would be kept within that RectangleShape member. The extra information I was considering is pathfinding related, like weights, isWall, etc.

Thanks.