Wednesday, October 12, 2022
HomeGame Developmentsport loop - Why is processing the Home windows message queue so...

sport loop – Why is processing the Home windows message queue so gradual?


PeekMessage, TranslateMessage, and DispatchMessage usually take a very long time (typically greater than a millisecond for a single message). Is that this regular? How do individuals cope with this? I thought of dealing with messages on a separate thread, however that might introduce pointless complexity and would not actually resolve the issue.

Here is what my sport loop appears like:

int WINAPI WinMain(...)
{
    // ...
    
    bool operating = true;
    whereas( operating )
    {
        operating = poll_messages();
        replace();
        render();
        SwapBuffers( no matter );
    }
    
    // ...
}
    
bool poll_messages()
{
    bool keep_running = true;
    MSG msg;
    whereas( PeekMessageA(&msg, NULL, 0, 0, PM_REMOVE) )
    {
        TranslateMessage( &msg );
        DispatchMessageA( &msg );

        if( msg.message == WM_QUIT )
            keep_running = false;
    }
    
    return keep_running;
}

And a part of the window proc:

LRESULT CALLBACK wnd_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
{
    LRESULT end result = 0;

    change( msg )
    {
        // ...
        
        // I eliminated most circumstances, however this is one of the frequent offenders.
        // just about on a regular basis is spent in GetRawInputData when dealing with this message.
        case WM_INPUT:
        {
            BYTE information[ sizeof(RAWINPUT) ];
            UINT data_size = furo_arr_cnt(information);
            UINT bytes_written = GetRawInputData( HRAWINPUT(lparam), RID_INPUT, information, &data_size, sizeof(RAWINPUTHEADER) );
            if( bytes_written == data_size )
            {
                RAWINPUT* raw_input = (RAWINPUT*)information;
                if( raw_input->header.dwType == RIM_TYPEMOUSE &&
                    raw_input->information.mouse.usFlags == MOUSE_MOVE_RELATIVE )
                {
                    int x_relative = raw_input->information.mouse.lLastX;
                    int y_relative = raw_input->information.mouse.lLastY;

                    // convert from display area to OpenGL viewport area
                    y_relative = -y_relative;

                    platform_raw_mouse_delta_event( x_relative, y_relative );
                }
            }
            else
                log_err( "didn't get uncooked mouse enter (ec=%u)", GetLastError() );

            break;
        }
        
        // ...

        default:
            end result = DefWindowProcA( hwnd, msg, wparam, lparam );
            break;
    }

    return end result;
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments