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

Author Topic: SFML multicolor text class  (Read 5770 times)

0 Members and 1 Guest are viewing this topic.

MrOnlineCoder

  • Jr. Member
  • **
  • Posts: 87
    • View Profile
SFML multicolor text class
« on: December 09, 2017, 07:51:27 pm »
Hello.

In my project I really needed multicolor text, for showing info about game objects and chat text, like this:

Price: 2500 Gold

or

Damage: 100-120

But 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 text

Escaping % 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/579ddc7c513a8fc6f5b024aa8b43ffbb
Source: https://gist.github.com/MrOnlineCoder/69a3bd3e18e2958526b6bac14dd8d0c9

I am open to any suggestions or comments.

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: SFML multicolor text class
« Reply #1 on: December 09, 2017, 09:01:22 pm »
I think a better approach would be to do it like a stream with operators or using some BB-code like syntax. That'd be more flexible/nice IMO. I think that idea was floating around on the forum before.
Back to C++ gamedev with SFML in May 2023

MrOnlineCoder

  • Jr. Member
  • **
  • Posts: 87
    • View Profile
Re: SFML multicolor text class
« Reply #2 on: December 09, 2017, 09:11:29 pm »
Well, it just satisfies my needs, so I didn't think about bb code or streams so.  :)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: SFML multicolor text class
« Reply #3 on: December 10, 2017, 02:39:11 am »
If you ever need something a little cleaner, you might find this RichText class useful.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

MrOnlineCoder

  • Jr. Member
  • **
  • Posts: 87
    • View Profile
Re: SFML multicolor text class
« Reply #4 on: December 10, 2017, 11:41:35 am »
Wow, haven't found that! Thanks

 

anything