如何在 SFML 2.5 中调整 window 大小时保持 windows 纵横比?
How to keep the windows aspect ratio while resizing the window in SFML 2.5?
我想知道是否有办法在调整 window 大小时保持 windows 纵横比,就像在 photoshop 中缩放图层并按 shift 时一样。
我终于做到了我想做的事。我听 sf::Event::Resized and calling sf::Window::setSize then I adapted the code from this post 看起来像这样 :
// set screen size
float screenWidth = 800.f;
float screenHeight = 600.f;
// get the resized size
sf::Vector2u size = _window.getSize();
// setup my wanted aspect ratio
float heightRatio = screenHeight / screenWidth;
float widthRatio = screenWidth / screenHeight;
// adapt the resized window to my wanted aspect ratio
if (size.y * widthRatio <= size.x)
{
size.x = size.y * widthRatio;
}
else if (size.x * heightRatio <= size.y)
{
size.y = size.x * heightRatio;
}
// set the new size
_window.setSize(size);
我想知道是否有办法在调整 window 大小时保持 windows 纵横比,就像在 photoshop 中缩放图层并按 shift 时一样。
我终于做到了我想做的事。我听 sf::Event::Resized and calling sf::Window::setSize then I adapted the code from this post 看起来像这样 :
// set screen size
float screenWidth = 800.f;
float screenHeight = 600.f;
// get the resized size
sf::Vector2u size = _window.getSize();
// setup my wanted aspect ratio
float heightRatio = screenHeight / screenWidth;
float widthRatio = screenWidth / screenHeight;
// adapt the resized window to my wanted aspect ratio
if (size.y * widthRatio <= size.x)
{
size.x = size.y * widthRatio;
}
else if (size.x * heightRatio <= size.y)
{
size.y = size.x * heightRatio;
}
// set the new size
_window.setSize(size);