From f26c9366cdbc1d2a10082fc3a52e0ae6eb8408d3 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Fri, 24 Oct 2014 23:22:26 +0200 Subject: [PATCH] Optimize the two threads and how drawing is handled. --- src/de/teamteamteam/spacescooter/Main.java | 2 +- src/de/teamteamteam/spacescooter/gui/GameFrame.java | 7 +++++-- .../spacescooter/threads/PaintThread.java | 2 +- .../spacescooter/threads/TimingThread.java | 13 +++++++------ .../spacescooter/threads/UpdateThread.java | 8 ++++---- 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/de/teamteamteam/spacescooter/Main.java b/src/de/teamteamteam/spacescooter/Main.java index 60d22e9..c05566f 100644 --- a/src/de/teamteamteam/spacescooter/Main.java +++ b/src/de/teamteamteam/spacescooter/Main.java @@ -35,7 +35,7 @@ public class Main { gf.draw(); //Draw nothing for the first time. } }); - + //Initialize GameThread PaintThread paintThread = new PaintThread(gf); paintThread.start(); diff --git a/src/de/teamteamteam/spacescooter/gui/GameFrame.java b/src/de/teamteamteam/spacescooter/gui/GameFrame.java index a1ffd8c..20b81ee 100644 --- a/src/de/teamteamteam/spacescooter/gui/GameFrame.java +++ b/src/de/teamteamteam/spacescooter/gui/GameFrame.java @@ -39,9 +39,12 @@ public class GameFrame extends JFrame { 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.bufferStrategy = this.getBufferStrategy(); - } /** @@ -71,7 +74,7 @@ public class GameFrame extends JFrame { } while (this.bufferStrategy.contentsRestored()); this.bufferStrategy.show(); } 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) { diff --git a/src/de/teamteamteam/spacescooter/threads/PaintThread.java b/src/de/teamteamteam/spacescooter/threads/PaintThread.java index 06a5a40..0bded86 100644 --- a/src/de/teamteamteam/spacescooter/threads/PaintThread.java +++ b/src/de/teamteamteam/spacescooter/threads/PaintThread.java @@ -13,7 +13,7 @@ public class PaintThread extends TimingThread { public PaintThread(GameFrame gf) { this.gf = gf; - this.setName("GameThread"); + this.setName("PaintThread"); this.setHz(60); } diff --git a/src/de/teamteamteam/spacescooter/threads/TimingThread.java b/src/de/teamteamteam/spacescooter/threads/TimingThread.java index 0ffc7f9..47adc66 100644 --- a/src/de/teamteamteam/spacescooter/threads/TimingThread.java +++ b/src/de/teamteamteam/spacescooter/threads/TimingThread.java @@ -19,11 +19,10 @@ public abstract class TimingThread extends Thread { public void run() { while (true) { long workStart = System.nanoTime(); - // do the actual work this.work(); - long workDone = System.nanoTime(); + //calculate time of work long workTime = (workDone - workStart); long timeToWait = this.workInterval - workTime; @@ -32,14 +31,16 @@ public abstract class TimingThread extends Thread { // wait using sleep for bigger intervals if (msToWait > 1) { 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) { System.err.println("[" + this.getName() + "]" + e.getStackTrace()); } } - - while (this.workInterval - (System.nanoTime() - workStart) > 100); - //System.out.println("[" + this.getName() + "]: "+ (System.nanoTime() - workStart)); + + //wait manually for the rest of the interval + long sleepUntil = workStart + this.workInterval; + while ((sleepUntil- System.nanoTime()) > 100); } } diff --git a/src/de/teamteamteam/spacescooter/threads/UpdateThread.java b/src/de/teamteamteam/spacescooter/threads/UpdateThread.java index f29dc81..744dd54 100644 --- a/src/de/teamteamteam/spacescooter/threads/UpdateThread.java +++ b/src/de/teamteamteam/spacescooter/threads/UpdateThread.java @@ -9,13 +9,13 @@ public class UpdateThread extends TimingThread { public UpdateThread() { this.setName("UpdateThread"); - this.setHz(100); + this.setHz(60); } public void work() { - // Update all the entities - this.updateBackgrounds(); - this.updateEntities(); + // Update all the entities + this.updateBackgrounds(); + this.updateEntities(); } private void updateBackgrounds() {