SDL2 input events on iOS

Hi,

I have a thread for handling events and works fine on the desktop.
When i run it on iphone the events don’t reach the thread unless i call SDL_PumpEvents() on the main thread, which is the same of not having a thread at all!

The code is like:

start handling input on a thread
while(!quit) {
    physics()
    render()
    SDL_RenderPresent

}

Thread:    
SDL_Event e;
 while (SDL_WaitEvent(&e)) {
    if mouse down ...
}

There is any specific way to use thread on iOS rather than
std::thread t1(inputEvents,…
t1.detach();

so it receives the events correclty?

No, most input and windowing-related Apple system APIs (including event polling) must happen on the main thread: Diagnosing memory, thread, and crash issues early | Apple Developer Documentation

Additionally, SDL_Render must use the main thread, for similar reasons: https://github.com/libsdl-org/SDL/blob/SDL2/include/SDL_render.h#L44-L45

You can do other work on non-main thread as long as it’s not window/input/SDL_Render-related and as long as you don’t have other race conditions.

Typically you’d run SDL_PollEvent in a loop every frame on the main thread instead of doing SDL_WaitEvent somewhere else.

Yes, but what puzzles me is that running events on main thread is limited by fps.

As long as you have a while-loop of SDL_PollEvent every frame, then you will receive all input events that happened since the last frame because the OS queues them up. You won’t miss any events that way even with really low framerate.

In theory yes. I have run two tests, one as you suggested with poll event loop on main thread and another with a separate thread, the first case is very slow, not responsice compared to the 2nd.

Most games and engines (including my own) use that approach regardless of whether they use SDL, with no problems. Do you mind sharing your code? Maybe there’s a bug in it somewhere.

I might share the code…
I rercorded a video for each version but cant upload because it says “newbies” cannot upload files.

Make sure that your event polling is running in its own while loop:

while(!quit) {
    SDL_Event event;
    while(SDL_PollEvent(&event)) {
        switch(event.type) {
        // set flags based on events here
        }
    }

    // Respond to event flags here
}