Results 1 to 6 of 6

Thread: Printing Text

  1. #1
    Junior Member Newbie
    Join Date
    Nov 2009
    Posts
    5

    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).

    Code :
     
    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.

    Code :
     
    // 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.

  2. #2
    Junior Member Newbie
    Join Date
    Nov 2009
    Location
    Cairo,Egypt
    Posts
    19

    Re: Printing Text

    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,str ing[i]);
    }
    };

  3. #3
    Junior Member Newbie
    Join Date
    Nov 2009
    Posts
    5

    Re: Printing Text

    Thanks Ahmed!!!!

    That code worked perfectly, thank you once again

  4. #4
    Junior Member Newbie
    Join Date
    Nov 2009
    Posts
    5

    Re: Printing Text

    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.

    Code :
     
                    // 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.

  5. #5
    Junior Member Newbie
    Join Date
    Nov 2009
    Location
    Cairo,Egypt
    Posts
    19

    Re: Printing Text

    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,str ing[i]);
    }
    };

  6. #6
    Junior Member Newbie
    Join Date
    Nov 2009
    Posts
    5

    Re: Printing Text

    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?

Similar Threads

  1. Printing text
    By DmanPwns in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 12-12-2009, 08:30 AM
  2. Replies: 0
    Last Post: 11-15-2002, 02:58 AM
  3. Printing text
    By imported_Ramesh in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 11-22-2000, 01:46 PM
  4. Printing Text with OGL
    By Noah in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 07-13-2000, 09:58 AM
  5. printing text
    By nfect in forum OpenGL: Advanced Coding
    Replies: 2
    Last Post: 02-08-2000, 05:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean