Events, keyboard & mouseSDL 3.2
SDL_SetWindowRelativeMouseMode
Hides and locks the cursor and reports pure mouse movement. For first-person cameras.
Signature
bool SDL_SetWindowRelativeMouseMode(SDL_Window *window, bool enabled);
How it works
With relative mode on, the cursor disappears and cannot leave the window, and mouse motion events keep coming even when the mouse would have hit the screen edge. Read the movement from e.motion.xrel and e.motion.yrel: how far the mouse moved since the last event. Turn it off when a menu opens.
In SDL3 the mode belongs to a window (SDL2 had a global SDL_SetRelativeMouseMode).
Parameters
windowSDL_Window *
The window that captures the mouse.
enabledbool
true to turn relative mode on, false to free the cursor.
Return value
true on success, false on failure.
Example
example.cpp
1SDL_SetWindowRelativeMouseMode(window, true);
2// in the event loop:
3if (e.type == SDL_EVENT_MOUSE_MOTION) {
4 yaw += e.motion.xrel * 0.1f;
5 pitch -= e.motion.yrel * 0.1f; // screen y grows down
6}