2 tgui::Gui

Started by Flaze07, 11 July 2017, 18:33:05

Flaze07

hi...is it allowed to have more than one tgui::Gui

if it is allowed...I wonder why menu.draw makes my program crash

here's part of the code

while (app_win.isOpen())
    {
        sf::Event event;

        if (condition == Condition::start)
        {
            while (app_win.pollEvent(event))
            {
                if (event.type == sf::Event::Closed) app_win.close();
                menu.handleEvent(event);
            }

            app_win.clear();
            menu.draw();
            app_win.display();
        }
        else if (condition == Condition::single)
        {
            sf::Time elapsed = clock.restart();
            snake1.update(elapsed);
            snake1.action();
            snake1.check_apple(apple);
            if (snake1.collision(app_win))
            {
                condition = Condition::over;
            }

            app_win.clear();
            app_win.draw(apple.getShape());
            for (auto& a : snake1.getSnake())
            {
                app_win.draw(a);
            }
            app_win.display();
        }
        else if (condition == Condition::multi)
        {
            sf::Time elapsed = clock.restart();
            snake1.update(elapsed);
            snake1.action();
            snake1.check_apple(apple);
            if (snake1.check_snake(snake2))
            {
                condition = Condition::over;
            }
            if (snake1.collision(app_win))
            {
                condition = Condition::over;
            }
            snake2.update(elapsed);
            snake2.action();
            snake2.check_apple(apple);
            if (snake2.check_snake(snake1))
            {
                condition = Condition::over;
            }
            if (snake2.collision(app_win))
            {
                condition = Condition::over;
            }
            app_win.clear();
            app_win.draw(apple.getShape());
            for (auto& a : snake1.getSnake())
            {
                app_win.draw(a);
            }
            for (auto& a : snake2.getSnake())
            {
                app_win.draw(a);
            }
            app_win.display();
        }
        else if (condition == Condition::over)
        {
            while (app_win.pollEvent(event))
            {
                if (event.type == sf::Event::Closed) app_win.close();
                over.handleEvent(event);
            }

            if (oldcon == Condition::multi);
            app_win.clear();
            over.draw();
            app_win.display();
        }


full code is here https://gist.github.com/Flaze07/f008e65c0c85bcf44f0c46037447f7b5

Flaze07

I decided to make it like this
before

menu->add(button1, "button1");

after

menu->add(button1);


and now...the problem is in menu.handleEvent(event)
and somehow there's problem in...let me attach an image

texus

You can have multiple Gui objects, although it is advised to use a single Gui object and have multiple Panel/Group widgets inside it of which you only show one and hide the others.

The draw function fails at an assert. Some compilers and IDEs will clearly display the assertion in the output, but I know that some just crash without displaying much information. The reason why it asserts is because the Gui objects are not correctly initialized. They need to be told to which window they have to draw. The initialization should look like this (or use the setWindow/setTarget function if you can't initialize them on the same line).
Code (cpp) Select
tgui::Gui menu{app_win};
tgui::Gui over{app_win};


Edit: are u running the code in release mode instead of debug mode? It looks like assertions are disabled based on your image.

Flaze07

haha silly me,
forgot to initialize them with the sf::RenderWindow...
now I look like a total idiot

Flaze07

also do you prefer

tgui::Gui gui{app_win};

or

tgui::Gui gui(app_win);

?

texus

I prefer the first one.

Flaze07