# peekmessage while vs if (main/game loop)

This topic is 802 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi all,

Can someone freshen up my memory on this one?

In my d3d9 engine I've Always had a main/ game loop like this:

	bool run = true;

while(run)
{
_mainApp.mInput.UpdateKeys();

while(PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
if(msg.message == WM_QUIT) run = false;
}
_mainApp.mTimer.Update();


Currently I'm starting from scratch with my new d3d11 engine, and during learning d3d11, I've found this other way:

	while(msg.message != WM_QUIT)
{
if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE))			// WHILE ???
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
{
//mTimer.Tick();

if(!mAppPaused )
{
CalculateFrameStats();
DrawScene();
}



Is there a performance penalty/ advantage on either one of them compared to the other?

Using 'if' from a 'simple viewpoint' sounds best, because you then only do something if there's a message. With 'while' it sounds like waiting till a message occurs.

Any input/ pointers are appreciated.

##### Share on other sites

Just in case vstrakh's answer isn't clear, first one is right, second one is wrong (it will process only one event per frame).

##### Share on other sites
Thanks guys, clear. I can continue with my nice new engine :)
Will keep you posted

##### Share on other sites
and during learning d3d11, I've found this other way:

Just in case vstrakh's answer isn't clear, first one is right, second one is wrong (it will process only one event per frame).

so, what does that tell you about the quality of the example dx11 code you're learning from?   its obviously not  real world code, nor has it ever been tested, otherwise they would have caught the fact that it doesn't even fricking work.   i'm not saying all the code is BS, but keep your eyes peeled.   example code tends to usually be about 90% correct, with the other 10% being whacked stuff like you found in that dx11 code.

caveat emptor, and you get what you pay for.

EDIT: its actually worse, because they didn't catch the error by inspection - which is entirely possible - and should have occurred before any testing, heck it should have been caught by inspection before they even compiled.  does the tutorial you're using come with sample code? or just snippets?  does the sample code compile at max warning level with no warning or errors? IE will it even compile, or is it just some person posting some "almost code" pseudocode on a website?

i've run into these issues even with supposedly high quality tutorials such as rastertek.

Edited by Norman Barrows

##### Share on other sites

@Norman barrows: the book (D3D11 Luna) focusses on learning, not performance.

So you're right about not wanting to adopt everything directly into say a d3d11 engine.

The resulting loop, all working fine:

int CAppl::Run()
{
MSG msg = {0};
mTimer.Reset();

while(msg.message != WM_QUIT)
{
while(PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

mTimer.Tick();

if(!mAppPaused )
{
CalculateFrameStats();
DrawScene();
}
else
{
Sleep(100);
}
}
return (int)msg.wParam;
}

Edited by cozzie

##### Share on other sites

@Norman barrows: the book (D3D11 Luna) focusses on learning, not performance.

I wouldn't consider that "performance"; I'd consider it "getting the basics right".  Put it this way: if tutorial code that I expected to learn from was actually teaching me fundamental bad practices, I'd be mightily annoyed at the tutorial.  I should demand no less of a professionally written book.

##### Share on other sites

The resulting loop, all working fine

It won't work if you get a WM_QUIT message followed by some other message in the same frame. (That's why there was the run = false statement in the first version.)

##### Share on other sites
the book (D3D11 Luna) focusses on learning, not performance.

it would seem it doesn't even focus on functioning correctly at all, much less performance.

personally i've always had the impression that Luna is one of these folks who translates DX docs into books for profit.  IE "can't do, so they teach". Has the guy ever made a game?

strange that his works tend to be so well regarded when it would appear he can't even write a message loop correctly.

but enough perfectionism.

follow his code. anything that doesn't work, simply check it against a second source. that's what i always do.  eventually between the various sources and your own knowledge, you'll get all the bits and pieces correct.

PS if a book doesn't come with full source that compiles and runs correctly straight out of the box, its a bad omen.

Edited by Norman Barrows

##### Share on other sites

@Kylotan: thanks, fixed that. See the result below.

@Norman: the book explicitly mentions that it's for learning purposes of d3d11, I don't believe things like this make the D3D11 book from Frank Luna less valuable. I believe lot's of people learned the d3d11 basics from that book.

int CAppl::Run()
{
MSG msg = {0};
mTimer.Reset();

bool run = true;
while(run)
{
while(PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
if(msg.message == WM_QUIT) run = false;
}

mTimer.Tick();

if(!mAppPaused )
{
CalculateFrameStats();
if(!DrawScene())
{
MessageBox(NULL, err_render.c_str(), err_windowtext.c_str(), MB_ICONERROR);
return(int)msg.wParam;
}
}
else
{
Sleep(100);
}
}
return (int)msg.wParam;
}


Edited by cozzie

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
14
5. 5
frob
12

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633662
• Total Posts
3013229
×