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

Author Topic: [SOLVED] accent marks  (Read 1794 times)

0 Members and 1 Guest are viewing this topic.

carlospereira

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
[SOLVED] accent marks
« 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
« Last Edit: March 18, 2013, 12:33:30 am by carlospereira »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: accent marks
« Reply #1 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.
Laurent Gomila - SFML developer

carlospereira

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: accent marks
« Reply #2 on: March 18, 2013, 12:31:52 am »
Using the std::wstring works fine!

Thank you very much!!