blob: ecb2917c9f12d874ac68f551d2d30fe5aab3651e (
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
package subconscious.graphics;
import subconscious.Game;
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.MouseMotionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.ComponentListener;
import java.awt.event.ComponentEvent;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
import javax.swing.JPanel;
/* Scene
* Abstract model class for all scenes of the game. Each scene is in a different
* thread and renders with a double buffered. As threads, scenes can be paused
* from rendering.
*
* It is also responsable for all inputs, which are transferred to the Game
* object which controls the internal state of the game.
*/
@SuppressWarnings("serial")
public abstract class Scene extends JPanel
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 Game game;
protected boolean running;
protected boolean paused;
protected Object pauseLock;
protected BufferStrategy buffer;
protected Dimension canvasSize;
protected Canvas canvas;
// TODO: make accessible from user settings
protected int guiSize = 2;
protected Scene() {
this.game = null;
// thread control
this.running = false;
this.paused = false;
this.pauseLock = new Object();
// watch for resize
this.addComponentListener(this);
// set up canvas
this.canvasSize = GameWindow.WINDOW_SIZE;
this.canvas = new Canvas();
this.canvas.setBounds(0, 0, this.canvasSize.width, this.canvasSize.height);
this.canvas.setIgnoreRepaint(true);
this.canvas.addKeyListener(this);
this.canvas.addMouseListener(this);
this.canvas.addMouseMotionListener(this);
this.canvas.addMouseWheelListener(this);
}
protected Scene(Game g) {
// call default contructor
this();
this.game = g;
}
public void run() {
long beginLoopTime;
long endLoopTime;
long currentUpdateTime = System.nanoTime();
long lastUpdateTime;
long deltaLoop;
// initialize canvas buffer
this.canvas.createBufferStrategy(2);
this.buffer = this.canvas.getBufferStrategy();
this.canvas.requestFocus();
this.running = true;
this.paused = false;
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(currentUpdateTime - lastUpdateTime);
endLoopTime = System.nanoTime();
deltaLoop = endLoopTime - beginLoopTime;
if (deltaLoop > this.DESIRED_DELTA_LOOP) {
// TODO: late => skip frame
} else {
try {
Thread.sleep((this.DESIRED_DELTA_LOOP - deltaLoop)/(1000*1000));
} catch (InterruptedException e ) {
}
}
}
}
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;
synchronized (this.pauseLock) {
this.pauseLock.notifyAll();
}
}
// automagically set the canvas size to the parent's size
// WARNING: does not always work
// TODO: why doesn't this always work?
public synchronized void updateCanvasSize() {
this.canvasSize = this.getParent().getSize();
}
// this one always works
public synchronized void updateCanvasSize(Dimension newSize) {
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) {}
@Override
public void keyReleased(KeyEvent e) {
// TODO: Escape pauses the game
}
/* mouse listener */
@Override public void mouseClicked(MouseEvent e) {}
@Override public void mouseEntered(MouseEvent e) {}
@Override public void mouseExited(MouseEvent e) {}
@Override public void mousePressed(MouseEvent e) {}
@Override public void mouseReleased(MouseEvent e) {}
@Override public void mouseDragged(MouseEvent e) {}
@Override public void mouseMoved(MouseEvent e) {}
@Override public void mouseWheelMoved(MouseWheelEvent e) {}
/* component listener */
@Override
public void componentResized(ComponentEvent e) {
this.updateCanvasSize();
}
@Override public void componentMoved(ComponentEvent e) {}
@Override public void componentShown(ComponentEvent e) {}
@Override public void componentHidden(ComponentEvent e) {}
}
|