summaryrefslogtreecommitdiffstats
path: root/src/Scene.java
diff options
context:
space:
mode:
authorNao Pross <naopross@thearcway.org>2018-11-19 12:05:03 +0100
committerNao Pross <naopross@thearcway.org>2018-11-19 12:05:03 +0100
commite6fb6cc1ba478d7f89df9f968306e639df2f8df1 (patch)
tree9f43592664adb3cc123180f779c302de83f87952 /src/Scene.java
parentUpdate GameWindow and remove parent frame dependency on Scene (diff)
downloadSubconscious-java-e6fb6cc1ba478d7f89df9f968306e639df2f8df1.tar.gz
Subconscious-java-e6fb6cc1ba478d7f89df9f968306e639df2f8df1.zip
Set up CardLayout for GameWindow and Scene pause/resume
Diffstat (limited to '')
-rw-r--r--src/Scene.java69
1 files changed, 59 insertions, 10 deletions
diff --git a/src/Scene.java b/src/Scene.java
index d8c826e..0ecf22f 100644
--- a/src/Scene.java
+++ b/src/Scene.java
@@ -15,24 +15,29 @@ import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
import javax.swing.JPanel;
-// TODO: implement pause (rendering) function
+import java.awt.event.ComponentEvent;
+import java.awt.event.ComponentListener;
+
public abstract class Scene extends JPanel
- implements Runnable, KeyListener, MouseListener,
- MouseMotionListener, MouseWheelListener {
+ implements Runnable, KeyListener, MouseListener, MouseMotionListener,
+ MouseWheelListener, ComponentListener {
protected final long DESIRED_FPS = 60;
protected final long DESIRED_DELTA_LOOP = (1000*1000*1000)/DESIRED_FPS;
- protected BufferStrategy buffer;
+ protected boolean running;
+ protected boolean paused;
+ protected Object pauseLock;
+ protected BufferStrategy buffer;
protected Dimension canvasSize;
- protected boolean running;
protected Canvas canvas;
// TODO: make accessible from user settings
protected int guiSize = 2;
protected Scene() {
+
this.canvasSize = GameWindow.WINDOW_SIZE;
this.canvas = new Canvas();
@@ -45,7 +50,7 @@ public abstract class Scene extends JPanel
this.canvas.addMouseWheelListener(this);
}
- public void initCanvasBuffer() {
+ private void initCanvasBuffer() {
this.canvas.createBufferStrategy(2);
this.buffer = this.canvas.getBufferStrategy();
@@ -62,15 +67,29 @@ public abstract class Scene extends JPanel
this.initCanvasBuffer();
this.running = true;
+ this.paused = false;
+ pauseLock = new Object();
while (running) {
beginLoopTime = System.nanoTime();
+ synchronized (this.pauseLock) {
+ if (this.paused) {
+ try {
+ this.pauseLock.wait();
+ } catch (InterruptedException ex) {
+ ex.printStackTrace();
+ break;
+ }
+ }
+ }
+
this.render();
lastUpdateTime = currentUpdateTime;
currentUpdateTime = System.nanoTime();
- this.update((int) ((currentUpdateTime - lastUpdateTime)/(1000*1000)));
+
+ this.update(currentUpdateTime - lastUpdateTime);
endLoopTime = System.nanoTime();
deltaLoop = endLoopTime - beginLoopTime;
@@ -87,22 +106,43 @@ public abstract class Scene extends JPanel
}
}
+ public synchronized void stop() {
+ this.running = false;
+
+ // stop thread even if paused
+ this.resume();
+ }
+
+ public synchronized void pause() {
+ this.paused = true;
+ }
+
+ public synchronized void resume() {
+ if (!paused)
+ return;
+
+ this.paused = false;
+ this.pauseLock.notifyAll();
+ }
+
// automagically set the canvas size to the parent's size
- public void updateCanvasSize() {
+ public synchronized void updateCanvasSize() {
this.canvasSize = this.getParent().getSize();
}
- public void setCanvasSize(Dimension newSize) {
+ public synchronized void setCanvasSize(Dimension newSize) {
this.canvasSize = newSize;
}
protected abstract void render();
- protected abstract void update(int deltaNanoTime);
+ protected abstract void update(long deltaNanoTime);
@Override
public void keyTyped(KeyEvent e) {}
public void keyPressed(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
+
+ @Override
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
@@ -112,4 +152,13 @@ public abstract class Scene extends JPanel
public void mouseMoved(MouseEvent e) {}
public void mouseWheelMoved(MouseWheelEvent e) {}
+ @Override
+ public void componentResized(ComponentEvent e) {
+ this.updateCanvasSize();
+ }
+
+ public void componentMoved(ComponentEvent e) {}
+ public void componentShown(ComponentEvent e) {}
+ public void componentHidden(ComponentEvent e) {}
+
}