Results 1 to 3 of 3

Thread: centering text

  1. #1
    Senior Member Regular Contributor
    Join Date
    Mar 2005
    Posts
    115

    centering text

    I have a game that I want to be able to center text in. The only problem is that the way I thought would work just makes my game stop responding. I don't know of any other way to center it efficiently without having to count the characters in each string. here is my code hopefully you can figure it out.

    void renderBitmapString(float x,float y,float z,char *string, int center) {
    char *c;
    char *b;
    int amount=0;
    for(b=string;b!='\0';b++)
    amount++;
    if(center == 1)
    glRasterPos3f(x-amount/2, y,z);
    else
    glRasterPos3f(x, y,z);
    for (c=string; *c != '\0'; c++) {
    glutBitmapCharacter(GLUT_BITMAP_9_BY_15, *c);
    }
    }

  2. #2
    Senior Member OpenGL Pro
    Join Date
    May 2000
    Location
    Naarn, Austria
    Posts
    1,102

    Re: centering text

    The algorithm for centering is correct, but you forgot a * in your first for loop...

    for(b = string; *b!='\0'; b++)
    amount++;

    Alternatively, you could just use the standard c function strlen:
    amount = strlen(string);

  3. #3
    Senior Member Regular Contributor
    Join Date
    Mar 2005
    Posts
    115

    Re: centering text

    Thanks alot! now it works great!

Similar Threads

  1. Replies: 1
    Last Post: 07-27-2014, 06:17 PM
  2. Centering a 3D model
    By glfreak in forum OpenGL: General
    Replies: 8
    Last Post: 11-10-2009, 12:16 AM
  3. Centering a Polygon
    By dmats in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 03-18-2009, 04:31 PM
  4. Centering Images
    By Richard145 in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 12-21-2006, 04:55 AM
  5. Problem with centering the text!
    By in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 02-24-2003, 11:46 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