From f290fd64b6771e934f30c855d67cf5915f25f6f8 Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Tue, 20 Nov 2018 20:09:58 +0100 Subject: Remove Scene.absoluteRender(), add Scene.build() absoluteRender was a useless method since the normal render can be extended by overriding and calling super.render() Scene.build() is a new method which runs once when the scene thread starts, and is meant to be used to initialize the scene. --- src/subconscious/graphics/BattleScene.java | 10 ++++++++-- src/subconscious/graphics/GameWindow.java | 6 +++--- src/subconscious/graphics/MapEditorScene.java | 8 ++++++-- src/subconscious/graphics/MapScene.java | 10 +--------- src/subconscious/graphics/Scene.java | 21 ++++++++++++++++----- 5 files changed, 34 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/subconscious/graphics/BattleScene.java b/src/subconscious/graphics/BattleScene.java index 5cdfad7..b1def18 100644 --- a/src/subconscious/graphics/BattleScene.java +++ b/src/subconscious/graphics/BattleScene.java @@ -33,6 +33,7 @@ import javax.swing.JButton; @SuppressWarnings("serial") public class BattleScene extends MapScene implements ActionListener { + // TODO: move out of graphics and rename Turn or TurnState private enum Mode { NONE, ATTACK, MOVE }; @@ -63,7 +64,6 @@ public class BattleScene extends MapScene implements ActionListener { this.setLayout(new BorderLayout()); - // TODO: make a method buildUi() ? JPanel bottomPanel = new JPanel(); bottomPanel.setLayout(new GridLayout(1,4)); @@ -91,7 +91,13 @@ public class BattleScene extends MapScene implements ActionListener { } @Override - protected void absoluteRender(Graphics2D g) { + protected void build() { + + } + + @Override + protected void render(Graphics2D g) { + super.render(g); //draw cursor //g.setColor(Palette.BLUE); //g.fillOval(this.realX-10*this.guiSize, this.realY-10*this.guiSize, 20*this.guiSize, 20*this.guiSize); diff --git a/src/subconscious/graphics/GameWindow.java b/src/subconscious/graphics/GameWindow.java index d7bb655..b0f48fd 100644 --- a/src/subconscious/graphics/GameWindow.java +++ b/src/subconscious/graphics/GameWindow.java @@ -116,15 +116,15 @@ public class GameWindow extends JFrame implements ActionListener { // build thread this.scene = scene; - this.sceneThread = new Thread(this.scene); - // for debugging - this.sceneThread.setName("Scene rendering Thread"); // add to UI this.root.add(this.scene, SCENE_CARD); ((CardLayout) this.root.getLayout()).show(this.root, SCENE_CARD); // start scene + this.sceneThread = new Thread(this.scene); + // for debugging + this.sceneThread.setName("Scene rendering Thread"); this.sceneThread.start(); this.scene.updateCanvasSize(this.getSize()); } diff --git a/src/subconscious/graphics/MapEditorScene.java b/src/subconscious/graphics/MapEditorScene.java index c4a9e11..17e3c0c 100644 --- a/src/subconscious/graphics/MapEditorScene.java +++ b/src/subconscious/graphics/MapEditorScene.java @@ -76,11 +76,15 @@ public class MapEditorScene extends MapScene implements ActionListener { this.add(this.canvas, BorderLayout.CENTER); } + + @Override + protected void build() {} + @Override - protected void update(long deltaNanoTime) {} + protected void render(Graphics2D g) {} @Override - protected void absoluteRender(Graphics2D g2d) {} + protected void update(long deltaNanoTime) {}; @Override public void mouseClicked(MouseEvent e) { diff --git a/src/subconscious/graphics/MapScene.java b/src/subconscious/graphics/MapScene.java index acbf7b6..db12f63 100644 --- a/src/subconscious/graphics/MapScene.java +++ b/src/subconscious/graphics/MapScene.java @@ -172,15 +172,13 @@ public abstract class MapScene extends Scene { } @Override - protected void render() { + protected void render(Graphics2D g) { // TODO: remove, map should never be NULL // this is an old piece of code used for MapEditor if (this.map == null) { return; } - Graphics2D g = (Graphics2D) this.buffer.getDrawGraphics(); - // clear canvas g.setColor(Palette.BLACK); g.fillRect(0, 0, this.canvasSize.width, this.canvasSize.height); @@ -223,14 +221,8 @@ public abstract class MapScene extends Scene { ex.printStackTrace(); } - this.absoluteRender(g); - - g.dispose(); - this.buffer.show(); } - protected abstract void absoluteRender(Graphics2D g); - @Override public void keyPressed(KeyEvent e) { super.keyPressed(e); diff --git a/src/subconscious/graphics/Scene.java b/src/subconscious/graphics/Scene.java index ecb2917..1c5002d 100644 --- a/src/subconscious/graphics/Scene.java +++ b/src/subconscious/graphics/Scene.java @@ -74,6 +74,8 @@ public abstract class Scene extends JPanel this.canvas.addMouseListener(this); this.canvas.addMouseMotionListener(this); this.canvas.addMouseWheelListener(this); + + this.build(); } protected Scene(Game g) { @@ -83,6 +85,13 @@ public abstract class Scene extends JPanel this.game = g; } + /* abstract methods */ + // runs when the the scene thread starts + protected abstract void build(); + // runs on each tick + protected abstract void render(Graphics2D g); + protected abstract void update(long deltaNanoTime); + public void run() { long beginLoopTime; long endLoopTime; @@ -112,11 +121,17 @@ public abstract class Scene extends JPanel } } - this.render(); + Graphics2D g = (Graphics2D) this.buffer.getDrawGraphics(); + + this.render(g); + + g.dispose(); + this.buffer.show(); lastUpdateTime = currentUpdateTime; currentUpdateTime = System.nanoTime(); + // TODO: change to this.game.update(...) this.update(currentUpdateTime - lastUpdateTime); endLoopTime = System.nanoTime(); @@ -169,10 +184,6 @@ public abstract class Scene extends JPanel this.canvasSize = newSize; } - /* abstract methods */ - protected abstract void render(); - protected abstract void update(long deltaNanoTime); - /* key listener */ @Override public void keyTyped(KeyEvent e) {} @Override public void keyPressed(KeyEvent e) {} -- cgit v1.2.1