E_net4
E_net4

Reputation: 30042

SDL_GetRelativeMouseState strange behaviour

I have an application in SDL 2.0.3 that enters relative mouse mode before entering the main game loop. In addition, function mouse_input is called in each step:

int mdltx = 0, mdlty = 0;

void mouse_input () {
    auto r = SDL_GetRelativeMouseState(&mdltx, &mdlty);

    if (mdltx != 0 || mdlty != 0)
        cout << "(" << mdltx << "," << mdlty << ")" << endl;

    // Update mouse key presses
    mpul = !!(r&SDL_BUTTON(1)) | ((!!(r&SDL_BUTTON(3)))<<1);
}

According to the documentation of SDL_GetRelativeMouseState:

(...) x and y are set to the mouse deltas since the last call to SDL_GetRelativeMouseState() or since event initialization.

I've added the output lines for debugging purposes, because the resulting effect in the application was very awkward. It turns out that each time I move the mouse (and only when I move it), the console prints values in an unreasonable range. Below is a sample from doing simple mouse movements. The affected axis seems correct (moving horizontally will set mdlty to 0 and moving vertically will set mdltx to 0), but the numbers can get much higher than the screen resolution, and all of them are positive, regardless of the direction I move the mouse.


(342,216)

(47290,0)

(23696,0)

(23730,0)

(23764,0)

(23799,0)

(71190,0)

(117970,83397)

(23491,41802)

(23457,0)

(23423,83811)

(0,41871)

(23389,208322)

(23355,82847)

(0,41320)

(46812,0)


I have been looking around the web for people having the same problem, without any success. Also note that this application was previously made for SDL 1, relying on SDL_GetMouseState and SDL_WarpMouse, but the latter function does not seem to do anything in some platforms. I'm working on the application under an Arch Linux + LXDE installation, which seems to simply ignore the mouse warp. This is the same machine where this other strange behaviour is happening.

The question is: why is this happening and how can I fix it with compatibility in mind, while keeping the advantages of having relative mouse mode? I even wonder if it could be an issue within SDL itself.

Upvotes: 1

Views: 2474

Answers (2)

NegatioN
NegatioN

Reputation: 727

For anyone else struggling with this problem, it might help to set:

SDL_SetHintWithPriority(SDL_HINT_MOUSE_RELATIVE_MODE_WARP, "1", SDL_HINT_OVERRIDE);
SDL_SetRelativeMouseMode(SDL_TRUE);

This seems to give you relative mouse-output, where the center of the screen is (0,0).

However for me it doesn't currently reset the Cursor-coordinates properly, so while every frame resets this to (0,0), it jumps straight to the previous coord += movement.

This is a lot better than the alternative though, it seems.

Upvotes: 2

E_net4
E_net4

Reputation: 30042

The same code is working in a different machine, so this seems to be a bug. And it seems that some people found it too, with a broader range of misbehaviours than the one mentioned. https://bugzilla.libsdl.org/show_bug.cgi?id=2150

Upvotes: 1

Related Questions