From: Kevin M. <ke...@vr...> - 2002-02-02 16:43:42
|
What I've done is map a key to exit() in the keyboard function void keyboarddown( char k, int x, int y) { switch (k) { case 'q': exit( 0 ); } } To give the appearance of "waiting". you can pop up a message when the game is over that says press 'q' to exit. you can have bool flags that are set when you want this message to appear, which also disable any gameplay. you'd test for these flags in your idle/draw function and draw game graphics if true, or "time to quit" text if false (for example). basically, you just let glut (and your graphics loop) continue running until you trigger the 'q' key. if you wanted something to happen after you hit 'q' (like an animation or someone waving goodbye for example), then your 'q' could flip a flag which is detected in your draw loop. then the conditional section in your draw loop would now (seeing this flag) operate an animation. at the end of the anim, you'd enter a state in which you'd then call exit(0). basically what you're looking at dong is a state machine. whether it is some formal statemachine data structure, or just coded into your app (most people do this on simple games), a state machine is just a series of flags that causes your code to operate differently on... example draw() void draw() { switch( gamestate ) { case 0: // draw intro graphics. // draw text: "press any key to start game" case 1: // draw game graphics // do pong physics case 2: // draw text: "game over, press q to quit, or r to restart" case 3: // draw exit animation // when anim done, set gamestate = 4; case 4: exit( 0 ); } } then in your keyboard func void keyboard() { switch( k ) { case 'q': if gamestate == 2 then set gamestate = 3; break; case 'r': if gamestate == 2 then set gamestate = 1; break; } // the "press any key case" if gamestate == 0 then set gamestate = 1; } @--@---@---@----@-----@------@------@-----@----@---@---@--@ Kevin Meinert __ _ __ http://www.vrac.iastate.edu/~kevn \ || \| \ / ` Virtual Reality Applications Center \ ||.-'|--\ Howe Hall, Iowa State University, Ames Iowa \|| \| \`__, ----------------------------------------------------------- On Sat, 2 Feb 2002, Lou Herard wrote: > I've been trying to get glut to wait for a user input before the window closes at the end of my program, but I can't do it. I have tried making a stop condition to let the program "know" that it has ended, and then test the stop condition in the keyboard function; that doesn't work. I've also tried using getchar(), but that just freezes the program. How do I get glut to wait for user input in a function other than my keyboard callback function? > > -Lou > > P.S. The program is my Pong program. If you haven't seen it, it's probably because it's not up on source forge yet. > |