I’m trying to use SDL2 library with JUCE. Though it is working, I’m still wondering if I could run a custom job synchronized with JUCE’s event dispatching. This job would contain SDL’s event iteration.
Currently I use a JUCE timer to trigger the SDL event iteration at 60 Hz. I thought it may induce more latency than synchronously triggering.
This is the fake code of current implementation:
void sdl_mainloop_step()
{
SDL_Event e;
while ( SDL_PollEvent( &e ) )
{
switch ( e.type )
{
case SDL_QUIT:
exit( 0 );
case SDL_MOUSEMOTION:
{
auto win = SDL_GetWindowFromID( e.motion.windowID );
// dispatch SDL event by SDL window
break;
}
}
}
}
// a global singleton that starts SDL trigger timer
// the SDL iteration is queued in JUCE message loop at 60Hz
struct SdlEventIterator : public juce::Timer
{
static SdlEventIterator& getInstance()
{
static SdlEventIterator instance;
return instance;
}
private:
SdlEventIterator()
{
startTimerHz( 60 );
}
void timerCallback() override
{
juce::MessageManager::callAsync( [](){ sdl_mainloop_step(); } );
}
};
What I’m seeking for is something like:
juce::EventManager::registerExtraJobInEventLoop( [](){ sdl_mainloop_step(); } );