summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNao Pross <naopross@thearcway.org>2018-12-03 01:20:30 +0100
committerNao Pross <naopross@thearcway.org>2018-12-03 01:20:30 +0100
commit0febfb73ac89d3ef3bca659ffa24e58d4659b3fc (patch)
treebc7754007fae2f76a166c7d657faf000f88c7f55
parentRefractor sleep in Scene rendering loop for better performance (diff)
downloadSubconscious-java-0febfb73ac89d3ef3bca659ffa24e58d4659b3fc.tar.gz
Subconscious-java-0febfb73ac89d3ef3bca659ffa24e58d4659b3fc.zip
Update PerfView to show FPS instead of DeltaTime
-rw-r--r--src/subconscious/graphics/Scene.java8
-rw-r--r--src/subconscious/graphics/widget/PerfView.java9
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