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

Author Topic: Sending OpenGL commands after window is closed?  (Read 3163 times)

0 Members and 2 Guests are viewing this topic.

Guni

  • Newbie
  • *
  • Posts: 20
    • View Profile
Sending OpenGL commands after window is closed?
« on: March 03, 2012, 02:04:54 am »
Hi, my question is this: Is it safe to send OpenGL commands when a window has been set as the active window, then closed?

Hopefully that is a yes or no question. If not, here is a more detailed description of my problem.

I have been using OpenGL with SFML for some time now. I am running Windows 7 Ultimate, with SFML 2.0, with static linking. This problem occurs both when I create a context with OpenGL 3.3 and 4.1.

In my main loop, the window polls for input, does its OpenGL commands, then calls Display() on the active window. If during the polling for events, sf::Event::Closed is detected, Close() is called active window immediately.

What this means is that for at least one iteration of the main loop, OpenGL commands are being sent despite the active window being closed.

It looks something like this:

Code: [Select]

 sf::Window window(sf::VideoMode(800, 600), "SFML window");
//initialize OpenGL (set up shaders, etc.)

while (window.isOpen())
{
sf::Event anEvent;
while (window.PollEvent(anEvent))
{
if (anEvent.Type == sf::Event::Closed)
window.Close();
}

//OpenGL commands...
window.Display();
}


After the window is closed, the main loop exits, and the program quits.

This works fine, but I made some additions to my OpenGL code to use Vertex Array Objects and Depth Buffering, without changing any SFML-related code and now when I close the window, instead of exiting smoothly, the program crashes. Debugging this shows that sending a draw command then modifying a uniform variable causes the program to crash. This is kind of unusual, since OpenGL commands themselves do not generally cause a program to crash.

To workaround this I have had to modify the main loop a little bit.

Code: [Select]

while (window.isOpen())
{
sf::Event anEvent;
bool isOpen = true;
while (window.PollEvent(anEvent))
{
if (anEvent.Type == sf::Event::Closed)
isOpen = false;
}

//OpenGL commands...
window.Display();
if (!isOpen)
window.Close();
}


Now, OpenGL commands are never being sent to a closed window.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Sending OpenGL commands after window is closed?
« Reply #1 on: March 03, 2012, 10:21:18 am »
When the window is closed, its OpenGL context is destroyed. So if you use OpenGL functions they will fail.

However there are two very simple ways of handling that:

- put the event loop at the end of the main loop, so that nothing is done after the window is closed.

- call (and check) window.Activate() before calling your OpenGL functions -- it returns false when the window has been closed.
Laurent Gomila - SFML developer

Guni

  • Newbie
  • *
  • Posts: 20
    • View Profile
Sending OpenGL commands after window is closed?
« Reply #2 on: March 03, 2012, 09:06:53 pm »
Yes, I am making too much of a relatively unimportant problem. However, if I put the event loop at the end of my main loop, and let's say I stall the main loop if 1/60 of a second has not passed, wouldn't that cause a perpetual "one frame delay" in the visual responsiveness of the game? Again, probably not a particularly important consideration.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Sending OpenGL commands after window is closed?
« Reply #3 on: March 03, 2012, 09:26:21 pm »
Quote
However, if I put the event loop at the end of my main loop, and let's say I stall the main loop if 1/60 of a second has not passed, wouldn't that cause a perpetual "one frame delay" in the visual responsiveness of the game?

Not if you put the event loop after window.Display(). The only difference is that it will be right before the loop condition, instead of right after. Which is exactly what we want.
Laurent Gomila - SFML developer

 

anything