From b42c8f3a1a8bd0c4a3726323cbc4cd81745b1160 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Fri, 24 Oct 2014 17:27:52 +0200 Subject: [PATCH] Timing-Test --- .../spacescooter/threads/GameThread.java | 42 +++++++++++-------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/src/de/teamteamteam/spacescooter/threads/GameThread.java b/src/de/teamteamteam/spacescooter/threads/GameThread.java index 51b2449..9aea0d4 100644 --- a/src/de/teamteamteam/spacescooter/threads/GameThread.java +++ b/src/de/teamteamteam/spacescooter/threads/GameThread.java @@ -17,31 +17,20 @@ public class GameThread extends Thread { private long lastFrame; /** - * 60FPS => 1/60 in nanoseconds. + * 60FPS => 1/60s in nanoseconds (10^-9). */ - private long frameTime = 16666666L; + private long frameTime = (1000L*1000L*1000L) / 60L; public GameThread(GameFrame gf) { this.setName("GameThread"); this.gf = gf; +// this.setPriority(Thread.MAX_PRIORITY); } public void run() { final GameFrame gf = this.gf; // :'-( - this.lastFrame = System.nanoTime(); while (true) { - //If we have to wait for more than 1.5ms, sleep 1ms - if((System.nanoTime() - this.lastFrame) > 1500000) { - try { - 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); + long frameStart = System.nanoTime(); //Update all the entities this.updateBackgrounds(); @@ -53,8 +42,27 @@ public class GameThread extends Thread { gf.draw(); } }); - //Update time for the last frame - this.lastFrame = System.nanoTime(); + + long frameDone = System.nanoTime(); + long workTime = (frameDone - frameStart); + //System.out.println("Arbeitszeit: " + (workTime) + "ns"); + + long timeToWait = this.frameTime - workTime; + long msToWait = timeToWait / 1000000; + + //wait using sleep for bigger intervals + if(msToWait > 1) { + try { + Thread.sleep(msToWait); + } catch(InterruptedException e) { + System.err.println(e.getStackTrace()); + } + } + System.out.println("1: " + (System.nanoTime() - frameStart)); + System.out.println("2: " + (System.nanoTime() - frameStart)); + System.out.println("3: " + (System.nanoTime() - frameStart)); + System.out.println("4: " + (System.nanoTime() - frameStart)); + System.out.println("5: " + (System.nanoTime() - frameStart)); } }