From 0febfb73ac89d3ef3bca659ffa24e58d4659b3fc Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Mon, 3 Dec 2018 01:20:30 +0100 Subject: Update PerfView to show FPS instead of DeltaTime --- src/subconscious/graphics/Scene.java | 8 ++++---- src/subconscious/graphics/widget/PerfView.java | 9 ++++++--- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/subconscious/graphics/Scene.java b/src/subconscious/graphics/Scene.java index c5938f1..01b8fe9 100644 --- a/src/subconscious/graphics/Scene.java +++ b/src/subconscious/graphics/Scene.java @@ -247,14 +247,15 @@ public abstract class Scene extends Panel } // update game and widgets - this.update(timeDiff); - this.updateWidgets(timeDiff); - this.game.update(timeDiff); + this.update(Scene.DESIRED_DELTA_LOOP_NS - timeDiff); + this.updateWidgets(Scene.DESIRED_DELTA_LOOP_NS - timeDiff); + this.game.update(Scene.DESIRED_DELTA_LOOP_NS - timeDiff); afterTime = System.nanoTime(); timeDiff = afterTime - beforeTime; sleepTime = (Scene.DESIRED_DELTA_LOOP_NS - timeDiff) - overSleepTime; + // if sleep is needed (too fast) if (sleepTime > 0) { try { @@ -265,7 +266,6 @@ public abstract class Scene extends Panel overSleepTime = (System.nanoTime() - afterTime) - sleepTime; - // if sleep is not needed (too slow) } else { overSleepTime = 0L; diff --git a/src/subconscious/graphics/widget/PerfView.java b/src/subconscious/graphics/widget/PerfView.java index 4483146..7ec7372 100644 --- a/src/subconscious/graphics/widget/PerfView.java +++ b/src/subconscious/graphics/widget/PerfView.java @@ -12,7 +12,7 @@ import java.awt.Graphics2D; * final game. */ public class PerfView extends Widget implements Dynamic { - protected long lastDeltaNanoTime = 0; + protected long lastDeltaNanoTime = 1; public PerfView(String uniqueName, int x, int y) { // the size depends on the font @@ -23,8 +23,8 @@ public class PerfView extends Widget implements Dynamic { public void render(Graphics2D g) { g.setFont(Fonts.DEFAULT); - String text = "DeltaTime (us): " - + Long.toString(this.lastDeltaNanoTime/1000); + String text = "FPS: " + + Long.toString(1000*1000*1000/this.lastDeltaNanoTime); this.width = g.getFontMetrics().stringWidth(text); this.height = g.getFontMetrics().getHeight() + 10; @@ -38,6 +38,9 @@ public class PerfView extends Widget implements Dynamic { @Override public void update(long deltaNanoTime) { + if (deltaNanoTime == 0) + deltaNanoTime = 1; + this.lastDeltaNanoTime = deltaNanoTime; } } \ No newline at end of file -- cgit v1.2.1