SFML Window Resizing is very ugly

时光总嘲笑我的痴心妄想 提交于 2021-02-11 15:53:32

问题


When I resize my sfml window, when I cut resize to make it smaller and resize to make it larger, it gives you a really weird effect.

How do I make the resizing more prettier? The code is from the installation tutorial for code::blocks. Code (same as the code in the installation tutorial for code::blocks on the sfml website):

#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;
}

回答1:


You need to manage the resize of the window. Otherwise the coordinates are wrong. Here is an excerpt of your code with the solution. Credits go to the author of this forum post, this is where I once found it when I was looking for a solution: https://en.sfml-dev.org/forums/index.php?topic=17747.0

Additionally you can set the new coordinates based on the new size. The link gives you more information.

// 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
        }
    }


来源:https://stackoverflow.com/questions/61447069/sfml-window-resizing-is-very-ugly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!