SFML community forums

Help => Window => Topic started by: wintertime on November 03, 2013, 11:50:19 pm

Title: WaitEvent sleeping a bit too often
Post by: wintertime on November 03, 2013, 11:50:19 pm
When looking through the SFML code I found it is always doing a sleep on waitEvent when the SFML queue is empty, even if process(Joystick)Events can get fresh events from the OS at first try.
Wouldn't it be better to restructure this code from WindowImpl.cpp
bool WindowImpl::popEvent(Event& event, bool block)
{
    // If the event queue is empty, let's first check if new events are available from the OS
    if (m_events.empty())
    {
        if (!block)
        {
            // Non-blocking mode: process events and continue
            processJoystickEvents();
            processEvents();
        }
        else
        {
            // Blocking mode: process events until one is triggered

            // Here we use a manual wait loop instead of the optimized
            // wait-event provided by the OS, so that we don't skip joystick
            // events (which require polling)
            while (m_events.empty())
            {
                processJoystickEvents();
                processEvents();
                sleep(milliseconds(10));
            }
        }
    }

    // Pop the first event of the queue, if it is not empty
    if (!m_events.empty())
    {
        event = m_events.front();
        m_events.pop();

        return true;
    }

    return false;
}
 
a bit, to get this where it does a first try always?
bool WindowImpl::popEvent(Event& event, bool block)
{
    // If the event queue is empty, let's first check if new events are available from the OS
    if (m_events.empty())
    {
        processJoystickEvents();
        processEvents();

        if (!block)
        {
            // Non-blocking mode: stop if no events found
            if (m_events.empty())
                return false;
        }
        else
        {
            // Blocking mode: process events until one is triggered

            // Here we use a manual wait loop instead of the optimized
            // wait-event provided by the OS, so that we don't skip joystick
            // events (which require polling)
            while (m_events.empty())
            {
                sleep(milliseconds(10));
                processJoystickEvents();
                processEvents();
            }
        }
    }

    // Pop the first event of the queue
    event = m_events.front();
    m_events.pop();

    return true;
}
 
Title: Re: WaitEvent sleeping a bit too often
Post by: Laurent on November 04, 2013, 07:38:56 am
It's done.

Thanks for your help :)