glutBitmapCharacter

Started by
4 comments, last by beun 15 years, 9 months ago
Hi, I'm trying to display many numbers on the screen, and I only know the way that is considered dumb and inefficient. if (numb == 1) { glutBitmapCharacter(GLUT_BITMAP_9_BY_15 , '1'); } else if (numb == 2) { glutBitmapCharacter(GLUT_BITMAP_9_BY_15 , '2'); } else if (numb == 3) { glutBitmapCharacter(GLUT_BITMAP_9_BY_15 , '3'); } . . . else if (numb == 10) { glutBitmapCharacter(GLUT_BITMAP_9_BY_15 , '1'); glutBitmapCharacter(GLUT_BITMAP_9_BY_15 , '0'); } else if (numb == 11) { glutBitmapCharacter(GLUT_BITMAP_9_BY_15 , '1'); glutBitmapCharacter(GLUT_BITMAP_9_BY_15 , '1'); } . . . continues up to 500 Is there better way of doing this? If I just use the variable numb in the glutBitmapCharacter bracket, it just displays weird symbols. Thanks!
Advertisement
What is numb?

Do you want to print the value the person presses?

int numb = 0;

std::cin << numb;
glutBitmapCharacter(GLUT_BITMAP_9_BY_15 , numb);
Oops sorry, I didn't explain it enough.

Bascially I have a sqaure grid (m by n), and I name the intersections (grid points I guess?) 1 through m*n.
So for example, think of a Sudoku grid which is 9 by 9. Thus I have 1 through 81 grid points. I basically draw that grid (which I have no problem with), then I'm trying to display the grip point number (1~81) on the grid as well.

The numbers are in order of:
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18
19 20 . . .
.
.
.

so my loop basically figures out its position in the grid then prints it, but using the dumb way I've shown earlier. Bascially these numbers are all displayed at the same time. (I'm planning on adding a toggle switch but that's a side thing)

So to wrap it up, I have a counter that is called 'numb' which basically stores the number that has to be displayed (the grid point).
The code I put up earlier:

if (numb == 1) {
glutBitmapCharacter(GLUT_BITMAP_9_BY_15 , '1');
}

is basically a long way of doing this.
My grid can be as big as 600 nodes, and I don't want to have 600 if and else if lines, you know what I mean?

glutBitmapCharacter(GLUT_BITMAP_9_BY_15 , numb);

I've tried it this way, basically just inserting numb directly, but this resulted in weird symbols being printed. Not sure if that's just me, but it doesn't seem to like anything that is not surrounded with quotations.

Thanks!
Well, glutBitmapCharacter requires a character (an ascii-code).
First, you have to split your number into its digits, e.g. 123 -> 1, 2 and 3.
That's explained by ToohrVyk in this thread.

Then you have to have the ascii-codes (www.asciitable.com). Now you can do 48 |plus| num ('0'=48). So 0 has to be 48, 1 has to be 49,... And do that for every (in your case 2) digit. You can optionally include a if(num_10!=0){ so it won't print a 0. This is the fastest way to do this I think.

So your code should look like this. I haven't tested it, I don't know if it's right. (I couldn't write a plus in this form):
int num_1, num_10, num;for (num=0;num<100;num++) {   num_1 = num % 10;   num_10 = (num / 10) % 10;   if (num_10){      glRasterPos2f(... // Set the position for the first digit. x = leftX |plus| num_1*constant; y = topY |plus| num_10*constant      glutBitmapCharacter(GLUT_BITMAP_9_BY_15 , 48 |plus| num_10);   }   glRasterPos2f(... // Set the position for the second digit. x = leftX |plus| num_1*constant+SizeOfCharacter; y = topY |plus| num_10*constant    glutBitmapCharacter(GLUT_BITMAP_9_BY_15 , 48 |plus| num_1);}


*EDIT: it works with printf.

[Edited by - beun on July 5, 2008 11:04:21 AM]
Hey, It worked!

Thanks a lot.
I wasn't sure what you meant by |plus|, but I guess that literally meant +.
It works great and the code is a LOT shorter.
Thank you!
No problem, I couldn't type a plus, so i replaced it by |plus|.

This topic is closed to new replies.

Advertisement