Hello, SFML Forums.
This post is for people who mix custom OpenGL code with SFML.
It's not new that frameworks (like SFML) don't expose low level functionality to be able to keep the same OpenGL context when creating a new window (
https://stackoverflow.com/questions/12881049/recreate-window-without-destroying-the-context).
I'm writing this post to show how I managed to minimally adapt SFML's code to support this feature on Windows.
Before, I used to manually delete all my OpenGL resources, call window.create(), then create then back. This is cumbersome and slow, specially when a lot of gigabytes of data are already sitting in the GPU. OpenGL resources included VAO, VBO, FBO, UBO, Shaders, Textures, etc. Now, after recreating the window, I only have to update stuff that actually depend on the window size, like my FBO's GL_TEXTURE_RECTANGLE's with
glTexImage2D. Everything is preserved.
We are going to change some things in
sfml-window's project.
Firstly, add the directory
src\SFML\Window\Win32 to the project's include path. Then, in
src/SFML/Window/Window.cpp, add the header
#include <SFML/Window/Win32/WglContext.hpp> just below the other headers in the beginning of the file.
Go to
SFML/Window/Win32/WglContext.hpp and make this function public:
// ...
public:
void createSurface(HWND window, unsigned int bitsPerPixel);
private:
// ...
Then, simply change the beginning of this function:
////////////////////////////////////////////////////////////
void Window::create(VideoMode mode, const String& title, Uint32 style, const ContextSettings& settings)
{
if (m_context)
{
delete m_impl;
m_impl = priv::WindowImpl::create(mode, title, style, settings);
auto ptr = (sf::priv::WglContext*)m_context;
ptr->createSurface(m_impl->getSystemHandle(), mode.bitsPerPixel);
wglMakeCurrent(ptr->m_deviceContext, ptr->m_context);
initialize();
return;
}
// ...
I also noticed that a point for improvement for SFML is remembering the previous state of mouse grabbed, visibility, etc.
Obviously, this is a workaround, and not very pretty. Note that this adaption skips some tests done later in the function, like validating the window's style.
I expect SFML to support this natively in the future, where each platform-specific implementation does its thing.
Cya.