Allow confining a hardare cursor to the window rect automatically
As far as I can see, SDL_SetWindowMouseGrab only works with a software cursor, and a hardware cursor is unaffected by this.
The mouse can still be restricted manually in Windows using ClipCursor. However, it would be nice if there was an automatic and platform-agnostic way of doing this.
You mean something like SDL_SetWindowMouseRect?
You mean something like SDL_SetWindowMouseRect?
In theory, yes. However, it does not seem to work for me?
SDL_Rect rect;
SDL_GetWindowPosition(SDLWindow, &rect.x, &rect.y);
SDL_GetWindowSize(SDLWindow, &rect.w, &rect.h);
SDL_SetWindowMouseRect(SDLWindow, &rect);
is ineffective (the rect appears to be correct, my screen size at (0,0) for a fullscreen window).
RECT client_rect;
GetClientRect(MainWindow, &client_rect);
POINT ul = {client_rect.left, client_rect.top};
POINT lr = {client_rect.right, client_rect.bottom};
MapWindowPoints(MainWindow, nullptr, &ul, 1);
MapWindowPoints(MainWindow, nullptr, &lr, 1);
RECT clip_rect = {ul.x, ul.y, lr.x, lr.y};
ClipCursor(&clip_rect);
works as desired.
The rectangle is relative to the client area, so x and y would be 0,0. Although mouse grab should also work. Can you run the SDL testsprite test program and press Ctrl-G and see if that works?
The rectangle is relative to the client area, so x and y would be 0,0. Although mouse grab should also work. Can you run the SDL testsprite test program and press Ctrl-G and see if that works?
That appears to work, including even with a window that's over 2 monitors with different DPI settings. Intriguing.