永远不会结束Win32消息循环

时间:2009-11-29 09:59:34

标签: c++ windows winapi

我有以下代码:

MSG mssg;

// run till completed
while (true) {

    // is there a message to process?
    while(PeekMessage( &mssg, NULL, 0, 0, PM_REMOVE)) {
        // dispatch the message
        TranslateMessage(&mssg);
        DispatchMessage(&mssg);
    }
    if(mssg.message == WM_QUIT){
        break;
    }
    // our stuff will go here!!
    Render();
    listeners->OnUpdate();
}

一旦它以peekmessage进入内部循环,它就不会在应用程序关闭之前退出。因此,如果我在Render()和OnUpdate()上放置一个断点,它们将永远不会在应用程序的生命周期内被调用。

这与我被告知的herehere相反。我该怎么做呢?

1 个答案:

答案 0 :(得分:1)

典型的游戏循环有这种形式:

MSG mssg;
bool notdone = true;
// run till completed
while (  notdone  ) {

        // is there a message to process?
        if (PeekMessage( &mssg, NULL, 0, 0, PM_REMOVE)) {
            if (mssg.message == WM_QUIT) notdone = false;

            // dispatch the message
            TranslateMessage(&mssg);
            DispatchMessage(&mssg);
        } else {
            // our stuff will go here!!
            Render();
            listeners->OnUpdate();
        }
}