Unfortunately, solving the CPU hogging with an artificial frame rate limitation would hurt me when i actually need the full frame rate (e.g. when dragging stuff around or in widget change animations).
I'm not sure I see the problem. When you don't want the limit, don't set the limit. It's not like you need to make a choice now and it's set in stone for every application you generate from here on out (or even within a single application.) Also if you don't want to update/render when there are no events processed, don't.
while ( window.isOpen() )
{
bool eventsProcessed = false ;
sf::Event event ;
while ( window.pollEvent(event) )
{
eventsProcessed = true ;
// process events
}
if ( eventsProcessed )
{
window.clear() ;
// stuff ...
window.display() ;
}
else
{
// ...
}
}
i tried to do it like in your code suggestion. The problem is that it doesn't process expose events.
You could keep track of the time since the last time you drew the window and update/redraw periodically if the time is excessive. Or you could implement an OS-specific solution.
const sf::Time maxTimeNotDisplayed = sf::seconds(1.0/20.0f) ;
window.display() ;
sf::Clock lastDisplay ;
while ( window.isOpen() )
{
bool eventsProcessed = false ;
sf::Event event ;
while ( window.pollEvent(event) )
{
eventsProcessed = true ;
// process events
}
if ( eventsProcessed || lastDisplay.getElapsedTime() > maxTimeNotDisplayed )
{
window.clear() ;
// stuff ...
window.display() ;
lastDisplay.restart() ;
}
else
{
// ...
}
}