Factor out the framerate limiter into a class
Patch from @x-qq
This commit is contained in:
@@ -26,6 +26,7 @@
|
||||
#include "scen.menus.hpp"
|
||||
#include "res_image.hpp"
|
||||
#include "prefs.hpp"
|
||||
#include "framerate_limiter.hpp"
|
||||
|
||||
/* Globals */
|
||||
bool All_Done = false;
|
||||
@@ -207,8 +208,7 @@ void init_scened(int argc, char* argv[]) {
|
||||
|
||||
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);
|
||||
@@ -219,9 +219,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();
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user