Skip to content

Instantly share code, notes, and snippets.

@slouken
Created March 19, 2023 18:23
Show Gist options
  • Save slouken/7c41d29a252d58778794311f555ebe8c to your computer and use it in GitHub Desktop.
Save slouken/7c41d29a252d58778794311f555ebe8c to your computer and use it in GitHub Desktop.
Accumulating relative mouse motion
while (SDL_PollEvent(&event) > 0) {
if (event.type == SDL_EVENT_MOUSE_MOTION) {
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) {
SDL_Log("Relative motion: %g,%g\n", dx, dy);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment