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

Author Topic: [SOLVED] Can't load same font file twice  (Read 1537 times)

0 Members and 1 Guest are viewing this topic.

Pinco

  • Newbie
  • *
  • Posts: 13
    • View Profile
[SOLVED] Can't load same font file twice
« on: January 28, 2018, 01:14:29 pm »
I have two classes, 'Textbox' and 'Label', that display text and in order to have the font locally declared I'm loading the font every time in the constructor. However while in 'Label' it loads without issues in 'Textbox' it fails to load, is it because you can load a font from the same file only once?

Would a solution be loading the font in a sf::InputStream globally and then setting the font individually (in each constructor)?

Textbox constructor
Textbox::Textbox(int max_chars, bool numeric) {
        // Object constructor, creates a white box with black outlines.

        max_length = max_chars;
        m_gshape.setSize(Vector2f(6 + 15 * max_length, 30));
        m_gshape.setFillColor(Color::White);
        m_gshape.setOutlineThickness(2);
        m_gshape.setOutlineColor(Color(60, 60, 60));

        if (!default_font.loadFromFile(FONT_LOCATION))
                cout << "ERROR LOADING FONT" << endl;

        m_textbox.setFont(default_font);
        m_textbox.setCharacterSize(25);
        m_textbox.setFillColor(Color::Black);

        // Fill the TB with tight amount of chars in order to build the box around it.
        if (max_chars > 1)
                m_textbox.setString(to_string((int)pow(10, max_chars - 1)));    // Sets the length of the box relative to the maxium amount of chars that can be entered.
        else
                m_textbox.setString("0");       // You can only enter 1 char so 0 is just a placeholder to build the TB

        if (numeric) {  // Values between ASCII 47->58 are the numbers 0->9
                min_ascii = 47;
                max_ascii = 58;
        }
}

Label constructor
Label::Label(string text) {
        // Object constructor, black text 25 pixel high.
        if (!default_font.loadFromFile(FONT_LOCATION))
                cout << "ERROR LOADING FONT" << endl;

        m_label.setFont(default_font);
        m_label.setCharacterSize(25);
        m_label.setFillColor(Color::Black);
        m_label.setString(text);

        m_label.setOrigin(m_label.getGlobalBounds().left, m_label.getGlobalBounds().top);
}

sf::Font default_font is declared locally in the header of each class as a private member.
« Last Edit: February 04, 2018, 01:02:54 pm by Pinco »
My potato is 5 petaflops faster than your high end pc.
https://imgur.com/gallery/0S5spVm

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10837
    • View Profile
    • development blog
    • Email
Re: Can't load same font file twice
« Reply #1 on: January 28, 2018, 02:53:15 pm »
Check the console to know why it failed to load. Loading the same font multiple times is certainly possible.

Generally it's advised to load fonts and other resources just once. I tend to use a resource holder as Thor provides and pass resources down by reference: http://www.bromeon.ch/libraries/thor/tutorials/v2.0/resources.html
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Pinco

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Can't load same font file twice
« Reply #2 on: February 04, 2018, 01:02:40 pm »
Check the console to know why it failed to load. Loading the same font multiple times is certainly possible.

Generally it's advised to load fonts and other resources just once. I tend to use a resource holder as Thor provides and pass resources down by reference: http://www.bromeon.ch/libraries/thor/tutorials/v2.0/resources.html

Turns out I had the FONT_LOCATION string poorly declared as a const string in the header, it's now working correctly.
My potato is 5 petaflops faster than your high end pc.
https://imgur.com/gallery/0S5spVm

 

anything