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 constructorTextbox::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 constructorLabel::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.