【问题标题】:How to keep the windows aspect ratio while resizing the window in SFML 2.5?如何在 SFML 2.5 中调整窗口大小时保持窗口纵横比?
【发布时间】:2020-10-30 20:57:35
【问题描述】:

我想知道是否有办法在调整窗口大小的同时保持窗口的纵横比,就像在 Photoshop 中缩放图层并按 shift 时一样。

【问题讨论】:

标签: c++ sfml


【解决方案1】:

我终于设法做我想做的事。我听sf::Event::Resized 并调用sf::Window::setSize 然后我改编了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);

【讨论】:

  • 在使用鼠标调整大小时它是否“即时”工作?
  • 好的,我试过了,我可以看到它没有,即使在事件轮询之外,在一个单独的线程中不断更新。我想这是平台的限制。
猜你喜欢
  • 2021-10-05
  • 1970-01-01
  • 2013-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-23
  • 2018-06-11
  • 1970-01-01
相关资源
最近更新 更多