Alright so I had a thread earlier about calculating FPS after window.display() is called, but my while loop doesn't always call window.display() only when there is user input. CPU usage hits a peak of 20% for the program because of the while loop. I implemented the following to limit it:
void Root::CheckElapsedTime(sf::Clock &clock) {
if (elapsedTime >= 1.0f / float(framesPerSecond)) {
std::cout << "While Loop: " << 1.0f / elapsedTime << std::endl;
elapsedTime = 0;
return;
}
sf::sleep(sf::seconds(1.0f / float(framesPerSecond) - elapsedTime));
elapsedTime += clock.restart().asSeconds();
CheckElapsedTime(clock);
}
I have the frame-limit set to 30, and FPS is just shy of 30 which is good. CPU usage drops to a peak of 3%. Is this implementation correct, and is there a better way to optimize it?