user2561614
user2561614

Reputation: 51

SDL_GetMouseState doesn't work to get initial mouse position

Is there a way to get initial position of mouse in SDL 2.0 ?
I try to get mouse coordinates by SDL_GetMouseState(&mouse_x,&mouse_y), however I get the result I expected only after using the function SDL_PollEvent() and also I can't see a value other than (0,0) if the mouse has not been moved at least once since begining of the program.Although I don't check SDL_MOUSEMOTION and connect SDL_GetMouseState() to it, I get mouse coordinates only when mouse is moved.So what's wrong? Or is SDL_GetMouseState() suitable to do so?
Edit : Why, Why is there no any answer?

Upvotes: 3

Views: 4575

Answers (3)

Dave Brown
Dave Brown

Reputation: 1

I know it's late, but just chipping in with a resolution I found. If you get zero coordinates, call SDL_GetGlobalMouseState(&x, &y) then offset x and y by the window.x and window.y coordinates.

Upvotes: 0

Julien
Julien

Reputation: 2258

The SDL updates the position of the mouse internally in SDL_PrivateSendMouseMotion, which is called by various mouse related functions in the same file.

These functions are called in the event loop processing function WIN_WindowProc in response to the mouse events dispatched by Windows.

Thus, if you do not move the mouse, no event is dispatched and the SDL does not know where the mouse is. The solution is to wait for a mouse event before requesting the position and to find a workaround until this event.

Upvotes: 2

vladimirm
vladimirm

Reputation: 261

Try calling SDL_PumpEvents() before SDL_GetMouseState().

Upvotes: 2

Related Questions