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

SDL2,SDL3: Use events to update gamepad inputs #8508

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

RT2Code
Copy link
Contributor

@RT2Code RT2Code commented Mar 21, 2025

Makes use of SDL events to update gamepad inputs on SDL2 and SDL3 backends. This way, inputs are only updated when required instead of systematically on every frame, and it provides the benefit of handling multiple input events between frames accurately, which should behave better on low framerates.

Apart from input update frequency, there should be no differences: Gamepad modes are supported, and events are merged between multiple gamepads if necessary. The only exception is how the ImGuiBackendFlags_HasGamepad flag is updated. I don't know if there was a specific reason for this or if it was an oversight, but currently, this flag is only updated if ImGuiConfigFlags_NavEnableGamepad is specified, which can lead to situations where there are gamepads connected but ImGuiBackendFlags_HasGamepad is not set. I changed it to always be updated, regardless of ImGuiConfigFlags_NavEnableGamepad status (but gamepad inputs are still not processed if it's not set of course).

@ocornut
Copy link
Owner

ocornut commented Mar 21, 2025

FYI historically I think gamepad inputs were gated by ImGuiConfigFlags_NavEnableGamepad partly because people had performances issues with gamepad access on some platforms. I'm not sure how relevant it is now.

Also linking to #8329 and #6559.

@ocornut
Copy link
Owner

ocornut commented Mar 21, 2025

FYI historically I think gamepad inputs were gated by ImGuiConfigFlags_NavEnableGamepad partly because people had performances issues with gamepad access on some platforms. I'm not sure how relevant it is now.

See #8075 for GLFW

ocornut added a commit that referenced this pull request Mar 21, 2025
…ags_HasGamepad regardless of ImGuiConfigFlags_NavEnableGamepad being set. (#8508)
@ocornut
Copy link
Owner

ocornut commented Mar 21, 2025

The only exception is how the ImGuiBackendFlags_HasGamepad flag is updated. I don't know if there was a specific reason for this or if it was an oversight, but currently, this flag is only updated if ImGuiConfigFlags_NavEnableGamepad is specified, which can lead to situations where there are gamepads connected but ImGuiBackendFlags_HasGamepad is not set. I changed it to always be updated, regardless of ImGuiConfigFlags_NavEnableGamepad status (but gamepad inputs are still not processed if it's not set of course).

For that part I have pushed 979c7d7 separately for SDL2, SDL3, OSX backends.
Actually submitting gamepad inputs.

I am a little worried about the potential effect of unstable/always changing analog inputs on input tricking code (ImGui::UpdateInputEvents()), it might in theory severely interfere with other inputs pretty badly. It will tend to set key_changed=true in that loop, and the order of events may have side effects.

ocornut added a commit that referenced this pull request Mar 21, 2025
…erfere with trickling of mouse position events (#4921, #8508)
ocornut added a commit to ocornut/imgui_test_engine that referenced this pull request Mar 21, 2025
@ocornut
Copy link
Owner

ocornut commented Mar 21, 2025

I am a little worried about the potential effect of unstable/always changing analog inputs on input tricking code (ImGui::UpdateInputEvents()), it might in theory severely interfere with other inputs pretty badly. It will tend to set key_changed=true in that loop, and the order of events may have side effects

Pushed a fix 102f3f3 for this + basic test ocornut/imgui_test_engine@99d4ed8

@RT2Code RT2Code force-pushed the gamepad-poll-events branch from 44e7afe to 8d017ab Compare March 22, 2025 01:03
@RT2Code
Copy link
Contributor Author

RT2Code commented Mar 22, 2025

For that part I have pushed 979c7d7 separately for SDL2, SDL3, OSX backends. Actually submitting gamepad inputs.

Oh, I thought that disabling inputs with ImGuiConfigFlags_NavEnableGamepad off was done to actually disable the nav feature, I didn't think of it as two different things, but it effectively still makes sense to handle inputs even with the nav off. I updated my PR to emit input events even with ImGuiConfigFlags_NavEnableGamepad off.

I am a little worried about the potential effect of unstable/always changing analog inputs on input tricking code (ImGui::UpdateInputEvents()), it might in theory severely interfere with other inputs pretty badly. It will tend to set key_changed=true in that loop, and the order of events may have side effects

Pushed a fix 102f3f3 for this + basic test ocornut/imgui_test_engine@99d4ed8

Awesome, I would have suggested to only use events for gamepad buttons as a workaround, but this is better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants