Jump to content
  • Advertisement
Sign in to follow this  
chosenkill6

SDL_TTF help

This topic is 2609 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

I am trying to output a variable to a font but i cant seem to get this down. I get an error saying that it does not accept 4 arguments...
Here is my line of code tht is giving me th error:

time_surface = TTF_RenderText_Solid(time_font,"%d",time,font_color);

time is an int value so i thought i could use the C way to output stuff but that doesnt seem to be working. any help appreciated.

Thanks

Share this post


Link to post
Share on other sites
Advertisement
Better check the documentation instead of guessing http://jcatki.no-ip.org:8080/SDL_ttf/SDL_ttf.html#SEC43

You will have to construct the string before sending it to TTF_RenderText_Solid.

Share this post


Link to post
Share on other sites

Better check the documentation instead of guessing http://jcatki.no-ip...._ttf.html#SEC43

You will have to construct the string before sending it to TTF_RenderText_Solid.


i read somewhere that to convert an int to string i need to use <sstream>
I am okay with this, but then again how do i output to TTF_Renderfont bacause it only accepts 3 arguments. I would still need to put "%s" and then add my string so again that becomes 4 arguments...
I wonder why they didnt just allow integer output...

Share this post


Link to post
Share on other sites
Okay i figured it out :P

I converted the int to a string and then converted that to a const char* and then passed that to TTF render and it worked

Thanks!

Share this post


Link to post
Share on other sites
Something like this should work
std::ostringstream oss;
oss << time;
std::string time_string = oss.str();
time_surface = TTF_RenderText_Solid(time_font,time_string.c_str(),font_color);


EDIT: I see you solved it. Good!

Share this post


Link to post
Share on other sites
"%d" and "%s" are format strings. It only works with functions that accept format strings, such as printf() or sprintf(). These functions are usually vararg, in that they take a variable number of arguments. You cannot expect format strings to work the same way when passed to regular functions which take a prepared string.


I wonder why they didnt just allow integer output...
[/quote]
Because it is not just integers. It is also floats. It can be pointer values. And characters. And booleans. And that only addresses the cases where you just want to pass a single primitive type. You might want to output a mixture of all of the above. They could have supported a vararg interface:

TTF_RenderText_Solid_Formatted(time_font, font_color, "The time is now %d:%d:%d", hours, minutes, seconds);

You can write this function yourself though, so there is no need to support it in the library. Some low level libraries give you just what is difficult or impossible to do in the native language. It is up to you to build higher level functionality in that library.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!