0

I'm making a fps based game. Currently I have a wnd_proc function to capture all events for input (WM_MOUSEMOVE, WM_KEYUP, WM_KEYDOWN, ...). My mouse is used to move the camera like in a fps. It works fine when I am only moving the mouse. But when I am pressing on keys to move the character and moving the mouse around at the same time, there is a delay in detecting the mouse movement in the game. I notice the delay also when I press on multiple keys.

For the keyboard events, instead of using my own input class to check whether the keys are pressed, I use GetAsyncKeyState() to read the keyboard stuff and this fixes the multiple keys delay. But my mouse is still the problem. Is there a Async function for the mouse movement?

--EDIT--

This is my input system class which is called during the wndproc function.

class InputSystem : public System
{
    ...
    ...
    ...
    void ProcessInput(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        LPBYTE lpb;
        UINT dwSize;
        RAWINPUT *raw;
        switch (message)
        {
        case WM_INPUT:
        {
            GetRawInputData((HRAWINPUT)lParam, 
                RID_INPUT, 
                NULL, 
                &dwSize, 
                sizeof(RAWINPUTHEADER));

            lpb = new BYTE[dwSize];

            if (lpb == NULL)
                return;

            if (GetRawInputData((HRAWINPUT)lParam,
                RID_INPUT,
                lpb,
                &dwSize,
                sizeof(RAWINPUTHEADER)) != dwSize)
                MessageBox(hWnd, L"GetRawInputData returning wrong size!", L"Orhhor! Bao zha liao!", MB_OK);

            raw = (RAWINPUT*)lpb;

            if (raw->header.dwType == RIM_TYPEKEYBOARD)
            {
                if (raw->data.keyboard.Message == WM_KEYDOWN ||
                    raw->data.keyboard.Message == WM_SYSKEYDOWN)
                {
                    input.ReadInput(raw->data.keyboard.VKey);
                }
                else if (raw->data.keyboard.Message == WM_KEYUP ||
                    raw->data.keyboard.Message == WM_SYSKEYUP)
                {
                    input.ResetInput(raw->data.keyboard.VKey);
                }
            }

            delete[] lpb;
            return;
        }

While below is my input class

class Input
{
    bool keys[256];
    bool prevKeys[256];
    ...
    ...
    ...
}

void Input::InitInput()
{
    ...
    ...
    ...

    auto hWnd = CORE->GetSystem<WindowManager>()->GetWindowHandle();

    RAWINPUTDEVICE Rid[2];
    Rid[0].usUsagePage = 0x01;
    Rid[0].usUsage = 0x06;      //keyboard
    Rid[0].dwFlags = RIDEV_INPUTSINK;
    Rid[0].hwndTarget = hWnd;

    if (RegisterRawInputDevices(Rid, 1, sizeof(Rid[0])) == false)
        assert("Keyboard not registered.");

    Rid[0].usUsagePage = 0x01;
    Rid[0].usUsage = 0x02;      //mouse
    Rid[0].dwFlags = RIDEV_INPUTSINK;
    Rid[0].hwndTarget = hWnd;

    if (RegisterRawInputDevices(Rid, 1, sizeof(Rid[1])) == false)
        assert("Mouse not registered.");

    for (int i = 0; i < 256; ++i)
    {
        keys[i] = false;
        prevKeys[i] = false;
    }
}

void Input::ReadInput(WPARAM w)
{

    keys[w] = true;
    return;
}

void Input::ResetInput(WPARAM w)
{
    keys[w] = prevKeys[w] = false;
    return;
}

bool Input::KeyPressed(int key)
{
    if (!prevKeys[key] && keys[key])
    {
        prevKeys[key] = true;
        return true;
    }
    return false;
}

bool Input::KeyDown(int key)
{
    return keys[key];
}
mLjH
  • 425
  • 2
  • 7
  • 11

0 Answers0