nXq*_*Xqd 5 c++ winapi gdi+ game-loop
我在win32 GDI中创建游戏马里奥.我已经实现了游戏的新循环:
PeekMessage(&msg,NULL,0,0,PM_NOREMOVE);
while (msg.message!=WM_QUIT)
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else // No message to do
{
gGameMain->GameLoop();
}
}
Run Code Online (Sandbox Code Playgroud)
但是我的游戏一直在运行,直到我按下Ctrl + Alt + Del(鼠标光标正在滚动).
我一直在使用类似的东西:
MSG msg;
while (running){
if (PeekMessage(&msg, hWnd, 0, 0, PM_REMOVE)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
try{
onIdle();
}
catch(std::exception& e){
onError(e.what());
close();
}
}
Run Code Online (Sandbox Code Playgroud)
onIdle是实际的游戏lopp实现,onError()是一个错误处理程序(将错误描述作为参数),"running"是全局bool变量或类成员.将"running"设置为false会关闭游戏.
我认为这实际上取决于你的背景.Windows将仅发送WM_QUIT以响应您的应用程序调用PostQuitMessage.这里常见的(如果不是很好的)解决方案是在程序想要结束时使用bool退出消息循环.