Using unifont with SDL_TTF

Hi,

I’ve written a small console in SDL2 with SDL_TTF on Windows (in Visual Studio 12).
As a font I’m using Unifont. I want to display any character in the font, but any way I tried
has either limited me to the first byte (0x0000 to 0x00FF) or I’m getting a bunch of symbols.

This is the code I’m currently stuck with:

Code:

const char * ConvertToUTF8(const wchar_t * pStr) {
static char szBuf[1024];
WideCharToMultiByte(CP_UTF8, 0, pStr, -1, szBuf, sizeof(szBuf), NULL, NULL);
return szBuf;
}

wchar_t s = L’\u2600’;
const char *msg = ConvertToUTF8(&s);

SDL_Surface *surf = TTF_RenderUTF8_Blended_Wrapped(font, msg, color, wrapWidth);

This outputs this:

[Image: http://i.imgur.com/kVf6jNw.jpg ]

The first symbol is the one I’m after. But unfortunately I have no idea where the rest comes from or how to get rid of it.
I’m not experienced in text encoding and what I have so far, I’ve cobbled together from different examples I found via Google.

I would be very thankful if someone could tell me what I have to change and if its not too much trouble, what I’m doing wrong.

This is not the font problem, but it’s in the code. You supplied -1 as the string length into WideCharToMultiByte(), which means the input string is null-terminated. However your input string is not null-terminated string.

Null-terminated string is an array of character that ended with 0 (L’\0’ for wchar_t) at the last element of array. When a function expected null-terminated string, usually it iterates through the memory until it finds 0. In your case the function does not stop right after the character you want, but it process subsequence characters until it find 0. This happens becuase the next memory location next to &s is not 0.

To correct this … change the declaration of s to be something like this :-

Code:
wchar_t s[] = L"\u2600";
const char *msg = ConvertToUTF8(&s);

Notice it’s an array.

This also works as well (and might be more common used).

Code:
wchar_t *s = L"\u2600";
const char *msg = ConvertToUTF8(&s);

Corrections :-

Code:
wchar_t s[] = L"\u2600";
const char *msg = ConvertToUTF8(s);

and

Code:
wchar_t *s = L"\u2600";
const char *msg = ConvertToUTF8(s);

Thank you very much. That solved the problem. And now I know the difference between the single and double quotes. :wink:

You must null-terminate the buffer yourself. Store the value retuned by
WideCharToMultiByte and then set szBuf[value] = '\0’On Sep 6, 2014 11:13 PM, “Bubbles” wrote:

Hi,

I’ve written a small console in SDL2 with SDL_TTF on Windows (in Visual
Studio 12).
As a font I’m using Unifont. I want to display any character in the font,
but any way I tried
has either limited me to the first byte (0x0000 to 0x00FF) or I’m getting
a bunch of symbols.

This is the code I’m currently stuck with:

Code:

const char * ConvertToUTF8(const wchar_t * pStr) {
static char szBuf[1024];
WideCharToMultiByte(CP_UTF8, 0, pStr, -1, szBuf, sizeof(szBuf), NULL,
NULL);
return szBuf;
}

wchar_t s = L’\u2600’;
const char *msg = ConvertToUTF8(&s);

SDL_Surface *surf = TTF_RenderUTF8_Blended_Wrapped(font, msg, color,
wrapWidth);

This outputs this:

The first symbol is the one I’m after. But unfortunately I have no idea
where the rest comes from or how to get rid of it.
I’m not experienced in text encoding and what I have so far, I’ve cobbled
together from different examples I found via Google.

I would be very thankful if someone could tell me what I have to change
and if its not too much trouble, what I’m doing wrong.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org