Sign in to follow this  
nano511

problem with SDL_ttf

Recommended Posts

nano511    103
Im trying to make a function for my 2d engine that will apply text to the screen. Here is the function

[code]

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 );
}
[/code]


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?

Share this post


Link to post
Share on other sites
lukeymoo    106
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 *


Share this post


Link to post
Share on other sites
lukeymoo    106
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.

Share this post


Link to post
Share on other sites
boogyman19946    1487
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.

Share this post


Link to post
Share on other sites
Wooh    1088
You should probably learn to give better variable names. One problem is that you have two variables named font.
[code]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 );
}[/code]

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.

Share this post


Link to post
Share on other sites
nano511    103
My function looks like this:
[code]

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 };
[b]SDL_Surface *TEXT = TTF_RenderText_Solid( font, text.c_str(), textColor );[/b]

ApplySurface( x, y, TEXT, screen );

FreeSurface( TEXT );
}
[/code]

When it gest to the bolded line i get a break error. :(

Share this post


Link to post
Share on other sites
nano511    103
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

Share this post


Link to post
Share on other sites
nano511    103
[quote name='Wooh' timestamp='1313021214' post='4847411']
The font you pass to the function, has it been created with TTF_OpenFont?
[/quote]

Nope. I did this...
[code]
TTF_Font *font;
[/code]

I learned it from lazy foo'

E:nvm i think you were talking about this
[code]
font = TTF_OpenFont( "Media/A Love of Thunder.tff", 28 );
[/code]

Share this post


Link to post
Share on other sites
lukeymoo    106
[quote][color=#1C2837][size=2]Okay i got it working but i dont understand what you mean about a variable to hold the directory? [/size][/color][/quote]
std::string filename = "gfx\\foo.bmp";

and use that in your function [color=#1C2837][font=CourierNew, monospace][size=2][color=#000000]TTF_OpenFont[/color][color=#666600]([/color][color=#000000]filename.c_str()[/color][color=#666600], fontSize[/color][color=#666600]);[/color][/size][/font][/color]
[color=#1C2837][font=CourierNew, monospace][size=2][color=#666600]
[/color][/size][/font][/color]
[color=#1C2837][font=CourierNew, monospace][size=2][color=#666600]But you seem to have it all well so never mind about that.[/color][/size][/font][/color]

Share this post


Link to post
Share on other sites
rip-off    10976
You should add more error checking to your code. Practically every SDL function can return an error. If you had checked TT_OpenFont() for errors you would have seen that it had failed to open the file before your program decided to crash.

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