Hello.
In my project I really needed multicolor text, for showing info about game objects and chat text, like this:
Price: 2500 Goldor
Damage: 100-120But I didn't found a solution online, so I wrote one by myself.
Usage:Basically, this is just a copy of original sf::Text class with one modification in ensureGeometryUpdate method.
When setting Text string, you can specify new text color using % character (like printf):
FText text;
text.setFont(yourfont);
text.setPosition(100,100);
text.setString("%0Format%2ted %3text");
which will draw:
Formatted textEscaping % is done by writing two %:
test.setString("%1Damage: %2100 %%");
Damage: 100 %Character-color pair is translated in
sf::Color FText::getColorForChar(char c):
sf::Color FText::getColorForChar(char c) {
switch (c)
{
case '0':
return sf::Color::Black;
case '1':
return sf::Color::White;
case '2':
return sf::Color::Red;
case '3':
return sf::Color::Green;
case '4':
return sf::Color::Blue;
case '5':
return sf::Color::Yellow;
case '6':
return sf::Color::Magenta;
case '7':
return sf::Color::Cyan;
default:
return sf::Color::Black;
}
}
You can expand number of available colors by adding new cases to this method.
Code:
Header:
https://gist.github.com/MrOnlineCoder/579ddc7c513a8fc6f5b024aa8b43ffbbSource:
https://gist.github.com/MrOnlineCoder/69a3bd3e18e2958526b6bac14dd8d0c9I am open to any suggestions or comments.