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

Author Topic: SFML inheritance and sf::Drawable  (Read 3546 times)

0 Members and 1 Guest are viewing this topic.

Chunker120

  • Newbie
  • *
  • Posts: 31
    • View Profile
SFML inheritance and sf::Drawable
« on: September 14, 2013, 09:08:24 pm »
I have 3 simple classes.

class Element : public sf::Drawable
{
     private:
                ....

    public:
         virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const {}
};

class Button : public gui::Button
{
    public:
        virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
        {
              switch(type)
                {
                        case ButtonType::TextOnly:
                                if(displayBody)
                                        target.draw(body);

                                target.draw(text);
                                break;

                        case ButtonType::IconOnly:
                                if(displayBody)
                                        target.draw(body);

                                target.draw(icon);
                                break;

                        case ButtonType::IconText:
                                if(displayBody)
                                        target.draw(body);

                                target.draw(text);
                                target.draw(icon);
                                break;
                }
        }
};

class UserInterface : public sf::Drawable
        {
                private:
                        std::map<std::string, Element> components;

                public:
                        UserInterface();

                        virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
                        void add(std::string name, Element element);

                        std::map<std::string, Element> getList() { return components; }
                        std::vector<Element> etc;
        };

and this is the draw function in UserInterface:
void gui::UserInterface::draw(sf::RenderTarget& target, sf::RenderStates states) const
        {
                for (auto && iter : components)
                {
                        target.draw(iter.second);
                }
        }

What this should do is simple, it creates a container class called UserInterface, to which you can add any class that inherits from Element. Every class that inherits from the base class Element has a different overriden draw() function.

The problem is, this doesn't work. It successfully adds new classes to the container, however the draw() function doesn't work. It should be as simple as:

Button button(etc);
UserInterface UI;

UI.add(button);

...

window.draw(UI);

Kojay

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Re: SFML inheritance and sf::Drawable
« Reply #1 on: September 14, 2013, 09:23:56 pm »
You will need to implement the draw function for Element.

Chunker120

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: SFML inheritance and sf::Drawable
« Reply #2 on: September 14, 2013, 09:29:30 pm »
You will need to implement the draw function for Element.
Can you elaborate? Because I can't really make the element draw() function do anything, since element is just an interface.

Kojay

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Re: SFML inheritance and sf::Drawable
« Reply #3 on: September 14, 2013, 09:42:37 pm »
You decide what Element is. I have merely observed you doing this:

for (auto && iter : components)
{
    target.draw(iter.second);
}
 

For this to work, the draw function for Element needs to have been implemented.

Since you inherit from it and have it overriden, then you must make sure the function from the derived object is called. In which case, you will need to be storing pointers to Element in your map, or you fall foul of object slicing.

« Last Edit: September 14, 2013, 09:46:58 pm by Kojay »