row*_*man 3 c++ cpu-usage sfml
下面的代码用于空窗口,但在我的Intel i3上显示相对较高的CPU使用率25%.我也试过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;
}
}
Run Code Online (Sandbox Code Playgroud)
因为你没有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();
}
Run Code Online (Sandbox Code Playgroud)
来自SFML文档:
如果设置了限制,则每次调用后窗口将使用一小段延迟,
display()以确保当前帧持续足够长的时间以匹配帧速率限制.