Jump to content
  • Advertisement
Sign in to follow this  
DestX

[SDL_TTF]Printing variables?

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

Hello, I've recently run into a problem with a program I'm working on, what I need is to show text on the screen, and within it include some integer variables(Pretty much like printf()'s %d does for the console box), I've looked into the documentation of it, but I can't seem to find anything = Can someone point me at a solution please? Thanks

Share this post


Link to post
Share on other sites
Advertisement
You need to convert your integer to a string and pass the string to SDL_TTF. The best way to do that would depend on what programming language you are using.

Share this post


Link to post
Share on other sites
C++.

I thought about converting it earlier, but I couldn't find a way, and didn't want to dig too much into it in case there was just another function for it on the TTF that I might've missed or something, I'll try looking into that then, thanks.

Share this post


Link to post
Share on other sites
The Standard C++ Library provides a class that can be used to convert data to and from text. It is called stringstream, and it works in the same way as file streams and the standard stream objects (std::cout, std::cin etc) work.

Example:

std::stringstream stream;
stream << "The value of the variable foo is " << foo;
stream << " and the value of the variable bar is " << bar;

SDL_Surface * surface = TTF_RenderText_Solid(font, stream.str().c_str(),colour);



The boost library provides a convenience function, boost::lexial cast:

std::string string = "foo is " + boost::lexical_cast<std::string>(foo);

SDL_Surface * surface = TTF_RenderText_Solid(font, string.c_str(),colour);

Share this post


Link to post
Share on other sites
Wow, you guys are fast O.o
Thanks alot for the answers.

Just one last thing, when I try to make a stringstream like you showed, I get the following:
error C2079: 'str' uses undefined class 'std::basic_stringstream<_Elem,_Traits,_Alloc>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Alloc=std::allocator<char>
1> ]

Am I missing an #include(I already have fstream, iostream, string, cstring and ctype to deal with streams and strings I need on the program) or did I do something wrong?

Share this post


Link to post
Share on other sites
create a function



#include <sstream>
#include <stdarg.h>
void drawText(SDL_Surface* dst_surf,TTF_Font* font, SDL_Color txt_color, const char* format,...)
{

va_list args;
va_start (args, format);
vsprintf (buffer,format, args);
va_end (args);
txt=buffer;

SDL_Surface* txtSurface = TTF_RenderText_Blended(font, txt,txt_color);

int x=100;int y=100;
SDL_Rect txtLocation ={x,y,0,0};
SDL_BlitSurface(txtSurface, NULL, dst_surf,&txtLocation);


}

example:

drawText(screen,font,color,"hello, world %i",123456);



Share this post


Link to post
Share on other sites
Don't forget to free the surface if you are going to use a function. You should also include error checking.

For C++, you can take advantage of some of the language features to automate the memory management and cache the text so that you reduce the number of calls to TTF_RenderText:

class Text
{
Text(const std::string &text, const SDL_Color &colour, TTF_Font *font)
:
surface(0),
colour(colour),
font(font)
{
assert(font);
surface = generate(text);
}

~Text()
{
SDL_FreeSurface(text);
}

void render(int x, int y, SDL_Surface *destination)
{
SDL_Rect rect = {x,y};
SDL_BlitSurface(surface, 0, destination, &rect);
}

void updateText(const std::string &text)
{
SDL_Surface *temp = generate(text);
std::swap(temp, surface);
SDL_FreeSurface(temp);
}

private:
SDL_Surface *generate(const std::string &text)
{
SDL_Surface *surface = TTF_RenderText_Blended(font, text.c_str(), colour);
if(!surface)
{
throw std::runtime_error(TTF_GetError());
}
return surface;
}

SDL_Surface *surface;
SDL_Color colour;
TTF_Font *font;
// disable copying
Text(const Text &);
Text &operator=(const Text &);
};


This way, you can still set the text to whatever you want, using std::stringstream:

#include <sstream>

int main()
{
bool running = true;

Text status("nothing yet", someColour, someFont);

while(running)
{
if(player.hits(rocket))
{
if(player.died())
{
status.updateText("You died!");
}
else
{
std::stringstream stream;
stream << "Your health is " << player.health();
status.updateText(stream.str());
}
}

status.render(10, 10, screen);
}
}

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!