mirror of
https://github.com/mkxp-z/mkxp-z.git
synced 2025-09-10 12:02:53 +02:00
Make FPS counter a little more accurate
This commit is contained in:
parent
aa9bf22034
commit
1683483170
3 changed files with 19 additions and 50 deletions
|
@ -44,6 +44,7 @@
|
|||
#include <SDL_image.h>
|
||||
#include <SDL_timer.h>
|
||||
#include <SDL_video.h>
|
||||
#include <SDL_mutex.h>
|
||||
|
||||
#ifdef MKXPZ_STEAM
|
||||
#include "steamshim_child.h"
|
||||
|
@ -435,6 +436,7 @@ struct GraphicsPrivate {
|
|||
Quad screenQuad;
|
||||
|
||||
std::vector<unsigned long long> avgFPSData;
|
||||
SDL_mutex *avgFPSLock;
|
||||
|
||||
/* Global list of all live Disposables
|
||||
* (disposed on reset) */
|
||||
|
@ -448,6 +450,7 @@ struct GraphicsPrivate {
|
|||
frameCount(0), brightness(255), fpsLimiter(frameRate),
|
||||
useFrameSkip(rtData->config.frameSkip), frozen(false), last_update() {
|
||||
avgFPSData = std::vector<unsigned long long>();
|
||||
avgFPSLock = SDL_CreateMutex();
|
||||
|
||||
recalculateScreenSize(rtData);
|
||||
updateScreenResoRatio(rtData);
|
||||
|
@ -462,7 +465,11 @@ struct GraphicsPrivate {
|
|||
fpsLimiter.resetFrameAdjust();
|
||||
}
|
||||
|
||||
~GraphicsPrivate() { TEXFBO::fini(frozenScene); }
|
||||
~GraphicsPrivate() {
|
||||
TEXFBO::fini(frozenScene);
|
||||
SDL_DestroyMutex(avgFPSLock);
|
||||
|
||||
}
|
||||
|
||||
void updateScreenResoRatio(RGSSThreadData *rtData) {
|
||||
Vec2 &ratio = rtData->sizeResoRatio;
|
||||
|
@ -555,12 +562,13 @@ struct GraphicsPrivate {
|
|||
|
||||
swapGLBuffer();
|
||||
|
||||
|
||||
if (avgFPSData.size() > 30)
|
||||
SDL_LockMutex(avgFPSLock);
|
||||
if (avgFPSData.size() > 40)
|
||||
avgFPSData.erase(avgFPSData.begin());
|
||||
|
||||
unsigned long long time = shState->runTime();
|
||||
avgFPSData.push_back(time - last_update);
|
||||
SDL_UnlockMutex(avgFPSLock);
|
||||
|
||||
last_update = time;
|
||||
}
|
||||
|
@ -581,10 +589,13 @@ struct GraphicsPrivate {
|
|||
|
||||
double averageFPS() {
|
||||
double ret;
|
||||
SDL_LockMutex(avgFPSLock);
|
||||
for (unsigned long long times : avgFPSData)
|
||||
ret += times;
|
||||
|
||||
return 1 / (ret / avgFPSData.size() / 1000 / 1000);
|
||||
ret = 1 / (ret / avgFPSData.size() / 1000000);
|
||||
SDL_UnlockMutex(avgFPSLock);
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -615,10 +626,6 @@ unsigned long long Graphics::getDelta() {
|
|||
|
||||
void Graphics::update() {
|
||||
p->checkShutDownReset();
|
||||
auto test = p->averageFPS();
|
||||
if (test > 30) {
|
||||
int a = 1;
|
||||
}
|
||||
p->checkSyncLock();
|
||||
|
||||
#ifdef MKXPZ_STEAM
|
||||
|
|
|
@ -32,6 +32,7 @@
|
|||
#include <al.h>
|
||||
#include <alc.h>
|
||||
#include <alext.h>
|
||||
#include <cmath>
|
||||
|
||||
#include "sharedstate.h"
|
||||
#include "graphics.h"
|
||||
|
@ -137,11 +138,6 @@ void EventThread::process(RGSSThreadData &rtData)
|
|||
fullscreen = rtData.config.fullscreen;
|
||||
int toggleFSMod = rtData.config.anyAltToggleFS ? KMOD_ALT : KMOD_LALT;
|
||||
|
||||
fps.lastFrame = SDL_GetPerformanceCounter();
|
||||
fps.displayCounter = 0;
|
||||
fps.acc = 0;
|
||||
fps.accDiv = 0;
|
||||
|
||||
if (rtData.config.printFPS)
|
||||
fps.sendUpdates.set();
|
||||
|
||||
|
@ -314,8 +310,8 @@ void EventThread::process(RGSSThreadData &rtData)
|
|||
{
|
||||
if (!displayingFPS)
|
||||
{
|
||||
fps.immInitFlag.set();
|
||||
fps.sendUpdates.set();
|
||||
|
||||
fps.sendUpdates.set();
|
||||
displayingFPS = true;
|
||||
}
|
||||
else
|
||||
|
@ -751,36 +747,8 @@ void EventThread::notifyFrame()
|
|||
if (!fps.sendUpdates)
|
||||
return;
|
||||
|
||||
uint64_t current = SDL_GetPerformanceCounter();
|
||||
uint64_t diff = current - fps.lastFrame;
|
||||
fps.lastFrame = current;
|
||||
|
||||
if (fps.immInitFlag)
|
||||
{
|
||||
fps.immInitFlag.clear();
|
||||
fps.immFiniFlag.set();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
static uint64_t freq = SDL_GetPerformanceFrequency();
|
||||
|
||||
double currFPS = (double) freq / diff;
|
||||
fps.acc += currFPS;
|
||||
++fps.accDiv;
|
||||
|
||||
fps.displayCounter += diff;
|
||||
if (fps.displayCounter < freq && !fps.immFiniFlag)
|
||||
return;
|
||||
|
||||
fps.displayCounter = 0;
|
||||
fps.immFiniFlag.clear();
|
||||
|
||||
int32_t avgFPS = fps.acc / fps.accDiv;
|
||||
fps.acc = fps.accDiv = 0;
|
||||
|
||||
SDL_Event event;
|
||||
event.user.code = avgFPS;
|
||||
event.user.code = round(shState->graphics().averageFrameRate());
|
||||
event.user.type = usrIdStart + UPDATE_FPS;
|
||||
SDL_PushEvent(&event);
|
||||
}
|
||||
|
|
|
@ -131,13 +131,7 @@ private:
|
|||
|
||||
struct
|
||||
{
|
||||
uint64_t lastFrame;
|
||||
uint64_t displayCounter;
|
||||
AtomicFlag sendUpdates;
|
||||
AtomicFlag immInitFlag;
|
||||
AtomicFlag immFiniFlag;
|
||||
double acc;
|
||||
uint32_t accDiv;
|
||||
} fps;
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue