Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Failing To Load Font  (Read 17537 times)

0 Members and 1 Guest are viewing this topic.

azoundria

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Failing To Load Font
« on: January 03, 2014, 10:52:12 pm »
My program:

int main()
{
        sf::Font font;
        if (!font.loadFromFile("Xolonium-Regular.otf"))
        {
                // error...
        }
    sf::RenderWindow window(sf::VideoMode::getDesktopMode(), "SFML works!", sf::Style::Fullscreen);
        window.setVerticalSyncEnabled(true);

        sf::Text title;
        title.setFont(font); // font is a sf::Font
        title.setString("Hello World!");
        title.setCharacterSize(64); // in pixels, not points!
        title.setColor(sf::Color::White);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
                        switch (event.type)
                        {
                                case sf::Event::KeyPressed: //Key pressed
                                        switch (event.key.code)
                                        {
                                        case sf::Keyboard::Escape:
                                                window.close();
                                                break;
                                        }
                                        break;
                        }
        }

        // clear the window with black color
        window.clear(sf::Color::Black);
               
        window.draw(title);
        window.display();
    }

    return 0;
}

Displays this weird text in the command prompt:



And displays this dialog:



According to this thread, OTF format should be supported:
http://en.sfml-dev.org/forums/index.php?topic=72.0

When I open the font it works fine to view in Windows. It's a font that's available on this site here:
http://www.fontspace.com/severin-meyer/xolonium

The font is in the same directory as sfml-graphics-2.dll and others... which I assume is the correct directory. It seems awfully strange if this is simply failing to find the file.

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: Failing To Load Font
« Reply #1 on: January 03, 2014, 11:48:49 pm »
I had a similar issue some time ago. I linked the release libs in Debug-mode. So you should check your linker settings because for me (latest SFML source) the font is working correctly.



AlexAUT


azoundria

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: Failing To Load Font
« Reply #2 on: January 04, 2014, 12:25:35 am »
By debug-mode, do you mean this:



And by release libs, are they under linker > additional dependencies?



I tried to change each name to add a -d for debug, but the error persists. I am now trying from scratch with the arial font from Windows/fonts.

#include <SFML/Graphics.hpp>
#include <iostream>

int main () {

        sf::Font font;
        if (!font.loadFromFile("arial.ttf"))
        {
                std::cout << "Load Failed!" <<std::endl;
                return 0;
        }

        sf::RenderWindow window(sf::VideoMode(200, 200), "SFML Text Test!");
        sf::Text text;

        // select the font
        text.setFont(font); // font is a sf::Font

        // set the string to display
        text.setString("Hello world");

        // set the character size
        text.setCharacterSize(24); // in pixels, not points!

        // set the color
        text.setColor(sf::Color::Red);

        // set the text style
        text.setStyle(sf::Text::Bold | sf::Text::Underlined);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();
        window.draw(text);
        window.display();
    }

    return 0;
}

It runs into that error before even going into the failure code.

Daddi

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
    • http://foxdev.de/
    • Email
Re: Failing To Load Font
« Reply #3 on: January 04, 2014, 01:08:35 am »
From where do you load the font file? It should be in the same folder as the executable. You can't read from Windows' global fonts :)

cal

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Failing To Load Font
« Reply #4 on: January 04, 2014, 03:23:14 am »
I agree with AlexAUT. It seems as though, he's set his debug configuration to use the release libs (although it's hard to tell by the screenshot since the text is cutoff).
You've got to change the configuration so that building in debug uses the debug libs like so (notice the -d after the lib name):
http://www.mediafire.com/view/pkyz60ecwdpwf2d
and so that building in release uses the release libs:
http://www.mediafire.com/view/qacelje8ysmwbxq
« Last Edit: January 04, 2014, 03:26:14 am by cal »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Failing To Load Font
« Reply #5 on: January 04, 2014, 09:42:00 am »
Make sure you do a clean rebuild after changing the link settings to use debug libs.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

azoundria

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: Failing To Load Font
« Reply #6 on: January 05, 2014, 02:34:27 am »
Thanks For Your Help!

I now have a beautiful text title!


On this page here:
http://www.sfml-dev.org/tutorials/2.0/start-vc.php

I would suggest to also highlight in red the 'Configuration: Release' as it comes up by default in Debug. Or, set the files to -d instead.


And perhaps the note from below that table can be included in the text and fonts tutorial, to be extra safe.

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: Failing To Load Font
« Reply #7 on: January 05, 2014, 02:52:56 am »
And perhaps the note from below that table can be included in the text and fonts tutorial, to be extra safe.

When you link against the wrong libs its random what will happen/where it will crash. So this is not a good idea  ;)



AlexAUT

vodo232

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: Failing To Load Font
« Reply #8 on: November 02, 2024, 05:39:22 pm »
so, this can also happen because of a bad ttf file. for example, this arial.ttf made me debug for a long time for my wsl cpp program: https://github.com/matomo-org/travis-scripts/blob/master/fonts/Arial.ttf