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);