Main loop problems?

Started by
4 comments, last by Eponick 20 years, 9 months ago
I was wondering why my window seems to be going EXTREEMLY slow with my main game loop? heres the code for the DoMessage class:

void NEWWINDOW::DoMessage()
{
	while(1)
	{
		if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
		{
			if(msg.message == WM_QUIT)
				break;

			MainLoop();

			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else
		{
			MainLoop();
		}
	}
} 
And inside my main loop is just a thing to draw some simple graphics and flip the backbuffer to the front. Its never done this in any of my other games, but now that im implementing the loop straight into my game core its being very slow.
Lead ProgrammerDawn of Daria
Advertisement
Why are you putting your MainLoop() twice? Your MainLoop() should only be in the else{} part of that statement.
_______________________________________Pixelante Game Studios - Fowl Language
What about if someones hitting a key?
Then it wont do the MainLoop will it?

Well, I took it out and its still doing the same thing.

[edited by - Eponick on July 5, 2003 3:23:45 AM]
Lead ProgrammerDawn of Daria
Here's my main loop:

//=========================================================
// Game Code Complete reference: Chapter 7, page 227
//=========================================================

MSG msg;
ZeroMemory( &msg, sizeof(msg) );
while( msg.message!=WM_QUIT )
{
// Part 1: Do idle work until a message is seen in the message queue
while (!PeekMessage(&msg, NULL, NULL, NULL, PM_NOREMOVE))
{
g_App.MainLoop();
}

// Part 2: Pump messages until the queue is empty
do
{
if (GetMessage(&msg, NULL, NULL, NULL))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

} while (PeekMessage(&msg, NULL, NULL, NULL, PM_NOREMOVE));
}



Also, inside the call to MainLoop(), I call Win32's Sleep() API to keep the game from sucking up all the CPU time. I find calling it with 2ms - Sleep(2) - seems to work well.

This might not solve your speed problem, but it should solve user interface response time issues. If you have a framerate issue you might begin your search for the problem inside your main loop.

I hope this helps!


Cheers,

Mike McShaffry
===================================================
mrmike@mcshaffry.com (http://www.mcshaffry.com)



[edited by - mrmike on July 5, 2003 2:11:13 PM]

[edited by - mrmike on July 5, 2003 2:12:31 PM]

[edited by - mrmike on July 5, 2003 2:13:21 PM]

[edited by - mrmike on July 5, 2003 2:14:12 PM]
Cheers,Mike McShaffry===================================================mrmike@mcshaffry.com (http://www.mcshaffry.com)
Hey, thanks! It was just taking up craploads of CPU time is why windows was working slow.
I used that Sleep(2); and now everything is fine :D
Lead ProgrammerDawn of Daria
You can use Sleep(0) to give up the rest of your time slice. This works just as well.

This topic is closed to new replies.

Advertisement