运行 最基本的 sfml 应用程序时的性能问题

Performance-issue when running the most basic sfml application

我目前正在从事一个 SFML 项目,我过去做过一些项目。但是我现在遇到了一个大问题。我有严重的性能问题。我用一个简单的 Main-function 替换了我的所有代码,您可以在 SFML 网站上找到它,但是应用程序非常滞后,需要很长时间才能再次关闭它。

我试过清理溶液,但没有用。看任务管理器也找不到问题。 CPU-、GPU-、DISK-、MEMORY-使用似乎还不错。 运行 我的一些旧问题工作正常。没有任何滞后。

我已将包含目录添加到 "additional include directories", 我已将库添加到 "additional library directories", 我已经链接到我的附加依赖项(例如 sfml-audio-d.lib), 我已将必要的 dll 粘贴到我的 Debug/Release 文件夹中。

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

根据所提供的信息,很难说这是从哪里来的。由于您的代码中没有时间步长,因此它可能以最大 FPS 运行。我总是建议在做图形时考虑时间步长。时间步长是不同帧之间的时间。有几种方法可以处理这个问题。 Fix Your Timestep 网页完美地总结了它们。这是一种参考。

我做了一个快速的代码调整来给你一些指导。代码适用于 Linux,但也适用于 Visual Studio。

#include <SFML/Graphics.hpp>
#include <iostream>

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

    window.setFramerateLimit(60);

    // Timing
    sf::Clock clock;

    while (window.isOpen())
    {
        // Update the delta time to measure movement accurately
        sf::Time dt = clock.restart();

        // Convert to seconds to do the maths
        float dtAsSeconds = dt.asSeconds();

        // For debuging, print the time to the terminal
        // It illustrates the differences
        std::cout << "Time step: " << dtAsSeconds << '\n';

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

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

    return 0;
}