PDA

View Full Version : Writing integers as text



Kaega
08-17-2010, 12:09 AM
I have been trying to display an integer in OpenGL as text, and haven't been having any luck. This is my output code for text.


void output(int x, int y, char *str){
int len, i;

glRasterPos2f(x, y);
len = (int) strlen(str);
for (i = 0; i < 4; i++)
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, str[i]);
}

I can't find a way to convert an integer to char*. Is there another way to do this? Any help would be appreciated.

PS: Preferably I would like to combine text and numbers into one variable is possible)

ZbuffeR
08-17-2010, 01:59 AM
(not an OpenGl question)
Use itoa or sprintf.
Be sure to pass a big enough char buffer.
Or use Java to avoid shooting yourself in the foot.

Alfonse Reinheart
08-17-2010, 02:56 AM
Use itoa or sprintf.
Be sure to pass a big enough char buffer.
Or use Java to avoid shooting yourself in the foot.

In one breath, you advise sprintf, but in another you advise Java to avoid "shooting yourself in the foot?" Wouldn't it be much easier to just advise "snprintf" or "_snprintf", where the foot-shooting is not possible, and which doesn't require rewriting his entire application in a language that he might not know?

BTW, never use sprintf. Ever. This page (http://www.gotw.ca/publications/mill19.htm) has a good survey of your options for converting numbers to strings.

Kaega
08-17-2010, 10:24 AM
Thank you Alfonse,

I did already come across the command itoa. It's non standard command and is not available in my compiler, so that was not an option. It does make it look easy though.

As far as sprintf goes, those commands don't seem to work in an OpenGL window as far as I can tell ZbuffeR. Using them doesn't display anything at all. Which is why I am using glutBitmapCharacter.

Anyway, I'll have a look at this article. So far it looks promising. Thanks again for the help Alfonse.