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

Author Topic: Processing an sf::Drawable derived class during draw method  (Read 2493 times)

0 Members and 1 Guest are viewing this topic.

Ilman

  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Email
Okay, so here's the situation:
I have a Button class, containing sf::Text and a sf::RectangleShape and a sf::FloatRect, telling what part of the window it must cover. I need to draw it on 2 different windows and I want it to scale with those windows automatically. The thing is that virtual void draw is const, so I can't change the size of the RectangleShape and the Text while drawing.
So, currently I have 2 choices:
Either copy the text and rectangleshape to new ones and draw those, which is waste of performance and typing or
create a specific update function for this case and call it every time the windows get resized which may make my code a lot more messy and I need to track that for every single button I create, which may cause bugs.
So, I was wondering if there is an easier way to accomplish this or resort to the second option?

My current code:

in UI\Button.hpp
namespace UI
{
    class Button : public sf::Drawable, public sf::Transformable
    {
        private:
            sf::Text mText;
            sf::FloatRect mButtonSize;
            sf::RectangleShape mButtonRect;
            ...



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

in UI\Button.cpp
void UI::Button::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
    states.transform *= getTransform();

    sf::Vector2f tmp = sf::Vector2f(target.getView().getSize().x * mButtonSize.width, target.getView().getSize().y * mButtonSize.height);

    mButtonRect.setSize(tmp);                    //changing size causes error due to const
    mButtonRect.setPosition(target.getView().getSize().x * mButtonSize.left, target.getView().getSize().y * mButtonSize.top);


    mText.setCharacterSize(mButtonRect.getSize().y * mTextRelativeSize);

    target.draw(mButtonRect, states);
    target.draw(mText, states);
}

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: Processing an sf::Drawable derived class during draw method
« Reply #1 on: May 29, 2016, 07:46:09 pm »
Easy method: mutable

Ilman

  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Email
Re: Processing an sf::Drawable derived class during draw method
« Reply #2 on: May 29, 2016, 07:49:46 pm »
Okay...I didn't know that existed. Thanks man.  ;D

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Processing an sf::Drawable derived class during draw method
« Reply #3 on: May 29, 2016, 10:09:31 pm »
This keyword is not made to get around design decisions. If you modify an object's state in a const function with the help of the mutable keyword, you're violently breaking the assumption that const functions can't change an object's state. This keyword should be reserved to members that are not part of the public state, the typical example being mutexes.

The only contract that sf::Drawable brings is a const draw function. If this contract doesn't suit your needs, then there's a simple solution: don't use it. Make your own drawable system with non-const draw functions.
Laurent Gomila - SFML developer

 

anything