Skip to main content
gamedev.net
🔒 Locked

glutMainLoop() confusion

Started by c_young Jan 17, 2007 at 6:41 PM 5 replies 3.7k views
Original Post
c_young
c_young
i first posted this thread asking about help for an openGL space shooter im trying to make saying i was having problems compiling. i fixed all the problems i think, but now when i run the program it completly freezes up the computer, to the point were i get a system error saying i have to restart. ive been playing around with different sections of the code, taking out lines here and their to see what the problem was. the only time i can get anything to even come up on the screen without the computer crashing is if i put the glutMainLoop(), which i origonally forgot to put in. but, if i put in the main loop then my game loop is never called and obviously nothing works. this is my main, which brings to the game loop, which runs a timer with all of the game function calls ( update, collision, render, ect. ) how and were should i be putting the glutMainLoop? am i supposed to be putting it inside my timer, or put my timer inside it? i really dont understant the point of the glutMainLoop() all i know is it wont run without it and once its called it never resturns. if someone could explain it more or tell me how i should be using it id be really happy

int main( int argc, char **argv )
{
	init_GL();
	init_Game();
	
	glutInit( &argc, argv );
	glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH );
	glutInitWindowPosition( 300, 250 );
	glutInitWindowSize( winWidth, winHeight );
	glutCreateWindow( " " );
	
	glutDisplayFunc( draw_Screen );
	game_Loop();
	
	return 0;
}

[Edited by - c_young on January 18, 2007 9:12:30 AM]
jmaupay
jmaupay
glutMainLoop() is necessary and is an infinite loop (never ending).

So your main() "must" look like:

glutInit( &argc, argv );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH );
glutInitWindowPosition( 300, 250 );
glutInitWindowSize( winWidth, winHeight );
glutCreateWindow( " " );

glutDisplayFunc( draw_Screen );
glutMainLoop() ;


Well this is the way all "windows" in a "event driven" program (aka windowed) is done.

Why ? Because this "main loop" ask Windows (assuming you are working under Windows, but it's the same with Xwindows = linux) to give inputs (reshape the window, mouse events, keyboard ...).

So what about your game_loop() ? How to call your game_loop() ?
Well, with that system you allready have a loop() so don't use a game_loop() but a game_update() wich updates all your internal data. Where to call it ?
=> if you need it to be called at each frame (each 3d image) then call game_update() in the draw_Screen(), at the beginning.
=> if you need it to be called only when nothing else has to be done, then add a glutIdleFunc(game_update()), but I don't think that's what you want.

One last thing: the draw_Screen() will only be called by the glutMainLoop() if the image needs to be rendered.
An image needs to be rendered in 2 cases:
=> either you changed the shape of the window (resize it with the mouse) or you change the order of windows, but it's not so often.
=> or you have to ask the window to be updated, (and this is the classical case), for that, you need to call glutPostRedisplay(). Where ? You gess ? In your draw_Screen() func (or alternatively, in glutIdleFunc()) !

Then your draw_Screen() must look like:
{
game_update() ; // I suppose it's what you do in game_loop

... // Then all your OpenGL calls

glutPostRedisplay() ;
}


One very last thing: NEVER do any init_GL() or any OpenGL call before you have created the window (glutCreateWindow) because OpenGL is not ready before and you will have some stanges effects (crash ...).

You should have a look at the glut example that are very, very well done. In your glut distribution, see Glut.3.7\progs\examples or Glut.3.7\progs\redbook for all examples from the OpenGL programming guide.
jma
c_young
c_young
i actualy ended up putting the main loop in the gameUpdate after the game timer, and it works really well now. i think the problem before wasnt that i was putting the mainloop in the wrong spot but that i was using idle to redisplay and not post redisplay

i didnt know about the initGL though, thats a big help because i was getting errors with that.

int main( int argc, char **argv ){	glutInit( &argc, argv );	glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH );	glutInitWindowPosition( 300, 250 );	glutInitWindowSize( winWidth, winHeight );	glutCreateWindow( " " );		init_GL();	init_Game();		glutDisplayFunc( draw_Screen );	game_Update();		return 0;}void game_Update( void ){	while( GAME_STATUS == ALIVE )	{		glutTimerFunc( 1000/60, timer, 1 );		glutMainLoop();	}}


the only problem im having now, doesnt really have anything to do with openGL, i have pretty much everything working, but when the player moves around the screen if you hold down w or something to move forward then when you let go you still keep on moving forward untill you press s to counter act it. i put in a line to set the x and y speeds to 0 to eliminate this, and it works but then the player moves really slow, and if i just increase the speed then it gets really chopy. and i still cant find anythign close to a good tutorial for loading textures on os x. i found a couple but theyre using cocoa wich is like crazy and yea i have no clue how to convert that over for glut
c_young
c_young
so your saying that it should be like this? i guess i get it, i never thought about it but what i did was pointless because it would never continue the while sence glutMainLoop never comes back right? so i should be putting the timer right in the main and then inside of it check the gamestatus?

should it be like this, and then just somehow check the gamestatus inside the timer?

int main( int argc, char **argv ){	glutInit( &argc, argv );	glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH );	glutInitWindowPosition( 300, 250 );	glutInitWindowSize( winWidth, winHeight );	glutCreateWindow( " " );		init_GL();	init_Game();		glutDisplayFunc( draw_Screen );        glutTimerFunc( 1000/60, timer, 1 );	glutMainLoop();		return 0;}



c_young
c_young
ok so i changed it around, and now that im looking at it i dont have to add anythign in to check the gamestatus because i was already checking it again in the timer. thanks alot i get it alot better now

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.