PDA

View Full Version : output a variable to screen???



chuck norrrissss
05-10-2011, 04:44 AM
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 :(
renderBitmapString(-1.2f, -0.2f , -5.0f, GLUT_BITMAP_HELVETICA_18, VARIABLE);



thanks for reading!

Aleksandar
05-10-2011, 05:24 AM
It is not an OpenGL question.
You are a beginner in programming, generally. :)
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. ;)

chuck norrrissss
05-10-2011, 05:38 AM
sorry, thought it was opengl problem :( what problem is it then?
yeah, not so good at programming :)

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?

mobeen
05-10-2011, 05:42 AM
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.

chuck norrrissss
05-10-2011, 05:58 AM
perfect!!
thank you :)

Alfonse Reinheart
05-10-2011, 03:32 PM
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.

The Little Body
05-10-2011, 04:26 PM
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")

chuck norrrissss
05-11-2011, 06:54 AM
VS gave me a warning using 'sprintf' so i changed it to 'sprintf_s' and all is working well.

is that ok?

ZbuffeR
05-11-2011, 09:05 AM
snprintf actually belongs to C99 standard, whereas sprintf_s is Microsoft only.

Alfonse Reinheart
05-11-2011, 03:47 PM
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.