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

Author Topic: GameLoop and checking if window has been closed  (Read 2242 times)

0 Members and 1 Guest are viewing this topic.

Sixoul

  • Newbie
  • *
  • Posts: 17
    • View Profile
GameLoop and checking if window has been closed
« 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.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: GameLoop and checking if window has been closed
« Reply #1 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;
}
« Last Edit: October 23, 2013, 03:06:24 am by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

 

anything