Jump to content
  • Advertisement
Sign in to follow this  
Adam4444

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

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

If you intended to correct an error in the post then please contact us.

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
Advertisement
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

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
Why do I have a feeling 'score_str' doesn't have enough memory allocated, and ends up overrunning something?

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[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[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
Put score_buf 128 characters instead of 20, not the ~100 extra bytes that will kill your requiered specs and you will be safe if the player has a very high score...

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!