diff options
author | Nao Pross <naopross@thearcway.org> | 2018-02-10 19:06:21 +0100 |
---|---|---|
committer | Nao Pross <naopross@thearcway.org> | 2018-02-10 19:06:21 +0100 |
commit | 21b40a8dcc55b3ec365b4a33f08310957155b50d (patch) | |
tree | fe839a00556d2120f52a115cdd3efa54f4de32fc /src/main/java/Subconscious.java | |
parent | Implement actor, create player (diff) | |
download | Subconscious-old-21b40a8dcc55b3ec365b4a33f08310957155b50d.tar.gz Subconscious-old-21b40a8dcc55b3ec365b4a33f08310957155b50d.zip |
Threaded double buffered rendering
Other changes:
- Tabs < Spaces
- Add jar manifest configuration to gradle.build
Diffstat (limited to '')
-rw-r--r-- | src/main/java/Subconscious.java | 131 |
1 files changed, 101 insertions, 30 deletions
diff --git a/src/main/java/Subconscious.java b/src/main/java/Subconscious.java index 8ce6b15..8fed909 100644 --- a/src/main/java/Subconscious.java +++ b/src/main/java/Subconscious.java @@ -7,87 +7,158 @@ import java.awt.Dimension; import java.awt.event.MouseListener; import java.awt.event.MouseEvent; +import java.awt.event.KeyListener; +import java.awt.event.KeyEvent; +import java.awt.event.ComponentListener; +import java.awt.event.ComponentEvent; +import java.lang.Runnable; +import java.lang.Thread; -public class Subconscious extends JPanel implements MouseListener { +import java.awt.Toolkit; + + +public class Subconscious + extends JPanel + implements Runnable, MouseListener, KeyListener, ComponentListener { public static final String TITLE = "Subconscious"; + public static final long RENDER_PERIOD_MS = 10; /* graphics */ - public JFrame window; - // public Graphics dbGraphics; // double buffered graphics - // public Image dbImage = null; // double buffered image - - public Scene currentScene; + private JFrame window; + private Scene currentScene; + + private Graphics dbg = null; // double buffered graphics + private Image dbImage = null; // double buffered image + + private Thread animator = null; /* game */ - public boolean gameOver = false; - public boolean running = false; + // private volatile boolean gamePaused = false; + private volatile boolean gameOver = false; + private volatile boolean running = false; public Subconscious() { createWindow(); + setFocusable(true); + requestFocus(); + // TODO remove demo currentScene = new WorldScene(new Dimension(200, 200), 50); - addMouseListener(this); + addMouseListener(this); + addKeyListener(this); + addComponentListener(this); } public void createWindow() { window = new JFrame(TITLE); window.setSize(new Dimension(800, 600)); - window.setLocationRelativeTo(null); - window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - window.add(this); + window.setMinimumSize(getSize()); + window.setLocationRelativeTo(null); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.add(this); } /* game logic */ public void gameLoop() { - + } public void gameRender() { - + if (dbImage == null) { + dbImage = createImage(getWidth(), getHeight()); + if (dbImage == null) { + System.out.println("ERROR: Cannot create image"); + return; + } + } + + dbg = dbImage.getGraphics(); + dbg.fillRect(0, 0, getWidth(), getHeight()); + + currentScene.render(dbg); } - public void start() { - running = true; - gameOver = false; + public void gameStart() { + if (animator == null || !running) { + animator = new Thread(this); + animator.start(); + } + } + + /* animator and game thread */ + public void run() { + long beforeTime, afterTime, sleepTime; window.setVisible(true); + beforeTime = System.currentTimeMillis(); + + running = true; while (running) { gameLoop(); gameRender(); repaint(); + // could be better with NanoSeconds, but this is + // probably enough for this + afterTime = System.currentTimeMillis(); + sleepTime = RENDER_PERIOD_MS - (beforeTime - afterTime); + try { // TODO replace with correct timing - Thread.sleep(20); + Thread.sleep(sleepTime); } catch (InterruptedException ex) { // } + + beforeTime = System.currentTimeMillis(); } + + System.exit(0); } /* graphics */ @Override public void paintComponent(Graphics g) { - currentScene.render(g); + if (dbImage != null) { + g.drawImage(dbImage, 0, 0, null); + } + + Toolkit.getDefaultToolkit().sync(); + g.dispose(); } - /* listener */ - public void mouseClicked(MouseEvent e) { - int x = e.getX(); - int y = e.getY(); - } - public void mousePressed(MouseEvent e) {} - public void mouseReleased(MouseEvent e) {} - public void mouseEntered(MouseEvent e) {} - public void mouseExited(MouseEvent e) {} + /* listeners */ + public void mousePressed(MouseEvent e) {} + public void mouseReleased(MouseEvent e) {} + public void mouseEntered(MouseEvent e) {} + public void mouseExited(MouseEvent e) {} + public void mouseClicked(MouseEvent e) { + int x = e.getX(); + int y = e.getY(); + } + + public void keyPressed(KeyEvent e) {} + public void keyTyped(KeyEvent e) {} + public void keyReleased(KeyEvent e) { + int keyCode = e.getKeyCode(); + + if (keyCode == KeyEvent.VK_Q && e.isControlDown()) { + running = false; + } + } + + public void componentHidden(ComponentEvent e) {} + public void componentShown(ComponentEvent e) {} + public void componentMoved(ComponentEvent e) {} + public void componentResized(ComponentEvent e) { + dbImage = null; // create new image with the correct size + } /* main */ public static void main(String args[]) { - Subconscious game = new Subconscious(); - game.start(); + Subconscious g = new Subconscious(); + g.gameStart(); } } |