SFML community forums

Help => Window => Topic started by: Sixoul on October 23, 2013, 02:43:44 am

Title: GameLoop and checking if window has been closed
Post by: Sixoul on October 23, 2013, 02:43:44 am
How should I handle checking if an event, such as closing the window, has occurred if I also need to be able to go through the game loop?

int main()
{
        MyGame game;
        game.setIsRunning(true);

        sf::RenderWindow gameWindow(sf::VideoMode(MyGame::width * MyGame::scale, MyGame::height * MyGame::scale), "My Game!");
       
        while (gameWindow.isOpen())
    {
        // check all the window's events that were triggered since the last iteration of the loop
        sf::Event event;
        while (gameWindow.pollEvent(event))
        {
            // "close requested" event: we close the window
            if (event.type == sf::Event::Closed)
                gameWindow.close();
        }
        }
       
        while ( game.getIsRunning() == true)
        {
                game.gameLoop();
        }

        return 0;

If I'm constantly running through a loop while the window is open I'll never get to the game loop until the window is closed. Sorry if this is a stupid question, I'm still new to SFML and game programming in general. Thanks for any advice or help.
Title: Re: GameLoop and checking if window has been closed
Post by: zsbzsb on October 23, 2013, 02:49:41 am
You need to review how program flow works and reread the official tutorials. This is some basic C++ so maybe you should learn more about programming before you attempt to write games.  ;)

int main()
{
        MyGame game;
        game.setIsRunning(true);

        sf::RenderWindow gameWindow(sf::VideoMode(MyGame::width * MyGame::scale, MyGame::height * MyGame::scale), "My Game!");
       
        while (gameWindow.isOpen()) // THIS IS YOUR GAME LOOP
    {
        // check all the window's events that were triggered since the last iteration of the loop
        sf::Event event;
        while (gameWindow.pollEvent(event))
        {
            // "close requested" event: we close the window
            if (event.type == sf::Event::Closed)
                gameWindow.close();
        }
        // DO YOUR UPDATING/DRAWING HERE
        }

        return 0;
}