SFML community forums

Help => Graphics => Topic started by: carlospereira on March 17, 2013, 03:25:22 am

Title: [SOLVED] accent marks
Post by: carlospereira on March 17, 2013, 03:25:22 am
Hi,

I need some help about displaying a std::string with special characters (accent marks, such as á, é, ã, ...).
I tried this to test the sf::string and std::string:

        sf::String str;
        std::string str2;

        ...

        while (window.pollEvent(event)) {
                ...
                case sf::Event::TextEntered:
                        str += event.text.unicode;
                        str2 += event.text.unicode;
                ...
        }

        window.draw(sf::Text(str));
        window.draw(sf::Text(str2));

The first draw draws correctly, but the second just shows a square for every special character (extended ascii codes, ie, event.text.unicode > 127).

But I need to use the std::string to manipulate strings, thus I can't use the first draw!!! And I can't display the text correctly using this class!!!

Can somebody help me?

PS.: I'm using the SFML 2.0.

Thank you,
Carlos
Title: Re: accent marks
Post by: Laurent on March 17, 2013, 09:20:53 am
event.text.unicode is a UTF-32 character. If you directly cast it to an 8-bit char, you get a latin-1 (ISO 8859-1) character. But when you pass the string to the sf::Text object, it is converted to UTF-32 using the provided locale; by default, it uses the current global locale which is the "C" one by default, and the character encoding used by this locale is not latin-1.

You see, it can become pretty complicated when you deal with encodings. The best solution to avoid that is to use sf::String or std::wstring. If you really want to keep std::string, you must convert it to UTF-32 yourself using the sf::Utf32::fromLatin1 function.
Title: Re: accent marks
Post by: carlospereira on March 18, 2013, 12:31:52 am
Using the std::wstring works fine!

Thank you very much!!