Optimize the two threads and how drawing is handled.
This commit is contained in:
parent
a733187887
commit
f26c9366cd
|
@ -35,7 +35,7 @@ public class Main {
|
||||||
gf.draw(); //Draw nothing for the first time.
|
gf.draw(); //Draw nothing for the first time.
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
//Initialize GameThread
|
//Initialize GameThread
|
||||||
PaintThread paintThread = new PaintThread(gf);
|
PaintThread paintThread = new PaintThread(gf);
|
||||||
paintThread.start();
|
paintThread.start();
|
||||||
|
|
|
@ -39,9 +39,12 @@ public class GameFrame extends JFrame {
|
||||||
|
|
||||||
this.setVisible(true);
|
this.setVisible(true);
|
||||||
|
|
||||||
|
//ignore the OS telling us to repaint - it's wasting our time.
|
||||||
|
this.setIgnoreRepaint(true);
|
||||||
|
|
||||||
|
//prepare the buffered strategy
|
||||||
this.createBufferStrategy(2);
|
this.createBufferStrategy(2);
|
||||||
this.bufferStrategy = this.getBufferStrategy();
|
this.bufferStrategy = this.getBufferStrategy();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -71,7 +74,7 @@ public class GameFrame extends JFrame {
|
||||||
} while (this.bufferStrategy.contentsRestored());
|
} while (this.bufferStrategy.contentsRestored());
|
||||||
this.bufferStrategy.show();
|
this.bufferStrategy.show();
|
||||||
} while (this.bufferStrategy.contentsLost());
|
} while (this.bufferStrategy.contentsLost());
|
||||||
Toolkit.getDefaultToolkit().sync();
|
Toolkit.getDefaultToolkit().sync(); //Tell the OS to update its graphics of the window.
|
||||||
}
|
}
|
||||||
|
|
||||||
public void drawBackgrounds(Graphics g) {
|
public void drawBackgrounds(Graphics g) {
|
||||||
|
|
|
@ -13,7 +13,7 @@ public class PaintThread extends TimingThread {
|
||||||
|
|
||||||
public PaintThread(GameFrame gf) {
|
public PaintThread(GameFrame gf) {
|
||||||
this.gf = gf;
|
this.gf = gf;
|
||||||
this.setName("GameThread");
|
this.setName("PaintThread");
|
||||||
this.setHz(60);
|
this.setHz(60);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,11 +19,10 @@ public abstract class TimingThread extends Thread {
|
||||||
public void run() {
|
public void run() {
|
||||||
while (true) {
|
while (true) {
|
||||||
long workStart = System.nanoTime();
|
long workStart = System.nanoTime();
|
||||||
|
|
||||||
// do the actual work
|
// do the actual work
|
||||||
this.work();
|
this.work();
|
||||||
|
|
||||||
long workDone = System.nanoTime();
|
long workDone = System.nanoTime();
|
||||||
|
//calculate time of work
|
||||||
long workTime = (workDone - workStart);
|
long workTime = (workDone - workStart);
|
||||||
|
|
||||||
long timeToWait = this.workInterval - workTime;
|
long timeToWait = this.workInterval - workTime;
|
||||||
|
@ -32,14 +31,16 @@ public abstract class TimingThread extends Thread {
|
||||||
// wait using sleep for bigger intervals
|
// wait using sleep for bigger intervals
|
||||||
if (msToWait > 1) {
|
if (msToWait > 1) {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(msToWait);
|
//make sure there is a little buffer for manual waiting loop left
|
||||||
|
Thread.sleep(Math.max(0,(msToWait - 1)));
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
System.err.println("[" + this.getName() + "]" + e.getStackTrace());
|
System.err.println("[" + this.getName() + "]" + e.getStackTrace());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
while (this.workInterval - (System.nanoTime() - workStart) > 100);
|
//wait manually for the rest of the interval
|
||||||
//System.out.println("[" + this.getName() + "]: "+ (System.nanoTime() - workStart));
|
long sleepUntil = workStart + this.workInterval;
|
||||||
|
while ((sleepUntil- System.nanoTime()) > 100);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,13 +9,13 @@ public class UpdateThread extends TimingThread {
|
||||||
|
|
||||||
public UpdateThread() {
|
public UpdateThread() {
|
||||||
this.setName("UpdateThread");
|
this.setName("UpdateThread");
|
||||||
this.setHz(100);
|
this.setHz(60);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void work() {
|
public void work() {
|
||||||
// Update all the entities
|
// Update all the entities
|
||||||
this.updateBackgrounds();
|
this.updateBackgrounds();
|
||||||
this.updateEntities();
|
this.updateEntities();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateBackgrounds() {
|
private void updateBackgrounds() {
|
||||||
|
|
Loading…
Reference in New Issue