Cannot get text to be displayed

This is a discussion on Cannot get text to be displayed within the Game Programming forums, part of the General Programming Boards category; I have just recently started working with OpenGL in school and am having trouble getting text to be displayed on ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    35

    Cannot get text to be displayed

    I have just recently started working with OpenGL in school and am having trouble getting text to be displayed on the screen. I am not familiar with all the functions yet, this was just a blank screen code provided by my proof in which I injected my own code for the text.

    Code:
    #include <windows.h>
    #include <math.h>
    #include <stdio.h>
    #include <time.h>
    #include <GL/gl.h>
    #include <GL/glut.g>
    #include <iostream>
    
    #define GLUT_DISABLE_ATEXIT_HACK
    
    #define win_width 640
    #define win_height 480
    
    int font = (int)GLUT_BITMAP_8_BY_13;
    
    using namespace std;
    
    void renderBitmapString(float x, float y, void *font, char *string)
    {
         char *c;
         glRasterPos2f(x, y);
         for (c=string; *c != '\0'; c++)
         {
             glutBitmapCharacter(font, *c);
          }
    }
    
    
    void init() {
    	glClearColor(0.3f, 0.0f, 0.0f, 1.0f);
    	glMatrixMode(GL_PROJECTION);
    	glLoadIdentity();
    	glViewport(0,0,win_width,win_height);
    	gluOrtho2D(0.0f, 256.0f, 0.0f, 256.0f);
    	
    } // end init()
    
    
    void display() {
    	glClear(GL_COLOR_BUFFER_BIT);
    	glutSwapBuffers();
    	renderBitmapString(50,50,(void *)font,"Testing Bitmap Text");
    } // end display()
    
    
    int main(int argc, char* argv[]) {
      glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
      glutInitWindowSize(win_width, win_height);
      glutCreateWindow("Snipa");
      init();
      glutDisplayFunc(display);
      glutMainLoop();
    }
    The window apears, and I can change the background colour accordingly, but no text, any advice would be great, thanks.

  2. #2
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    After looking into it, I think it has to do with the parameters im passing to the renderBitmapString function, but im not sure how the coordinates are created

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    In display():
    You need to render the string before you swap the buffers

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    glutSwapBuffers() is usually the second last command in a display function, right before glutPostRedisplay() when double buffering.

    Here's a function I use with glut for printing text messages:
    Code:
    void messg2screen (int pos, char *messg, const float fg[3]) {
    	int i, x, y, len = strlen(messg);
    	float bg[3];
    
    	switch (pos) {
    		case (TXTBLEFT):
    			x = 10;
    			y = 10;
    			break;
    		case (TXTBRIGHT):
    			x = Config.wid-12*strlen(messg)-10;
    			y = 10;
    			break;
    		case (TXTBCENTER):
    			x = Config.wid/2-12*strlen(messg)/2;
    			y = 10;
    			break;
    		case (TXTCENTER):
    			x = Config.wid/2-12*(strlen(messg)/2);
    			y = Config.high/2-12;
    			break;
    		case (TXTTCENTER):
    			x = Config.wid/2-12*(strlen(messg)/2);
    			y = Config.high-20;
    		default: break;
    	}
    
    	for (i=0;i<3;i++) bg[i] = 1.0f-fg[i];
    	glColor4f(bg[0],bg[1],bg[1],1.0f);
    	glWindowPos2i(x,y);
    	for (i=0; i<len; i++) glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,messg[i]);
    	glColor4f(fg[0],fg[1],fg[2],1.0f);
    	glWindowPos2i(x+2,y+2);
    	for (i=0; i<len; i++) glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,messg[i]);
    }
    This creates an inverse colored shadow so that it stands out regardless of the background. The predefined global enum for the switch/case is:
    Code:
    enum {
            TXTBLEFT,
            TXTBRIGHT,
            TXTBCENTER,
            TXTCENTER,
            TXTTCENTER
    };
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    Quote Originally Posted by Iconate View Post
    After looking into it, I think it has to do with the parameters im passing to the renderBitmapString function, but im not sure how the coordinates are created
    Ah perfect that worked thank you.

    Also I DID manage to get it working prior, but with another example I found online...however, for the X and Y coordinate for renderBitmapCharacter, I was using decimal numbers... the whole screen could be covered from +5 to -5 rather than the actual pixel placement itself.

    Why is this? The viewport was the same size as the window, but they also used a gluLookAt in their function. Is it because of this?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help in Compiling errors
    By sick in forum C Programming
    Replies: 2
    Last Post: 01-21-2010, 03:26 AM
  2. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 03:17 PM
  3. Can't display text from file
    By newbie543 in forum C Programming
    Replies: 1
    Last Post: 09-03-2005, 09:05 PM
  4. Replies: 1
    Last Post: 10-30-2002, 05:45 AM
  5. Structure problem
    By mattz in forum C Programming
    Replies: 10
    Last Post: 11-30-2001, 01:19 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21