运行最基本的SFML应用程序时的性能



我目前正在研究SFML项目,我过去曾做过一些。但是我现在遇到一个大问题。我有严重的性能问题。我用一个简单的主功能替换了我的所有代码,您可以在SFML网站上找到,但是该应用程序落后了很难,以至于永远需要再次关闭它。

我试图清洁解决方案,但这无济于事。通过查看任务管理器,我找不到任何问题。cpu-,gpu-,disk-,内存 - 似乎很好。运行我的一些较旧问题可以正常工作。没有任何laggs。

我添加了包含目录到"附加目录",我已经将库添加到"其他图书馆目录"中,我已经链接到我的其他依赖项(例如sfml-audio-d.lib(,我在调试/发行文件夹中粘贴了必要的DLL。

#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运行。我总是建议在执行图形时考虑时间步骤。时间步骤是不同帧之间的时间。有几种处理此问题的方法。修复您的时间段网页可以完美汇总它们。这是一种参考。

我进行了快速的代码改编,以为您提供一些指导。代码适用于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;
}

最新更新