diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/subconscious/Game.java | 4 | ||||
-rw-r--r-- | src/subconscious/graphics/Scene.java | 26 |
2 files changed, 12 insertions, 18 deletions
diff --git a/src/subconscious/Game.java b/src/subconscious/Game.java index ed2bea6..bf1baf6 100644 --- a/src/subconscious/Game.java +++ b/src/subconscious/Game.java @@ -2,10 +2,6 @@ package subconscious; import java.util.ArrayList; -import java.util.concurrent.locks.ReentrantLock; -import java.util.concurrent.locks.Lock; -import java.util.concurrent.locks.Condition; - /* Game * Contains informations about the current state of the game used in an * Obervable Object pattern. The Game Loop is managed in the graphics. diff --git a/src/subconscious/graphics/Scene.java b/src/subconscious/graphics/Scene.java index 132704e..05161d5 100644 --- a/src/subconscious/graphics/Scene.java +++ b/src/subconscious/graphics/Scene.java @@ -11,6 +11,10 @@ import subconscious.graphics.widget.PerfView; import java.util.ArrayList; import java.util.Map; +import java.util.concurrent.locks.ReentrantLock; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.Condition; + import java.io.InputStream; import java.io.IOException; @@ -36,6 +40,7 @@ import java.awt.event.ComponentEvent; import java.awt.image.BufferStrategy; + /* Scene * Abstract model class for all scenes of the game. Each scene is in a different * thread and renders with a double buffered. As threads, scenes can be paused @@ -77,7 +82,8 @@ public abstract class Scene extends Panel protected volatile boolean running = true; protected volatile boolean threadPaused = false; - protected Object pauseLock = new Object(); + protected Lock pauseLock = new ReentrantLock(); + protected Condition threadResumed = pauseLock.newCondition(); protected volatile Dimension canvasSize = GameWindow.WINDOW_SIZE; protected Canvas canvas = new Canvas(); @@ -198,7 +204,6 @@ public abstract class Scene extends Panel long gameDeltaTime; this.running = true; - this.threadPaused = false; // wait for the canvas to be ready // this is required otherwise createBufferStrategy() @@ -263,14 +268,10 @@ public abstract class Scene extends Panel } } - synchronized (this.pauseLock) { - if (this.threadPaused) { - try { - this.pauseLock.wait(); - } catch (InterruptedException ex) { - ex.printStackTrace(); - } - } + if (this.threadPaused) { + this.pauseLock.lock(); + this.threadResumed.awaitUninterruptibly(); + this.pauseLock.unlock(); } } } @@ -291,10 +292,7 @@ public abstract class Scene extends Panel return; this.threadPaused = false; - - synchronized (this.pauseLock) { - this.pauseLock.notifyAll(); - } + this.threadResumed.signal(); } |