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

Author Topic: Why "while(window.pollEvent(event))" and not "if(window.pollEvent(event))"?  (Read 2024 times)

0 Members and 1 Guest are viewing this topic.

DasOhmoff San

  • Guest
Hi, why do we use: while(window.pollEvent(event))
and not:                if     (window.pollEvent(event)) ?

I mean if we use while shouldn't it stop to clear, draw or display while we move our mouse for example?
I tested it but it works with while without problems and with if too.

But why does it work???
Sry for my englisch.

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
The while loop will run WHILE there is valid data ready to polled. The if statement will run once each frame IF there is data. Using the if statement will result in delayed and possibly unresponsive events (IE: keystrokes not being registered or being delayed).

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
You use while to pop all the pending events at every iteration of the main loop. If you use "if", you only process one event even if there are others waiting to be processed. Note that in practice it shouldn't make a big difference, because events are generated less often than your main loop rate. That also explains why your main loop is never blocked by the event loop.
Laurent Gomila - SFML developer

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
The event queue is not of infinite size and you may have events comming in faster than your FPS. So, if you only process one event per frame you may build up a huge backlog of unprocessed events. If you process every available event every frame and then draw the result, no backlog builds up and (assuming you actually handle all events properly), the end result will be just fine once you are done and actually draw the frame.

DasOhmoff San

  • Guest
You use while to pop all the pending events at every iteration of the main loop. If you use "if", you only process one event even if there are others waiting to be processed. Note that in practice it shouldn't make a big difference, because events are generated less often than your main loop rate. That also explains why your main loop is never blocked by the event loop.

The event queue is not of infinite size and you may have events comming in faster than your FPS. So, if you only process one event per frame you may build up a huge backlog of unprocessed events. If you process every available event every frame and then draw the result, no backlog builds up and (assuming you actually handle all events properly), the end result will be just fine once you are done and actually draw the frame.

The while loop will run WHILE there is valid data ready to polled. The if statement will run once each frame IF there is data. Using the if statement will result in delayed and possibly unresponsive events (IE: keystrokes not being registered or being delayed).

ohhhhh, now i unterstand.
It's much more simple than i tought it would be.

Thank you a lot gui's.