Page 1 of 1

Halp

Posted: Tue May 11, 2010 6:51 am
by Scrye
I'm not retarded or anything, although I wouldn't say I'm the new to the SDL API. I kinda skimmed through my book, and seemingly read bits and pieces. I stopped for about a week and trying to get dedicated again.

Anyways, I've been viewing some shit online and in my book, and wanted to try something with the TTF fonts extension for libsdl. Anyone know how to show an integer along with the text it actually shows?

DOS example:

Code: Select all

int x = 1;

std::cout << x << std::endl;
Sorta like:

Code: Select all

text = TTF_RenderText_Solid( font, "The value of X is: ", int x,  textColor );
I'm probably looking like a moron for trying it, anyone know the correct way?

Re: Halp

Posted: Tue May 11, 2010 7:02 am
by Live-Dimension
An int is obviously not a string. Having said that, numeric to string/string to numberic is a royal pain in the ass for something seemingly so easy. Generally it works something like this. Note that it's for a std::string and I'm almost certain.

http://notfaq.wordpress.com/2006/08/30/ ... to-string/

Code: Select all

template <class T>
inline std::string to_string (const T& t)
{
std::stringstream ss;
ss << t;
return ss.str();
}
Note that it returns a string of type std::string instead of the needed type of const char *. Construct your string you want to use for the font in with a std::string then use string.c_str() to get a const char * type.

Atleast, this is how I think it works!

Re: Halp

Posted: Tue May 11, 2010 7:02 am
by K-Bal

Code: Select all

int x = 1337;
std::stringstream ss;

ss << x;

std::string cpp_String = ss.str();
Did not check it but something like this will do it.

Re: Halp

Posted: Tue May 11, 2010 8:43 am
by Falco Girgis
I always favor the C method to the C++ method in this scenario. With C++ you're going from a string stream, to a C++ string ,to a C string.

In C, it's a simple function call to sprintf:

Code: Select all

char buffer[200];
sprintf(buffer, "The number is: %d", number);
text = TTF_RenderText_Solid( font, buffer, textColor );
That's quite a bit prettier.

If you're writing this in C (rather than C++), you don't have a choice either. ;)

That function is located in "stdio.h"

Re: Halp

Posted: Tue May 11, 2010 8:52 am
by Scrye
GyroVorbis wrote:I always favor the C method to the C++ method in this scenario. With C++ you're going from a string stream, to a C++ string ,to a C string.

In C, it's a simple function call to sprintf:

Code: Select all

char buffer[200];
sprintf(buffer, "The number is: %d", number);
text = TTF_RenderText_Solid( font, buffer, textColor );
That's quite a bit prettier.

If you're writing this in C (rather than C++), you don't have a choice either. ;)

That function is located in "stdio.h"
Besides rewriting, I think I'll be fine.

Thanks everyone.

Re: Halp

Posted: Tue May 11, 2010 8:54 am
by avansc
GyroVorbis wrote:I always favor the C method to the C++ method in this scenario. With C++ you're going from a string stream, to a C++ string ,to a C string.

In C, it's a simple function call to sprintf:

Code: Select all

char buffer[200];
sprintf(buffer, "The number is: %d", number);
text = TTF_RenderText_Solid( font, buffer, textColor );
That's quite a bit prettier.

If you're writing this in C (rather than C++), you don't have a choice either. ;)

That function is located in "stdio.h"
i like this was better as well, but id make char buffer[200] to char buffer[11] 32bit int or [21] 64bit int.

ps: i might be off by one or so, just quickly did it in my head. and i know you just used 200 since thats kinda a number you see alot in examples to avoide confusion about why 11 and what not, that and 80(console width i think),

Re: Halp

Posted: Tue May 11, 2010 9:03 am
by K-Bal
I agree, simple number conversion isn't the strength of stringstream. But it is flexible and scalable. And you have an abstraction from the actual buffer (and it's size). In this scenario every method is fine.

For completeness: search for boost::lexical_cast.