diff options
Diffstat (limited to 'src/Scene.java')
-rw-r--r-- | src/Scene.java | 69 |
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) {} + } |