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

Open Beginner friendly
#6,168 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
86/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
c
Domain
desktop, game-dev

Research direction

Start in src/platforms/rcore_desktop_sdl.c at the SDL_PollEvent loop around the linked lines, then trace how GetMouseDelta is consumed by the core_3d_camera_free.c example. Reproduce with the provided cursor-disabled example at capped and unlimited FPS; done means relative motion behaves consistently under the SDL backend.

Written by the indexing model from the issue text.

Description

Please, before submitting a new issue verify and check:

  • I tested it on latest raylib version from master branch
  • I checked there is no similar issue already reported
  • I checked the documentation on the wiki
  • My code has no errors or misuse of raylib
Issue description

Relative mouse motion is frame-rate dependent when using the SDL backend with the cursor disabled. I noticed this after switching to SDL from the GLFW backend in my fps game project. It turns out that raylib overwrites relative mouse motion with the last SDL_MOUSEMOTION event in while (SDL_PollEvent(&event) != 0) loop. It should accumulate xrel and yrel instead. I can submit a PR with the fix.

Here:
https://github.com/raysan5/raylib/blob/a365e5617afb10230270d44ecb42c820d90cf6e9/src/platforms/rcore_desktop_sdl.c#L1735-L1742

Environment
  • PLATFORM_DESKTOP_SDL
  • GRAPHICS_API_OPENGL_33
  • Windows 11 Pro
  • MSVC 19.51.36257 for x64
Issue Screenshot

If possible, provide a screenshot that illustrates the issue. Usually an image is better than a thousand words.

Code Example

Provide minimal reproduction code to test the issue. Please, format the code properly and try to keep it as simple as possible, just focusing on the experienced issue.

Any code that uses Vector2 GetMouseDelta(void); function with the cursor disabled.

Below is a slightly modified core_3d_camera_free.c example which allows toggling FPS on and off to reproduce the issue.

#include "raylib.h"

int main(void)
{
    const int screenWidth = 800;
    const int screenHeight = 450;

    InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d camera free");

    // Define the camera to look into our 3d world
    Camera3D camera = { 0 };
    camera.position = (Vector3){ 10.0f, 10.0f, 10.0f }; // Camera position
    camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };      // Camera looking at point
    camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };          // Camera up vector (rotation towards target)
    camera.fovy = 45.0f;                                // Camera field-of-view Y
    camera.projection = CAMERA_PERSPECTIVE;             // Camera projection type

    Vector3 cubePosition = { 0.0f, 0.0f, 0.0f };

    DisableCursor();                    // Limit cursor to relative movement inside the window

    int targetFPS = 60;
    SetTargetFPS(targetFPS);

    // Main game loop
    while (!WindowShouldClose())        // Detect window close button or ESC key
    {

        if (IsKeyPressed(KEY_SPACE))
        {
            if (targetFPS == 0) targetFPS = 60;
            else targetFPS = 0;
            SetTargetFPS(targetFPS);
        }

        UpdateCamera(&camera, CAMERA_FREE);

        if (IsKeyPressed(KEY_Z)) camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };

        BeginDrawing();

            ClearBackground(RAYWHITE);

            BeginMode3D(camera);

                DrawCube(cubePosition, 2.0f, 2.0f, 2.0f, RED);

                DrawGrid(10, 1.0f);

            EndMode3D();

            DrawFPS(10, 10);
            DrawText("- Z to zoom to (0, 0, 0)", 40, 80, 10, DARKGRAY);

        EndDrawing();
        
    }

    CloseWindow();

    return 0;
}

In PLATFORM_DESKTOP_SDL camera rotation slows down when FPS is capped but works normally with unlimited FPS, unlike PLATFORM_DESKTOP_GLFW where it works fine in both cases.

Dominant language
C
Stars
34.8k
Forks
3.3k
Avg merge
17h 56m
Merged PRs (30d)
59

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from raysan5/raylib

All issues in raysan5/raylib

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.