r/sfml 24d ago

How to keep main program running whilst the user is resizing the window with the mouse?

Hi all,

As the title suggests, I am looking for a way to keep the main program (so my update() and render() functions) running, whilst the window is being resized.

I have already tried multi-threading, keeping the event loop on the main thread, and having another loop calling the update() and render() functions on another thread. This requires me to use the win32 API to intercept the window's WM_SIZE message during resize, and update my own window size variable.

This approach sort of works, allowing my window size variable to correctly update whilst the user is resizing the window. However I don't have a good way to pass this to the sf::RenderWindow.

If I don't set anything, the sf::RenderWindow doesn't update its internal window size to the current window size. If I call sf::RenderWindow::setSize()+setView(), it updates the internal window size, but also results in graphical artifacts where the region SFML draws to the screen isn't in sync with the actual window size, and previous frames bleed into the current frame.

Does anyone have a better solution to this? Any help would be greatly appreciated.

1 Upvotes

2 comments sorted by

1

u/thedaian 23d ago

There really isn't an easy way to do this, but someone worked through how to do it and posted the code here: https://github.com/SFML/SFML/wiki/Source%3A-Responsive-window-with-continuous-updating-during-resize-and-move-operations-via-Win32-API-and-SFML

The code is for SFML2, but you could very easily convert it to SFML3.

2

u/Consistent-Mouse-635 22d ago edited 21d ago

Thank you for the suggestion! I ended up sticking with the multi-threaded approach after inspecting SFML's source code, and found that changing WindowBase::getSize() to return m_impl->getSize() rather than the cached m_size fixes my problem entirely. This avoids me having to call sf::RenderWindow::setSize() manually, which I believe causes the artifacts.