Skip to content

[3ds] Load custom font in ImGui backend #209

@marcie1000

Description

@marcie1000

I'm not sure if it's the right place to ask this, so I apologize if it's not.
I'm using the 3DS ImGui backend from FTPD for a little homebrew game project. I'd like to use a custom font with it but couldn't figure how to make it work.

I first tried to follow the official ImGui instructions :

// main.cpp

// (...)
if (!imgui::ctru::init()) 
    return false;
imgui::citro3d::init();

auto &io = ImGui::GetIO();
// (...)
ImFont* font = io.Fonts->AddFontFromFileTTF("romfs:/Roboto-Regular.ttf", 13.0f);

while (aptMainLoop()) {

    hidScanInput ();
    auto const kDown = hidKeysDown ();
    if (kDown & KEY_START)
        break;

    imgui::ctru::newFrame();
    ImGui::NewFrame();
    
    // (...)
}

But in imgui::ctru::newFrame() the following assertion fails :

imgui_ctru.cpp:246: void imgui::ctru::newFrame(): Assertion `io.Fonts->IsBuilt () && "Font atlas not built! It is generally built by the renderer back-end. Missing call to renderer _NewFrame() function?"' failed.

I suppose it means that I should call something like imgui::citro3d::newFrame() but (unless I'm mistaken) there's no such thing in the citro3d backend.
I then tried to build the font directly after loading TTF :

ImFont* font = io.Fonts->AddFontFromFileTTF("romfs:/Roboto-Regular.ttf", 13.0f);
io.Fonts->Build();

but the following assertion fails:

imgui_draw.cpp:2483: static bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas): Assertion `font_offset >= 0 && "FontData is incorrect, or FontNo cannot be found."' failed.

I don't understand what it means or if I'm doing something wrong.

In an attempt to understand how the citro3d backend loads fonts, I tried to modify imgui_citro3d.cpp to make it load my font instead of the 3ds system font, taking inspiration from this example code from devkitPro that loads a font with citro2d library. However it seams to have no effect, the rendered font is still the 3ds system font.

// imgui_citro3d.cpp
// (...)
#include <citro2d.h>
// (...)
// Redefine C2D_Font_s to avoid compiler error "invalid use of incomplete type 'struct C2D_Font_s'"
// from https://github.com/devkitPro/citro2d/blob/master/source/internal.h
struct C2D_Font_s
{
	CFNT_s* cfnt;
	C3D_Tex* glyphSheets;
	float textScale;
};
// (...)
void imgui::citro3d::init ()
{
    // (...)
    // load the glyph texture sheets
	// auto const font      = fontGetSystemFont ();
    auto const c2dfont = C2D_FontLoad("romfs:/Roboto-Regular.bcfnt");
    auto const font = c2dfont->cfnt;
    auto const fontInfo  = fontGetInfo (font);
	auto const glyphInfo = fontGetGlyphInfo (font);
	// (...)
}

So my question is: is there a way to load a custom (TTF) font in imgui with citro3d / ctru backends? Am I missing something?

Thanks in advance

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions