游戏循环设计

Dar*_*911 1 c++ windows

我想创建一个简单的游戏循环。我正在考虑这两个版本:

v.1:

while (continueRunning) {

        PeekMessage(&msg, 0, 0, 0, PM_REMOVE);

        TranslateMessage(&msg);
        DispatchMessage(&msg);

        if (WM_QUIT == msg.message) {
            continueRunning = false;
        }

        // run the game logic

    }
Run Code Online (Sandbox Code Playgroud)

和 v.2:

while(continueRunning)
{
    // peek for messages
    while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);

        if (msg.message == WM_QUIT)
            continueRunning = false;
    }

    // run the game logic
}
Run Code Online (Sandbox Code Playgroud)

什么是更好的解决方案?是否需要有PeekMessage在while循环像在第二个例子?

Vla*_*ein 6

您的第一个版本缺少if大约PeekMessage. 无论如何,这将处理ONE在游戏周期消息(〜60每秒?)。那太慢了。

您应该在运行游戏逻辑之前处理所有消息,类似于您的v.2.