SFML community forums

Help => Graphics => Topic started by: Gaius on August 11, 2014, 04:48:33 pm

Title: How to make Class drawable?
Post by: Gaius on August 11, 2014, 04:48:33 pm
I'd like to make a class I wrote in C++ drawable by typing:

Class class;
window.draw(class);

instead of:

class.draw(window);

I read through the tutorial on how to do this, but I don't really understand how to implement it into my code.
It would really help me out if someone could briefly explain what is meant in the tutorial or could possibly give me an example of how to do this.
Title: Re: How to make Class drawable?
Post by: Laurent on August 11, 2014, 05:03:21 pm
The tutorial code can't be simpler:

class MyEntity : public sf::Drawable
{
private:

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

MyEntity entity;
window.draw(entity); // internally calls entity.draw

Now I guess that you wonder what to put in the draw function. It's simple: draw all the drawable SFML objects that your class has (VertexArray, Sprite, Text, Shape).

void MyEntity::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
    target.draw(m_sprite, states);
    target.draw(m_text, states);
    ....
}
Title: Re: How to make Class drawable?
Post by: G. on August 11, 2014, 05:04:55 pm
Don't forget the documentation, it contains an exemple of what you want to achieve in its detailed description. http://www.sfml-dev.org/documentation/2.1/classsf_1_1Drawable.php
Title: Re: How to make Class drawable?
Post by: Gaius on August 11, 2014, 05:15:14 pm
Now I guess that you wonder what to put in the draw function. It's simple: draw all the drawable SFML objects that your class has (VertexArray, Sprite, Text, Shape).

void MyEntity::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
    target.draw(m_sprite, states);
    target.draw(m_text, states);
    ....
}

Yeah, that was what I was stuck at. Thanks, I think I get it now.
Title: Re: How to make Class drawable?
Post by: Gaius on August 11, 2014, 05:16:24 pm
Don't forget the documentation, it contains an exemple of what you want to achieve in its detailed description. http://www.sfml-dev.org/documentation/2.1/classsf_1_1Drawable.php

That's what I meant by tutorial :P.