Sign in to follow this  
Toolmaker

SDL TTF_Font only rendering the lines...

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 blitting

SDL_FreeSurface(text);

Share this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Replicon
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);

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 this post


Link to post
Share on other sites
Quote:
Original post by Mushu
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.


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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this