Meph
Meph

Reputation: 372

SFML & OpenGL(glew) Why does my program fail when drawing to new window?

I have a problem regarding SFML windows, contexts and OpenGl drawing to it as follows:

When I create a new Window to draw to (because I need it Fullscreen or changed any delicate context settings at runtime) i use sf::Window::create(args...) to create a new window, the old one vanishes. To my understanding this does not affect my OpenGl calls, because the "active" states of the contexts the windows hold will be set automatically (So the new window will be active).

So why does OpenGl fail to draw anything to the new window or stops working ?

I use wrappers for SFMLContents(SFMLHandler) and hide all OpenGl calls in a class(graphics). Here's Code: The constructor of the MainApp:

MainApp::MainApp()
{
    IMediaHandle::ContextSettings settings;
    settings.depth = 24;
    settings.stencil = 8;
    settings.antialiasing = 0;
    settings.openglmajor = 3;
    settings.openglminor = 3;
    SFMLHandle.createWindow("Test", 800, 600, settings);
    graphics.init();
    //I want to be able to do the following line
    SFMLHandle.createWindow("Test", 200, 400, settings);
}

This code works just fine old window gets destroyed and new one comes up. But when i start rendering in the apploop later the programm crashes at the call of glBindVertexArray(0);

If I leave out the additional line my rendering works of course.

What do I have to do to apply the old drawing mechanism to the new Window ? I can always initialize my graphics again but do I have to ? Was this not the point of setting a Context active or inactive ?:

SFMLHandle.createWindow("Test", 800, 600, settings);
graphics.init();

SFMLHandle.createWindow("Test", 200, 400, settings);
// unnecessary ? 
graphics.init();

This code works, too, with no crashes later.

I thank you for clearing this one up !

Upvotes: 0

Views: 330

Answers (1)

Peter K
Peter K

Reputation: 1382

When you create a new window, a new OpenGL context is made. SFML made it so that this context shares its resources with the old (or any other) context. This means your textures, buffers and shaders are still valid.

There are, however, OpenGL objects that cannot be shared between OpenGL contexts. Vertex Array Objects are among those and AFAIK Framebuffer Objects too. You will have to recreate these before you start drawing in your new context.

See also this page.

Upvotes: 2

Related Questions