Halp

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Scrye
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 11
Joined: Tue May 11, 2010 6:40 am
Current Project: Euphoric Fantasy
Favorite Gaming Platforms: PS3, XBOX360, Sega Dreamcast, Sega Genesis
Programming Language of Choice: C++
Location: Santa's House
Contact:

Halp

Post 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?
Live-Dimension
Chaos Rift Junior
Chaos Rift Junior
Posts: 345
Joined: Tue Jan 12, 2010 7:23 pm
Favorite Gaming Platforms: PC - Windows 7
Programming Language of Choice: c++;haxe
Contact:

Re: Halp

Post 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!
Last edited by Live-Dimension on Tue May 11, 2010 7:09 am, edited 1 time in total.
Image
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: Halp

Post 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.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Halp

Post 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"
User avatar
Scrye
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 11
Joined: Tue May 11, 2010 6:40 am
Current Project: Euphoric Fantasy
Favorite Gaming Platforms: PS3, XBOX360, Sega Dreamcast, Sega Genesis
Programming Language of Choice: C++
Location: Santa's House
Contact:

Re: Halp

Post 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.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Halp

Post 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),
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: Halp

Post 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.
Post Reply