1
votes

I'm trying to make a simple application using c++ and SFML. I would like for the application to work at any resolution - windowed or fullscreen.

The closest thing to a solution I've found in documentation is this simple snippet of code.

 if (event.type == sf::Event::Resized)
{
    // update the view to the new size of the window
    sf::FloatRect visibleArea(0, 0, event.size.width, event.size.height);
    window.setView(sf::View(visibleArea));
}

This creates an effect that looks like this http://i.imgur.com/RvCOuKi.png.

This is a start - but it's not exactly what I'm looking for. When I resize the window, the "original" viewing space is in the top left, with the newly visible objects all below or to the right of the original rectangle.

I think this method of resizing is ugly. When I put something at the center of the screen at one resolution, it should stay in the center even at another resolution. How do I center the view in such a way that this occurs?

A great example of this is minecraft. In minecraft, when you resize the window, the "original" viewport is in the very center and newly visible objects appear around the edges. How can I recreate this in SFML?

1

1 Answers

1
votes

Just use another constructor and pass the center of the current view:

if (event.type == sf::Event::Resized)
{
    // update the view to the new size of the window and keep the center
    window.setView(sf::View(window.getView().getCenter(), sf::Vector2f((float)event.size.width, (float)event.size.height)));
}