New FPS system, editable in config
This commit is contained in:
parent
3dda6a690a
commit
af8476c4ba
@ -1,3 +1,7 @@
|
|||||||
|
# General configuration
|
||||||
|
general:
|
||||||
|
maxFPS: 50
|
||||||
|
|
||||||
# Players config
|
# Players config
|
||||||
players:
|
players:
|
||||||
width: 250
|
width: 250
|
||||||
|
@ -10,6 +10,9 @@ typedef string configKey;
|
|||||||
|
|
||||||
struct ConfigData {
|
struct ConfigData {
|
||||||
|
|
||||||
|
unsigned maxFPS;
|
||||||
|
|
||||||
|
|
||||||
InvadersGrid grid;
|
InvadersGrid grid;
|
||||||
|
|
||||||
unsigned startXPosition;
|
unsigned startXPosition;
|
||||||
|
@ -160,6 +160,8 @@ void ConfigBuilder::readInvaderType(const configKey& baseKey, InvaderTypeDef& in
|
|||||||
|
|
||||||
void ConfigBuilder::readConfig() {
|
void ConfigBuilder::readConfig() {
|
||||||
|
|
||||||
|
collectedData.maxFPS = getInt("general.maxFPS");
|
||||||
|
|
||||||
readGrid("grid");
|
readGrid("grid");
|
||||||
|
|
||||||
// players
|
// players
|
||||||
|
@ -84,14 +84,19 @@ WinValue Game::playGame(){ // returns when game is finished
|
|||||||
basePos = Position(200, 0);
|
basePos = Position(200, 0);
|
||||||
// GAMELOOP
|
// GAMELOOP
|
||||||
|
|
||||||
const int MAX_FPS = 60;
|
// computed in advance for performance reasons
|
||||||
|
chrono::milliseconds maxFrameTime = chrono::milliseconds(1000/confData.maxFPS);
|
||||||
|
|
||||||
chrono::milliseconds fpsTime;
|
|
||||||
unsigned tmpFps = 0;
|
unsigned tmpFps = 0;
|
||||||
unsigned fps = 0;
|
unsigned fps = 0;
|
||||||
|
typedef chrono::high_resolution_clock::time_point MyTimePoint;
|
||||||
|
MyTimePoint fpsStartTime = {};
|
||||||
while(window.isOpen()){
|
while(window.isOpen()){
|
||||||
|
|
||||||
chrono::time_point startTime = chrono::high_resolution_clock::now();
|
MyTimePoint startTime = chrono::high_resolution_clock::now();
|
||||||
|
if(fpsStartTime.time_since_epoch()==chrono::seconds(0)){
|
||||||
|
fpsStartTime = startTime;
|
||||||
|
}
|
||||||
|
|
||||||
pm.startFrame();
|
pm.startFrame();
|
||||||
|
|
||||||
@ -118,19 +123,16 @@ WinValue Game::playGame(){ // returns when game is finished
|
|||||||
|
|
||||||
pm.endFrame();
|
pm.endFrame();
|
||||||
|
|
||||||
chrono::time_point endTime = chrono::high_resolution_clock::now();
|
MyTimePoint endTime = chrono::high_resolution_clock::now();
|
||||||
chrono::milliseconds frameTime = chrono::duration_cast<chrono::milliseconds>(endTime-startTime);
|
|
||||||
fpsTime = fpsTime + frameTime;
|
// This code is counted as part of frames, but that's not really something we can control
|
||||||
if(fpsTime>chrono::seconds(1)){
|
if(fpsStartTime+chrono::seconds(1) < endTime){
|
||||||
fps = tmpFps;
|
fps = tmpFps;
|
||||||
tmpFps = 0;
|
tmpFps = 0;
|
||||||
fpsTime = chrono::milliseconds(0);
|
fpsStartTime = {};
|
||||||
}else ++tmpFps;
|
}else ++tmpFps;
|
||||||
|
|
||||||
// old implementation, too volatile
|
this_thread::sleep_until(startTime+maxFrameTime);
|
||||||
// cout << "FPS : " << chrono::milliseconds(1000)/frameTime << endl;
|
|
||||||
|
|
||||||
this_thread::sleep_until(startTime+chrono::duration<double, ratio<1, MAX_FPS>>(1));
|
|
||||||
}
|
}
|
||||||
return WinValue::NOBODY;
|
return WinValue::NOBODY;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user