我正在开发一个Windows游戏,遇到了这个问题:
bool game_cont;
LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_QUIT: case WM_CLOSE: case WM_DESTROY: game_cont = false; break;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
int WINAPI WinMain(/*lots of parameters*/)
{
//tedious initialization
//game loop
while(game_cont)
{
//give message to WinProc
if(!GameRun()) game_cont = false;
}
return 0;
}
我在想是否有比将
game_cont
设为全局变量更好的方法来实现这个功能(暂不考虑计时器等问题)。简而言之,我需要能够从WinProc
退出WinMain
中的while循环,以便如果用户以游戏菜单之外的方式关闭游戏,则程序不会继续在内存中运行。(当我在没有WinProc
中的game_cont..
语句测试时,它确实会这样做)。此外,
GameRun
基本上是一个布尔值,当游戏结束时返回false,否则返回true。
WM_QUIT
时退出,并通过调用PostQuitMessage
处理WM_DESTROY
。 - Ben Voigt