Fix sleeping, now everything runs smooooooooothly.

This commit is contained in:
Jan Philipp Timme 2014-10-24 11:11:15 +02:00
parent a276298174
commit 11bd4bff13
3 changed files with 17 additions and 9 deletions

View File

@ -24,7 +24,7 @@ public class StarBackground extends Background {
private int offset = 0; private int offset = 0;
public void update(long millisecondsSinceLastCall) { public void update(long millisecondsSinceLastCall) {
this.offset -= 15; this.offset -= 5;
if(Math.abs(this.offset) > StarBackground.img.getWidth()) { if(Math.abs(this.offset) > StarBackground.img.getWidth()) {
this.offset += StarBackground.img.getWidth(); this.offset += StarBackground.img.getWidth();
} }

View File

@ -32,7 +32,6 @@ public class Player extends Entity {
} }
public void update(long millisecondsSinceLastCall) { public void update(long millisecondsSinceLastCall) {
System.out.println(millisecondsSinceLastCall / (1000.0/60.0));
int offset = (int) ((3.0F/16.0F) * millisecondsSinceLastCall); int offset = (int) ((3.0F/16.0F) * millisecondsSinceLastCall);
if(Keyboard.isKeyDown(KeyEvent.VK_UP)) { if(Keyboard.isKeyDown(KeyEvent.VK_UP)) {
this.y -= offset; this.y -= offset;

View File

@ -16,6 +16,11 @@ public class GameThread extends Thread {
private long lastFrame; private long lastFrame;
/**
* 60FPS => 1/60 in nanoseconds.
*/
private long frameTime = 16666666L;
public GameThread(GameFrame gf) { public GameThread(GameFrame gf) {
this.setName("GameThread"); this.setName("GameThread");
this.gf = gf; this.gf = gf;
@ -23,18 +28,22 @@ public class GameThread extends Thread {
public void run() { public void run() {
final GameFrame gf = this.gf; // :'-( final GameFrame gf = this.gf; // :'-(
this.lastFrame = System.currentTimeMillis(); this.lastFrame = System.nanoTime();
while (true) { while (true) {
//If it is not time yet, sleep and continue the loop. //If we have to wait for more than 1.5ms, sleep 1ms
if((System.currentTimeMillis() - this.lastFrame) <= 15) { if((System.nanoTime() - this.lastFrame) > 1500000) {
try { try {
Thread.sleep(1); Thread.sleep(1); //wait 1 ms
} catch(InterruptedException e) { } catch(InterruptedException e) {
System.err.println(e.getStackTrace()); System.err.println(e.getStackTrace());
} }
continue; 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.updateBackgrounds();
this.updateEntities(); this.updateEntities();
@ -45,7 +54,7 @@ public class GameThread extends Thread {
} }
}); });
//Update time for the last frame //Update time for the last frame
this.lastFrame = System.currentTimeMillis(); this.lastFrame = System.nanoTime();
} }
} }
@ -53,7 +62,7 @@ public class GameThread extends Thread {
Iterator<Background> i = Background.backgrounds.iterator(); Iterator<Background> i = Background.backgrounds.iterator();
while(i.hasNext()) { while(i.hasNext()) {
Background b = i.next(); 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<Entity> i = Entity.entities.iterator(); Iterator<Entity> i = Entity.entities.iterator();
while(i.hasNext()) { while(i.hasNext()) {
Entity e = i.next(); Entity e = i.next();
e.update(System.currentTimeMillis() - this.lastFrame); e.update(System.nanoTime() - this.lastFrame);
} }
} }
} }