Reputation: 95
So, I'm making a game in C++ with OpenGL, and I want my cursor to stay in the middle of the screen.
At the start of each frame I call
POINT pt;
pt.x = 400;
pt.y = 300;
ClientToScreen(hWnd, &pt);
SetCursorPos(pt.x, pt.y);
Then, on WM_MOUSEMOVE event I do
POINT p;
GetCursorPos(&p);
ScreenToClient(hWnd, &p);
dx = p.x - 400;
dy = p.y - 300;
The window is 800x600, so I just wrote those numbers, that will be obviously changed later.
It works, but it is very sloppy. Like if some frames would be dropped.
Upvotes: 0
Views: 367
Reputation: 597941
You can use ClipCursor()
to restrict the mouse's movement. The mouse will not be able to move outside of the specified rectangle until you release it.
Upvotes: 1
Reputation: 94469
You could try installing a hook via SetWindowsHookEx
(passing WH_MOUSE
or maybe even WH_MOUSE_LL
) and simply discarding all move events before the corresponding window messages even get dispatched.
Upvotes: 0