The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
Possible bug in SDL_SetRelativeMouseMode
Bob Rubbens
Guest

Hey all,

Here's (maybe) a small bug I found while working with SDL_SetRelativeMouseMode. An example gif can be seen here:

https://dl.dropboxusercontent.com/u/65227065/SDLBUG/sdlbug.gif


What happens is I click, the game switches to the game state, and the mouse is set to relative mode. I move the mouse a bit, and then I press escape which causes the game to go back to the level state. In that process the relative mode is set to false. I then don't move the mouse and click my mouse button. As you can see, only a mousebuttonup event is reported. This works consistently.

I'm quite sure it's not a mistake in my code since the event loop only consists of:


while(SDL_PollEvent(&e)) {
        if (e.type == SDL_MOUSEBUTTONDOWN) {
            NNB_INFO << "SDL_MOUSEBUTTONDOWN " << e.type;
        } else if (e.type == SDL_MOUSEBUTTONUP) {
            NNB_INFO << "SDL_MOUSEBUTTONUP " << e.type;
        } else if (e.type == SDL_MOUSEMOTION) {
            NNB_INFO << "SDL_MOUSEMOTION " << e.type;
        }

        event(e); // Gamestate specific event code is called here
    }


So if SDL_MOUSEBUTTONDOWN is not printed it must have been missed by SDL. When I click the mouse a second time it is also not doing intensive calculations, just polling for SDL_Events. I can try to whip up a little test program but that will take some time.

Is there anyone with hands-on SDL experience that can take a look at this for me? Or am I just missing the small letters in the documentation ("use of SDL_SetRelatvieMouseMode makes the the prevents the next mousebuttondown event" or something)?


Regards,

Bob Rubbens
remmy


Joined: 31 Oct 2015
Posts: 2
I have experienced a similar issue on Windows with SDL 2.0.3. My workaround is to call SDL_PumpEvents immediately before any call to SDL_SetRelativeMouseMode.

With relative mouse mode off, SDL processes standard Windows events (eg. WM_LBUTTONDOWN). SDL_PumpEvents pulls these events from the Windows queue and puts them in the SDL queue.

When you turn on relative mouse mode, SDL enables "Raw Input" mode (a special input mode on Windows), and changes the behavior of SDL_PumpEvents. Now it discards WM_LBUTTONDOWN messages (and others). It instead processes WM_INPUT messages.

My problem comes when Windows has messages waiting for SDL to pump, but you change the relative mouse mode before pumping them. In my case we were doing a bunch of work in between SDL_PollEvent and the subsequent call to SDL_SetRelativeMouseMode. It was during this processing time that the mouse button would usually be released and then its message lost.

Calling SDL_PumpEvents right before switching modes empties the Windows message queue and therefore minimizes the chance that a message will be lost.