pthom / hello_imgui

Hello, Dear ImGui: unleash your creativity in app development and prototyping
https://pthom.github.io/hello_imgui
MIT License
606 stars 91 forks source link

Cannot load a font during frame rendering #53

Closed XMNXofficial closed 1 year ago

XMNXofficial commented 1 year ago

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

pthom commented 1 year ago

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:

https://github.com/pthom/hello_imgui/blob/0a2859a942c3d4fb86b652929b7f3e4b1cd98e76/src/hello_imgui_demos/hello_imgui_demo_classic/hello_imgui_demo_classic.main.cpp#L4-L11

Then, you need to use RunnerParams and fill the correct callback: https://github.com/pthom/hello_imgui/blob/0a2859a942c3d4fb86b652929b7f3e4b1cd98e76/src/hello_imgui_demos/hello_imgui_demo_classic/hello_imgui_demo_classic.main.cpp#L15-L22

And then use the font: https://github.com/pthom/hello_imgui/blob/0a2859a942c3d4fb86b652929b7f3e4b1cd98e76/src/hello_imgui_demos/hello_imgui_demo_classic/hello_imgui_demo_classic.main.cpp#L40-L43

XMNXofficial commented 1 year ago

Thank you very much for your great help!