Timing-Test

This commit is contained in:
Jan Philipp Timme 2014-10-24 17:27:52 +02:00
parent aa9cc7a634
commit b42c8f3a1a
1 changed files with 25 additions and 17 deletions

View File

@ -17,31 +17,20 @@ public class GameThread extends Thread {
private long lastFrame; 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) { public GameThread(GameFrame gf) {
this.setName("GameThread"); this.setName("GameThread");
this.gf = gf; this.gf = gf;
// this.setPriority(Thread.MAX_PRIORITY);
} }
public void run() { public void run() {
final GameFrame gf = this.gf; // :'-( final GameFrame gf = this.gf; // :'-(
this.lastFrame = System.nanoTime();
while (true) { while (true) {
//If we have to wait for more than 1.5ms, sleep 1ms long frameStart = System.nanoTime();
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);
//Update all the entities //Update all the entities
this.updateBackgrounds(); this.updateBackgrounds();
@ -53,8 +42,27 @@ public class GameThread extends Thread {
gf.draw(); 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));
} }
} }