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

Author Topic: Combine two or more drawable objects into one  (Read 3717 times)

0 Members and 1 Guest are viewing this topic.

notthatimportantboi

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Combine two or more drawable objects into one
« on: September 20, 2017, 12:14:20 am »
Hey Guys,
I was wondering is there a way to combine two or more drawable objects into one? Like sf::RectangleShape and sf::Text into one drawable object called let's say combShape? Because I want to draw these objects with one call of mWindow.draw(combShape)

Hapax

  • Hero Member
  • *****
  • Posts: 3381
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Combine two or more drawable objects into one
« Reply #1 on: September 20, 2017, 12:27:01 am »
Yes, but the result may or may not be what you want.

You can create a class that stores as many objects as you need, inherit the class from sf::Drawable, implement the draw method by drawing all of the objects and then the class can be drawn as if a single drawable.
Note that this reduces your code to window.draw(allTheThings); but does not reduce the number of graphics driver draw calls; it is still one per drawable within the class. It's worth noting that this shouldn't be a problem until you're making many hundreds of draw calls...

The other option (to reduce graphics driver draw calls) can be a lot more complicated depending on which drawables you are using. The idea is to create a single object that draws everything; chances are, you'll need a sf::VertexArray (or a vector of sf::Vertex) for this. This means, though, that you have to be able to (re)construct the objects you require to 'batch'. The most complicated of objects would be sf::Text; it's probably best to just stick to a draw call per text...
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Combine two or more drawable objects into one
« Reply #2 on: September 20, 2017, 06:27:12 am »
... and the last option is to draw everything to a sf::RenderTexture, store the result and draw it with a sprite.
Laurent Gomila - SFML developer

Hapax

  • Hero Member
  • *****
  • Posts: 3381
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Combine two or more drawable objects into one
« Reply #3 on: September 20, 2017, 01:01:22 pm »
Yes, if the objects don't change much, the render texture is most likely the better option.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

notthatimportantboi

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Combine two or more drawable objects into one
« Reply #4 on: September 21, 2017, 07:25:34 am »
Thank you guys! I think I will inherit the sf::Drawable and just override the function with the things I want to draw.

 

anything