For loop for animation in Display() function

I have a vector saving 5 cube coordinates in oxy plan.

Task: 1st cube roll 90 degrees, then disappear then 2nd cube roll 90 dg … until the last cube in the vector.
Problem: whenever the program runs, all the cube will appear and roll the same time.

Q: How can I solve this problem?

  • I checked the value in for loop (temp), and value of (count) as below:
    angle 1 count 1
    temp0
    temp1
    temp2
    temp3
    temp4
    temp5
    temp6
    temp7
    angle 2 count 2
    temp0
    temp1
    temp2
    temp3
    temp4
    temp5
    temp6
    temp7

=> I do not know why the for loop does not wait for for the “presskey” action to increase the angle value before move to the next temp value.

Would you guys please help me on this case. Thanks.

int Count(0);
void Display(void) {

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
SetLight();

glPushMatrix();

ConclusiveAxis();
DrawGrid();

for (int temp = 0; temp < cube.coordinates.size(); temp++) {
  cout<< "temp" << temp << endl;	
       if ((cube.coordinates[temp] == true)) {
		sDirection = true;

		if (Count <= 90) {

			glPushMatrix();
			glTranslatef(cube.coordinates[temp].x - 0.5, cube.coordinates[temp].y - 0.5, 0.0);
			glRotatef(angle, 0, 1, 0);
			glTranslatef(-0.5, 0.5, 0.5);
			glColor3f(0.5, 1.5, 1.0);
			glutSolidCube(1);
			glPopMatrix();
		}
		else if (Count > 90) {
			angle = 0;
			Count = 0;
		}
		else {
			sDirection = false;
		}
	}
	else {
	}
}

glPopMatrix();
glutSwapBuffers();

}

void SpecialKey(unsigned char key, int x, int y) {
switch (key) {
case 'r':
	//condition for next check	
	if (sDirection == true)
	{
		angle++;
		glutPostRedisplay();
		Count++;
		cout << "angle " << angle << " count " << Count << endl;
	}
	else
	{
		glutPostRedisplay(NULL);
	}
	break;
}

}


Well I am not quite sure of the problem the code below is not being run:

else {
sDirection = false;
}

since “Count” will either be greater than 90 or less than or equal to 90 and nothing else.