Fix FPS counter "frame" skip

This commit is contained in:
UnicornFreedom 2025-08-22 17:17:01 +02:00
parent b03078ff27
commit 667a74519a
No known key found for this signature in database
GPG Key ID: B4ED0DB6B940024F
2 changed files with 15 additions and 13 deletions

View File

@ -55,7 +55,7 @@ object OcelotDesktop extends LoggingConfiguration with Logging {
def emulationPaused_=(paused: Boolean): Unit = {
_emulationPaused = paused
// avoid sudden jumps of TPS counter after a pause
if (!paused) tpsCounter.skipFrame()
if (!paused) tpsCounter.skipSecond()
}
private val TickerIntervalHistorySize = 5

View File

@ -9,29 +9,31 @@ class FPSCalculator {
def fps: Float = _fps
private var _skipFrame = false
private var _skipSecond = false
/**
* Next tick will not count towards the overall statistics.
* Current second will not count towards the overall statistics.
* (In case the measurements were corrupted or distorted somehow.)
*/
def skipFrame(): Unit = _skipFrame = true
def skipSecond(): Unit = _skipSecond = true
var dt: Float = 0
def tick(): Unit = {
val currentTime = System.currentTimeMillis()
if (!_skipFrame) {
dt = (currentTime - prevFrameTime) / 1000f
numFrames += 1
if (currentTime - prevTime > 1000) {
val delta = currentTime - prevTime
prevTime = currentTime
_fps = numFrames.asInstanceOf[Float] / delta * 1000f
numFrames = 0
if (!_skipSecond) {
_fps = numFrames.toFloat / delta * 1000.0f
} else {
_skipSecond = false
}
numFrames = 0
}
prevFrameTime = currentTime