Page 1 of 1

Problem trying to use a custom font for the GUI

Posted: Wed Nov 01, 2017 7:36 pm
by tronfortytwo
Hi! This is my first project using Irrlicht.

I created a device and I am trying to create a GUI, but I fail to set a custom font as the one in use.
This is my code:

Code: Select all

 
    irr::gui::IGUIFont *tatofont;
    // guienv is the IGUIEnvironment pointer
    guienv->addFont("../data/fonts/Dashley.ttf", tatofont);
    
    if (tatofont)
         // skin is the IGUISkin pointer
        skin->setFont(tatofont);
    
    skin->setFont(guienv->getBuiltInFont(), irr::gui::EGDF_TOOLTIP);
    
    // background image
    guienv->addImage(
         // driver is the IVideoDriver pointer
        driver->getTexture("../data/artwork/background.jpg"),
        irr::core::position2d<int>(0,0) );
    
    // title
    guienv->addStaticText(
        L"TATO ADVENTURES",
        irr::core::rect<irr::s32>(10,10, window_x, window_y) );
    
    // new game button
    guienv->addButton(
        irr::core::rect<irr::s32>(20, 50, window_x/3, 70), 0, BUTTON_NEW,
        L"NEW GAME", L"Start the adventure!" );
        
    // quit button
    guienv->addButton(
        irr::core::rect<irr::s32>(20, 90, window_x/3, 110), 0, BUTTON_QUIT,
        L"QUIT", L"You really want to leave us?" );
    
    // render it all
    // device is the IrrlichtDevice pointer
    while(device->run() && driver)
        if (device->isWindowActive())
        {
            driver->beginScene(true, true, irr::video::SColor(0,200,200,200));
 
            guienv->drawAll();
    
            driver->endScene();
        }
 
Stdout doesn't print any error, and in fact everything works as expected. The only problem is that the font used by the GUI is still the default one, and not the one I loaded. I tryed also with a .bmp font with the same result. Thank you very much to anyone helps me out :)

Re: Problem trying to use a custom font for the GUI

Posted: Wed Nov 01, 2017 7:50 pm
by MartinVee
IGUIEnvironment::addFont adds a previously loaded font see reference. The "name" parameter is just the internal name of your font, and it's not a file path. The code as it is just pass an invalid (probably null) pointer to the addFont method, which probably does absolutely nothing since it's null.

Although Irrlicht doesn't support TTF Font, you can use the excellent CGUITTFont class (that you can find here). It's easy to integrate and use, and brings with it a nice i18n library!

Re: Problem trying to use a custom font for the GUI

Posted: Wed Nov 01, 2017 8:16 pm
by tronfortytwo
Thank you so much! You're right, I was missing a piece, now it does work. Thank you also for suggesting me the CGUITTFont class, I'll use it.