From 11bd4bff13ad9c3c5b58fee4a10e99a771d9863a Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Fri, 24 Oct 2014 11:11:15 +0200 Subject: [PATCH] Fix sleeping, now everything runs smooooooooothly. --- .../background/StarBackground.java | 2 +- .../spacescooter/entities/Player.java | 1 - .../spacescooter/threads/GameThread.java | 23 +++++++++++++------ 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/de/teamteamteam/spacescooter/background/StarBackground.java b/src/de/teamteamteam/spacescooter/background/StarBackground.java index 026a9a9..df3bc84 100644 --- a/src/de/teamteamteam/spacescooter/background/StarBackground.java +++ b/src/de/teamteamteam/spacescooter/background/StarBackground.java @@ -24,7 +24,7 @@ public class StarBackground extends Background { private int offset = 0; public void update(long millisecondsSinceLastCall) { - this.offset -= 15; + this.offset -= 5; if(Math.abs(this.offset) > StarBackground.img.getWidth()) { this.offset += StarBackground.img.getWidth(); } diff --git a/src/de/teamteamteam/spacescooter/entities/Player.java b/src/de/teamteamteam/spacescooter/entities/Player.java index 0380ddd..4f76819 100644 --- a/src/de/teamteamteam/spacescooter/entities/Player.java +++ b/src/de/teamteamteam/spacescooter/entities/Player.java @@ -32,7 +32,6 @@ public class Player extends Entity { } public void update(long millisecondsSinceLastCall) { - System.out.println(millisecondsSinceLastCall / (1000.0/60.0)); int offset = (int) ((3.0F/16.0F) * millisecondsSinceLastCall); if(Keyboard.isKeyDown(KeyEvent.VK_UP)) { this.y -= offset; diff --git a/src/de/teamteamteam/spacescooter/threads/GameThread.java b/src/de/teamteamteam/spacescooter/threads/GameThread.java index 33eeed5..51b2449 100644 --- a/src/de/teamteamteam/spacescooter/threads/GameThread.java +++ b/src/de/teamteamteam/spacescooter/threads/GameThread.java @@ -16,6 +16,11 @@ public class GameThread extends Thread { private long lastFrame; + /** + * 60FPS => 1/60 in nanoseconds. + */ + private long frameTime = 16666666L; + public GameThread(GameFrame gf) { this.setName("GameThread"); this.gf = gf; @@ -23,18 +28,22 @@ public class GameThread extends Thread { public void run() { final GameFrame gf = this.gf; // :'-( - this.lastFrame = System.currentTimeMillis(); + this.lastFrame = System.nanoTime(); while (true) { - //If it is not time yet, sleep and continue the loop. - if((System.currentTimeMillis() - this.lastFrame) <= 15) { + //If we have to wait for more than 1.5ms, sleep 1ms + if((System.nanoTime() - this.lastFrame) > 1500000) { try { - Thread.sleep(1); + Thread.sleep(1); //wait 1 ms } catch(InterruptedException e) { System.err.println(e.getStackTrace()); } continue; } + + //If we have to wait for less than 1.5ms, wait manually + while((this.frameTime - (System.nanoTime() - this.lastFrame)) > 100); + //Update all the entities this.updateBackgrounds(); this.updateEntities(); @@ -45,7 +54,7 @@ public class GameThread extends Thread { } }); //Update time for the last frame - this.lastFrame = System.currentTimeMillis(); + this.lastFrame = System.nanoTime(); } } @@ -53,7 +62,7 @@ public class GameThread extends Thread { Iterator i = Background.backgrounds.iterator(); while(i.hasNext()) { Background b = i.next(); - b.update(System.currentTimeMillis() - this.lastFrame); + b.update(System.nanoTime() - this.lastFrame); } } @@ -61,7 +70,7 @@ public class GameThread extends Thread { Iterator i = Entity.entities.iterator(); while(i.hasNext()) { Entity e = i.next(); - e.update(System.currentTimeMillis() - this.lastFrame); + e.update(System.nanoTime() - this.lastFrame); } } }