gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 04:18:43 24 4
gpt4 key购买 nike

我目前正在从事一个 SFML 项目,我过去做过一些项目。但是我现在遇到了一个大问题。我有严重的性能问题。我用一个简单的主函数替换了我的所有代码,您可以在 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;
}

最佳答案

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

关于c++ - 运行最基本的 sfml 应用程序时的性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55982288/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com