简体   繁体   中英

GLFW-style infinite mouse movement in SDL2?

GLFW has a function which does exactly what I need:

glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);

This function lets the mouse to move freely without being bounded to the window or the screen.

One solution I found is to reset the mouse position each frame using:

 SDL_WarpMouseInWindow(window, defaultMousePositionX, defaultMousePositionY);

But I would still like to know If there is something like glfwSetInputMode() in SDL2.

If you require relative movement, FPS style, SDL has a SDL_SetRelativeMouseMode function as seen here . This forces SDL to only report motion events, so the mouse position doesn't change.

Another method would be to track the current and next positions yourself and manually calculating the differential, to get the distance moved.

// Last position
lastX = currX;
lastY = currY;

// Current position
currX = event.motion.x;
currY = event.motion.y;

// Motion
motionX = currX - lastX;
motionY = currY - lastY;

This could translate smoothly to other control methods too, like analogue sticks and touch controls, giving you a more uniform experience should you decide to use other platforms.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM