Jump to content
  • Advertisement
Sign in to follow this  
savail

[WinAPI & DirectX]A common message loop for normal programs

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey,
I'm not sure if I put my stuff correctly in the message loop(Render()):
while(GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);

if(!Graphics::Obj().Render())
{
MessageBox(NULL, "Failed to render", "Error", MB_OK | MB_ICONEXCLAMATION);
break;
}
}


becouse when I close the window I get an error from the MessageBox function above. Probably becouse when I'm handling messages before rendering the window is firstly destroyed and then DirectX doesn't have a window to draw on. When I put my Render() function above translating and dispatching messages I don't get the error, but mostly people advise to handle messages before doing anything else?This is how I'm handling messages to close window:

LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_CLOSE:
DestroyWindow( hwnd );
break;

case WM_DESTROY:
PostQuitMessage( 0 );
break;
//...
default:
return DefWindowProc( hwnd, msg, wParam, lParam );
}
return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
You should do something like this for message loop:
bool running = true;

while (running)
{
while (PeekMessage(&msg, NULL, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
{
running = false;
break;
}

TranslateMessage(&msg);
DispatchMessage(&msg);
}

if (running)
{
if (!Graphics::Obj().Render())
{
MessageBox(NULL, "Failed to render", "Error", MB_OK | MB_ICONEXCLAMATION);
break;
}
}
}


Processing only one message per frame is really bad.

Share this post


Link to post
Share on other sites
but I need a message loop not for a game but for a normal windows program (like Ms Paint etc).

Share this post


Link to post
Share on other sites
So for normal (for example Ms Paint, Word) programs, the message loop should use GetMessage() function in order to pick messages? And where should be done rendering in such message loop?

Share this post


Link to post
Share on other sites
Most "normal" programs redraw the client area in their WM_PAINT handler, since this message is sent to notify the app that a portion of the client area needs to be redrawn for some reason (for instance, if a portion of the client area was obscured by another window but is now visible).

A good portion of apps won't even explicitly draw anything manually...they'll just use common controls (buttons, scrollbars, listview's, etc.) that can handle drawing themselves.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!