The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
a question about drawing wchar_t with SDL_TTF
jeroen.clarysse


Joined: 22 Feb 2010
Posts: 69
given a std::wstring variable named m_text, I do the following :


size_t s = 2 * sizeof(wchar_t)*(m_text.size() + 1);
char * convertedChar = (char*) malloc(s);

size_t res=0;
ppw_wcstombs(&res, convertedChar, s, m_text.c_str(), m_text.size());

convertedChar[m_text.size()] = 0;

SDL_Surface *msg1Sur = TTF_RenderUTF8_Blended_Wrapped(m_font->get_ttf_font(), convertedChar, m_fontcolor, (Uint32)m_force_width);


this works nicely as long as the input string doesn’t contain unicode characters. As soon as I use something like a curly quote (“) the drawing stops right before that character


can anyone point me what I’m doing wrong ?
_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
a question about drawing wchar_t with SDL_TTF
jeroen.clarysse


Joined: 22 Feb 2010
Posts: 69
replying to myself… as usual, you can search for a solution a whole day and when you finally give up and ask for help, you bump into the solution while closing tabs on stackoverflow :-)

the solution is dead simple :


typedef std::basic_string<Uint16, std::char_traits<Uint16>, std::allocator<Uint16> > u16string;
u16string utext( m_text.begin(), m_text.end() );
SDL_Surface *msg1Sur = TTF_RenderUNICODE_Blended_Wrapped(m_font->get_ttf_font(), utext.c_str(), m_fontcolor, (Uint32)m_force_width);


i hope this helps others


Quote:
On 07 Nov 2016, at 12:34, jeroen clarysse wrote:

given a std::wstring variable named m_text, I do the following :


size_t s = 2 * sizeof(wchar_t)*(m_text.size() + 1);
char * convertedChar = (char*) malloc(s);

size_t res=0;
ppw_wcstombs(&res, convertedChar, s, m_text.c_str(), m_text.size());

convertedChar[m_text.size()] = 0;

SDL_Surface *msg1Sur = TTF_RenderUTF8_Blended_Wrapped(m_font->get_ttf_font(), convertedChar, m_fontcolor, (Uint32)m_force_width);


this works nicely as long as the input string doesn’t contain unicode characters. As soon as I use something like a curly quote (“) the drawing stops right before that character


can anyone point me what I’m doing wrong ?
_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org