SFML community forums

Help => Window => Topic started by: ElChupacabra on October 25, 2008, 03:26:43 am

Title: [solved]Drawable Class
Post by: ElChupacabra on October 25, 2008, 03:26:43 am
Hi guys,
Can you maybe help me with this? How do I create a class which I can hand over to the sf::RenderWindow::Draw() function? Lets say my class consists of drawable objects like sf::String and sf::Shape. In other words what I want to do is:
Code: [Select]

class myButton
{
  Shape shape;
  String string;
  myButton(..stuff) { ... stuff}
};
int main()
{
  RenderWindow App();
  myButton button();
  App.Draw(button);
...
}
Title: [solved]Drawable Class
Post by: Imbue on October 25, 2008, 04:05:02 am
Give this a try.

Code: [Select]
class myButton : public sf::Drawable
{
    public:
        myButton() {}

    private:
        sf::Shape shape;
        sf::String string;

        virtual void Render(sf::RenderTarget& Window) const
        {
            Window.Draw(shape);
            Window.Draw(string);
        }
};


BTW, this will also give your button lots of other methods like SetPosition() etc. SFML handles all that automatically. :D
Title: [solved]Drawable Class
Post by: ElChupacabra on October 26, 2008, 08:18:15 pm
Thanks for the answer, looks good. But I keep getting the message "`RenderTarget' has not been declared". I use Codeblocks on windows. System.hpp,Window.hpp and Graphics.hpp are included.
It works however when I redefine the Render function this way:
Code: [Select]
protected: virtual void Render(const RenderWindow &Window) const
Title: [solved]Drawable Class
Post by: Laurent on October 26, 2008, 08:31:41 pm
RenderTarget is for SVN (next) version.