C++ Int to String?

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
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

C++ Int to String?

Post by JS Lemming »

I've made a Text function thing from the image splicer i wasa working on, but i have a problem. It works great like this:

Code: Select all

Text(g_Display,Font,10,50,"It looks like a dead camel with no shoes.");
But I want to also be able to print out numbers. I guess I just need a way to convert a number to a string is all. Anyone know how?
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Post by MarauderIIC »

To a C++ string:

Code: Select all

string itos(int parm) {
    char buffer[5096];
    string retval = "";
    sprintf(buffer, "%i", parm);
    retval = buffer;
    return retval;
}
To go to a C-string, you'll probably want to just do itos(parm).c_string() -- that's safest. Otherwise you risk buffer overflows, memory being overwritten, and junk. It can be done, though. Just return 'buffer'. Note that the variable goes out-of-scope at the end of the fn and therefore MAY be overwritten, and if you make it static then you have to copy the data elsewhere -- or every time you call the fn, your char* will point to the same address, the data of which will change.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
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:

Post by Falco Girgis »

JS Lemming, you are aware that SDL has an additional library called SDL_TTF (I think that was the name) that is like t3h |_33+ text stuff. It has everything you'd ever want to do with text. What exactly is this text function for?
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

Post by JS Lemming »

Thanks Mar.

GyroVorbis, Using an image font is all about speed. Real fonts can be painfully slow.

"What exactly is this text function for?"

To output readable information to the user, mostly likely in our demos. An example would be displaying the intensity in the blood engine thing.

Plus with this I won't have to set up and include all that SDL_TTF crap.
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
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:

Post by Falco Girgis »

I don't know...
I've never heard anybody ever diss SDL_ttf. It has so many functions and is super easy to use. Fancy effects as well.

Then the fact you don't have to write your own.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Post by MarauderIIC »

I bet that any text-to-screen functions in a graphics library are made to be used :)
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Tvspelsfreak
Chaos Rift Junior
Chaos Rift Junior
Posts: 272
Joined: Wed Sep 29, 2004 5:53 pm
Favorite Gaming Platforms: NES, SNES
Programming Language of Choice: C/C++
Location: Umeå, Sweden
Contact:

Post by Tvspelsfreak »

You could do it the printf way (done in C-style):

Code: Select all

void print_text(char *str, ...){
	char buffer[1024];

	va_list argptr;  
	va_start(argptr,str); 
	vsprintf(buffer,str,argptr); 
	va_end(argptr);
	
	/* Print the buffer */
}
Also, include:

Code: Select all

#include <stdio.h>
#include <stdarg.h>
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

Post by JS Lemming »

Mar's owns because it doesn't require any more includes AND I don't have sstream.h.
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
User avatar
vmrob
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 97
Joined: Fri Sep 03, 2004 5:13 pm

Post by vmrob »

i was looking at maurador's function i have a different one..
yours uses the buffer and mine uses a string stream. what is the danger of this? and how does sprintf work?

Code: Select all

string numbertostring(float fl)
{
    ostringstream result;
    result<<fl;
    return result.str();
}
yeah.... forgot about that. you do need <sstream.h>
Last edited by vmrob on Sun Oct 31, 2004 9:44 pm, edited 1 time in total.
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:

Post by Falco Girgis »

JSL, you're telling me that you're going to go out and make your own text things, when there is a perfectly good text library out there with everything you'd ever need and more ready to use?

There is nothing wrong with SDL_ttf. Maybe that Blitz monstrosity you're used to had a text function that significantly reduced speed/performance, but SDL_ttf isn't going to (even if it did, it'd be a TINY bit). The library has everything you'd ever need for text.
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

Post by JS Lemming »

Have you even tried it? Just wondering. If you have post me the code. Really.

And it wasn't like I was making some huge complex text function. All i did was splice a image font then associate each character in the string to a certain tile. Just a matter of converting a char to an int. (int)char!

Show me the code you used to check out all of "SDL_ttf" cool features please.
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
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:

Post by Falco Girgis »

Oh :oops:
Well, in that case rock on.

I thought you were going out and basically making your own insane SDL_ttf. Actually, no, I've never personally used SDL_ttf. But I will show you what it can do (I have it set up and all). It seriously is really good.
Post Reply