通常情况下,如果不处理下,再小的游戏也会让CPU使用率100%.
应该用Sleep(1)把多余的片段还给CPU,
像这样:
while(true)
{
DWORD start_time = GetTickCount();
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if (msg.message == WM_QUIT) break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
update(float(GetTickCount() - start_time));
while((GetTickCount() - start_time) < 30) Sleep(1);
}
update(float timeDelta)是游戏逻辑与画面更新的函数。
效果图: