I have class Menu that inherits from public Drawable and override draw method and owns Button class instance.
class Menu : public sf::Drawable
{
public:
Menu();
virtual ~Menu();
protected:
private:
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
Button* m_Button_3x3;
Button* m_Button_9x9;
Button* m_Button_16x16;
Button* m_Button_99x99;
sf::Sprite* m_Sprite3x3;
sf::Sprite* m_Sprite_9x9;
sf::Sprite* m_Sprite_16x16;
sf::Sprite* m_Sprite_99x99;
sf::Texture m_texture;
};
It uses constructor to define Sprites and Buttons:
Menu::Menu()
{
m_texture.loadFromFile("menu.png");
m_Sprite3x3 = new sf::Sprite(m_texture, sf::IntRect(0,0,400,300));
m_Sprite_9x9 = new sf::Sprite(m_texture, sf::IntRect(400,0,800,300));
m_Sprite_9x9->setPosition(400, 0);
m_Sprite_16x16 = new sf::Sprite(m_texture, sf::IntRect(0,300,400,600));
m_Sprite_16x16->setPosition(0, 300);
m_Sprite_99x99 = new sf::Sprite(m_texture, sf::IntRect(400,300,800,600));
m_Sprite_99x99->setPosition(400, 300);
Button* m_Button_3x3 = new Button(m_Sprite3x3);
Button* m_Button_9x9 = new Button(m_Sprite_9x9);
Button* m_Button_16x16 = new Button(m_Sprite_16x16);
Button* m_Button_99x99 = new Button(m_Sprite_99x99);
}
It contains Buttons that I want to display by using theirs overridden function draw:
class Button : public sf::Drawable
{
friend class Menu;
public:
Button(sf::Sprite* sprite);
virtual ~Button();
protected:
private:
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
sf::Sprite m_sprite;
};
Here's function draw with global extern window (window is only one so I think it's not too bad to have it as a global var):
void Button::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
extern sf::RenderWindow app;
app.draw(m_sprite);
}
but somehow I cannot call Buttons::draw in Menu::draw : - causes Segfault
void Menu::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
extern sf::RenderWindow app;
m_Button_3x3->draw(app, states);
}
that causes Segfault as well:
void Menu::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
extern sf::RenderWindow app;
app.draw(*m_Button_3x3);
}
works only that but it's not elegant - moreover - it displays correctly but causes segfault when app is closing:
void Menu::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
extern sf::RenderWindow app;
app.draw(*m_Sprite3x3);
}
So what is correct way to inherit Drawable in dervied class? Like
app.draw(menu) ->
menu::draw(){app.draw(button)}->
button::draw(){app.draw(member_button_sprite)}
Is it a more elegant, clever way to do that?