Factor out the framerate limiter into a class

Patch from @x-qq
This commit is contained in:
2020-02-01 21:03:56 -05:00
parent 3e686cb908
commit f9f4d5671c
10 changed files with 62 additions and 21 deletions

View File

@@ -21,6 +21,7 @@
#include "cursors.hpp"
#include "res_image.hpp"
#include "prefs.hpp"
#include "framerate_limiter.hpp"
cUniverse univ;
@@ -163,8 +164,7 @@ void init_main_window (sf::RenderWindow& mainPtr, sf::View& mainView) {
void handle_events() {
sf::Event currentEvent;
sf::Clock framerate_clock;
const sf::Int64 desired_microseconds_per_frame { 1000000 / 60 }; // us / FPS
cFramerateLimiter fps_limiter;
while(!All_Done) {
while(mainPtr.pollEvent(currentEvent)) handle_one_event(currentEvent);
@@ -172,9 +172,7 @@ void handle_events() {
redraw_everything();
// Prevent the loop from executing too fast.
const sf::Int64 remaining_time_budget = desired_microseconds_per_frame - framerate_clock.getElapsedTime().asMicroseconds();
if(remaining_time_budget > 0) sf::sleep(sf::microseconds(remaining_time_budget));
framerate_clock.restart();
fps_limiter.frame_finished();
}
}