SFML community forums

Help => Window => Topic started by: Glocke on March 20, 2015, 09:04:19 am

Title: [SOLVED] Proper way to handle Resize
Post by: Glocke on March 20, 2015, 09:04:19 am
Hi, I'm working with window resize events in order to realign the user interface depending on the window's size. Therefore I discovered RenderWindow::mapPixelToCoords() in order to align the position correctly. But unfortunately the dimension of the interface components (e.g. a CircleShape) is stretched.

So I started handling my "own default view" by resizing it, so the shape isn't streched anymore. But I'm not sure whether this is the proper way to handle window resize or not. Any suggestions? Here's a minimal example code:

#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>

int main() {
        sf::RenderWindow window{{640, 480}, "Resize Test"};
        sf::CircleShape shape{50.f};
        shape.setOrigin({50.f, 50.f});
       
        // create own view
        sf::View view = window.getDefaultView();
       
        while (window.isOpen()) {
                sf::Event event;
                while (window.pollEvent(event)) {
                        if (event.type == sf::Event::Closed) {
                                window.close();
                        } else if (event.type == sf::Event::Resized) {
                                // resize my view
                                view.setSize({
                                        static_cast<float>(event.size.width),
                                        static_cast<float>(event.size.height)
                                });
                                window.setView(view);
                                // and align shape
                                shape.setPosition(window.mapPixelToCoords(sf::Vector2i{window.getSize() / 2u}));
                        }
                }
               
                window.clear();
                window.draw(shape);
                window.display();
        }
}
 

Kind regards
Glocke
Title: Re: Proper way to handle Resize
Post by: zsbzsb on March 20, 2015, 12:02:38 pm
That works too just fine.

http://www.sfml-dev.org/tutorials/2.2/graphics-view.php#showing-more-when-the-window-is-resized
Title: Re: Proper way to handle Resize
Post by: Glocke on March 20, 2015, 12:50:07 pm
http://www.sfml-dev.org/tutorials/2.2/graphics-view.php#showing-more-when-the-window-is-resized
Ok thx. But I'm switching between two views: hud/gui and my game scene. Each time a resize event is received, I notify the game state to resize the view, which is used for drawing the game scene. So I'd need to handle my hud/gui view as well (resize it) in order to be able to reapply it when drawing ui elements.

So I'd need to store either my hud/gui view or the visible area of this view (which doesn't make a real difference). I assume this is the correct and "official" way, because it's implied by the tutorial link.

Again, thanks for the link ... :)
Title: Re: Proper way to handle Resize
Post by: zsbzsb on March 20, 2015, 01:16:20 pm
There is no "correct" way, just as long as you don't do it a "wrong" way.  ;)

The tutorials just show one way of doing it.
Title: Re: Proper way to handle Resize
Post by: Glocke on March 20, 2015, 02:44:32 pm
There is no "correct" way, just as long as you don't do it a "wrong" way.  ;)
Well, but there's an intended way from the point of the dev team :) (Seems to be the way to tutorial shows).

Which alternatives are there?
Title: Re: Proper way to handle Resize
Post by: BlueCobold on March 21, 2015, 08:13:37 pm
An alternative would be to ignore the events altogether and calculate your view and layout positions based on the current size of the used RenderWindow each frame - cached if you want, so you only do it if required. That way you don't need to pass down events. You have to pass down the RenderTarget anyway though.