1

I'm creating text in SFML 2.1 (It doesn't really matter) in c++.

Setting a text string looks like that: text.setString("something");

Ok, but because my game language is Polish, I have to enter some characters like ą,ż,ł,ś,ć etc. , which are not 'supported' in my game's ASCII encoding.

Iv'e came up with this solution: text.setString(L"śomęthińg");

But problems appear when you try to combine wstring and string from another function. For example:

text.setString(L"Name: " + PlayerName() );

I've tried converting it to string or creating temporary variables, but either did not work, or it erased this 'special characters'...

FULL EXAMPLE:

std::string PlayerName()
{
    std::string name = "John";
    return name;
}

int main()
{
    sf::Text hello;
    hello.setString(L"Hello " + PlayerName() + L", how are you?");
    //I need to use L" "

    window.draw(hello);
}

Any idea?

Bionicl
  • 46
  • 7
  • Please provide a http://stackoverflow.com/help/mcve – Yakk - Adam Nevraumont Jan 26 '15 at 20:30
  • 1
    What type is returned by `PlayerName()`? Can you provide a code sample that lets us see what you're doing? – templatetypedef Jan 26 '15 at 20:31
  • You need to convert `PlayerName()` to a wstring. You can use the code from this post to do that: http://stackoverflow.com/a/18597384/4342498 – NathanOliver Jan 26 '15 at 20:47
  • 2
    Basically what you're going to want to do is replace all `std::string` with `std::wstring` wherever possible :/ Actually, I just saw SFML has it's own string type. Use _that_: [sf::String](http://www.sfml-dev.org/documentation/2.0/classsf_1_1String.php) – Mooing Duck Jan 26 '15 at 21:02
  • Isn't there any option to change Unicode or something related to it, in order to just use normal string in the whole project ? – Bionicl Jan 26 '15 at 21:04
  • 1
    @Bionicl: Visual Studio has a `Character Set: Use Unicode Character Set`, and it is completely unrelated and doesn't help you in the slightest here. – Mooing Duck Jan 26 '15 at 21:06
  • @Bionicl - That setting affects the `TCHAR`, `LP(C)TSTR`, `_T() macro` and things like that. It has absolutely no effect on `std::string ` and `std::wstring`. – PaulMcKenzie Jan 26 '15 at 21:12

1 Answers1

1

Ok, I got it!

As @MooingDuck said, SFML has it's own string type, which is very powerful in this type of problems.

The way I've did it:

std::string PlayerName()
{
    std::string name = "John";
    return name;
}

int main()
{
    sf::String helloText = L"Hello ";
    helloText += PlayerName();
    helloText += L", how are you?";

    sf::Text hello;
    hello.setString(helloText);

    window.draw(hello);
}

Thanks a lot for help!

Bionicl
  • 46
  • 7