【问题标题】:Performance-issue when running the most basic sfml application运行最基本的 sfml 应用程序时的性能问题
【发布时间】:2019-05-04 11:59:55
【问题描述】:

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

我已尝试清理解决方案,但无济于事。看任务管理器也没发现什么问题。 CPU-, GPU-, DISK-, MEMORY-使用似乎还不错。 运行我的一些旧问题可以正常工作。没有任何滞后。

我已将包含目录添加到“其他包含目录”中, 我已将库添加到“其他库目录”, 我已经链接到我的附加依赖项(例如 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;
}

【问题讨论】:

    标签: c++ sfml visual-studio-2019


    【解决方案1】:

    根据提供的信息,很难说这是从哪里来的。由于您的代码中没有时间步长,因此它可能以最大 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;
    }
    

    【讨论】:

    • 感谢您的回答!有用!我之前放弃了那种解决方案,因为它在我的其他项目中也没有。有谁知道为什么?默认情况下是否有任何帧限制我可能错误地停用了?无论如何,非常感谢!
    • 我的荣幸!您的代码中缺少 setFramerateLimit。你可以试一试,SFML 将通过在绘图调用后应用睡眠来尝试匹配帧速率限制。我总是同时使用时间步长和 setFramerateLimit。
    猜你喜欢
    • 1970-01-01
    • 2011-06-06
    • 2012-06-05
    • 2020-01-28
    • 2021-06-21
    • 2012-06-18
    • 1970-01-01
    • 1970-01-01
    • 2014-06-26
    相关资源
    最近更新 更多