1
Graphics / Re: Preserve aspect ratio / letterboxing?
« 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);
}
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);
}