Sign in to follow this  
Adam4444

Simple task... that drives me crazy (SDL_ttf)

Recommended Posts

I've just finished a tetris game, just one litle thing left; showing the current score for the player. I try to do this with:
sprintf(score_str, "Score: %ld", score);
text_surface = TTF_RenderText_Solid(font, score_str, (SDL_Color) {255, 255, 255});

text_surface gets the vale NULL! What am I doing wrong? When I print score_str with printf() everything looks like it should. Thanks in advance.

Share this post


Link to post
Share on other sites
Quote:
Original post by Adam4444
Quote:
Original post by rip-off
Use TTF_GetError() to tell you why.


OK. Then I get "SDL_UpperBlit: passed a NULL surface". And what does that mean. :S


SDL_BlitSurface is actually 2 functions inside SDL, UpperBlit and LowerBlit. AFAIK, lower blit does the raw transfer and upper blit performs checks on two surfaces and the rectangles to blit to/from. So SDL_UpperBlit was performing these checks and failed because one of the surfaces was NULL.

However, inspecting the SDL_ttf source code I can't find any calls to SDL_BlitSurface, SDL_UpperBlit or SDL_LowerBlit. This means that the error is coming from your own code.

Try the following, if you haven't already:

0) check the return value of TTF_Init(), printing TTF_GetError() if its below zero
1) check the font pointer returned by TTF_OpenFont(), and print TTF_GetError() if NULL
2) put some checks just before calling SDL_BlitSurface(), somewhere in your code you are blitting a NULL surface...

Share this post


Link to post
Share on other sites
Hmm... It seams like TTF_RenderText_Solid() doesn't like spaces, because when I changed the sprintf() call to sprintf(score_str, "Score:%ld", score); it worked! I just removed the space and it work. That's weird. :P

Share this post


Link to post
Share on other sites
Quote:
Original post by Adam4444
Hmm... It seams like TTF_RenderText_Solid() doesn't like spaces, because when I changed the sprintf() call to sprintf(score_str, "Score:%ld", score); it worked! I just removed the space and it work. That's weird. :P


I use SDL_ttf with spaces in the strings... its odd that it doesn't work.

Share this post


Link to post
Share on other sites
Quote:
Original post by Replicon
Why do I have a feeling 'score_str' doesn't have enough memory allocated, and ends up overrunning something?


Yepp, of course there wasn't enough space. :P Silly error of me. Anyways, thanks for your time.

EDIT: I don't think so after all...

Here's the whole code for the function:
void draw_sidebar(void)
{
int i, j;
SDL_Surface *text_surface;
SDL_Rect src_rect, dest_rect;
SDL_Color text_color;
char score_buf[20];

SDL_FillRect(display_surface, &(SDL_Rect) {WIDTH * BLOCK_SIZE, 0, SIDEBAR_WIDTH, SIDEBAR_HEIGHT}, SDL_MapRGB(display_surface->format, 0, 0, 0));

text_color.r = text_color.g = text_color.b = 255;
/* Show the next figure. */
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
if (next_figure.pattern[i][j] != NOTHING) {
src_rect.x = src_rect.y = 0;
src_rect.w = src_rect.h = BLOCK_SIZE;
dest_rect.x = (j * BLOCK_SIZE) + (WIDTH * BLOCK_SIZE);
dest_rect.y = (i * BLOCK_SIZE) + 20;
dest_rect.w = dest_rect.h = BLOCK_SIZE;
SDL_BlitSurface(blocks[next_figure.pattern[i][j]], &src_rect, display_surface, &dest_rect);
}
}
}

/* Generate the text "Next:" over the next figure. */
text_surface = TTF_RenderText_Solid(font, "Next:", text_color);
src_rect.x = src_rect.y = 0;
src_rect.w = dest_rect.w = text_surface->w;
src_rect.h = dest_rect.h = text_surface->h;
dest_rect.x = WIDTH * BLOCK_SIZE + 8;
dest_rect.y = 2;
SDL_BlitSurface(text_surface, &src_rect, display_surface, &dest_rect);

/* View the score, level, and rows deleted. */
sprintf(score_buf, "Score: %lu", score);
text_surface = TTF_RenderText_Solid(font, score_buf, text_color);
src_rect.x = src_rect.y = 0;
src_rect.w = dest_rect.w = text_surface->w;
src_rect.h = dest_rect.h = text_surface->h;
dest_rect.y = 120;
SDL_BlitSurface(text_surface, &src_rect, display_surface, &dest_rect);


/* Draw the separation line, which seperate the sidebar from the game. */
boxRGBA(display_surface, (WIDTH * BLOCK_SIZE), 0, (WIDTH * BLOCK_SIZE) + 2, HEIGHT * BLOCK_SIZE, 200, 200, 200, 255);
SDL_UpdateRect(display_surface, 0, 0, 0, 0);
SDL_FreeSurface(text_surface);
}

Share this post


Link to post
Share on other sites
This sounds like a problem I was having for quite a long time. It turns out that there was a bug in freetype for a while that caused sdl_ttf to fail rendering strings with spaces. If this works when you remove the space then that's probably what you're running into.

The Gentoo bug regarding this problem is here: http://bugs.gentoo.org/show_bug.cgi?id=139494

It covers the details pretty well, and I assume this affected other platforms the same too.

Share this post


Link to post
Share on other sites
Quote:
Original post by nemebean
This sounds like a problem I was having for quite a long time. It turns out that there was a bug in freetype for a while that caused sdl_ttf to fail rendering strings with spaces. If this works when you remove the space then that's probably what you're running into.

The Gentoo bug regarding this problem is here: http://bugs.gentoo.org/show_bug.cgi?id=139494

It covers the details pretty well, and I assume this affected other platforms the same too.


Aha, thanks fot that information. But I think I skip the space, it gives the game a kind of hardcore feeling; http://www.burnie.nu/files/atetris_screenshot.png .
But it's good to know when I develop my next game. :)

Share this post


Link to post
Share on other sites
You have in your code this line:
SDL_BlitSurface(text_surface, &src_rect, display_surface, &dest_rect);
Change '&src_rect' to NULL and it should work. I may be mistaken, but by doing this:
src_rect.w = dest_rect.w = text_surface->w;
src_rect.h = dest_rect.h = text_surface->h;
I would think your surface wouldn't show on-screen at all.(It looks like you are telling it to draw a non-existent portion of the texture) Are you sure text_surface is returning NULL, or are you assuming so, because it's not working? Ofcourse, this wouldn't explain why 'Next:' works, or why the score works without the space...

What exactly is happening? Is the text showing up on-screen? Partially on-screen? Nothing showing up? Is text_surface actually returing NULL? Is the text 'Next:' working fine?

[Edit:] NM, seems it's been solved.

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