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

Author Topic: How to make Class drawable?  (Read 4640 times)

0 Members and 1 Guest are viewing this topic.

Gaius

  • Newbie
  • *
  • Posts: 11
    • View Profile
How to make Class drawable?
« 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: How to make Class drawable?
« Reply #1 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);
    ....
}
Laurent Gomila - SFML developer

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: How to make Class drawable?
« Reply #2 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

Gaius

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: How to make Class drawable?
« Reply #3 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.

Gaius

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: How to make Class drawable?
« Reply #4 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.