Jump to content
  • Advertisement
Sign in to follow this  
sheep19

load unicode text from file and rendering in SDL

This topic is 2600 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to load some unicode text from a text file and render it in SDL. Here's my code:


SDL_Color c = {0x00, 0xff, 0x00};
std::wifstream in(L"data/string.txt");
std::wstring str;
in >> str;

SDL_Surface *msg = TTF_RenderUNICODE_Blended(font, (Uint16*)str.c_str(), c);


font is defined as a TTF_Font object.

The text file contains the string "?????". However, it's not displayed, but some other characters (like garbage).
I tried changing the encoding of the file to other encodings like ANSI, utf-8, utf-8 without BOM, UCS2 BE, UCS2 LE but none displays the desired result. The characters change when I change the encodings but they remain garbage.

I changed the encodings using notepad++.

What is the problem? I thought UCS2 BE or LE would work because I am using an std::wstring on Windows, but it doesn't. What's wrong?

Share this post


Link to post
Share on other sites
Advertisement
You just stumbled into one of the major quirks of the standard library.

Rather than try to explain it myself, allow me to point you to a good explanation somebody else gave me when I stumbled into this same scenario.

Link to post
Link to full thread

It might be smarter to use UTF8 encoding for your files, and then read it in with a regular ifstream, then convert the UTF8 string to wstring with MultiByteToWideChar.

Share this post


Link to post
Share on other sites
If you decide to use UTF-8 for your files you can just use TTF_RenderUTF8_Blended and no conversion is needed.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!