-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
310 lines (269 loc) · 8.82 KB
/
Main.java
File metadata and controls
310 lines (269 loc) · 8.82 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
/**
* @author Edgar Quillion <edgarquill@gmail.com>
* @version Version 1
* @since 1.6
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
public class Main implements Runnable
{
// FULL SCREEN OR NOT
public static boolean FULL_SCREEN = false;
// SCREEN MEASUREMENTS AND REFRESH
private static final int WIDTH = 640;
private static final int HEIGHT = 480;
private long desiredFPS = 60;
// NAME OF WINDOW
private String title = "Base";
// GAME OBJECT INIT
private Game game;
/*
* Initialise all of your Objects in here
* In our case we have our game object where we will do this
*/
public void setup()
{
game = new Game(WIDTH, HEIGHT);
}
/*
* Method that handles all the movements that are supposed to happen
* So initialise all the x and y changes here
* deltaTime tells you how many nanoseconds have passed by since this function
* was last called
*/
protected void update(int deltaTime)
{
game.update();
}
/*
* Method where you paint to the screen
*/
protected void render(Graphics2D g)
{
game.draw(g);
}
/*
* Keyboard Action listeners
* Key esc closes the program by default
*/
private class KeyInputHandler extends KeyAdapter
{
public void keyPressed(KeyEvent e)
{
// USER PRESSED ESC
if(e.getKeyCode()==27)
{
// IF WE ARE FULL SCREEN IT IS BETTER FOR US TO CLEAR THINGS UP
if(FULL_SCREEN)
{
graphicsDevice.setDisplayMode(originalDisplayMode);
graphicsDevice.setFullScreenWindow(null);
}
System.exit(0);
}
// TELL GAME OBJECT WHAT WE PRESSED
game.keyPressed(e);
}
public void keyReleased(KeyEvent e)
{
// TELL GAME OBJECT WHAT KEY WAS RELEASED
game.keyReleased(e);
}
}
/*
* Main game loop
*/
public void run()
{
// INITIALIZING VALUES FOR TIME CALCULATIONS
long beginLoopTime;
long endLoopTime;
long currentUpdateTime = System.nanoTime();
long lastUpdateTime;
long deltaLoop;
// SETUP ANYTHING WE NEED BEFORE WE START RUNNING THE GAME
setup();
// START THE GAME THIS IS OUR GAME LOOP. RUNNING WILL NEVER BE FALSE I HAD THIS HERE BEFORE WHEN I HAD LEVELS
// BUt LONG SINCE MOVED IT TO THE GAME OBJECT
while(running)
{
// WE TAKE THE TIME
beginLoopTime = System.nanoTime();
// DRAW WHAtEVER WE HAVE TO
render();
// THIS CALCULATION IS NOT CRUCIAL REALLY AT THE SCALE WE ARE DEALING WITH BUT THIS IS FOR FUTURE
lastUpdateTime = currentUpdateTime;
currentUpdateTime = System.nanoTime();
update((int) ((currentUpdateTime - lastUpdateTime)/(1000*1000)));
// SEE HOW LONG IT TOOK US TO DO UPDATE AND DRAWING
endLoopTime = System.nanoTime();
deltaLoop = endLoopTime - beginLoopTime;
// IF WE ENTER THIS IF STATEMENT THEN WE ARE KIND OF SCREWED OUR GAME LAGS
if(deltaLoop > desiredDeltaLoop)
{
//Do nothing. We are already late. I honestly don't even know why I have this if here :)
}
// WE UPDATED AND DREW THINGS QUICK ENOUGH SO THEREFORE WE WILL WAIT IN ORDER TO MAKE THE GAME SMOOTH
else
{
try
{
Thread.sleep((desiredDeltaLoop - deltaLoop)/(1000*1000));
}
catch(InterruptedException e)
{
//Do nothing, is it even possible for me to fail at putting thread to sleep?
}
}
}
}
/*
* Method to initialise graphics that are going to be painted to the screen
*/
private void render()
{
// CREATE THE GRAPHICS
Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
// MAKE THE PICTURE WHERE WE WILL PAINT EVERYTHING AT ONCE
g.clearRect(0, 0, WIDTH, HEIGHT);
// PAINT ANYTHING WE NEED HERE
render(g);
g.dispose();
// SHOW THE WHOLE IMAGE RATHER THAN PAININT ONE OBJECT AT A TIME
bufferStrategy.show();
}
/*
* Mouse Action Listeners
*/
private class MouseControl extends MouseAdapter
{
public void mouseEntered(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
}
}
/*
* Mouse Motion Listeners
*/
private class MouseMotion extends MouseMotionAdapter
{
public void mouseMoved(MouseEvent e)
{
}
}
/*
* public static void main
* I do not even have to explain this
*/
public static void main(String [] args)
{
Main ex = new Main();
new Thread(ex).start();
}
// WILL HAVE THREE DISPLAY MODES, 32 bit, 16bit and 8 bit
private static DisplayMode MODES[] = new DisplayMode[]
{
new DisplayMode(WIDTH, HEIGHT, 32, 0), new DisplayMode(WIDTH, HEIGHT, 16, 0),
new DisplayMode(WIDTH, HEIGHT, 8, 0)
};
/*
* Let's see which display mode is suitable for our computer
*/
private static DisplayMode getBestDisplayMode(GraphicsDevice device)
{
// LOOP THROUGH 32 THEN 16 THEN 8 BIT DISPLAY MODES AND SEE WHICH IS THE FIRST ONE WE CAN HANDLE
for (int x = 0, xn = MODES.length; x < xn; x++)
{
DisplayMode[] modes = device.getDisplayModes();
for (int i = 0, in = modes.length; i < in; i++)
{
if (modes[i].getWidth() == MODES[x].getWidth() && modes[i].getHeight() == MODES[x].getHeight() && modes[i].getBitDepth() == MODES[x].getBitDepth())
{
return MODES[x];
}
}
}
return null;
}
// INITIALIZING SOME FRAME RATE THINGS
private long desiredDeltaLoop = (1000*1000*1000)/desiredFPS;
private boolean running = true;
// INITIALIZING SOME JAVA THINGS
private JFrame frame;
private Canvas canvas;
private BufferStrategy bufferStrategy;
private GraphicsDevice graphicsDevice;
private DisplayMode originalDisplayMode;
/*
* Main thread
*/
public Main()
{
// REMOVE ThIS If YOU DO NOT WANT TO ASK FOR SCREEN MODES
/* No full screen asking anymore, this annoys me
int answer = JOptionPane.showConfirmDialog(null, "Do you want to play in full screen", "Full screen?", 0);
if(answer == 0)
{
FULL_SCREEN = true;
}
else if(answer == 1)
{
FULL_SCREEN = false;
}
else
{
System.exit(0);
}
*/
// SET UP THE WINDOW AND GRAPHICS
GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
graphicsDevice = graphicsEnvironment.getDefaultScreenDevice();
originalDisplayMode = graphicsDevice.getDisplayMode();
// CREATE NEW Frame
frame = new JFrame(title);
if(FULL_SCREEN)
{
frame.setUndecorated(true);
graphicsDevice.setFullScreenWindow(frame);
if (graphicsDevice.isDisplayChangeSupported())
{
graphicsDevice.setDisplayMode(getBestDisplayMode(graphicsDevice));
}
}
else
{
frame.setSize(WIDTH, HEIGHT);
}
Rectangle bounds = frame.getBounds();
// CREATE NEW JPanel WITH SPECIFIED WIDTH AND HEIGHT
JPanel panel = (JPanel) frame.getContentPane();
// I had to add the -9 to readjust window size
panel.setPreferredSize(new Dimension(WIDTH-9, HEIGHT-9));
panel.setLayout(null);
// CREATE A Canvas INSIDE JPanel WITH SPECIFIED WIDTH AND HEIGHT
canvas = new Canvas();
// once again I add 1 because java is stupid that's why
canvas.setBounds(0, 0, WIDTH+1, HEIGHT+1);
//canvas.setBounds(bounds);
canvas.setIgnoreRepaint(true);
panel.add(canvas);
// ADD LISTENERS
canvas.addKeyListener(new KeyInputHandler());
canvas.addMouseListener(new MouseControl());
canvas.addMouseMotionListener(new MouseMotion());
// INITIALIZE THE WINDOW
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
// SET GRAPHICS DEVICE
canvas.createBufferStrategy(2);
bufferStrategy = canvas.getBufferStrategy();
// FOCUS ON THE WINDOW
canvas.requestFocus();
}
}