pthom / hello_imgui

Hello, Dear ImGui: unleash your creativity in app development and prototyping

Home Page:https://pthom.github.io/hello_imgui

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot load a font during frame rendering

XMNXofficial opened this issue · comments

Hellođź‘‹!
I want to load a ttf font into hello_imgui,but it can't work;

void Draw()
{
    ImFont* font1;    ImFont* font1;
    static bool bool1 = true;
    if (bool1)
    {
        font1 = HelloImGui::LoadFontTTF_WithFontAwesomeIcons(
            "/usr/share/fonts/truetype/ms-core-fonts/AndaleMo.TTF", 25.0f);
        bool1 = false;
    }

    ImGui::PushFont(font1);
    ImGui::Text("Hello");
    ImGui::PopFont();

⬇⬇It likely load success,but it show "unknown".
And when i select it,my program will crash ( show: 18762 segmentation fault (core dumped) ./hello_world ).
image

_example_integration.zip

Hi,

Thanks for sharing your demo code, it makes it easier to answer.

Fonts need to be loaded at the appropriate moment during initialization. I updated a demo example to showcase how to load fonts.

First, you will need to create a function (or lambda) for loading the font:

// Demonstrate how to load additional fonts (fonts - part 1/3)
ImFont * gCustomFont = nullptr;
void MyLoadFonts()
{
HelloImGui::ImGuiDefaultSettings::LoadDefaultFont_WithFontAwesomeIcons(); // The font that is loaded first is the default font
gCustomFont = HelloImGui::LoadFontTTF("fonts/Akronim-Regular.ttf", 40.f); // will be loaded from the assets folder
}

Then, you need to use RunnerParams and fill the correct callback:

HelloImGui::RunnerParams params;
params.appWindowParams.windowGeometry.size = {1280, 720};
params.appWindowParams.windowTitle = "Dear ImGui example with 'Hello ImGui'";
params.imGuiWindowParams.defaultImGuiWindowType = HelloImGui::DefaultImGuiWindowType::NoDefaultWindow;
// Fonts need to be loaded at the appropriate moment during initialization (fonts - part 2/3)
params.callbacks.LoadAdditionalFonts = MyLoadFonts; // LoadAdditionalFonts is a callback that we set with our own font loading function

And then use the font:

// Demo custom font usage (fonts - part 3/3)
ImGui::PushFont(gCustomFont);
ImGui::Text("Custom font");
ImGui::PopFont();

Thank you very much for your great help!