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

Author Topic: Combination of several sf::View  (Read 1321 times)

0 Members and 1 Guest are viewing this topic.

panhandler

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Combination of several sf::View
« on: July 27, 2017, 05:16:29 pm »
Hello,

I practice working with sfml and trying to write my own GUI.
Now I'm trying to create widgets that represent a container for other widgets and now I'm faced with a problem.
I have a widget called "Form" - it has its own sf :: View and an array with different widgets.
This widget is the base for all the others, which must be containers (Window, Group, Tab, ...). The rendering function looks like this:
void Form::draw(sf::RenderTarget& target, sf::RenderStates states) const {
    sf::View oldView = target.getView();
    target.setView(m_view);
    for (auto& widget : m_widgets) {
        target.draw(*widget, states);
    }
    target.setView(oldView);
}
Everything works fine if I do not use nested containers (because one view replaces another), but still, how do I implement this feature?
The only solution I could think of was to replace sf :: View with sf :: RenderTexture, like this:
void Form::draw(sf::RenderTarget& target, sf::RenderStates states) const {
    sf::RenderTexture render;

    render.create(m_width, m_height);
    render.clear(sf::Color::Transparent);
    for (auto& widget : m_widgets) {
        render.draw(*widget, states);
    }
    render.display();

    sf::Sprite sprite(render.getTexture());
    sprite.setPosition(m_position);

    target.draw(sprite, states);
}
And it even works, but the new problem is a very rare frame rate, so I can't use this implementation.
So far I don't know other methods of realizing my idea, maybe you have.
I will be glad to any help, thanks.
« Last Edit: July 27, 2017, 06:57:59 pm by Laurent »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Combination of several sf::View
« Reply #1 on: July 27, 2017, 06:59:48 pm »
The second solution could work, just don't create the render-texture every time. Re-use the same one as much as possible, and if it's bigger than needed then set the sprite's textureRect to draw only the relevant part of it.
Laurent Gomila - SFML developer

panhandler

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Combination of several sf::View
« Reply #2 on: July 27, 2017, 07:40:04 pm »
Unfortunately, this does not work, because redraw() will be called as often as the widgets from the container will visually change (when hovered, pressed, ...(and if there are many of them, then the chance for updating the renderer is higher))
Also, unstable rendering is complicated by the fact that widgets from the container don't have callbacks (not provided and not used in practice, except for this case), which could tell that the container needs redraw. In this case, I need to create a texture every time. That's why I can't avoid FPS losing  :-[