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

Author Topic: How exactly does the event handler loop process WHILE other things happens?  (Read 1682 times)

0 Members and 1 Guest are viewing this topic.

BobbyT432

  • Newbie
  • *
  • Posts: 8
    • View Profile
Hello,
I'm completely new to SFML and have just begun reading the docs and messing around. I've noticed that I can run my event handler loop and check for events, while outside the loop other things are going on.
How does this loop not stop the program from continuing?

For example:
        // Event handler
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::MouseMoved)
                       
                        {
                                triangle[2].position = sf::Vector2f(event.mouseMove.x, event.mouseMove.y);
                        }
                }

                // Input
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
                        {
                                player.move(0, -speed);
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
                        {
                                player.move(-speed, 0);
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
                        {
                                player.move(0, speed);
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
                        {
                                player.move(speed, 0);
                        }

I can move my mouse around AND control keyboard inputs at the same exact time. How does it read that outside the event loop?

Thanks!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
The event loop runs typically just a few iterations (most likely none or one), because it ends when there's no more event to process, and in the lifetime of your app, there's much more time without events than with.
Laurent Gomila - SFML developer

BobbyT432

  • Newbie
  • *
  • Posts: 8
    • View Profile
Ah okay, so how does it work though if I'm constantly making it use the event handler? (For example I had an event that detected for mouse movement) and I was moving my mouse all over the screen WHILE using WASD to move my character ( which is input being detected outside the loop).

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
It'll (very quickly) loop through all the events during that frame (although, rarely will be more than one) and then, once all events have been processed by your code, it'll leave the loop and finish the frame.

As long as you don't have slow processing directly in the event loop, it should be done very fast indeed.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

BobbyT432

  • Newbie
  • *
  • Posts: 8
    • View Profile
Thanks a ton, I actually understand this now finally

 

anything