problem with SDL_ttf

Started by
13 comments, last by rip-off 12 years, 8 months ago
Im trying to make a function for my 2d engine that will apply text to the screen. Here is the function



void Graphics::ApplyText( int x, int y, std::string string, int r, int g, int b, std::string font, int fontSize )
{
SDL_Color textColor = { r, g, b };
TTF_Font *font = TTF_OpenFont( font.c_str(), fontSize );
SDL_Surface *text = TTF_RenderText_Solid( font, string, textColor );

ApplySurface( x, y, text, screen );
}



The problem is that it says, "No suitable conversion function from "std::string" to "TTF_Font *" exists. And, "No suitable conversion function from "std::string" to "const char *" exists. How can i fix this?
Advertisement
Const Char * and String are two different variable types, and you also used string as the second parameter.
You should rename the variable string to something less conflicting and use the .c_str() to convert it to const char *


Okay thanks! Now what about the font? When Lazy Foo' does it he doesnt use .c_str()
For the TTF_OpenFont(const char *, int ptsize)

font by itself should be the font's filename, and c._str() should convert that to const char *
I recommend making a variable in your 'Graphics' class that holds the directory of a font and use that instead.
I thought it was because you have two variables named font (one pointer to TTF_Font and the other std::string so you have no suitable conversion from one to the other) and the second argument of TTF_RenderText should a C style string (const char*) but you're passing std::string to it so you have to call member function c_str() to get what you want.

Yo dawg, don't even trip.

Okay i got it working but i dont understand what you mean about a variable to hold the directory?
You should probably learn to give better variable names. One problem is that you have two variables named font.
void Graphics::ApplyText( int x, int y, std::string string, int r, int g, int b, std::string fontName, int fontSize )
{
SDL_Color textColor = { r, g, b };
TTF_Font *font = TTF_OpenFont( fontName.c_str(), fontSize );
SDL_Surface *text = TTF_RenderText_Solid( font, string.c_str(), textColor );

ApplySurface( x, y, text, screen );
}


SDL is a C library so it doesn't have std::string. In C you often use const char* for strings instead. That is why you need to use c_str() to get a const char* to pass to the SDL functions all the time.

Your ApplyText function has some flaws. First of all you never close the font with TTF_CloseFont and you never free the surface with SDL_FreeSurface. If you call this function many times you will run out of memory (so called memory leak). Another problem could be that the function is slow. Maybe the function is fast enough for you. If so, then good for you I guess, but loading fonts and creating surfaces is quite slow operations so you might want to reuse the font and/or text surface.
My function looks like this:


void Graphics::ApplyText( int x, int y, std::string text, int r, int g, int b, TTF_Font* font )
{
SDL_Color textColor = { r, g, b };
SDL_Surface *TEXT = TTF_RenderText_Solid( font, text.c_str(), textColor );

ApplySurface( x, y, TEXT, screen );

FreeSurface( TEXT );
}


When it gest to the bolded line i get a break error. :(
What's a "break error"?
like when that box comes up saying access violation blah blah blah and gives you the choice to break or continue. IDK why i call them break erros lol it just kinda sounds right XD

This topic is closed to new replies.

Advertisement