Factor out the framerate limiter into a class
Patch from @x-qq
This commit is contained in:
@@ -253,6 +253,7 @@
|
||||
<ClCompile Include="..\..\..\src\tools\prefs.win.cpp" />
|
||||
<ClCompile Include="..\..\..\src\tools\undo.cpp" />
|
||||
<ClCompile Include="..\..\..\src\tools\winutil.win.cpp" />
|
||||
<ClCompile Include="..\..\..\src\tools\framerate_limiter.cpp" />
|
||||
<ClCompile Include="..\..\..\src\universe\creature.cpp" />
|
||||
<ClCompile Include="..\..\..\src\universe\living.cpp" />
|
||||
<ClCompile Include="..\..\..\src\universe\party.cpp" />
|
||||
@@ -337,6 +338,7 @@
|
||||
<ClInclude Include="..\..\..\src\tools\undo.hpp" />
|
||||
<ClInclude Include="..\..\..\src\tools\vector2d.hpp" />
|
||||
<ClInclude Include="..\..\..\src\tools\winutil.hpp" />
|
||||
<ClInclude Include="..\..\..\src\tools\framerate_limiter.hpp" />
|
||||
<ClInclude Include="..\..\..\src\universe\creature.hpp" />
|
||||
<ClInclude Include="..\..\..\src\universe\living.hpp" />
|
||||
<ClInclude Include="..\..\..\src\universe\party.hpp" />
|
||||
|
@@ -623,6 +623,9 @@
|
||||
<ClCompile Include="..\..\..\src\tools\cursors.win.cpp">
|
||||
<Filter>Tools</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\..\src\tools\framerate_limiter.cpp">
|
||||
<Filter>Tools</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\..\src\scenario\vehicle.cpp">
|
||||
<Filter>Scenario</Filter>
|
||||
</ClCompile>
|
||||
@@ -804,6 +807,9 @@
|
||||
<ClInclude Include="..\..\..\src\tools\winutil.hpp">
|
||||
<Filter>Tools</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\..\src\tools\framerate_limiter.hpp">
|
||||
<Filter>Tools</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\..\src\tools\vector2d.hpp">
|
||||
<Filter>Tools</Filter>
|
||||
</ClInclude>
|
||||
|
@@ -284,6 +284,8 @@
|
||||
91EF27791B693D5F00666469 /* item_write.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 91EF27781B693D5F00666469 /* item_write.cpp */; };
|
||||
91EF277B1B693D6E00666469 /* monst_read.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 91EF277A1B693D6E00666469 /* monst_read.cpp */; };
|
||||
91EF277D1B693D7D00666469 /* monst_write.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 91EF277C1B693D7D00666469 /* monst_write.cpp */; };
|
||||
91F3205023E65EA3009650AF /* framerate_limiter.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 91F3204E23E65EA3009650AF /* framerate_limiter.cpp */; };
|
||||
91F3205123E65EA3009650AF /* framerate_limiter.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 91F3204F23E65EA3009650AF /* framerate_limiter.hpp */; };
|
||||
91F6F8E318F87F3700E3EA15 /* sfml-audio.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 91F6F8DD18F87F3700E3EA15 /* sfml-audio.framework */; };
|
||||
91F6F8E418F87F3700E3EA15 /* sfml-audio.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 91F6F8DD18F87F3700E3EA15 /* sfml-audio.framework */; };
|
||||
91F6F8E518F87F3700E3EA15 /* sfml-audio.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 91F6F8DD18F87F3700E3EA15 /* sfml-audio.framework */; };
|
||||
@@ -805,6 +807,8 @@
|
||||
91EF277A1B693D6E00666469 /* monst_read.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = monst_read.cpp; sourceTree = "<group>"; };
|
||||
91EF277C1B693D7D00666469 /* monst_write.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = monst_write.cpp; sourceTree = "<group>"; };
|
||||
91F06E8F1A2EBEE70038E902 /* special_parse.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = special_parse.hpp; sourceTree = "<group>"; };
|
||||
91F3204E23E65EA3009650AF /* framerate_limiter.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = framerate_limiter.cpp; sourceTree = "<group>"; };
|
||||
91F3204F23E65EA3009650AF /* framerate_limiter.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = framerate_limiter.hpp; sourceTree = "<group>"; };
|
||||
91F6F8DD18F87F3700E3EA15 /* sfml-audio.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = "sfml-audio.framework"; path = "/Library/Frameworks/sfml-audio.framework"; sourceTree = "<absolute>"; };
|
||||
91F6F8DE18F87F3700E3EA15 /* sfml-graphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = "sfml-graphics.framework"; path = "/Library/Frameworks/sfml-graphics.framework"; sourceTree = "<absolute>"; };
|
||||
91F6F8E018F87F3700E3EA15 /* sfml-system.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = "sfml-system.framework"; path = "/Library/Frameworks/sfml-system.framework"; sourceTree = "<absolute>"; };
|
||||
@@ -1154,11 +1158,13 @@
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
91C688E70FD702B9000F6D01 /* cursors.mac.mm */,
|
||||
91F3204E23E65EA3009650AF /* framerate_limiter.cpp */,
|
||||
91EC481018FBABB100BB1E86 /* prefs.mac.mm */,
|
||||
912283C80FD0E16C00B21642 /* undo.cpp */,
|
||||
919145FF18E63B70005CF3A4 /* winutil.mac.mm */,
|
||||
91C688E60FD702B9000F6D01 /* cursors.hpp */,
|
||||
91574CC323CB97C5004766F8 /* enum_map.hpp */,
|
||||
91F3204F23E65EA3009650AF /* framerate_limiter.hpp */,
|
||||
91C2A6E21B8244F700346948 /* gitrev.hpp */,
|
||||
91EC480E18FBAA8700BB1E86 /* prefs.hpp */,
|
||||
917B573F100B956C0096C978 /* undo.hpp */,
|
||||
@@ -1536,6 +1542,7 @@
|
||||
isa = PBXHeadersBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
91F3205123E65EA3009650AF /* framerate_limiter.hpp in Headers */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
@@ -1936,6 +1943,7 @@
|
||||
91EC1F0623DDFF9D00271891 /* res_image.cpp in Sources */,
|
||||
91EC1F0723DDFF9D00271891 /* res_sound.cpp in Sources */,
|
||||
91EC1F0823DDFF9D00271891 /* res_strings.cpp in Sources */,
|
||||
91F3205023E65EA3009650AF /* framerate_limiter.cpp in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
|
@@ -22,6 +22,7 @@
|
||||
#include "mathutil.hpp"
|
||||
#include "cursors.hpp"
|
||||
#include "prefs.hpp"
|
||||
#include "framerate_limiter.hpp"
|
||||
|
||||
using namespace std;
|
||||
using namespace ticpp;
|
||||
@@ -430,9 +431,7 @@ void cDialog::run(std::function<void(cDialog&)> onopen){
|
||||
// This method is a main event event loop of the dialog.
|
||||
void cDialog::handle_events() {
|
||||
sf::Event currentEvent;
|
||||
|
||||
sf::Clock framerate_clock;
|
||||
const sf::Int64 desired_microseconds_per_frame { 1000000 / 60 }; // us / FPS
|
||||
cFramerateLimiter fps_limiter;
|
||||
|
||||
while(dialogNotToast) {
|
||||
while(win.pollEvent(currentEvent)) handle_one_event(currentEvent);
|
||||
@@ -441,9 +440,7 @@ void cDialog::handle_events() {
|
||||
draw();
|
||||
|
||||
// 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();
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -31,6 +31,7 @@
|
||||
#include "prefs.hpp"
|
||||
#include "button.hpp"
|
||||
#include "enum_map.hpp"
|
||||
#include "framerate_limiter.hpp"
|
||||
|
||||
bool All_Done = false;
|
||||
sf::RenderWindow mainPtr;
|
||||
@@ -197,8 +198,7 @@ void init_boe(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);
|
||||
@@ -224,9 +224,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();
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -3,6 +3,7 @@ Import("env platform")
|
||||
|
||||
tools = Split("""
|
||||
undo.cpp
|
||||
framerate_limiter.cpp
|
||||
../location.cpp
|
||||
../mathutil.cpp
|
||||
../porting.cpp
|
||||
|
13
src/tools/framerate_limiter.cpp
Normal file
13
src/tools/framerate_limiter.cpp
Normal file
@@ -0,0 +1,13 @@
|
||||
|
||||
#include "framerate_limiter.hpp"
|
||||
|
||||
cFramerateLimiter::cFramerateLimiter(int desired_fps)
|
||||
: desired_microseconds_per_frame { 1000000 / desired_fps } {
|
||||
|
||||
}
|
||||
|
||||
void cFramerateLimiter::frame_finished() {
|
||||
const sf::Int64 remaining_time_budget = this->desired_microseconds_per_frame - this->clock.getElapsedTime().asMicroseconds();
|
||||
if(remaining_time_budget > 0) sf::sleep(sf::microseconds(remaining_time_budget));
|
||||
this->clock.restart();
|
||||
}
|
20
src/tools/framerate_limiter.hpp
Normal file
20
src/tools/framerate_limiter.hpp
Normal file
@@ -0,0 +1,20 @@
|
||||
|
||||
#ifndef FRAMERATE_LIMITER_HPP
|
||||
#define FRAMERATE_LIMITER_HPP
|
||||
|
||||
#include <SFML/Graphics.hpp>
|
||||
|
||||
class cFramerateLimiter {
|
||||
public:
|
||||
|
||||
cFramerateLimiter(int desired_fps = 60);
|
||||
|
||||
void frame_finished();
|
||||
|
||||
private:
|
||||
|
||||
sf::Clock clock;
|
||||
const sf::Int64 desired_microseconds_per_frame;
|
||||
};
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user