bool System_Start();
开始运行设定好的框架。
bool CALL HGE_Impl::System_Start()
{
MSG msg;
if(!hwnd)
{
_PostError("System_Start: System_Initiate wasn't called");
return false;
}
if(!procFrameFunc) {
_PostError("System_Start: No frame function defined");
return false;
}
bActive=true;
// 主循环
for(;;)
{
// Process window messages if not in "child mode"
// (if in "child mode" the parent application will do this for us)
if(!hwndParent)
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if (msg.message == WM_QUIT) break;
TranslateMessage(&msg);
DispatchMessage(&msg);
continue;
}
}
// 检测鼠标是否在窗体内
_UpdateMouse();
//如果hge窗体获得焦点,进入主循环
if(bActive || bDontSuspend)
{
// 确保1ms的时间间隔,防止在同一时间内与用户代码冲突
do { dt=timeGetTime() - t0; } while(dt < 1);
//当时间到达下一帧时或运行在无限制的FPS模式下,执行以下模块
if(dt >= nFixedDelta)
{
// fDeltaTime转换成秒计时
fDeltaTime=dt/1000.0f;
// 时间间隔太长会丢失焦点,画面抖动
if(fDeltaTime > 0.2f)
{
fDeltaTime = nFixedDelta ? nFixedDelta/1000.0f : 0.01f;
}
// 计时
fTime += fDeltaTime;
// 更新参数,为下一帧计算.
// and count FPS
t0=timeGetTime();
if(t0-t0fps <= 1000) cfps++;
else
{
nFPS=cfps; cfps=0; t0fps=t0;
_UpdatePowerStatus();
}
// 执行用户代码
if(procFrameFunc()) break;
if(procRenderFunc) procRenderFunc();
// If if "child mode" 返回
if(hwndParent) break;
// 清空输入事件
_ClearQueue();
}
// 如果两帧时间间隔太近,等待一会儿.
else
{
if(nFixedDelta && dt+3 < nFixedDelta) Sleep(1);
}
}
// 主循环结束,sleep一会儿,
// 注意不能时间太长,以防操作系统重绘窗体
else Sleep(1);
}
_ClearQueue();
bActive=false;
return true;
}