Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Game loop in Win32 API

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_QUIT)
{
    if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    else // No message to do
    {
        gGameMain->GameLoop();  
    }
}

But my game just running until I press Ctrl + Alt + Del ( mouse cursor is rolling ).

like image 833
Dzung Nguyen Avatar asked May 12 '10 08:05

Dzung Nguyen


1 Answers

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.

like image 140
SigTerm Avatar answered Nov 15 '22 04:11

SigTerm