0
votes

I'm working on Direct3D Game engine, I got my mouse and keyboard setup from my previous project, but recently ran into problem:

The thing is, my engine is for a shooter/slasher game, so it requires a direct input, which was set in previous project using RAWMOUSE and WM_INPUT message parsing.

    case WM_INPUT:
        if (wParam == RIM_INPUT)
        {
            dword dwSize = 48;
            RAWINPUT raw;

            if (GetRawInputData((HRAWINPUT)lParam, RID_INPUT, &raw, &dwSize,
                sizeof(RAWINPUTHEADER)) != dwSize)
                OutputDebugString(TEXT("GetRawInputData does not return correct size !\n"));

            if (raw.header.dwType == RIM_TYPEMOUSE)
            {
                this->mouse.TranslateMouseInput(raw.data.mouse);
            }
            break;
        }

Problem is, that code returns only relative movement of mouse, it's useful in setting up a camera lookup, but when it comes to menu or basic GUI, which are controlled by mouse (buttons etc...) things go bad: GUI and other controls require cursor position to be calculated relative to client rectangle.

Closest thing I found to answer in WinAPI is GetCursorPos() combined with GetWindowRect(), but if window has a title bar relative offset of it does not count and (0,0) coordinate is set at the top of the title bar, but not on the client region.

How do I solve the issue without losing a relative mouse movement?

-Should I try parsing WM_MOUSEMOVE messages with WM_INPUT?

I've looked for a popular input solution for game engines, but didn't find any particular one.

Additional Info:

1
Your question's title says "how to get relative mouse position" but it sounds like what you are asking about is how to get absolute mouse position in your question details.jwezorek
@jwezorek corrected, thanksIlya Pakhmutov
@RetiredNinja Thanks, it seems like an answerIlya Pakhmutov

1 Answers

4
votes

Your best option when you need absolute mouse position is to use standard WM_MOUSE* messages, and only use WM_INPUT for relative mouse position. Typically you have a mode transition when bringing up a UI or some other aspect for getting in/out of relative 'look' mode.

WM_INPUT provides only relative information in most cases, and if you attempt to emulate absolute yourself you will be missing a lot of pointer ballistics that makes it feel really off.

Note that if you want your game to support use via remote desktop (typically for testing), then you need to deal with emulating relative input from absolute position because in the case of a virtual desktop you never get relative, only absolute, from WM_INPUT. This is indicated by MOUSE_VIRTUAL_DESKTOP.

You may want to take a look at the Mouse class implementation in DirectX Tool Kit.