setGlobalFont

Started by Garwin, 05 May 2022, 21:37:26

Garwin

I tried to use different than default font.

Class Game - constructor:

tgui::Font m_tguiFont ("data/fonts/font.ttf");

Than there is another class handling menu and I would like to set global font for TGUI:

m_gui.setWindow (m_game->m_window);
tgui::Font::setGlobalFont (m_game->m_tguiFont);



Code is compiled without an issue but starting program it gives error which I have no clue why there is problem with backend.

TGUI assertion: getBackend() was called while there is no backend
Assertion failed!

Program: C:\Projects\Picture\bin\Debug\Picture.exe
File: C:/Program Files (x86)/Jenkins/workspace/TGUI-0.10/src/Backend/Window/Backend.cpp, Line 76

Expression: globalBackend != nullptr


Any help would be appretiated.

note: hideWithEffect is really nice function with abiity to give tgui::Picture a transparent layer blocking for certain time interacting with GUI. Really easy and nice.

texus

#1
You are attempting to create a tgui::Font object before the Gui object has been attached to a window.

One issue with TGUI supporting multiple rendering backends is that it complicates the initialization and destruction a bit. All TGUI objects (except a root Backend or Gui object) have to be initialized after the backend is created, and have to be destructed before the backend is destructed.

By default, TGUI hides the backend with the Gui class. Unless you explicitly construct a backend object in your code (which you probably shouldn't do), the backend will automatically be created when m_gui.setWindow is called and this backend would be destroyed when the (last) Gui object is destroyed.

If a TGUI function is called while the backend doesn't exist (not yet created or already destroyed), then you will get a "getBackend() was called while there is no backend" assertion (the getBackend() function is being called by the constructor of tgui::Font in the Class Game constructor this case).

The debugger can provide you with a call stack that shows where in your code such error occurs btw, in case you have trouble finding where the error is coming from in the future.

Garwin

Thanks a lot. It helps.