summaryrefslogtreecommitdiffstats
path: root/src/Scene.java
blob: dcc3e5e85a5b06d7805475d8c2d63ff3012d281f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import java.awt.Canvas;
import java.awt.Graphics2D;
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelListener;
import java.awt.event.MouseWheelEvent;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Scene extends JPanel  implements Runnable, KeyListener, MouseListener, MouseMotionListener, MouseWheelListener {
	protected int WIDTH;
	protected int HEIGHT;
	protected BufferStrategy buffer;
	protected long desiredFPS = 60;
	protected long desiredDeltaLoop = (1000*1000*1000)/desiredFPS;
	protected JFrame frame;
	protected boolean running;
	protected Canvas canvas;
	protected int guiSize = 2;

	protected Scene(JFrame frame, Sub sub) {
		this.frame = frame;
		//getting window size
		this.WIDTH = (int) this.frame.getSize().getWidth();
		this.HEIGHT = (int) this.frame.getSize().getHeight();

		this.canvas = new Canvas();	

		this.canvas.setBounds(0, 0, this.WIDTH, this.HEIGHT);
		this.canvas.setIgnoreRepaint(true);

		this.canvas.addKeyListener(this);
		this.canvas.addMouseListener(this);
		this.canvas.addMouseMotionListener(this);
		this.canvas.addMouseWheelListener(this);
	}

	public void start() {
		this.canvas.createBufferStrategy(2);
		this.buffer = this.canvas.getBufferStrategy();

		this.canvas.requestFocus();
		this.running = true;
		new Thread(this).start();
	}

	public void run() {
		long beginLoopTime;
		long endLoopTime;
		long currentUpdateTime = System.nanoTime();
		long lastUpdateTime;
		long deltaLoop;

		while (running) {
			beginLoopTime = System.nanoTime();

			//getting window size
			this.WIDTH = (int) this.frame.getSize().getWidth();
			this.HEIGHT = (int) this.frame.getSize().getHeight();

			this.render();

			lastUpdateTime = currentUpdateTime;
			currentUpdateTime = System.nanoTime();
			this.update((int) ((currentUpdateTime - lastUpdateTime)/(1000*1000)));

			endLoopTime = System.nanoTime();
			deltaLoop = endLoopTime - beginLoopTime;

			if (deltaLoop > this.desiredDeltaLoop) {
				//late
			} else {
				try {
					Thread.sleep((this.desiredDeltaLoop - deltaLoop)/(1000*1000));
				} catch (InterruptedException e ) {
					
				}
			}
		}
	}

	protected void render() {}
	protected void update(int deltaTime) {}

	@Override
	public void keyTyped(KeyEvent e) {}
	public void keyPressed(KeyEvent e) {}
	public void keyReleased(KeyEvent e) {}
	public void mouseClicked(MouseEvent e) {}
	public void mouseEntered(MouseEvent e) {}
	public void mouseExited(MouseEvent e) {}
	public void mousePressed(MouseEvent e) {}
	public void mouseReleased(MouseEvent e) {}
	public void mouseDragged(MouseEvent e) {}
	public void mouseMoved(MouseEvent e) {}
	public void mouseWheelMoved(MouseWheelEvent e) {}

}