diff options
Diffstat (limited to 'src/Scene.java')
-rw-r--r-- | src/Scene.java | 41 |
1 files changed, 22 insertions, 19 deletions
diff --git a/src/Scene.java b/src/Scene.java index 08608cf..529a2f6 100644 --- a/src/Scene.java +++ b/src/Scene.java @@ -16,15 +16,19 @@ import javax.swing.JFrame; import javax.swing.JPanel; // TODO: implement pause (rendering) function -public class Scene extends JPanel implements Runnable, KeyListener, MouseListener, MouseMotionListener, MouseWheelListener { +public abstract class Scene extends JPanel + implements Runnable, KeyListener, MouseListener, + MouseMotionListener, MouseWheelListener { + + protected final long DESIRED_FPS = 60; + protected final long DESIRED_DELTA_LOOP = (1000*1000*1000)/DESIRED_FPS; + // TODO: lowercase not constant values protected int WIDTH; protected int HEIGHT; protected BufferStrategy buffer; - // TODO: make final - protected long desiredFPS = 60; - // TODO: make final - protected long desiredDeltaLoop = (1000*1000*1000)/desiredFPS; + + // TODO: remove frame reference protected JFrame frame; protected boolean running; protected Canvas canvas; @@ -32,10 +36,9 @@ public class Scene extends JPanel implements Runnable, KeyListener, MouseListen // TODO: make accessible from user settings protected int guiSize = 2; - // TODO: remove sub argument - protected Scene(JFrame frame, Sub sub) { + protected Scene(JFrame frame) { this.frame = frame; - //getting window size + // getting window size this.WIDTH = (int) this.frame.getSize().getWidth(); this.HEIGHT = (int) this.frame.getSize().getHeight(); @@ -50,14 +53,11 @@ public class Scene extends JPanel implements Runnable, KeyListener, MouseListen this.canvas.addMouseWheelListener(this); } - // TODO: fix this weird memory shit - public void start() { + public void initCanvasBuffer() { this.canvas.createBufferStrategy(2); this.buffer = this.canvas.getBufferStrategy(); - this.canvas.requestFocus(); - this.running = true; - new Thread(this).start(); + this.canvas.requestFocus(); } public void run() { @@ -67,6 +67,10 @@ public class Scene extends JPanel implements Runnable, KeyListener, MouseListen long lastUpdateTime; long deltaLoop; + this.initCanvasBuffer(); + + this.running = true; + while (running) { beginLoopTime = System.nanoTime(); @@ -84,11 +88,11 @@ public class Scene extends JPanel implements Runnable, KeyListener, MouseListen endLoopTime = System.nanoTime(); deltaLoop = endLoopTime - beginLoopTime; - if (deltaLoop > this.desiredDeltaLoop) { - // late => skip frame + if (deltaLoop > this.DESIRED_DELTA_LOOP) { + // TODO: late => skip frame } else { try { - Thread.sleep((this.desiredDeltaLoop - deltaLoop)/(1000*1000)); + Thread.sleep((this.DESIRED_DELTA_LOOP - deltaLoop)/(1000*1000)); } catch (InterruptedException e ) { } @@ -96,9 +100,8 @@ public class Scene extends JPanel implements Runnable, KeyListener, MouseListen } } - // TODO: make abstract - protected void render() {} - protected void update(int deltaTime) {} + protected abstract void render(); + protected abstract void update(int deltaNanoTime); @Override public void keyTyped(KeyEvent e) {} |