Page 1 of 1

Drawing integers to the screen in SDL

Posted: Sat Mar 13, 2010 5:00 pm
by Bullet Pulse
I have been working on an SDL Pickin' Sticks game, and the only thing I can't figure out is how to write the number of sticks picked up to the screen.
I'm not sure if I have to use a different function, convert the integer to a string, or what.

Thanks :)

Re: Drawing integers to the screen in SDL

Posted: Sat Mar 13, 2010 5:05 pm
by GroundUpEngine
Yo, just use the C style 'sprintf' ->

Code: Select all

string sticks_picked;

char temp[16];
sprintf(temp, "Score: ( %i )", player.score);
sticks_picked += temp;

..
Render_Text(sticks_picked);

Re: Drawing integers to the screen in SDL

Posted: Sat Mar 13, 2010 5:28 pm
by Bullet Pulse
GroundUpEngine wrote:Yo, just use the C style 'sprintf' ->

Code: Select all

string sticks_picked;

char temp[16];
sprintf(temp, "Score: ( %i )", player.score);
sticks_picked += temp;

..
Render_Text(sticks_picked);
Hmm. When I added that code, it just repeats Score: (0) across my screen, and then after about 10 seconds, it all disappears and doesn't come back up.

Edit: Okay, I fixed it. It should be

Code: Select all

sticks_picked = temp;

Re: Drawing integers to the screen in SDL

Posted: Sat Mar 13, 2010 5:30 pm
by GroundUpEngine
Sweet! ;)

Re: Drawing integers to the screen in SDL

Posted: Sat Mar 13, 2010 6:50 pm
by K-Bal
You should take a look at stringstreams if you are using C++.

Re: Drawing integers to the screen in SDL

Posted: Sat Mar 13, 2010 11:12 pm
by Falco Girgis
K-Bal wrote:You should take a look at stringstreams if you are using C++.
Yeah. The way you're doing it now with sprintf() is the C-style. Not that it really matters, but if you want to take the object oriented C++ route, you probably should have a look at string streams.

Re: Drawing integers to the screen in SDL

Posted: Sun Mar 14, 2010 5:00 pm
by Live-Dimension

Code: Select all

std::string Utilities::NumberToString(float number)
	{
		std::stringstream out;
		out << number;
		return out.str();
	}
std::string Utilities::NumberToString(int number)
	{
		std::stringstream out;
		out << number;
		return out.str();
	}
std::string Utilities::NumberToString(double number)
	{
		std::stringstream out;
		out << number;
		return out.str();
	}
Yes, I could probably use generics, but I don't want to try and add a string or "ship" class by mistake. I'd rather the compiler complain to me. Come to think of it, wouldn't the compiler complain if it wasn't something stringstream couldn't take?