How to include TGUI into a state manager

Started by Demonic Ice, 28 November 2015, 16:12:30

Demonic Ice

Hello TGUI-forum!

I recently decided to give TGUI a try and wanted to include it into my engine.
However I am not able to set it up as I want.

First of all there is my overall cpp part which calls the game to run.
Then the next called cpp file creates the new state via constructor.
The following step would be calling the state's voids:

1. An event void
Dealing with all the event points.

2. An update void
Just updating or doing whatever the state is supposed to do.

3. A draw function
Drawing all visualised things.

All these 3 are passing the arguement of the SFML window.

Now, my target is to efficiently part TGUI within this structure and not outside.

In contrast I succeeded to plainly implement TGUI into the cpp where the window itself gets defined.
Also I did succeed to implement TGUI into the draw void of the state.
However both of these ways are not in my interest of a final way of working.
The first one denies me to efficiently work with TGUI.
On the second attempt it will declare TGUI over and over again.

Therefore I tried this:

tgui::Gui gui(window);

became
tgui::Gui gui;
and got declared in the class part from the state in which I wanted to use TGUI.

Then the constructor adds all the buttons I wanted to display.

gui.setFont("assets/fonts/OpenSans-Regular.ttf");
auto play = std::make_shared<tgui::Button>();
play->setPosition(tgui::bindWidth(gui)*0.15, tgui::bindHeight(gui)*0.125);
play->setSize(tgui::bindWidth(gui)*0.7, tgui::bindHeight(gui)*0.15);
play->setText("Play");
gui.add(play);


Handling the events will then simply be stored in the handle event void.
However now I start to get to some issues.

How do I tell where the window is at for TGUI?
I tried something ugly with a bool and an if already.
Sadly I get a null pointer error once my program gets to the moment of drawing, though.

The bool thing was something like:
Quote
if (!gui_active){

gui_active = true;
tgui::Gui gui(window);

}
if (gui_active){

gui.draw();

}
The bool was set to false in first place of course. Moreover this felt really bad in terms of design.

Sadly gui.draw() lacks the possibility to pass the window as an arguement.
Therefore I am a bit clueless right now.

I would be really happy if anyone could help me out of this problem!

Thanks for taking your time to read this thread!





texus

A gui is bound to a certain window, if you don't pass the window in the constructor then you have to call gui.setWindow(window) once from somewhere else.

The best place to store the gui is probably where you store your window. Without a gui you would need the window in all of these stages anyway, now you just need both the window and the gui (they can be in some struct of data that has to be shared between all these places). So why can't you pass such a struct as argument instead of only the sfml window?

Demonic Ice

Thanks a lot, texus!!

I feel a bit embarrassed now since the solution would have been so much easier as you just stated.
Well, when I said that I only pass the window, I had my first logical mistake already.
I am actually passing everything and not just the window.

Therefore I now declared your gui at the same place where I define my window.
Then I pass this object to my constructor and do the rest as I wanted.

Well and sorry, I should have checked your documentation for the gui.setWindow().

However you really helped me a lot in the end!!! Also with such an incredible quick reply, wow.
I am amazed by your passion, keep it up! Have a great day!