Sign in to follow this  
chimpy

SDL_ttf states text has no width...

Recommended Posts

chimpy    122
Hello, I am trying to render a string of text using SDL_ttf but when I go to add rendered text to an SDL_Surface, it causes an error saying the "text has zero width". I have built and run successfully the SDL_ttf sample code, so I know this *should* work. This is the code: SDL_Color clrFg = {255,100,200,200}; // Blue ("Fg" is foreground) SDL_Surface *sText; sText = TTF_RenderText_Solid(font, "Chimp", clrFg ); I already called TTF_Init, and loaded the font using this code: font = TTF_OpenFontIndex("VeraBI.ttf", 16,0); No errors are given for initialization or font loading. Just when I go to render it I get the width error. Any ideas? I will give any more information if needed. Thanks. [Edited by - chimpy on April 21, 2008 6:53:04 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by chimpy
Hello,

I am trying to render a string of text using SDL_ttf but when I go to add rendered text to an SDL_Surface, it causes an error saying the "text has zero width". I have built and run successfully the SDL_ttf sample code, so I know this *should* work.

This is the code:

SDL_Color clrFg = {255,100,200,200}; // Blue ("Fg" is foreground)
SDL_Surface *sText;
sText = TTF_RenderText_Solid(font, "Chimp", clrFg );

I already called TTF_Init, and loaded the font using this code:


font = TTF_OpenFontIndex("VeraBI.ttf", 16,0);

No errors are given for initialization or font loading. Just when I go to render it I get the width error.

Any ideas? I will give any more information if needed.
Thanks.


First, I think SDL_Color only ask for 3 integers as the arguments you have 4?

Second, I don't think errors are going to be given, because your code is well in a debugging/compiling since correct, but to a right way of doing so incorrect.

You have converted the text to an image, so now you must draw that image onto the surface.

This code is part of my game engine/framework Hope it helps!


//Draw the text
void Graphics::draw_text(TTF_Font* font, char* string, int x, int y, int Fr, int Fg, int Fb, int bR, int bG, int bB)
{
SDL_Color foregroundColor = {Fr, Fg, Fb};
SDL_Color backgroundColor = {bR, bG, bB};

SDL_Surface* textSurface = TTF_RenderText_Shaded(font, string, foregroundColor, backgroundColor);

SDL_Rect textLocation = {x, y, 0, 0};

SDL_BlitSurface(textSurface, NULL, screen, &textLocation);

SDL_FreeSurface(textSurface);
}



Share this post


Link to post
Share on other sites
sprite_hound    461
Quote:
Original post by aleisterbukowski
First, I think SDL_Color only ask for 3 integers as the arguments you have 4?


Technically SDL_Color does have 4 values. The compiler may complain if you only initialise with 3 values instead.


SDL_Color c = {255, 0, 30, 50};
//c.r == 255;
//c.g == 0;
//c.b == 30;
//c.unused = 50;


I don't know how official the 4th value is though, so it's possibly best not to use it for anything.

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