The Group::draw function is not supposed to be called directly. With the current design of the gui, you cannot draw widgets directly, all drawing has to happen via gui.draw().
What you are trying to do isn't supported, the gui always draws everything at once, the only solutions that I can provide are workarounds.
The code that you need looks more like this:
#include "TGUI/TGUI.hpp"
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "Group Test");
tgui::Gui gui(window);
tgui::Button::Ptr button_1 = tgui::Button::create("Button 1");
button_1->setPosition(5, 5);
tgui::Button::Ptr button_2 = tgui::Button::create("Button 2");
button_2->setPosition(5, 30);
tgui::Group::Ptr group = tgui::Group::create();
group->setSize(400, 300);
group->add(button_1);
gui.add(group);
tgui::Group::Ptr group2 = tgui::Group::create();
group2->add(button_2);
gui.add(group2);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
window.close();
break;
}
gui.handleEvent(event);
}
window.clear();
group->setVisible(true);
group2->setVisible(false);
gui.draw();
// SFML rendering here will be between group and group2
group->setVisible(false);
group2->setVisible(true);
gui.draw();
window.display();
}
}
Alternatively, using a canvas:
#include "TGUI/TGUI.hpp"
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "Group Test");
tgui::Gui gui(window);
tgui::Button::Ptr button_1 = tgui::Button::create("Button 1");
button_1->setPosition(5, 5);
tgui::Button::Ptr button_2 = tgui::Button::create("Button 2");
button_2->setPosition(5, 30);
tgui::Group::Ptr group = tgui::Group::create();
group->setSize(400, 300);
group->add(button_1);
gui.add(group);
// Warning: the default canvas size is (100%,100%) to fill the screen which can cause performance overhead when changing the gui view.
// Set a static size (by passing size to create function or calling setSize) if you don't want it to resize itself when resizing the gui.
tgui::Canvas canvas = tgui::Canvas::create();
gui.add(canvas);
tgui::Group::Ptr group2 = tgui::Group::create();
group2->add(button_2);
gui.add(group2);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
window.close();
break;
}
gui.handleEvent(event);
}
// This clear/draw/display code can be called anywhere, if the contents is static you could even do it before the main loop
canvas->clear(sf::Color::Transparent);
// SFML rendering here will be between group and group2
canvas->display();
window.clear();
gui.draw();
window.display();
}
}