Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mouse coordinates are floating point #6950

Merged
merged 3 commits into from
Dec 30, 2022
Merged

Conversation

slouken
Copy link
Collaborator

@slouken slouken commented Dec 30, 2022

You can get sub-pixel mouse coordinates and motion depending on the platform and display scaling.

Fixes #2999

@slouken
Copy link
Collaborator Author

slouken commented Dec 30, 2022

@ericwa, can you take a look at this?

@slouken slouken force-pushed the highdpi-mouse branch 4 times, most recently from 976fa3e to 37fe532 Compare December 30, 2022 04:21
You can get sub-pixel mouse coordinates and motion depending on the platform and display scaling.

Fixes libsdl-org#2999
@slouken slouken force-pushed the highdpi-mouse branch 3 times, most recently from a10efdf to 0f2ba1e Compare December 30, 2022 06:07
This function is useful for accumulating relative mouse motion if you want to only handle whole pixel movement.
e.g.
static float dx_frac, dy_frac;
float dx, dy;

/* Accumulate new motion with previous sub-pixel motion */
dx = event.motion.xrel + dx_frac;
dy = event.motion.yrel + dy_frac;

/* Split the integral and fractional motion, dx and dy will contain whole pixel deltas */
dx_frac = SDL_modff(dx, &dx);
dy_frac = SDL_modff(dy, &dy);
if (dx != 0.0f || dy != 0.0f) {
    ...
}
@ericwa
Copy link
Contributor

ericwa commented Dec 30, 2022

Sure.

I did a bit of testing with testwm2 and looking at the displayed SDL_GetGlobalMouseState + SDL_GetMouseState

  • macOS 10.15.7, 200% OS scaling:
    • without --allow-highdpi: looks fine, getting decimal mouse coords for SDL_GetMouseState between (0, 0) and (639, 479). SDL_GetGlobalMouseState is also fractional. I was using the mac's trackpad for this.
    • with--allow-highdpi: also fine, same as above (except rendering is highdpi now)
  • Windows 11, cmake + VS2022, 125% OS scaling
    • without enabling DPI awareness: get integer mouse coords only. I think this is expected because Windows is scaling everything before it gets to SDL in this case
    • with SDL_WINDOWS_DPI_SCALING=1 environment variable: now getting decimal mouse coords for SDL_GetMouseState between (0, 0) and (639, 479), like on macOS

All looks good so far

@slouken
Copy link
Collaborator Author

slouken commented Dec 30, 2022

Great, thanks!

@slouken slouken merged commit 7f23d71 into libsdl-org:main Dec 30, 2022
@slouken slouken deleted the highdpi-mouse branch December 30, 2022 07:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SDL_MouseMotionEvent with SDL_WINDOW_ALLOW_HIGHDPI flag
2 participants