Question

I have a couple of if-statements and if one of them is true it will print a character on the screen.

char d = 'a';
char c = 'b';

if(A == TRUE) //see comments below
{
Print(200,200,d);
}

if(B == TRUE)
{
Print(200,200,c);
}

void Print(int x, int y, char u)
{
glRasterPos2f(x,y);
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, u);
}

The problem is that if A is true, it will print 'a' on the screen correctly, but if afterwards B is true and A not anymore it will print 'b' on the screen while the character 'a' is still there. How can I solve this so that only one character is on the screen. Any help would be appreciated.

Big thanks to datenwolf. The most important thing is to put the glutPostRedisplay in the right place. I placed it at the end of my void Print(..) function. I just looked where I should place it and for me this works now (it was just a small adjustment).

Was it helpful?

Solution

OpenGL's design is built on the premise, that the while window is redrawn if anything needs a change. Normally all drawing operations should be done from only a central display function. Changes that need to be reflected in what's visible on the screen should be dealt with by preparing the list of things to be drawn and then issue a full redraw event.

Update

Actually you have a completely different problem

In the statement if(A = TRUE) two things are happening:

  • A gets assigned the value TRUE

  • the result of that assignment (which is TRUE) is tested for truth, which it is and hence will always pass.

The same goes for if(B = TRUE).

The right operator to use is == (two equal signs in a row). With most compilers you can enable a warning and turn that into an error, that assignments are not allowed in a branching statement. A stylistic approach is to write tests for equality as

if( TRUE == A )

which avoids accidents like yours. However in your case you can simply write

if( A )

without any comparison operators at all. And I strongly suggest you do that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top