下面的代码是针对一个空窗口的,但在我的 Intel i3 上显示了 25% 的相对较高的 CPU 使用率。我还尝试了 setFramerateLimit
没有任何变化。有没有办法降低 CPU 使用率?
#include<SFML/Window.hpp>
void processEvents(sf::Window& window);
int main()
{
sf::Window window(sf::VideoMode(800, 600), "My Window", sf::Style::Close);
window.setVerticalSyncEnabled(true);
while (window.isOpen())
{
processEvents(window);
}
return 0;
}
void processEvents(sf::Window& window)
{
sf::Event event;
window.pollEvent(event);
switch (event.type)
{
case sf::Event::Closed:
window.close();
break;
}
}
最佳答案
由于您没有在循环中调用 window.display()
,因此请注意将线程暂停适当的时间,使用 sf::RenderWindow::setVerticalSyncEnabled 进行设置
或 sf::RenderWindow::setMaxFramerateLimit
。
试试这个:
while (window.isOpen())
{
processEvents(window);
// this makes the thread sleep
// (for ~16.7ms minus the time already spent since
// the previous window.display() if synced with 60FPS)
window.display();
}
来自 SFML Docs :
If a limit is set, the window will use a small delay after each call to
display()
to ensure that the current frame lasted long enough to match the framerate limit.
关于c++ - 简单程序的高CPU使用率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34314892/