Skip to content

Commit

Permalink
Change SDL event handling to use pump/peep isntead of poll
Browse files Browse the repository at this point in the history
As recommended in
libsdl-org/SDL#4794 (comment). Note
that the linked PR itself will also fix this without any changes our
side, but we will still get some extra efficiency by refactoring how we
are processing events regardless.

Should help with high poll rate input devices to some degree.
  • Loading branch information
peppy committed Oct 13, 2021
1 parent e33a447 commit 5ba0db5
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions osu.Framework/Platform/SDL2DesktopWindow.cs
Expand Up @@ -577,13 +577,23 @@ private void pollMouse()

protected void ScheduleCommand(Action action) => commandScheduler.Add(action, false);

private const int events_per_peep = 64;
private SDL.SDL_Event[] events = new SDL.SDL_Event[events_per_peep];

/// <summary>
/// Poll for all pending events.
/// </summary>
private void pollSDLEvents()
{
while (SDL.SDL_PollEvent(out var e) > 0)
handleSDLEvent(e);
SDL.SDL_PumpEvents();

int incoming;

while ((incoming = SDL.SDL_PeepEvents(events, events_per_peep, SDL.SDL_eventaction.SDL_GETEVENT, SDL.SDL_EventType.SDL_FIRSTEVENT, SDL.SDL_EventType.SDL_LASTEVENT)) > 0)

This comment has been minimized.

Copy link
@DomGries

DomGries Oct 13, 2021

My suggestion with do { } while (incoming == events_per_peep); calls PeepEvents only once if less than events_per_peep events are returned whereas in your case you call PeepEvents at least twice

This comment has been minimized.

Copy link
@peppy

peppy Oct 13, 2021

Author Owner

Fair suggestion, thanks.

{
for (int i = 0; i < incoming; i++)
handleSDLEvent(events[i]);
}
}

private void handleSDLEvent(SDL.SDL_Event e)
Expand Down

0 comments on commit 5ba0db5

Please sign in to comment.