SDL TTF_Font only rendering the lines...

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

Recommended Posts

I need to write a simple game(pong) to get out of an intro C programming course. Now, since it's C, using DX would be far overcomplicated, so I figured, why not use SDL. I whipped up SDL, and the TTF_Font lib. I basicly copy & pasted the TTF_Font code to set it up and render it. This is the code I'm using:
TTF_Font *initFont()
{
TTF_Font *font;

if (TTF_Init() < 0)
return 0;

font = TTF_OpenFont("verdana.ttf", 20);
TTF_SetFontStyle(font, 1); // RENDER_UNICODE

return font;
}

void drawText(SDL_Surface *screen, SDL_Color *color, int x, int y, const char *string)
{
SDL_Rect rect;
SDL_Surface *text = TTF_RenderText_Solid(font, string, *color);

if (text != NULL)
{
rect.x = x;
rect.y = y;
rect.w = text->w;
rect.h = text->h;

SDL_BlitSurface(text, NULL, screen, &rect);
}
}


However, this is the output: That doesn't look like it should be... I have no idea what's wrong with it, and I'm wondering if anyone could shed some light on why this happens. Toolmaker

Share on other sites
Dunno what the problem is, but you've got a massive memory leak - you have to free the surfaces returned by TTF_Render* calls with SDL_FreeSurface when you're done using them.

I'd assume the problem is either with the font or the unicode thing - I don't see a problem anywhere else.

Share on other sites
I just compared your code with my (working) text-render code, and the only difference I see is that you're using Unicode, and a different font/font size. Have you tried experimenting with turning off Unicode, varying the font size, or varying the font file?

Share on other sites
This is extremely unlikely, but is it possible that you're not getting a surface that matches your display format? Try this out (no guarantee or anything);

SDL_Surface *tmp = TTF_RenderText_Solid(font, string, *color);SDL_Surface *text = SDL_DisplayFormat(tmp);SDL_FreeSurface(tmp);// ...do your blittingSDL_FreeSurface(text);

Share on other sites
I tried several other font files and sizes. The bigger the point size, the more unreadable it becomes.

Also, I'm using UTF8, instead of UNICODE. Excerpt from the showfont.c demo:
	enum {		RENDER_LATIN1,		RENDER_UTF8,		RENDER_UNICODE	} rendertype;

So, 1 is UFT8. No idea why I put RENDER_UNICODE there... Also, when I change the function call to TTF_RenderUTF8_Solid, I still get the same problem.

Toolmaker

Share on other sites
im still a little new to SDL, but did you try
TTF_RenderText_Solid?
check lazy foo's site and go to lessone 7, it shows TTF font

Share on other sites
Quote:
 Original post by RepliconThis is extremely unlikely, but is it possible that you're not getting a surface that matches your display format? Try this out (no guarantee or anything);

That shouldn't make a difference. The blit call automatically corrects surface format differences. The reason you'd use SDL_DisplayMode is so that the blit call doesn't have to do it every pass.

I still think its a bug with the UTF8/UNICODE text rendering - have you tried just using the LATIN1 set? Do all of the render modes (solid, shaded, and blended) do the same thing?

Also, I just checked the docs for TTF_SetFontstyle and I have no idea wtf you're passing it - its not even on the list. This is definitely the problem. KILL THAT CALL - you don't need it. You specifiy UNICODE/UTF8 with your choice of render* call.

EDIT: Actually, 0x01 requests a bold font.

Share on other sites
Quote:
 Original post by MushuAlso, I just checked the docs for TTF_SetFontstyle and I have no idea wtf you're passing it - its not even on the list. This is definitely the problem. KILL THAT CALL - you don't need it. You specifiy UNICODE/UTF8 with your choice of render* call.EDIT: Actually, 0x01 requests a bold font.

That's it, that fixed the problem :).