-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameRunner.java
More file actions
381 lines (310 loc) · 8.33 KB
/
GameRunner.java
File metadata and controls
381 lines (310 loc) · 8.33 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.*;
import java.awt.image.*;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.awt.geom.Point2D;
public class GameRunner extends JFrame implements WindowListener, KeyListener, MouseListener, MouseMotionListener, Runnable
{
private GraphicsDevice gd = null; //Needed for fullscreen mode.
private BufferStrategy strategy = null; // Needed for double buffering.
private Thread thread;
private boolean shouldStop;
private int fps = 20;
private long period;
private double effectiveFPS;
private double effectiveUPS;
//locking object used to synchronize event dispatch thread and game thread
private Object lock = new Object();
/*SCREEN SCALING PART 1*/
private int desiredWidth = 1280;
private int desiredHeight = 1024;
private AffineTransform screenScale;
private AffineTransform inverseScale = new AffineTransform();
/*END PART 1*/
private SnakeScreen screen;
public static void main(String[] args)
{
new GameRunner();
}
public GameRunner() {
super(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration());
setIgnoreRepaint(true);
try {
init();
}
catch (Exception ex) {
ex.printStackTrace();
exit();
}
}
/**
* Called by the constructors, this initializes fields and
* sets up the Graphics environment for display.
* @throws Exception
*/
private void init() throws Exception {
//Set window properties
setTitle("Game Window");
setFocusTraversalKeysEnabled(false);
setResizable(false);
setIgnoreRepaint(true);
setUndecorated(true);
//Set the fullscreen
gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
if (!gd.isFullScreenSupported())
throw new Exception("Fullscreen is not supported");
/*SCREEN SCALING PART 2*/
gd.setFullScreenWindow(this);
pickBestDisplayMode();
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
setSize(d);
/*END PART 2*/
setLayout(null);
//Add action all of the listeners
addKeyListener(this);
addWindowListener(this);
addMouseListener(this);
addMouseMotionListener(this);
setFocusable(true);
requestFocus();
//Show the screen and make the buffers
setVisible(true);
createBufferStrategy(2);
strategy = getBufferStrategy();
dataInit();
createThread();
startThread();
}
/*SCREEN SCALING PART 3*/
private void pickBestDisplayMode() {
DisplayMode oldMode = gd.getDisplayMode();
try
{
gd.setDisplayMode(new DisplayMode(desiredWidth, desiredHeight, 32, DisplayMode.REFRESH_RATE_UNKNOWN));
}
catch(Exception e)
{
gd.setDisplayMode(oldMode);
int currentWidth = oldMode.getWidth();
int currentHeight = oldMode.getHeight();
double widthRatio = currentWidth / (double)desiredWidth;
double heightRatio = currentHeight / (double)desiredHeight;
double scaleFactor = 1.0;
double xTrans = 0;
double yTrans = 0;
if(desiredWidth <= currentWidth && desiredHeight <= currentHeight)
{
scaleFactor = 1.0;
}
else if(desiredWidth >= currentWidth && desiredHeight >= currentHeight) {
scaleFactor = Math.min(widthRatio, heightRatio);
}
else if(desiredWidth >= currentWidth) {
scaleFactor = widthRatio;
}
else //desiredHeight > height
{
scaleFactor = heightRatio;
}
xTrans = (currentWidth - desiredWidth*scaleFactor)/(2 * scaleFactor);
yTrans = (currentHeight - desiredHeight*scaleFactor)/(2 * scaleFactor);
screenScale = AffineTransform.getScaleInstance(scaleFactor, scaleFactor);
screenScale.translate(xTrans, yTrans);
try {
inverseScale = screenScale.createInverse();
}
catch (Exception exc) {
exc.printStackTrace();
}
}
}
/*END PART 3*/
public void dataInit()
{
screen = new SnakeScreen(desiredWidth, desiredHeight);
}
public void setFPS(int f)
{
synchronized(lock)
{
fps = f;
period = (long)((1000.0/fps) * 1000000L);
}
}
public void windowClosing(WindowEvent e) {
exit();
}
public void windowActivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e) {}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e)
{
Point2D p = inverseScale.transform(e.getPoint(), null);
}
public void mouseDragged(MouseEvent e)
{
Point2D p = inverseScale.transform(e.getPoint(), null);
}
public void mouseMoved(MouseEvent e)
{
Point2D p = inverseScale.transform(e.getPoint(), null);
}
public void mouseReleased(MouseEvent e)
{
Point2D p = inverseScale.transform(e.getPoint(), null);
}
public void mousePressed(MouseEvent e)
{
Point2D p = inverseScale.transform(e.getPoint(), null);
}
//quits game when escape is pressed
public void keyPressed(KeyEvent e)
{
int code = e.getKeyCode();
if(code == KeyEvent.VK_ESCAPE)
exit();
screen.keyPressed(e);
}
public void render(Graphics g)
{
screen.render(g);
}
public void update()
{
screen.update();
}
public void exit() {
stopThread();
if (gd != null)
gd.setFullScreenWindow(null);
dispose();
System.exit(0);
}
public void run()
{
long beforeTime, afterTime, timeDiff, sleepTime;
long overSleepTime = 0L;
int noDelays = 0;
long excess = 0L;
synchronized(lock)
{
period = (long)((1000.0/fps) * 1000000L);
}
final int NO_DELAYS_PER_YIELD = 16;
final int MAX_FRAME_SKIPS = 5;
beforeTime = System.nanoTime();
long time = System.nanoTime();
long frameCounter = 0;
long updateCounter = 0;
while(!shouldStop)
{
synchronized (lock) {
update();
updateCounter++;
paintScreen();
afterTime = System.nanoTime();
timeDiff = afterTime - beforeTime;
sleepTime = (period - timeDiff) - overSleepTime;
}
if(sleepTime > 0)
{
try
{
TimeUnit.NANOSECONDS.sleep(sleepTime);
}
catch(Exception e)
{
}
overSleepTime = (System.nanoTime() - afterTime) - sleepTime;
}
else
{
excess -= sleepTime;
overSleepTime = 0L;
noDelays++;
if(noDelays >= NO_DELAYS_PER_YIELD)
{
Thread.yield();
noDelays = 0;
}
}
synchronized (lock)
{
int skips = 0;
while(excess > period && skips < MAX_FRAME_SKIPS)
{
excess -= period;
update();
updateCounter++;
skips++;
}
}
frameCounter++;
beforeTime = System.nanoTime();
if(beforeTime - time > 1000000000L) //1 second has passed
{
effectiveFPS = (double)frameCounter * 1000000000L / (beforeTime - time);
effectiveUPS = (double)updateCounter * 1000000000L / (beforeTime - time);
frameCounter = 0;
updateCounter = 0;
time = beforeTime;
}
}
thread = null;
}
private void paintScreen()
{
if(strategy == null)
return;
Graphics g = strategy.getDrawGraphics();
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
Graphics2D g2 = ((Graphics2D)g);
g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
g2.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_SPEED);
g2.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_SPEED);
g2.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE);
if(screenScale != null) {
((Graphics2D)g).transform(screenScale);
g.setClip(0, 0, desiredWidth, desiredHeight);
}
render(g);
strategy.show();
}
private synchronized void createThread()
{
if(thread == null)
{
thread = new Thread(this);
shouldStop = true;
}
}
private synchronized void startThread()
{
if(thread != null && shouldStop == true)
{
shouldStop = false;
thread.start();
}
}
private synchronized void stopThread()
{
shouldStop = true;
}
public synchronized boolean isRunning()
{
return thread != null && shouldStop == false;
}
}