summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNao Pross <naopross@thearcway.org>2018-12-03 00:16:45 +0100
committerNao Pross <naopross@thearcway.org>2018-12-03 00:16:45 +0100
commita3d71fc69d575eac5c63c0e920f1749aacc6e1e1 (patch)
tree642729f81e81fe7caef74bbee801a1b7588ce3b7
parentConnect ActorInfo to mouse events to show info of actor under cursor (diff)
downloadSubconscious-java-a3d71fc69d575eac5c63c0e920f1749aacc6e1e1.tar.gz
Subconscious-java-a3d71fc69d575eac5c63c0e920f1749aacc6e1e1.zip
Update Scene thread pause to use a higher level interface
Scene now uses locks from java.util.concurrent.lock instead of the internal Object.{notify(), wait()}
Diffstat (limited to '')
-rw-r--r--src/subconscious/Game.java4
-rw-r--r--src/subconscious/graphics/Scene.java26
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();
}