Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot load a font during frame rendering #53

Closed
XMNXofficial opened this issue Apr 4, 2023 · 2 comments
Closed

Cannot load a font during frame rendering #53

XMNXofficial opened this issue Apr 4, 2023 · 2 comments

Comments

@XMNXofficial
Copy link
Contributor

XMNXofficial commented Apr 4, 2023

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
Copy link
Owner

pthom commented Apr 4, 2023

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();

@pthom pthom changed the title TTF Font Load can't work in Ubuntu22.10 Cannot load a font during frame rendering Apr 4, 2023
@XMNXofficial
Copy link
Contributor Author

Thank you very much for your great help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants