output a variable to screen???

so i can output text to the screen…
renderBitmapString(-1.2f, -0.2f , -5.0f, GLUT_BITMAP_HELVETICA_18, “‘Y pos’”);

but how can i include a variable?
thought it would be like this but it gives errors :frowning:
renderBitmapString(-1.2f, -0.2f , -5.0f, GLUT_BITMAP_HELVETICA_18, VARIABLE);

thanks for reading!

It is not an OpenGL question.
You are a beginner in programming, generally. :slight_smile:
Last parameter of the function is a string, so you have to print value of your variable into the string, and then pass the string as a parameter of the function. :wink:

sorry, thought it was opengl problem :frowning: what problem is it then?
yeah, not so good at programming :slight_smile:

let me phrase my question differently.
i want to output a ‘variable’ onto the screen which i have created a bunch of graphics.
normally in c++, i would use cout but in this case i want the ‘variable’ (which is actually a score of the game) to go in a certain position on the screen.

and i thought the position part was opengl problem?

any ideas?

You can use a function like sprintf to do that. Simply do something like this,


int num = 10;
char buffer[10]={'\0'};
sprintf(buffer, "%d", num); //%d is for integers 
renderBitmapString(-1.2f, -0.2f , -5.0f, GLUT_BITMAP_HELVETICA_18, buffer);

Check the documentation for details of the funciton.

perfect!!
thank you :slight_smile:

sprintf(buffer, “%d”, num); //%d is for integers

NO! Bad! Never use sprintf. Ever. If you need to printf to a string, you use snprintf (or _snprintf if you’re on Visual Studio). The unsized sprintf can cause many problems.

This cause a buffer overflow if the buffer is too little and the value of num too big or too little (cf. -9223372036854775808 to 9223372036854775807 in 64 bits) .
(note that a buffer size of 33 bytes or more don’t seem to cause the buffer overflow problem when the second parameter of sprintf is “%d”)

VS gave me a warning using ‘sprintf’ so i changed it to ‘sprintf_s’ and all is working well.

is that ok?

snprintf actually belongs to C99 standard, whereas sprintf_s is Microsoft only.

snprintf actually belongs to C99 standard, whereas sprintf_s is Microsoft only.

True, but Visual Studio doesn’t implement the C99 standard. Which is why I suggested _snprintf.