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

Author Topic: Wide string literal from string stored in variable  (Read 2072 times)

0 Members and 1 Guest are viewing this topic.

Kladdy

  • Newbie
  • *
  • Posts: 12
    • View Profile
Wide string literal from string stored in variable
« on: August 12, 2019, 01:20:47 am »
Im having trouble with some accented European characters (åäö). I have read the documentation, and I know that you are supposed to use a wide literal for SFML to be able to display these characters.

The font I am using has these characters, as I can create a sf::Text with the European characters. Say I have the following code snippets:

This one works as expected:

sf::Text t;
t.setString(L"åäö");
t.setFont(...);
window.draw(t);

which draws 'åäö'.

But I have no idea how to draw a string containing "åäö" from a string variable? I tried to do this:

std::string temp = "åäö";
sf::String s(temp);

sf::Text t;
t.setString(s.toWideString());
font, draw, etc...

I also tried this

std::string narrow_string("åäö");
std::wstring wide_string = std::wstring(narrow_string.begin(), narrow_string.end());
const wchar_t* result = wide_string.c_str();

sf::Text t;
t.setString(result);
font, draw, etc...

but that does not work either. Any ideas?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Wide string literal from string stored in variable
« Reply #1 on: August 14, 2019, 02:50:01 am »
Rather than using wide characters, it's probably better to use UTF-8 encoded strings. They can be stored in standard (non-wide) strings.

The string literals would be specified like this:
u8"åäö"
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Kladdy

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Wide string literal from string stored in variable
« Reply #2 on: August 15, 2019, 04:42:27 pm »
Rather than using wide characters, it's probably better to use UTF-8 encoded strings. They can be stored in standard (non-wide) strings.

The string literals would be specified like this:
u8"åäö"
What are the advantages of this compared to wide strings?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Wide string literal from string stored in variable
« Reply #3 on: August 15, 2019, 07:32:18 pm »
Quote
What are the advantages of this compared to wide strings?
The definition of a wide string is rather fuzzy. The type, wchar_t, has no clear definition and has a different size on different platforms. Regarding the encoding, I don't think it is specified at all.
It's the same with string literals such as "...", it's hard to figure out which encoding is used to store the data.
UTF-8, on the other hand, is a well-known standard that has a clear definition and is easy/safe/portable to work with.
Laurent Gomila - SFML developer