From 0997541fc72252637b99237a401466d6bd06a9f4 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Wed, 6 Nov 2019 22:43:46 +0100 Subject: [PATCH] Simplify FPS management --- xSDL/xApplication.cpp | 12 ++++-------- xSDL/xApplication.h | 5 ++--- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/xSDL/xApplication.cpp b/xSDL/xApplication.cpp index 1d96a6b..9e9d862 100644 --- a/xSDL/xApplication.cpp +++ b/xSDL/xApplication.cpp @@ -311,7 +311,7 @@ void xApplication::render(){ /** Sets FPS */ void xApplication::setFps(const uint32_t fps){ - _fps = fps; + _fpstime = 1000.0/fps; } /** schedules the main loop */ @@ -322,19 +322,15 @@ void xApplication::schedule(){ this->_controller.handleEvent(&event); // wait to satisfy FPS - this->syncFps(_fps); + this->syncFps(); // render this->render(); } -/** synchronizes according to @fps +/** synchronizes according to FPS * to be used at the end of the UI loop */ -void xApplication::syncFps(const int fps){ - // set fps - if( fps != 0 ) - _fpstime = 1000/fps; - +void xApplication::syncFps(){ if( _lasttick == 0 ) _lasttick = SDL_GetTicks()-_fpstime; diff --git a/xSDL/xApplication.h b/xSDL/xApplication.h index 942a5c9..beaf794 100644 --- a/xSDL/xApplication.h +++ b/xSDL/xApplication.h @@ -50,7 +50,7 @@ void render(); // manage main loop and thread loops - void setFps(const uint32_t fps); + void setFps(const uint32_t fps=0); void schedule(); xAppState state; @@ -59,10 +59,9 @@ xApplication(const char *t, int w, int h); static xApplication* _instance; - void syncFps(const int fps=0); + void syncFps(); // fps management - uint32_t _fps = 60; uint32_t _lasttick; uint32_t _fpstime;