I use a (admittedly Windows specific) error box if font loading fails:
MessageBox(HWND_DESKTOP,"Failed loading arial.ttf","Fatal Error",MB_OK);
But once I have a font loaded I created a little (ugly) method of popping up a error message in the middle of the screen:
//pushes message 'stringIn' to the top of the rendering window
//automatically word wraps and centers text
void Game::centerScreenMessage(const std::string &stringIn)
{
sf::RectangleShape rect;
rect.setSize(sf::Vector2f(500.f,200.f));//setup a rectangle thats 500 pixels wide and 200 tall
rect.setFillColor(sf::Color(0,0,51)); //fill it with dark blue
rect.setPosition(float(int(mWindow.getSize().x/2.f - rect.getSize().x/2.f)),float(int(mWindow.getSize().y/2.f - rect.getSize().y/2.f))); //center the rectangle
sf::Text myText; //setup a text object to hold all the drawing information
myText.setFont(loginFont); //load the font
myText.setCharacterSize(24); //set size
myText.setColor(sf::Color(204,204,204)); //text color = light grey
myText.setString(stringIn); //set the string
sf::IntRect cSize = loginFont.getGlyph('a',myText.getCharacterSize(),false).bounds; //used to determine font height, character 'a' doesn't matter
std::deque<int> textSize; //container to hold widths of individual lines
textSize = wrapText(myText,rect); //fill container and linebreak myText
std::deque<sf::Text> texts; //make a container to hold all the new lines of text
int j=0;
mWindow.draw(rect); //draw the text box
for (unsigned int i = 0; i < textSize.size(); i++)
{
texts.push_back(sf::Text(myText)); //put a new element on
//set position so that the text is center justified:
texts.back().setPosition(float(int(mWindow.getSize().x/2 - textSize[i]/2)),float(int(mWindow.getSize().y/2 - (cSize.height*textSize.size()) / 2 + (i * cSize.height))));
j = myText.getString().toAnsiString().find('\n',j); //find the line break
if (j < 0)
texts.back().setString(myText.getString().toAnsiString()); //this is probably redundant
else
{
texts.back().setString(myText.getString().toAnsiString().substr(0,j)); //break the text string up and then continue processing
myText.setString(myText.getString().toAnsiString().substr(j+1));
}
mWindow.draw(texts.back()); //draw the text on top of the textbox
}
mWindow.display();
}
//wraps text 'textIn' by inserting line breaks based on the width of 'boundingBox' and the font and size of 'textIn'
//returns a deque containing the line width of every line that was segmented
std::deque<int> wrapText(sf::Text &textIn, const sf::RectangleShape &boundingBox)
{
int lineSize = 0; //counter used to see how long our current line is
int lastSpace = -1; //counter to keep track of the location of the last space we found
int maxWidth = 0;
std::deque<int> output;
sf::String outputString = "";
sf::String lastLine;
sf::Font fontIn = *textIn.getFont();
lastLine = textIn.getString();
for (unsigned int i = 0; i < lastLine.getSize(); i++)
{
sf::Glyph glyph = fontIn.getGlyph(lastLine[i], textIn.getCharacterSize(),false); //get the current character
if ((lineSize + glyph.advance) > boundingBox.getSize().x) //check for runoff
{
if (lastSpace != -1)
{
output.push_back(maxWidth- fontIn.getGlyph(' ',textIn.getCharacterSize(),false).advance); //put a new element in, but remove the size of the last space
outputString = outputString + lastLine.toAnsiString().substr(0,lastSpace) + '\n'; //put the current length of string onto the first line
lastLine = lastLine.toAnsiString().substr(lastSpace+1); //put the remainder back into lastLine to continue processing
}
else //processing one really long string with no spaces...
{
output.push_back(lineSize); //put the size of the current string of characters
outputString = outputString + lastLine.toAnsiString().substr(0,i) + '\n'; //put the current length of string onto the first line
lastLine = lastLine.toAnsiString().substr(i); //put the remainder back into lastLine to continue processing
}
i=-1; //reset our counters -- time to find out if you like overflow
lastSpace = -1;
maxWidth = 0;
lineSize = 0;
}
else
{
lineSize += glyph.advance; //increment linesize by the texture size of the current character
if (lastLine[i] == '\n') //if we come accross a natural line break
{
maxWidth = lineSize - glyph.advance; //ignore the size of the linebreak
output.push_back(maxWidth);
maxWidth = 0;//reset counters
lineSize = 0;
lastSpace = -1;
}
if (lastLine[i] == ' ') //track where the space is
{
lastSpace = i;
maxWidth = lineSize; //set string width to cutoff point
}
}
}
output.push_back(lineSize);
outputString = outputString + lastLine; //put the remainder of the string back in
textIn.setString(outputString);
return output;
}
I think I've done too much coding today, I just tried to preview this post by hitting F7 (build)...