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

Author Topic: Rendering on closed window  (Read 2552 times)

0 Members and 1 Guest are viewing this topic.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Rendering on closed window
« on: June 22, 2012, 07:44:54 pm »
The typical SFML example looks like this:
sf::RenderWindow window(...);
sf::Sprite sprite(...);

while (window.isOpen())
{
        sf::Event event;
        while (window.pollEvent(event))
        {
                if (event.type == sf::Event::Closed)
                        window.close();
        }

        window.clear();
        window.draw(sprite);
        window.display();
}

After the event has fired and the close() function has been called, the three rendering-related functions are still invoked. Earlier this led to OpenGL error messages, now I think the behavior is protected by the sf::RenderTarget::activate() calls. Is this a potential problem, or only an esthetical one?

In the Thor examples, I have always used for (;;) and return 0; to leave the loop directly.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Rendering on closed window
« Reply #1 on: June 22, 2012, 08:16:27 pm »
It's totally safe, but maybe I should adopt a "cleaner" convention.

Solution #1:
while (window.isOpen())
{
        window.clear();
        window.draw(sprite);
        window.display();

        sf::Event event;
        while (window.pollEvent(event))
        {
                if (event.type == sf::Event::Closed)
                        window.close();
        }
}

Solution #2:
bool running = true;
while (running)
{
        sf::Event event;
        while (window.pollEvent(event))
        {
                if (event.type == sf::Event::Closed)
                        running = false;
        }

        window.clear();
        window.draw(sprite);
        window.display();
}

I don't like yours :P infinite loops don't look clean and trigger warnings with some compilers. And although I'm not a big fan of the SESE (single entry, single exit) rule, for the main() function it feels better to have a single return statement at the end, rather than one in the middle of the loop.
Laurent Gomila - SFML developer

Celtic Minstrel

  • Jr. Member
  • **
  • Posts: 80
    • View Profile
Re: Rendering on closed window
« Reply #2 on: June 22, 2012, 10:31:20 pm »
Um, #2 still renders on the closed window. :P

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Rendering on closed window
« Reply #3 on: June 22, 2012, 10:46:59 pm »
#2 doesn't close the window, it leaves that to the destructor when main() returns. sf::Event::Closed is a close request, not a notification; if you don't explicitely close the window it remains open.
Laurent Gomila - SFML developer

Celtic Minstrel

  • Jr. Member
  • **
  • Posts: 80
    • View Profile
Re: Rendering on closed window
« Reply #4 on: June 23, 2012, 04:40:50 am »
Oh right. Duh. <_<

 

anything