SDL_ttf Help

I looked through showfont.c that comes the the ttf library and think I
am doing everything correctly, but nothing displays for me. Can someone
point out what I am doing wrong?

#include “SDL.h”
#include “SDL_ttf.h”

int main(int argc, char** argv){
SDL_Surface *text, *screen;
SDL_Color red;
SDL_Color *pCol;
SDL_Rect dst;
char string[40];
TTF_Font *font;

if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0){
fprintf(stderr, “Error Initializing SDL: %s\n”, SDL_GetError());
exit(-1);
}

atexit(SDL_Quit);

screen = SDL_SetVideoMode(640, 480, 32, SDL_DOUBLEBUF | SDL_HWSURFACE);
if(screen == NULL){
fprintf(stderr, “Couldn’t get main screen: %s\n”, SDL_GetError());
exit(-1);
}

if(TTF_Init() < 0){
fprintf(stderr, “Couldn’t initialize TTF Library: %s\n”, SDL_GetError());
exit(-1);
}

atexit(TTF_Quit);

font = TTF_OpenFont(“Inkburro.ttf”, 12);
if(font == NULL){
fprintf(stderr, “Couldn’t open font: %s\n”, SDL_GetError());
}

red.r = 0xFF;
red.g = 0x00;
red.b = 0x00;
pCol = &red;

dst.x = 93;
dst.y = 20;
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF));

strcpy(string, “Test for SDL_ttf!”);
text = TTF_RenderText_Solid(font, string, *pCol);
if(text != NULL){
SDL_BlitSurface(screen, NULL, text, &dst);
}
else{
fprintf(stderr, “Error rendering text: %s\n”, SDL_GetError());
}

SDL_Flip(screen);
SDL_Delay(2000);
}

I looked through showfont.c that comes the the ttf library and think I
am doing everything correctly, but nothing displays for me. Can someone
point out what I am doing wrong?

dst.x = 93;
dst.y = 20;

Not sure if you also need to set the .w and .h here. Worked fine for me
without. Is a 0 value for w/h interpreted as “the whole thing”?

if(text != NULL){
SDL_BlitSurface(screen, NULL, text, &dst);
}

source-surface, source-rect, dest-surface, dest-rect

This took me ages to spot, but clearly you’re blitting the screen to the
surface with your text on it… swap screen and text and it should be fine.

You might also want to consider adding SDL_FreeSurface (text); after the
blit, unless you really trust your system to clean up all your dynamically
allocated memory after you exit.On Mon, 14 Jan 2002, Wesley (Buck) Lemke wrote:


Mike.

Mike wrote:

I looked through showfont.c that comes the the ttf library and think I
am doing everything correctly, but nothing displays for me. Can someone
point out what I am doing wrong?

dst.x = 93;
dst.y = 20;

Not sure if you also need to set the .w and .h here. Worked fine for me
without. Is a 0 value for w/h interpreted as “the whole thing”?

if(text != NULL){
SDL_BlitSurface(screen, NULL, text, &dst);
}

source-surface, source-rect, dest-surface, dest-rect

This took me ages to spot, but clearly you’re blitting the screen to the
surface with your text on it… swap screen and text and it should be fine.

You might also want to consider adding SDL_FreeSurface (text); after the
blit, unless you really trust your system to clean up all your dynamically
allocated memory after you exit.

This was the problem. I found it just before I got this reply (took me
two days!).

Thanks for the help, the stupidest mistakes are the hardest to find!

Buck>On Mon, 14 Jan 2002, Wesley (Buck) Lemke wrote: