Jump to content
  • Advertisement
Sign in to follow this  
nano511

problem with SDL_ttf

This topic is 2479 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


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


Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!