Edit: Moved the thread to SFML development and give a more descriptive title in light of the sub-forum change
#include <SFML/Graphics.hpp>
#include <cstdlib>
void breakFont(sf::Font& f)
{
for (int i = 0; i < 30; ++i)
{
char a = 'a' + std::rand() % ('z' - 'a' + 1); //random char from 'a'..'z'
f.getGlyph(a, 30u, false);
}
}
int main(int argc, char * argv[])
{
std::srand(std::time(0x0));
sf::RenderWindow app(sf::VideoMode(640u, 480u), "Font");
app.setFramerateLimit(30u);
bool broken = false;
const std::string filename("OpenDyslexic-Regular.ttf");
sf::Font font;
font.loadFromFile(filename);
sf::Text text("font is broken", font, 30u);
while (app.isOpen())
{
sf::Event eve;
while (app.pollEvent(eve))
{
if (eve.type == sf::Event::Closed) app.close();
if (!broken && eve.type == sf::Event::KeyPressed)
{
broken = true;
font.loadFromFile(filename);
breakFont(font);
}
}
app.clear();
app.draw(text);
app.display();
}
}
Change filename to any font you want to use, it shouldn't matter, I used OpenDyslexic from
http://opendyslexic.org/Basically sf::Text displays garbage when sf::Font is reloaded and glyphs of same size get loaded in other order, if no glyphs get loaded it'll just display nothing, but is well defined, operator[] on map will insert new default constructed GlyphTable and return it's default constructed sf::Texture resulting in nothing.
It's a non issue for almost all sane use cases but I'm putting it out here with example so that it's known, I couldn't find it when I searched, if you want to ensure sf::Text is ok you can
call text.setString(text.getString()) clear the string and set it again to force updateGeometry to be called (you can't call it directly, it's private).