[SDL] SDL_SetColors (segfault)

Mike rapacity at gingin.net
Fri Jan 4 12:55:01 PST 2002


On Fri, 4 Jan 2002, Nick Jennings wrote:

>  Now , (thanks to Gabriele), I am able to run the program without
>  a segfault. However I see no text in the window. Can anyone help me
>  to determine why? I blindly copied this example from an example program
>  that printed text black on white, and the only modification to this I
>  made was to "reverse" the text to white on black. Is there an error in
>  the code while doing this? (I still do not quite understand the color
>  settings etc.) Or perhaps is it something I should be doing after
>  TTF_RenderTextSolid() ?

Well, TTF_RenderTextSolid() returns a surface with the text rendered on it
(I'm guessing this, just looking at the function prototype)

The relevant part of your code is:

   sprintf(string, "Test successful\n");
   TTF_RenderText_Solid(font, string, white);

   sleep(5);
        
   exit(0);

This call to TTF_RenderText_Solid() therefore creates a new surface, renders
the string on it in white, and then does nothing with it. What you need to
do is to blit the surface it creates onto the screen. font_surface should be
declared as "SDL_Surface *font_surface;"

   font_surface = TTF_RenderText_Solid (font, string, white);

Then to center it on the screen, use an SDL_Rect; declared as "SDL_Rect dest;"

   dest.x = (screen->w / 2) - (font_surface->w / 2);
   dest.y = (screen->h / 2) - (font_surface->h / 2);
   dest.w = font_surface->w;
   dest.h = font_surface->h;

   SDL_BlitSurface (font_surface, NULL, screen, &dest);

The first NULL is the SDL_BlitSurface() call is the source rectangle; you
can use this to copy a portion of a surface to another surface. Specifying
NULL tells SDL to just blit the entire thing.

Then you need up make sure the screen has been updated:

   SDL_UpdateRects (screen, 1, &dest);

And then you should hopefully see something on the screen. Note that you
really should check the return value of TTF_RenderText_Solid(), as it
returns NULL on error, but it's late and I can't be bothered. :-)
-- 
Mike.





More information about the SDL mailing list