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

Author Topic: Question: Why a window stays open when using loops?  (Read 1900 times)

0 Members and 1 Guest are viewing this topic.

complexity

  • Guest
Question: Why a window stays open when using loops?
« 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?


« Last Edit: August 06, 2013, 11:01:48 am by complexity »

Rhimlock

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: Question: Why a window stays open when using loops?
« Reply #1 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.
« Last Edit: August 06, 2013, 12:03:18 pm by Ghosa »

complexity

  • Guest
Re: Question: Why a window stays open when using loops?
« Reply #2 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?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Question: Why a window stays open when using loops?
« Reply #3 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.
Laurent Gomila - SFML developer

complexity

  • Guest
Re: Question: Why a window stays open when using loops?
« Reply #4 on: August 06, 2013, 11:46:50 am »
Thanks!

 

anything