SFML community forums

Help => General => Topic started by: kim366 on November 14, 2016, 02:29:58 pm

Title: Does it matter if a function is called within or outside the event block?
Post by: kim366 on November 14, 2016, 02:29:58 pm
Do the following code snippets do the exact same thing?

while (window.isOpen)
{
        sf::Event event;
        while (window.pollEvent(event))
        {
                if (event.type == sf::Event::mouseButtonPressed)
                {
                        // Do something here
                }
       
        } // Poll Events

} // Game Loop
 

bool bolean;

while (window.isOpen)
{
        bolean = false;

        sf::Event event;
        while (window.pollEvent(event))
        {
                if (event.type == sf::Event::mouseButtonPressed)
                {
                        boolean = true;
                }
       
        } // Poll Events

        if (boolean)
        {
                // Do the same thing here
        }

} // Gameloop
 

I am asking this, because you mentioned some sort of delay and timing in my last question and I want to know, if I am understanding this right, so sorry if this question seems stupid.

Thanks,
Kim
Title: Re: Does it matter if a function is called within or outside the event block?
Post by: Hapax on November 14, 2016, 03:19:10 pm
This would be a pretty common thing to do. Setting a variable from an event and then processing it outside the event loop makes a lot of sense.

I don't think you could consider this a 'delay'. Processing the event loop is mostly almost instantaneous; consider the fact that the final event to be processed is at the end of the loop. The following code (outside of the event loop) is then immediately executed.
Note that the entire "cycle" of the game loop will be happening many times per second so each one could be very quick indeed.
Title: Re: Does it matter if a function is called within or outside the event block?
Post by: kim366 on November 14, 2016, 03:39:53 pm
Thanks! Makes sense
Title: Re: Does it matter if a function is called within or outside the event block?
Post by: G. on November 14, 2016, 07:05:09 pm
If you have 2 (or more) mouseButtonPressed events at the same time (for example left mouse click and right mouse click at the same frame), "do something" will be executed twice in the first snippet of code but only once in the second.