SFML community forums

Help => Window => Topic started by: complexity on August 06, 2013, 10:36:27 am

Title: Question: Why a window stays open when using loops?
Post by: complexity on August 06, 2013, 10:36:27 am
Hi, everyone!

I am new to SFML and have some basic skills in C++ programming.

I don't get why a window stays open if loops are used:

#include <SFML/Graphics.hpp>

int main()
{
    sf::Window window;
    window.create(sf::VideoMode(800,600),"Title");
    while(1)
    {
    }
}

Without loops the window is going to close promptly. Why?


Title: Re: Question: Why a window stays open when using loops?
Post by: Rhimlock on August 06, 2013, 11:24:58 am
Simple:
You create your window in the main function, so it is closed when the main function ends.
With the loop you keep your main-function running endlessly.
Title: Re: Question: Why a window stays open when using loops?
Post by: complexity on August 06, 2013, 11:30:23 am
Hmm.. I thoguht just the empty loop block is executed not the whole main function.
So everytime I use loops in functions the functions will keep running as long as the loops are running, right?
Title: Re: Question: Why a window stays open when using loops?
Post by: Laurent on August 06, 2013, 11:43:26 am
No, what's inside the loop will keep running.

But here the window is destroyed at the end of the function, after the loop. So as long as the loop is running, the window stays open because the function has not reached the point where it destroys the window yet.
Title: Re: Question: Why a window stays open when using loops?
Post by: complexity on August 06, 2013, 11:46:50 am
Thanks!