Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Extending RectangleShape and Rendering  (Read 2246 times)

0 Members and 1 Guest are viewing this topic.

efeman

  • Newbie
  • *
  • Posts: 8
    • View Profile
Extending RectangleShape and Rendering
« 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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Extending RectangleShape and Rendering
« Reply #1 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)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

efeman

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Extending RectangleShape and Rendering
« Reply #2 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.

 

anything