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

Author Topic: draw function with several drawable in an object  (Read 1102 times)

0 Members and 1 Guest are viewing this topic.

djarkan

  • Newbie
  • *
  • Posts: 16
    • View Profile
draw function with several drawable in an object
« on: July 21, 2023, 02:35:06 pm »
hi

i have an object drawable with 5 sf::Sprite and a bitmapText
i did first this draw function before i tried to make the object drawable

void BottomPanel::draw()
{
   for(auto i = 0; i < 5; ++i) {
         m_window.draw(*m_sprites[i]);
    }
    m_window.draw(m_Text);
}


}


was good, tranformation are drawn good

now i have this

void BottomPanel::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
    for(auto& elem : m_sprites) {
        states.transform *= elem->getTransform();
        states.texture = elem->getTexture();
        target.draw(*elem, states);
    }

    states.transform *= m_Text.getTransform();
    states.texture = m_Text.getTexture();
    target.draw(m_Text, states);

}

i have a problem with the states.transform *= m_Text.getTransform();
sprites are not drawn where it should be

it's ok if i remove these 2 lines

how do I "fix" a position coords for my object, then i can adapt sprites coords by this origin ?

the contained objects are sf::Transformable so if i set a rotation it s drawn good without
states.transform *= m_Text.getTransform();

so do i need to get transform or not ?
same with textures ?

draw what i want with that one

void BottomPanel::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
    for(auto& elem : m_sprites) {
        target.draw(*elem, states);
    }
    target.draw(m_Text, states);
}


thx
« Last Edit: July 21, 2023, 10:23:57 pm by djarkan »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: draw function with several drawable in an object
« Reply #1 on: July 22, 2023, 05:53:43 pm »
You are multiplying the transform by each sprite in turn so each following sprite has each previous sprite(s)'s transform included.

Try storing the passed transform and then applying it for each object.

Something like this, maybe:
const sf::RenderStates baseStates{ states };
for (auto& elem : m_sprites) {
    states.transform = baseStates * elem->getTransform();
    // ..
    target.draw(*elem, states);
}

states = baseStates;

// ..
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*