Skip to content

[rcore][SDL] Accumulate relative mouse motion per frame - #6215

Merged
raysan5 merged 1 commit into
raysan5:masterfrom
snowyukitty:fix/sdl-relative-mouse-motion
Oct 4, 2026
Merged

raysan5 merged 1 commit into
raysan5:masterfrom
snowyukitty:fix/sdl-relative-mouse-motion

Conversation

@snowyukitty

Copy link
Copy Markdown
Contributor

With the cursor disabled, the SDL backend keeps only the last mouse-motion event polled in a frame. This loses movement when several events arrive between frames, making camera rotation depend on the frame rate.

Accumulate each event's relative displacement. Reset both mouse positions before polling a relative frame, so an empty first frame after DisableCursor() also returns zero instead of a stale absolute delta. Absolute cursor movement continues to use the latest position.

Fixes #6168.

Validation on Linux/Xvfb with real SDL event queues and the public GetMouseDelta() API:

  • SDL2: all 11 scenarios pass; the upstream baseline fails four. SDL3 3.4.18: all 12 pass; the baseline fails five, including fractional motion.
  • The same 64 input events produce (128, -64) both in one frame and across 64 frames. The baseline returns (2, -1) when they are batched.
  • Covered multiple/opposing/single/zero events, empty frames, switching into relative mode and back to absolute mode.
  • Windows x64 SDL2 2.32.10: MinGW cross-compilation and executable linking passed. Native Windows runtime testing was not performed.
  • The existing Clang-Tidy workflow reports the same three warnings in untouched rcore_drm.c on the baseline and patch. Existing SDL unused-function compiler warnings remain.
Minimal SDL2 event-queue reproduction

Build raylib with PLATFORM=PLATFORM_DESKTOP_SDL, then link this with raylib and SDL2. Run in an X11 session (or Xvfb). The expected delta is (128, -64).

#include "raylib.h"
#include <SDL.h>
#include <stdio.h>

extern void PollInputEvents(void);

int main(void)
{
    InitWindow(320, 240, "Relative mouse motion");
    DisableCursor();
    SDL_PumpEvents();
    SDL_FlushEvents(0, SDL_LASTEVENT);
    PollInputEvents();
    for (int i = 0; i < 64; i++)
    {
        SDL_Event event = { 0 };
        event.type = SDL_MOUSEMOTION;
        event.motion.windowID = 1;
        event.motion.xrel = 2;
        event.motion.yrel = -1;
        SDL_PushEvent(&event);
    }
    PollInputEvents();
    Vector2 delta = GetMouseDelta();
    printf("delta=(%.0f, %.0f)\n", delta.x, delta.y);
    CloseWindow();
    return 0;
}

@raysan5
raysan5 merged commit 5c9e83e into raysan5:master Oct 4, 2026
@raysan5

raysan5 commented Oct 4, 2026

Copy link
Copy Markdown
Owner

@snowyukitty thanks for the review

@goob228

goob228 commented Oct 4, 2026 •

Copy link
Copy Markdown

Cool that you also fixed another issue that I thought about by zeroing CORE.Input.Mouse.previousPosition before while loop. Thanks

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.

[rcore][SDL] Relative mouse motion is overwritten in event loop

3 participants