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

Author Topic: Built a button, have a question  (Read 1247 times)

0 Members and 1 Guest are viewing this topic.

grim

  • Guest
Built a button, have a question
« on: January 31, 2014, 12:46:09 am »
So I built a button class, this is the code more or less:

class GButton(float x,float y,void (*function)()) : public sf::RectangleShape(sf::vector2f(x,y)){}
    public:
        void functionCaller();
 

And it works, i can draw it to the window, the GButton can fully utilize all of the RectangleShape functions, the functionCaller() calls the function pointer and the called function does its job.

My question is this: is it possible to modify the class so that it draws the rectangleshape and a text object to the screen at the same time?

dabbertorres

  • Hero Member
  • *****
  • Posts: 506
    • View Profile
    • website/blog
Re: Built a button, have a question
« Reply #1 on: January 31, 2014, 05:47:26 am »
Short answer: yes!

Long answer: If you inherit from sf::Drawable, you are able to implement:
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const

Which would then let you do:
someSFMLWindow.draw(GButtonObject);
 

For an example, here is my code for a button:
class Button : public Widget
{
    // stuff
    private:
        sf::Text text;
        sf::Sprite sprite;
        virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
        {
                    target.draw(sprite, states);
                    target.draw(text, states);
            }
}
 
(my Widget class inherits from sf::Drawable)

Just realized, as you already inherit from sf::RectangleShape, you may not have to explicitly inherit from sf::Drawable yourself.

Read more here.
« Last Edit: January 31, 2014, 05:51:50 am by dabbertorres »

 

anything