From 23188df276302a992843c35f63b98065a7b00ca0 Mon Sep 17 00:00:00 2001 From: Anuken Date: Thu, 5 Jul 2018 15:40:31 -0400 Subject: [PATCH] Fixed slowdown when FPS got lower than TPS --- .../io/anuke/mindustry/core/ThreadHandler.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/core/src/io/anuke/mindustry/core/ThreadHandler.java b/core/src/io/anuke/mindustry/core/ThreadHandler.java index 46890f322f..e127b94941 100644 --- a/core/src/io/anuke/mindustry/core/ThreadHandler.java +++ b/core/src/io/anuke/mindustry/core/ThreadHandler.java @@ -17,7 +17,8 @@ public class ThreadHandler { private final Array toRun = new Array<>(); private final ThreadProvider impl; private float delta = 1f; - private long frame = 0; + private float smoothDelta = 1f; + private long frame = 0, lastDeltaUpdate; private float framesSinceUpdate; private boolean enabled; @@ -29,7 +30,7 @@ public class ThreadHandler { Timers.setDeltaProvider(() -> { float result = impl.isOnThread() ? delta : Gdx.graphics.getDeltaTime()*60f; - return Math.min(Float.isNaN(result) ? 1f : result, 12f); + return Math.min(Float.isNaN(result) ? 1f : result, 15f); }); } @@ -62,7 +63,7 @@ public class ThreadHandler { } public int getTPS(){ - return (int)(60/delta); + return (int)(60/smoothDelta); } public long getFrameID(){ @@ -138,8 +139,6 @@ public class ThreadHandler { long elapsed = TimeUtils.nanosToMillis(TimeUtils.timeSinceNanos(time)); long target = (long) ((1000) / 60f); - delta = Math.max(elapsed, target) / 1000f * 60f; - if (elapsed < target) { impl.sleep(target - elapsed); } @@ -151,6 +150,14 @@ public class ThreadHandler { rendered = false; } + long actuallyElapsed = TimeUtils.nanosToMillis(TimeUtils.timeSinceNanos(time)); + delta = Math.max(actuallyElapsed, target) / 1000f * 60f; + + if(TimeUtils.timeSinceMillis(lastDeltaUpdate) > 1000){ + lastDeltaUpdate = TimeUtils.millis(); + smoothDelta = delta; + } + frame ++; framesSinceUpdate = 0; }