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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - jeans123

Pages: [1]
1
Graphics / Re: Drawable object inheritance
« on: March 17, 2017, 01:55:39 pm »
Woaaah, it was so obvious that I am angry at myself I didn't notice it.

Yes - thank you, you're right.

I use new operator because my class Button doesn't have default constructor, so I cannot declare it as a Menu class member like: Button b1 (it causes compilation error - no default constructor for Button class)

;)

2
Graphics / Drawable object inheritance
« on: March 17, 2017, 01:39:34 pm »
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?

3
General discussions / Including SFML headers - specific or whole modules?
« on: February 24, 2017, 09:44:23 am »
Hi,

I don't know if that's good principle that I follow but I always try to be as much specific and precisely as I can in my programs.
So the question is - should I include whole module e.g
#include <SFML/Graphics.hpp>
or should I use specific header with functions I need:
#include <SFML/Graphics/Texture.hpp>

For now I prefer 2nd way because it doesn't include whole bunch of classes that I do not need.
What is good practise? And is it just for SFML or in general?

Pages: [1]