Simple game loop question

Started by
19 comments, last by GameDev.net 17 years, 8 months ago
I have always used glut for my openGL applications and I could just pass it a function to render and it always did it's thing. I'm trying to make something without glut this time. So how is the game loop usually created? Do you just fire off a new thread that constantly calls the render and logic code?
Advertisement
You don't need to fire up a thread for your game loop if you don't want to. A game loop is just that, a game loop, and you summed it up pretty well actually, except that you'd also want to check for user input.
I've seen some designs that created a thread for their current active object (e.g. a menu, or the game object with it's loop). In my own game, I've put the main loop in the game manager class, which is created and started from within the main function - no threading involved. It depends on your design, so far I haven't investigated deep enough to say which one is better.
Create-ivity - a game development blog Mouseover for more information.
My main routine looks like this:

int main(){	bool fullscreen = true;	int ret;	if (MessageBox(NULL,"Message", "Start FullScreen?",MB_YESNO|MB_ICONQUESTION)==IDNO)	{		fullscreen=FALSE;		}	if (!window.Create(16, 800, 600, "Title", fullscreen))	{		Msg("couldnt load window");		return 0;	}	Init();	window.Show(true);	ShowCursor(false);	while (!exitgame)	{		ret = window.ProcessMsg();		if (ret == 0)			break;		if (ret == 1) 			DoFrame();	}	DeInit();	ShowCursor(true);	window.Destroy();	return 0;}


The window.ProcessMsg() call, is the win32 message pump required to keep your window happy, if it returns zero then the app is closing.

My main function looks like this...

int main(int argc, char** argv){    Application app;    app.CreateWindow("The Demo",800,600, /*fullscreen=*/ false);    app.Initialize();    while (app.isRunning)       app.Run();    app.DeInitialize();        return 0;}


Application::Run handles all the rendering and input checking
-[Anudhyan][Website]
The main loop shouldnt be in a seperate thread to my opinion, that would be a waste, what you want is as little threads as possible.

A basic gameloop looks something like this.

void main(){	bool running = true;	MSG	msg;	while (running)	{		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))		{			TranslateMessage(&msg);			DispatchMessage(&msg);		}		else 		{			//render stuff here		}	}}


check nehe tuts for more code.
Quote:Original post by ronkfist
A basic gameloop looks something like this.

while (running)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
//render stuff here
}
}


I do not recommend such loops. See, between two consecutive frames only one
message is processed. It works well on high framerates, like in simple demos.
If you get ~25fps, you only process 25 messages per second. I couldnt believe
it, but such loops happen in professional commercial games! When you click
a mouse, you generate two messages, LBUTTONDOWN and LBUTTONUP. Thats a ~1.5
frame reaction, when you could have ~0.5 in this loop:


while (running)
{
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
//render stuff here
}


Don't be afraid of while loops. It will process at most few tens of messages
in a no-time.
Quote:Original post by Anonymous Poster
Quote:Original post by ronkfist
A basic gameloop looks something like this.

while (running)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
//render stuff here
}
}


I do not recommend such loops. See, between two consecutive frames only one
message is processed. It works well on high framerates, like in simple demos.
If you get ~25fps, you only process 25 messages per second. I couldnt believe
it, but such loops happen in professional commercial games! When you click
a mouse, you generate two messages, LBUTTONDOWN and LBUTTONUP. Thats a ~1.5
frame reaction, when you could have ~0.5 in this loop:


while (running)
{
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
//render stuff here
}


Don't be afraid of while loops. It will process at most few tens of messages
in a no-time.


Take a good look...
The one I posted will behave exactly the same way as yours and has more potential.
Quote:Original post by ronkfist
Take a good look...
The one I posted will behave exactly the same way as yours.


No, yours will process A message if one is there while his will process ALL of the messages.
Quote:Original post by Programmer16
Quote:Original post by ronkfist
Take a good look...
The one I posted will behave exactly the same way as yours.


No, yours will process A message if one is there while his will process ALL of the messages.


his loop doesn't render anything if there is a message though, so it works just as a while loop.
Quote:Original post by Anonymous Poster
Quote:Original post by Programmer16
Quote:Original post by ronkfist
Take a good look...
The one I posted will behave exactly the same way as yours.


No, yours will process A message if one is there while his will process ALL of the messages.


his loop doesn't render anything if there is a message though, so it works just as a while loop.


No, his will only stop for 1 message and then render while the other will stop until there are no messages.

This topic is closed to new replies.

Advertisement