Printing Text

Hi everyone, this is my first post here, hope you can help me.

I’m developing a game, and need to output text to the screen, however, so far I can’t seem to get it to work. I followed the tutorial (tutorial 13) on the NeHe site and have similar functions to the ones on there (BuildFont(), glPrint(“text”), KillFont() etc).



void BuildFont(GLvoid)
{
	HFONT font;

	base = glGenLists(96);

	font = CreateFont( -14, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY, FF_DONTCARE|DEFAULT_PITCH, "Arial");
	wglUseFontBitmaps(g_hDC, 32, 96, base);
	SelectObject(g_hDC, font);
}

void KillFont(GLvoid)
{
	glDeleteLists(base, 96);
}

void glPrint(const char *fmt, ...)
{
	char text[256];
	va_list ap;

	if(fmt == NULL) {
		return;
	}

	va_start(ap, fmt);
		vsprintf(text, fmt, ap);
	va_end(ap);

	glPushAttrib(GL_LIST_BIT);
	glListBase(base - 32);
	glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);
	glPopAttrib();
}



Once the font has been built, I’m using the following code to try and draw the text to the screen.



// draw all my graphics etc
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	glTranslatef(0.0f,0.0f,-1.0f);
        glColor4f( 255, 0, 0, 1);
		//glTranslatef(my_Camera.View().x, my_Camera.View().y, my_Camera.View().z);	
        glRasterPos2f(0.1f, 0.1f);
	glPrint("SCORE:", score);




Any help/advice would be greatly appreciated. Also, i don’t know if this is related, but the camera is always moving in my game, along the Z axis, and rotating on the Y axis.

Thanks in advance.

try this code
void print(int x, int y,int z, char *string)
{
//set the position of the text in the window using the x and y coordinates
glRasterPos2f(x,y);
//get the length of the string to display
int len = (int) strlen(string);

//loop to display character by character
for (int i = 0; i < len; i++) 
{
	glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,string[i]);
}

};

Thanks Ahmed!!!

That code worked perfectly, thank you once again :slight_smile:

Ok, so i finally got everything displaying onscreen. When I draw the text, it is drawn at the position of the raster. Which is fine.

But when I rotate the camera on the Y-axis, the raster doesn’t rotate with the camera, so even though the text is still clearly being drawn at the right location (0.1 and 0.1 on the raster) … the text seems to move, when i move the camera as the raster is stationary.

Is there any way I can rotate the raster? All i want to achieve is the text to be displayed in the top left/right corner of the window, not in relation to the camera.


 
                // draw everything else
                glTranslatef(my_Camera.View().x, my_Camera.View().y, my_Camera.View().z);
		glColor3ub(255, 0, 0); // change color to red
		print(0.1, 0.1, "SCORE: %d", score);
                .....


Any ideas?

Cheers.

that’s because u r using it in 2D

void print(int x, int y,int z, char *string)
{
//set the position of the text in the window using the x, y and z coordinates
glRasterPos3f(x,y,z);
//get the length of the string to display
int len = (int) strlen(string);

//loop to display character by character
for (int i = 0; i < len; i++)
{
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,string[i]);
}
};

I did try that, but it didn’t seem to work.

Is there anyway I can print this text on a quad? If so, I was thinking maybe i could use that instead of the raster?