I use SFML with MFC. I want to keep the ball I draw at the same position/size when resizing the window. I read that I have to use setView() to set the new view according to the new CView window size. I tried it but it did not work directly like instructed on the websites. Then I investigated and got it working with this version (all related code here):
// When I create the window I initialize SFML window in CView class:
sf::RenderWindow sfmlWindow(GetSafeHwnd());
// ...
// Then when I get a WM_SIZE windows message, I do this in its OnSize() handler:
sf::Event event;
while (sfmlWindow.pollEvent(event));
sf::FloatRect visibleArea(0, 0, sfmlWindow.getSize().x, sfmlWindow.getSize().y);
sf::View newView{ visibleArea };
sfmlWindow.setView(newView);
I am happy it finally works (took me long time to figure it out
), but... is this really how it should be done (the correct way)?
I have couple of questions:
1) Why do I need to poll all the events in order the RenderWindow to update its size? Also, I was thinking that should the RenderWindow automatically do this update when I resize the window - why it does not change its m_size variable automatically when I resize? sfmlWindow.getSize() will not get updated to the new size if I do not flush the events with the while loop above it. Because I did some reseach: If I do not do that while() loop to poll all the events, the RenderWindow m_size will not get updated to the new size, and I am guessing this is why the sf::view does not get the correct values for the newView.
2) If I always need to empty the sf event queue (like I do above), what is the correct way to do it and where/in which function(s)? Does this issue also occur with other events like mouse events? So if my MFC CView gets a mouse event do I have to first flush the SFML event queue in order the SFML to be in a correct state?