Drawing integers to the screen in SDL

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
Bullet Pulse
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 89
Joined: Sun Feb 21, 2010 6:25 pm

Drawing integers to the screen in SDL

Post 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 :)
Image
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: Drawing integers to the screen in SDL

Post 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);
User avatar
Bullet Pulse
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 89
Joined: Sun Feb 21, 2010 6:25 pm

Re: Drawing integers to the screen in SDL

Post 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;
Image
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: Drawing integers to the screen in SDL

Post by GroundUpEngine »

Sweet! ;)
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: Drawing integers to the screen in SDL

Post by K-Bal »

You should take a look at stringstreams if you are using C++.
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: Drawing integers to the screen in SDL

Post 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.
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: Drawing integers to the screen in SDL

Post 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?
Image
Post Reply