Game loop in Win32 API

前端 未结 3 1260
情话喂你
情话喂你 2020-12-16 06:41

I\'m creating game mario like in win32 GDI . I\'ve implemented the new loop for game :

PeekMessage(&msg,NULL,0,0,PM_NOREMOVE);

while (msg.message!=WM_Q         


        
3条回答
  •  忘掉有多难
    2020-12-16 07:17

    I've always been using something like that:

        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();
                }
        }
    

    onIdle is actual game lopp implementation, onError() is an error handler (takes error description as argument), and "running" is either a global bool variable or a class member. Setting "running" to false shuts down the game.

提交回复
热议问题