开发者

How do i call the display callback function in another function so that my timer function also can execute?

开发者 https://www.devze.com 2023-02-25 02:13 出处:网络
I have a display function that draws a circle and i have an update function using which i move the circle along the y axis. Once the circle reac开发者_C百科hes the bottom of the window i want to draw

I have a display function that draws a circle and i have an update function using which i move the circle along the y axis. Once the circle reac开发者_C百科hes the bottom of the window i want to draw a new circle at the top and move that circle in the same manner as the previous one.. How do i do this?

void display(void)
{
 int i;
 flag=0;
 glPointSize(2.0);
 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
 disp2();
 glFlush();
}


void update(int a)
{
  if(y1center>=100)
  {
                      y1center-=5;
                      glutPostRedisplay();
                      glutTimerFunc(40,update,0);
  }
  else
  {
      y1center=950;
      glutDisplayFunc(display);
      //glutPostRedisplay();
      display();
  }
}

This is what i tried to do in the update function but it did'nt work. It just drew a new circle at the top but that circle doesn't move.. disp2() draws the circle


It probably fails because your update function doesn't tell glut to call the update function again in 40 ms (glutTimerFunc), so once it's been in the else once the update function doesn't do anything anymore.

Anyway I've made a few small modifications, which should make this work change/create the following update function:

void update(int a)
{
  if(y1center>=100)
  {
        y1center-=5;      
  }
  else
  {
        y1center=950;      
  }  

  glutTimerFunc(40,update,0);
  glutPostRedisplay();
}


In update function, if you fall in the else (y1center < 100), you won't register the timer again (the glutTimerFunc(40,update,0) call).

0

精彩评论

暂无评论...
验证码 换一张
取 消