Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Does it matter if a function is called within or outside the event block?  (Read 2064 times)

0 Members and 1 Guest are viewing this topic.

kim366

  • Newbie
  • *
  • Posts: 35
    • View Profile
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

Hapax

  • Hero Member
  • *****
  • Posts: 3385
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Does it matter if a function is called within or outside the event block?
« Reply #1 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.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

kim366

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: Does it matter if a function is called within or outside the event block?
« Reply #2 on: November 14, 2016, 03:39:53 pm »
Thanks! Makes sense

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Does it matter if a function is called within or outside the event block?
« Reply #3 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.