|
|
发表于 2006-1-25 11:45:00
|
显示全部楼层
Re:请大家看看下面两种游戏主循环的写法,哪种更正确?
区别在于GetMessage和PeekMessage不同用法,没有消息的时候,一个会挂起等待,一个会返回,另外PeekMessage中的参数PM_NOREMOVE和PM_REMOVE含义不同,5楼已经说了,不明白的可以去看看MSDN,其实个人感觉两种写法含义差不多的。
但是!
第一种里面“return msg.wParam;”就说明了这个程序的最后清理工作必须放在消息里面处理了。而第二种的话,CleanUp放在main的最后面做也可以。
不过我倾向于第三种,详见DXSDK9。b(注意是b,不是c或者更新的什么):
bool bGotMsg;
MSG msg;
msg.message = WM_NULL;
PeekMessage( &msg, NULL, 0U, 0U, PM_NOREMOVE );
while( WM_QUIT != msg.message )
{
// Use PeekMessage() if the app is active, so we can use idle time to
// render the scene. Else, use GetMessage() to avoid eating CPU time.
if( m_bActive )
bGotMsg = ( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) != 0 );
else
bGotMsg = ( GetMessage( &msg, NULL, 0U, 0U ) != 0 );
if( bGotMsg )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
{
if( m_bActive )
{
Render3DEnvironment();
}
}
}
|
|