SFML community forums

Help => Window => Topic started by: Lagermeister on July 30, 2022, 09:11:50 pm

Title: Events - Events outside "while (window.pollEvent(event))"
Post by: Lagermeister on July 30, 2022, 09:11:50 pm
I am following a tutorial. The author has choosen a structure for handling keyboards inputs which i do not really understand.

This is the section for keyboard input.

A. Loop

while (window.pollEvent(event))
e.g. if (event.key.code == Keyboard::Return && state == State::PLAYING) state = State::PAUSED;


B. Realtime input

e.g. if (Keyboard::isKeyPressed(Keyboard::W)) player.moveUp();

C. Special Game State input

e.g. if (event.key.code = Keyboard::Num1) state = State::PLAYING; // No loop

I struggle to understand part C. Every time a key is pressed, event is created and added to a queue. Part A loops through the queue and removes any event. So which event is used by part C ? It is already in a new queue and what happens when there are several new events already in the queue ? Does event.key.code always point to the next event which should leave the queue ?

Thanks for help ?
         
   


         
Title: Re: Events - Events outside "while (window.pollEvent(event))"
Post by: kojack on July 30, 2022, 09:44:31 pm
Which tutorial is this?
event really shouldn't be used outside of the event loop, that would miss events if more than one arrives in a frame.
Title: Re: Events - Events outside "while (window.pollEvent(event))"
Post by: Lagermeister on July 30, 2022, 11:01:23 pm
The book is "Begin C++ Programming" , page 227

As part C has much more code lines so it was design decision but then he should have sent code to new functions.