SFML community forums

Help => Graphics => Topic started by: Kaphonaits on November 24, 2014, 07:55:44 am

Title: Preserve aspect ratio / letterboxing?
Post by: Kaphonaits on November 24, 2014, 07:55:44 am
I'm looking for something simliar to the functionality of this: http://wiki.libsdl.org/SDL_RenderSetLogicalSize

When the window is resized, all of the sprites should preserve their aspect ratio and "cut off edges", resulting in a letterbox effect to do so.

How would I do this?
Title: Re: Preserve aspect ratio / letterboxing?
Post by: Ixrec on November 24, 2014, 08:42:40 am
You would do this with a view.  The tutorial (http://sfml-dev.org/tutorials/2.1/graphics-view.php) doesn't state exactly how to achieve this particular effect, but it should be very simple once you understand how views work in general.
Title: Re: Preserve aspect ratio / letterboxing?
Post by: Kaphonaits on November 24, 2014, 11:40:34 pm
I am confused and would appreciate additional assistance.
Title: Re: Preserve aspect ratio / letterboxing?
Post by: Nexus on November 24, 2014, 11:47:49 pm
Please be more concrete about where you have problems.

Have you read the view tutorial and fully understood it?
Title: Re: Preserve aspect ratio / letterboxing?
Post by: Kaphonaits on November 25, 2014, 03:11:19 am
I am not quite sure.

Like I said, my main objective is to have all of my sprites maintain their aspect ratio when being resized as opposed to stretching to conform to the bounds of the window. If the aforementioned sprites do maintain their aspect ratio, this would result in a letterbox effect. For example, a sprite drawn at the size of 960x540 in a window of the same size, upon the window being resized to 1024x768, would result in black bars along the bottom and top of the window.

I do not quite understand how to accomplish this with views.
Title: Re: Preserve aspect ratio / letterboxing?
Post by: Hapax on November 25, 2014, 05:04:06 am
Pay some attention to this part (http://sfml-dev.org/tutorials/2.1/graphics-view.php#showing-more-when-the-window-is-resized). That should sort out your aspect ratio. It's simply a matter of scaling the sizes after that.
The clipping can be done by drawing black bars over it (as the simplest solution), or using a viewport (a part of the view) based on the ratio of the window/resized view and the intended window/view size.
Title: Re: Preserve aspect ratio / letterboxing?
Post by: AFS on November 26, 2014, 01:18:50 am
Pay attention to this part (http://sfml-dev.org/tutorials/2.1/graphics-view.php#showing-more-when-the-window-is-resized) (as Hapax said) and also on this one (http://sfml-dev.org/tutorials/2.1/graphics-view.php#defining-how-the-view-is-viewed). The solution is a combination of the examples shown there, and it's not as hard as it seems ;)
Title: Re: Preserve aspect ratio / letterboxing?
Post by: borderliner on December 27, 2018, 09:13:18 am
I'm looking for something simliar to the functionality of this: http://wiki.libsdl.org/SDL_RenderSetLogicalSize

When the window is resized, all of the sprites should preserve their aspect ratio and "cut off edges", resulting in a letterbox effect to do so.

How would I do this?

Dead topic, but for future users, here's the code I've written for my game to keep the aspect ratio even when you resize the window. I save the aspect ratio of the original window as soon as I create it.

if (m_event.type == sf::Event::Resized) {
    m_window_width = m_event.size.width;
    m_window_height = m_event.size.height;
    float new_width = m_initial_aspect_ratio * m_window_height;
    float new_height = m_window_width / m_initial_aspect_ratio;
    float offset_width = (m_window_width - new_width) / 2.0;
    float offset_height = (m_window_height - new_height) / 2.0;
    sf::View view = m_window->getDefaultView();
    if (m_window_width >= m_initial_aspect_ratio * m_window_height) {
        view.setViewport(sf::FloatRect(offset_width / m_window_width, 0.0, new_width / m_window_width, 1.0));
    } else {
        view.setViewport(sf::FloatRect(0.0, offset_height / m_window_height, 1.0, new_height / m_window_height));
    }

    m_window->setView(view);
}
 
Title: Re: Preserve aspect ratio / letterboxing?
Post by: Hapax on December 30, 2018, 11:07:16 am
There has also, since, been a post on the Wiki that shows an approach:
https://github.com/SFML/SFML/wiki/Source%3A-Letterbox-effect-using-a-view