Discussion:
[SDL] a question about drawing wchar_t with SDL_TTF
jeroen clarysse
2016-11-07 11:34:07 UTC
Permalink
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 ?
jeroen clarysse
2016-11-07 11:45:27 UTC
Permalink
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
Post by jeroen clarysse
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
Loading...