Simple Display-String Method in GLUT





8
Date Submitted Mon. Oct. 9th, 2006 6:38 AM
Revision 1 of 1
Helper shell
Tags 3D | CPlusPlus | GLUT | OpenGL
Comments 0 comments
This little function puts a string of a specified color and (centered) location. However, keep the word simple in mind. I purposfully not made it capable of supporting newlines.

By the way, this is my first snippet

void displayString(char string[], float r, float g, float b, int cx, int y)
{
        glColor3f(r, g, b); // our fonts color

        /* think of the following this way:
         * our center x is x.
         * take the length of our string and * by 9 for the char width
         * and thats how wide in pixels the resulting thing will be.
         * right?
         * now, divide that by 2 to center it
         */

        int x = cx - ((strlen(string) * 9) / 2);
        y = y - 7; // y - ~(15 / 2). id rather not use float, so 7 isn't too diff than 7.5

        for(unsigned int i = 0; i != strlen(string) - 1; ++i)
        {
                glRasterPos3f(x, y, 0);
                glutBitmapCharacter(GLUT_BITMAP_9_BY_15, string[i]);
                x = x + 9; // accomidate for the next letter
        }
}
 

Comments

There are currently no comments for this snippet.

Voting