Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Preserve aspect ratio / letterboxing?  (Read 8916 times)

0 Members and 1 Guest are viewing this topic.

Kaphonaits

  • Newbie
  • *
  • Posts: 21
    • View Profile
Preserve aspect ratio / letterboxing?
« 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?
The keyboard is mightier than the sword.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Preserve aspect ratio / letterboxing?
« Reply #1 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.

Kaphonaits

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Preserve aspect ratio / letterboxing?
« Reply #2 on: November 24, 2014, 11:40:34 pm »
I am confused and would appreciate additional assistance.
The keyboard is mightier than the sword.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Preserve aspect ratio / letterboxing?
« Reply #3 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?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Kaphonaits

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Preserve aspect ratio / letterboxing?
« Reply #4 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.
The keyboard is mightier than the sword.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Preserve aspect ratio / letterboxing?
« Reply #5 on: November 25, 2014, 05:04:06 am »
Pay some attention to this part. 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.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

AFS

  • Full Member
  • ***
  • Posts: 115
    • View Profile
Re: Preserve aspect ratio / letterboxing?
« Reply #6 on: November 26, 2014, 01:18:50 am »
Pay attention to this part (as Hapax said) and also on this one. The solution is a combination of the examples shown there, and it's not as hard as it seems ;)
« Last Edit: November 26, 2014, 01:31:52 am by AFS »

borderliner

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Re: Preserve aspect ratio / letterboxing?
« Reply #7 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);
}
 

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Preserve aspect ratio / letterboxing?
« Reply #8 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
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything