Link to home
Start Free TrialLog in
Avatar of jhshukla
jhshuklaFlag for United States of America

asked on

continuous updating of screen

I have the following main and some functions. the roam() handles keystrokes. I change my camera's location based on the key strokes but the window is not updated after each key stroke. I have to resize the window to update the screen. can any one tell me what is wrong here?

int main(int argc, char **argv)
{
  glutInit(&argc, argv);
  glutInitWindowSize(600,600);
  glutInitDisplayMode(GLUT_DEPTH);
  glEnable(GL_DEPTH_TEST);
  glutCreateWindow("two polygons");
  glutDisplayFunc(display);
  glutReshapeFunc(reshape);
  glutSpecialFunc(roam);
  glutMainLoop();
  return 0;             /* ANSI C requires main to return int. */
}

void reshape(int w, int h){...}
void display(void){...}
void roam(int key, int x, int y){
  switch(key){
    ...
  }
  reshape(w, h);
}
Avatar of gseidman
gseidman

Is there some reason you are using glutSpecialFunc() instead of glutKeyboardFunc()? Anyhow, you need to call glutPostRedisplay() any time you want the display to update, such as at the end of your keystroke handler.
I'd recommend that you update one per frame after your game has finished it's update logic. Normally, you'll render (via glutPostRedisplay) at the end of your mainloop as it's that function that usually calls the internal game logic.
Avatar of jhshukla

ASKER

glutSpecialFunc is for handling non ascii keys - i want arrow keys.
It shouldn't matter whether you use glutSpecialFunc() or glutKeyboardFunc(), you just need to call gluPostRedisplay() at the end of your handler.
I haven't tried glutPostRedisplay yet (but will do pretty soon). anyways I got it working by calling both display() and reshape(). If I call only one of them, it doesn't work. why?
plus, the response of the program lags by one keystroke. the first keystroke doesn't do anything and subsequent keystrokes generate the effect of the one just before them. how can I correct this?
It's likely to be with how your mainloop is structured. What does your mainloop in comparison to your keyboard handling routines?
Remember that display() and reshape() are your own functions, not GLUT or GL functions. You need to tell the GL subsystem to actually do something to the screen, and the way you do that is by calling glutPostRedisplay() (which will, in turn, call your display() function). The first thing to do to correct your update problems is to make sure that the GL subsystem knows when it should update, so please add the call to glutPostRedisplay() in your glutSpecialFunc() handler.
>> ... and the way you do that is by calling glutPostRedisplay() (which will, in turn, call your display() function).
I noticed that by adding printf statements in the display function. but it does not call the reshape function which actually renders the scene on the screen. do I need to call it manually or can I specify something so that it gets called automatically after display().


>> It's likely to be with how your mainloop is structured. What does your mainloop in comparison to your keyboard handling routines?
I am using glut's glutMainLoop(). from what I know so far, it handles all messages and communication between the window and the system.
ASKER CERTIFIED SOLUTION
Avatar of gseidman
gseidman

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> You're only offering 30, and the guidance I've given you already is worth far more than that.
If you are not satisfied with the final assignment, then you raise this question. And if that is the case you can always ask in the CS Forum.

And to be honest I think your help was just 25 pts so far. To appreciate the value of points you better visit a C/C++ forum and see what kind of questions and clarifications people ask.

And I asked for clarification on an issue, not a solution to the problem. plus you never said anything about lagging in response to key strokes. I would have happily given away 50 points, had you been a little nice about it.
I'm happy with the assignment. And I'm currently working on a 500 point issue in the C++ channel, so I do know what's going on.