2 tgui::Gui
« on: 11 July 2017, 18:33:05 »
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
Code: [Select]
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

Re: 2 tgui::Gui
« Reply #1 on: 11 July 2017, 18:42:01 »
I decided to make it like this
before
Code: [Select]
menu->add(button1, "button1");
after
Code: [Select]
menu->add(button1);

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

*

texus

  • *****
  • 1143
    • View Profile
    • Texus's Blog
Re: 2 tgui::Gui
« Reply #2 on: 11 July 2017, 18:44:17 »
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).
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.

Re: 2 tgui::Gui
« Reply #3 on: 11 July 2017, 18:47:59 »
haha silly me,
forgot to initialize them with the sf::RenderWindow...
now I look like a total idiot

Re: 2 tgui::Gui
« Reply #4 on: 11 July 2017, 18:49:30 »
also do you prefer
Code: [Select]
tgui::Gui gui{app_win};
or
Code: [Select]
tgui::Gui gui(app_win);
?

*

texus

  • *****
  • 1143
    • View Profile
    • Texus's Blog
Re: 2 tgui::Gui
« Reply #5 on: 11 July 2017, 18:54:45 »
I prefer the first one.

Re: 2 tgui::Gui
« Reply #6 on: 11 July 2017, 18:55:46 »
ahh thank you