Page 1 of 1

C++ Int to String?

Posted: Sat Oct 30, 2004 4:37 pm
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?

Posted: Sat Oct 30, 2004 10:15 pm
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.

Posted: Sat Oct 30, 2004 10:45 pm
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?

Posted: Sun Oct 31, 2004 10:30 am
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.

Posted: Sun Oct 31, 2004 11:21 am
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.

Posted: Sun Oct 31, 2004 11:40 am
by MarauderIIC
I bet that any text-to-screen functions in a graphics library are made to be used :)

Posted: Sun Oct 31, 2004 4:30 pm
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>

Posted: Sun Oct 31, 2004 5:18 pm
by JS Lemming
Mar's owns because it doesn't require any more includes AND I don't have sstream.h.

Posted: Sun Oct 31, 2004 9:44 pm
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>

Posted: Mon Nov 01, 2004 2:27 pm
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.

Posted: Mon Nov 01, 2004 6:10 pm
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.

Posted: Mon Nov 01, 2004 6:40 pm
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.