SFML 窗口大小调整非常难看

Cra*_*mer 1 c++ sfml

当我调整 sfml 窗口的大小时,当我剪切调整大小以使其更小并调整大小以使其更大时,它会给您带来非常奇怪的效果。 剪出一个绿色圆圈 一束“激光束”从绿色圆圈中射出

如何使调整大小更漂亮?该代码来自 code::blocks 的安装教程。代码(与sfml网站上code::blocks安装教程中的代码相同):

#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
    sf::CircleShape shape(100.f);
    shape.setFillColor(sf::Color::Green);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();
        window.draw(shape);
        window.display();
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*and 5

您需要管理窗口的大小调整。否则坐标是错误的。这是您的代码和解决方案的摘录。感谢此论坛帖子的作者,这是我在寻找解决方案时找到它的地方:https://en.sfml-dev.org/forums/index.php?topic =17747.0

此外,您可以根据新尺寸设置新坐标。该链接为您提供了更多信息。

// create own view
sf::View view = window.getDefaultView();

while (window.isOpen())
{
    sf::Event event;
    while (window.pollEvent(event))
    {
        if (event.type == sf::Event::Closed)
            window.close();

        if (event.type == sf::Event::Resized) {
            // resize my view
            view.setSize({
                    static_cast<float>(event.size.width),
                    static_cast<float>(event.size.height)
            });
            window.setView(view);
            // and align shape
        }
    }
Run Code Online (Sandbox Code Playgroud)