This commit is contained in:
RSDuck 2020-11-02 18:48:32 +01:00
parent fbca47381b
commit ad7791f726
1 changed files with 26 additions and 34 deletions

View File

@ -355,10 +355,10 @@ void EmuThread::run()
Input::Init(); Input::Init();
u32 nframes = 0; u32 nframes = 0;
u32 starttick = SDL_GetTicks(); double perfCountsSec = 1.0 / SDL_GetPerformanceFrequency();
u32 lasttick = starttick; double lastTime = SDL_GetPerformanceCounter() * perfCountsSec;
u32 lastmeasuretick = lasttick; double frameLimitError = 0.0;
u32 fpslimitcount = 0; double lastMeasureTime = lastTime;
char melontitle[100]; char melontitle[100];
@ -492,49 +492,43 @@ void EmuThread::run()
SDL_UnlockMutex(audioSyncLock); SDL_UnlockMutex(audioSyncLock);
} }
float framerate = (1000.0f * nlines) / (60.0f * 263.0f); double frametimeStep = nlines / (60.0 * 263.0);
{ {
u32 curtick = SDL_GetTicks(); double curtime = SDL_GetPerformanceCounter() * perfCountsSec;
u32 delay = curtick - lasttick;
bool limitfps = Config::LimitFPS && !fastforward; bool limitfps = Config::LimitFPS && !fastforward;
if (limitfps) if (limitfps)
{ {
float wantedtickF = starttick + (framerate * (fpslimitcount+1)); frameLimitError += frametimeStep - (curtime - lastTime);
u32 wantedtick = (u32)ceil(wantedtickF); if (frameLimitError < -frametimeStep)
if (curtick < wantedtick) SDL_Delay(wantedtick - curtick); frameLimitError = -frametimeStep;
if (frameLimitError > frametimeStep)
lasttick = SDL_GetTicks(); frameLimitError = frametimeStep;
fpslimitcount++;
if ((abs(wantedtickF - (float)wantedtick) < 0.001312) || (fpslimitcount > 60)) if (round(frameLimitError * 1000.0) > 0.0)
{ {
fpslimitcount = 0; SDL_Delay(round(frameLimitError * 1000.0));
starttick = lasttick; double timeBeforeSleep = curtime;
curtime = SDL_GetPerformanceCounter() * perfCountsSec;
frameLimitError -= curtime - timeBeforeSleep;
} }
} }
else
{ lastTime = curtime;
if (delay < 1) SDL_Delay(1);
lasttick = SDL_GetTicks();
}
} }
nframes++; nframes++;
if (nframes >= 30) if (nframes >= 30)
{ {
u32 tick = SDL_GetTicks(); double time = SDL_GetPerformanceCounter() * perfCountsSec;
u32 diff = tick - lastmeasuretick; double dt = time - lastMeasureTime;
lastmeasuretick = tick; lastMeasureTime = time;
u32 fps; u32 fps = round(nframes / dt);
if (diff < 1) fps = 77777;
else fps = (nframes * 1000) / diff;
nframes = 0; nframes = 0;
float fpstarget; float fpstarget = 1.0/frametimeStep;
if (framerate < 1) fpstarget = 999;
else fpstarget = 1000.0f/framerate;
sprintf(melontitle, "[%d/%.0f] melonDS " MELONDS_VERSION, fps, fpstarget); sprintf(melontitle, "[%d/%.0f] melonDS " MELONDS_VERSION, fps, fpstarget);
changeWindowTitle(melontitle); changeWindowTitle(melontitle);
@ -544,10 +538,8 @@ void EmuThread::run()
{ {
// paused // paused
nframes = 0; nframes = 0;
lasttick = SDL_GetTicks(); lastTime = SDL_GetPerformanceCounter() * perfCountsSec;
starttick = lasttick; lastMeasureTime = lastTime;
lastmeasuretick = lasttick;
fpslimitcount = 0;
emit windowUpdate(); emit windowUpdate();