SDL_SetRelativeMouseMode Starting Position

Hi,
I use SDL_SetRelativeMouseMode to hide the mouse cursor and block the mouse cursor in the window.
All works fine but the cursor start on the position upper-left of the window.
I tried lot of stuff but no way to warp the cursor on the center of the screen.
Maybe a missed feature of SDL 2 ?
Thanks for the help

Use this

Code:
SDL_WarpMouseInWindow(m_pWindow, WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2);

Note that I’m using two defines named WINDOW_WIDTH and WINDOW_HEIGHT so just create two defines named like those or just write the window width and window height instead of the define’s.

Use this:

Code:
SDL_WarpMouseInWindow(m_pWindow, WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2);

Note that I’m using two defines named WINDOW_WIDTH and WINDOW_HEIGHT so just create two defines named like those or just write the window width and window height instead of the define’s.

By using this function, the mouse cursor will be positioned at the center of the window at program start:

Code:

// Make sure that the mouse cursor is centered in the window at program start
SDL_WarpMouseInWindow(m_pWindow, WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2);

Note that I’m using two defines named WINDOW_WIDTH and WINDOW_HEIGHT so just create two defines named like those or just write the window width and window height instead of the define’s.

I tried a lot and it’s not possible to have the cursor start in the center of the window using : SDL_SetRelativeMouseMode( SDL_TRUE ).
I hope a fix can be there fast.

Using that the cursor start at the center of the window when the program start but once the mouse is moved the cursor is directly on the top-left of the screen.

Weird. May I see your code that’s creating the window, handling mouse events and stuff?

Nothing weird there. So what you want to do is position the mouse pointer in the middle of the window even though you’re moving the mouse, like in a FPS game. Correct?
How do you know that the mouse pointer is moved to the upper left corner? The mouse pointer isn’t visible when you’re using SDL_SetRelativeMouseMode. Is your m_MousePositionX and m_MousePositionY variables telling you this information?

Okay, I just tested it in one of my programs and SDL_SetRelativeMouseMode(SDL_TRUE) does indeed move the mouse pointer to the upper left corner and there’s nothing that can be done about it. I’ve tryed set the mouse pointer to the middle of the screen, each frame, by using SDL_WarpMouseInWindow(m_pWindow, WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2) but the mouse pointer stays in the upper left corner. I guess that’s just how the SetRelativeMouseMode function works.

What I’m doing when I want to center the mouse pointer in the window and also trap it inside the window is by first using this function:

Code:

SDL_WarpMouseInWindow(TheWindow, WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2);

Then I use this function to trap the mouse pointer inside the window:

Code:

SDL_SetWindowGrab(TheWindow, SDL_TRUE);

Note that the mouse pointer will still be visible when using this function.
If you want to hide the mouse pointer you can use this function to hide it:

Code:

SDL_ShowCursor(0);

The mouse pointer should, if you’ve made everything right, be centered and trapped inside the window when you’re starting the program.

If you would like to center the mouse pointer all the time even if the mouse is moved, use SDL_WarpMouseInWindow each frame to center the mouse pointer.

Code:

SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER );
m_SDLWindow = SDL_CreateWindow( Title.GetData(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, Width, Height, 0 );
SDL_SetRelativeMouseMode( SDL_TRUE );
// Start the main Loop.
SDL_Quit();

Here the event code of the mouse called each beginning of the main loop :

Code:

case SDL_MOUSEMOTION :
{
// Store the new mouse position.
m_MousePositionX = Event.motion.x;
m_MousePositionY = Event.motion.y;

// Store the new mouse delta.
m_MouseDeltaX = Event.motion.xrel;
m_MouseDeltaY = Event.motion.yrel;

// We stop here.
break;
}

I’m working on a RTS but DOTA style has the same problem.
I render the cursor using a sprite 32x32 and I want the mouse start at the center and can’t be outside the window.
All works good except the start on the center of the window, the only way is to warp the cursor in the center each frame and compute the mouse position ?

No problem. :smiley:

Thanks you ! That kill a big question, all works good :slight_smile: