It was a big task when I started using opengl to design a 3 D game to display a score board based on a variable. Most of my friends thought it would be a tough task to display a scoreboard which will be ticking each and every second of the game. Then I thought of generalizing the task and wanted to utilize my knowledge in basic C to code for the scenario.
The problem with displaying characters in opengl was that, One can only print just single character at a time on to the screen using the command
GLvoid
*font_style1 = GLUT_BITMAP_TIMES_ROMAN_24;
glRasterPos3f
(0.0, 0.0, 0.0);
glutBitmapCharacter(font_style1,
ch);
Here, the first line sets the Font type and its size. The second line adjusts the position of the character. Third line plots the character on to the screen, where 'ch' is the ascii value of required character.
So here arises the biggest question:
How can one find the ascii value of the required score and then print ?
Solution is simple:
Split the digits of the score into single digits. Then take the value and add it to 48, which gives the ascii value of that digit.Print the digit, adjust the next character position, find the next digit , repeat the same procedure.
So code wise we can say as,
void scoredisplay (int posx, int posy, int posz, int space_char, int scorevar)
{
int j=0,p,k;
GLvoid *font_style1 = GLUT_BITMAP_TIMES_ROMAN_24;
p = scorevar;
j = 0;
k = 0;
while(p > 9)
{
k = p % 10;
glRasterPos3f ((posx-(j*space_char)),posy, posz);
glutBitmapCharacter(font_style1,48+k);
j++;
p /= 10;
}
glRasterPos3f ((posx-(j*space_char)), posy, posz);
glutBitmapCharacter(font_style1,48+p);
}
{
int j=0,p,k;
GLvoid *font_style1 = GLUT_BITMAP_TIMES_ROMAN_24;
p = scorevar;
j = 0;
k = 0;
while(p > 9)
{
k = p % 10;
glRasterPos3f ((posx-(j*space_char)),posy, posz);
glutBitmapCharacter(font_style1,48+k);
j++;
p /= 10;
}
glRasterPos3f ((posx-(j*space_char)), posy, posz);
glutBitmapCharacter(font_style1,48+p);
}
here, the Scoredisplay() have folowing arguments
initial X position(Extreme right)
initial Y position
initial Z position
space in between each digits
variable which need to be displayed
Now what ever the value on the variable, the score board will be shown at the required X,Y,Z co-ordinates. I searched net a lot to find this sort of code during my project,to get a hint of how to display my scoreboard, but was disappointed. So when I was successful in implementing this on my project and many of my friend's project, I thought of sharing with you all.