16,472
社区成员
发帖
与我相关
我的任务
分享
void CGameWindow::MessageLoop( common::FUNCTION_CALL fun_call ) // fun_call是一个函数对象,用来调用渲染函数
{
bool bGotMsg;
MSG msg;
msg.message = WM_NULL;
::ZeroMemory( &msg, sizeof( msg ) );
::PeekMessage( &msg, NULL, 0u, 0u, PM_REMOVE );
while ( WM_QUIT != msg.message )
{
bGotMsg = ( ::PeekMessage( &msg, NULL, 0u, 0u, PM_REMOVE ) != 0 );
if( bGotMsg )
{
if( 0 == TranslateAccelerator( m_hWnd, NULL, &msg ) )
{
::TranslateMessage( &msg );
::DispatchMessage( &msg );
}
}
else
{
//WaitMessage(); // 注意!
fun_call();
}
}
}
HRESULT DXUTMainLoop( HACCEL hAccel )
{
HRESULT hr;
// Not allowed to call this from inside the device callbacks or reenter
if( GetDXUTState().GetInsideDeviceCallback() || GetDXUTState().GetInsideMainloop() )
{
if( GetDXUTState().GetExitCode() == 0 )
GetDXUTState().SetExitCode(1);
return DXUT_ERR_MSGBOX( "DXUTMainLoop", E_FAIL );
}
GetDXUTState().SetInsideMainloop( true );
// If DXUTCreateDevice*() or DXUTSetDevice() has not already been called,
// then call DXUTCreateDevice() with the default parameters.
if( !GetDXUTState().GetDeviceCreated() )
{
if( GetDXUTState().GetDeviceCreateCalled() )
{
if( GetDXUTState().GetExitCode() == 0 )
GetDXUTState().SetExitCode(1);
return E_FAIL; // DXUTCreateDevice() must first succeed for this function to succeed
}
hr = DXUTCreateDevice();
if( FAILED(hr) )
{
if( GetDXUTState().GetExitCode() == 0 )
GetDXUTState().SetExitCode(1);
return hr;
}
}
HWND hWnd = DXUTGetHWND();
// DXUTInit() must have been called and succeeded for this function to proceed
// DXUTCreateWindow() or DXUTSetWindow() must have been called and succeeded for this function to proceed
// DXUTCreateDevice() or DXUTCreateDeviceFromSettings() or DXUTSetDevice() must have been called and succeeded for this function to proceed
if( !GetDXUTState().GetDXUTInited() || !GetDXUTState().GetWindowCreated() || !GetDXUTState().GetDeviceCreated() )
{
if( GetDXUTState().GetExitCode() == 0 )
GetDXUTState().SetExitCode(1);
return DXUT_ERR_MSGBOX( "DXUTMainLoop", E_FAIL );
}
// Now we're ready to receive and process Windows messages.
bool bGotMsg;
MSG msg;
msg.message = WM_NULL;
PeekMessage( &msg, NULL, 0U, 0U, PM_NOREMOVE );
while( WM_QUIT != msg.message )
{
// Use PeekMessage() so we can use idle time to render the scene.
bGotMsg = ( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) != 0 );
if( bGotMsg )
{
// Translate and dispatch the message
if( hAccel == NULL || hWnd == NULL ||
0 == TranslateAccelerator( hWnd, hAccel, &msg ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
}
else
{
// Render a frame during idle time (no messages are waiting)
DXUTRender3DEnvironment();
}
}
// Cleanup the accelerator table
if( hAccel != NULL )
DestroyAcceleratorTable( hAccel );
GetDXUTState().SetInsideMainloop( false );
return S_OK;
}