PDA

View Full Version : centering text



t3jem
03-25-2005, 07:20 PM
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);
}
}

Overmind
03-26-2005, 01:56 AM
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);

t3jem
03-26-2005, 08:24 AM
Thanks alot! now it works great! :)