Why pollEvent is always used with a while loop like
while (window.pollEvent(event))
{
//doing something accordingly to the type of events happened...
}
?
why can it be
if (window.pollEvent(event))
{
//doing something accordingly to the type of events happened...
}
?
Is it solely because event could be not only just one event but a queue of events, and therefore it need to use while loop to process every event in the queue? If this is indeed the case, the rise another problem. I was just reading this book
SFML game development, and in the first chapter, it teaches you to write a program that allows you to move a circle by press 'w', 'a', 's' and 'd', whose code is shown below:
#include<SFML/Graphics.hpp>
class Game;
int main()
{
Game game;
game.run();
}
class Game
{
public:
Game();
void run();
private:
void processEvents();
void update(sf::Time deltaTime);
void render();
void handlePlayerInput(sf::Keyboard::Key key, bool isPressed);
sf::RenderWindow mWindow;
sf::CircleShape mPlayer;
bool mIsMovingUp = false;
bool mIsMovingDown = false;
bool mIsMovingRight = false;
bool mIsMovingLeft = false;
};
Game::Game():mWindow(sf::VideoMode(640,480),"SFML Application"),mPlayer()
{
mPlayer.setRadius(40.f);
mPlayer.setPosition(100.f,100.f);
mPlayer.setFillColor(sf::Color::Cyan);
}
void Game::run()
{
sf::Clock clock;
while (mWindow.isOpen())
{
sf::Time deltaTime = clock.restart();
processEvents();
update(deltaTime);
render();
}
}
void Game::processEvents()
{
sf::Event event;
while(mWindow.pollEvent(event))
{
switch(event.type)
{
case sf::Event::KeyPressed:
handlePlayerInput(event.key.code, true);
break;
case sf::Event::KeyReleased:
handlePlayerInput(event.key.code, false);
break;
case sf::Event::Closed:
mWindow.close();
break;
}
}
}
void Game::handlePlayerInput(sf::Keyboard::Key key, bool isPressed)
{
if(key == sf::Keyboard::W)
mIsMovingUp = isPressed;
else if(key == sf::Keyboard::S)
mIsMovingDown = isPressed;
else if(key == sf::Keyboard::A)
mIsMovingLeft = isPressed;
else if(key == sf::Keyboard:
)
mIsMovingRight = isPressed;
}
void Game::update(sf::Time deltaTime)
{
sf::Vector2f movement(0.f, 0.f);
if (mIsMovingUp)
movement.y -= 100.f;
if (mIsMovingDown)
movement.y += 100.f;
if (mIsMovingLeft)
movement.x -= 100.f;
if (mIsMovingRight)
movement.x += 100.f;
mPlayer.move(movement * deltaTime.asSeconds());
}
void Game::render()
{
mWindow.clear();
mWindow.draw(mPlayer);
mWindow.display();
}
In this code, if the object 'event' contained two events at a time while processEvents() was run, say a Event::KeyPressed and a Event::KeyReleased, then after the while loop terminated after finishing processing the two events, the values of four bool type object, 'mIsMovingUp' and the other three, would be set to be false, which would not result in a movement of the circle, but the user indeed had pressed one of the four keys, and was expecting to see the circle moving.
Therefore, I personally think that using while here with pollEvent would only jeopardize this program, and if would be a much better choice. Am I right?