因?yàn)轫?xiàng)目的原因,需要用游戲引擎HGE,于是分析了一下。
總結(jié):簡(jiǎn)單有效,但也過(guò)于簡(jiǎn)單了,下面是System_Start的注釋!
bool CALL HGE_Impl::System_Start()
{
MSG msg;
POINT pt;
RECT rc;
if(!hwnd) //如果系統(tǒng)沒(méi)有初始化
{
_PostError("System_Start: System_Initiate wasn't called");
return false;
}
if(!procFrameFunc) //如果幀函數(shù),沒(méi)有實(shí)現(xiàn)
{
_PostError("System_Start: No frame function defined");
return false;
}
bActive=true; //HGE進(jìn)入活動(dòng)狀態(tài)
for(;;)
{
if(!hwndParent) //如果沒(méi)有父句柄
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if (msg.message == WM_QUIT) break;
// TranslateMessage(&msg);
DispatchMessage(&msg);
continue;
}
}
//取光標(biāo)的位置,這個(gè)光標(biāo)是屏
GetCursorPos(&pt);
//取客戶端區(qū)域
GetClientRect(hwnd, &rc);
//當(dāng)光標(biāo)切換為當(dāng)前窗口所對(duì)應(yīng)的坐標(biāo)
MapWindowPoints(hwnd, NULL, (LPPOINT)&rc, 2);
//判斷鼠標(biāo)是否在當(dāng)前窗口
if(bCaptured || (PtInRect(&rc, pt) && WindowFromPoint(pt)==hwnd)) bMouseOver=true;
else bMouseOver=false;
//
if(bActive || bDontSuspend)
{
do
{
dt = timeGetTime() - t0; //t0以來(lái)經(jīng)過(guò)的毫秒數(shù),要確定要超過(guò)ms退出(相當(dāng)于等待ms)
}
while(dt < 1);
if( dt >= nFixedDelta ) //如果到了幀的時(shí)間
{
fDeltaTime = dt / 1000.0f;
if( fDeltaTime > 0.2f ) //如果超過(guò).2秒
{
if(nFixedDelta) fDeltaTime = nFixedDelta/1000.0f;
else fDeltaTime = 0.01f;
}
fTime += fDeltaTime; //累計(jì)游戲的時(shí)間
t0 = timeGetTime(); //當(dāng)前幀處理的時(shí)間
if( t0 - t0fps < 1000) cfps++; //小于毫秒時(shí),幀數(shù)加
else //如果超過(guò)毫秒時(shí)
{
nFPS = cfps; //設(shè)置當(dāng)前秒幀計(jì)數(shù)
cfps = 0; //計(jì)數(shù)器清
t0fps = t0; //重設(shè)計(jì)數(shù)時(shí)間
}
if( procFrameFunc() ) break; //執(zhí)得幀
if( procRenderFunc ) procRenderFunc(); //如果有渲染,則執(zhí)得渲染
if( hwndParent ) break;
_ClearQueue();
if(!bWindowed && nHGEFPS == HGEFPS_VSYNC) Sleep(1);
}
else
{
if(nFixedDelta && dt+3 < nFixedDelta) Sleep(1);
}
}
else Sleep(1);
}
_ClearQueue(); //清除沒(méi)有處理的事件
bActive=false;
return true;
}
本文來(lái)自CSDN博客,轉(zhuǎn)載請(qǐng)標(biāo)明出處:http://blog.csdn.net/zdhsoft/archive/2009/04/24/4106748.aspx