-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoader.java
184 lines (156 loc) · 4.43 KB
/
Loader.java
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
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
/**
* Implements the initialize, tick, render, and input methods of a specified
* Controller. It ensures that the tick/render loop operates on an individual
* Thread, that a JFrame is constructed, and that this component is linked to
* the JFrame and appropriate input listeners.
*
* @author Justin C
*/
public class Loader extends Canvas implements Runnable
{
/**
* The thread that will manage the execution of the tick/render methods.
*/
private Thread thread;
/**
* Stores whether or not the tick/render loop should continue running.
*/
private boolean running;
/**
* The Controller whose initialize, tick, render, and input methods will be
* called.
*/
private Controller control;
/**
* The number of rendering operations that have been successfully executed
* within the last second.
*/
private int frameRate;
/**
* The number of nanoseconds that are intended to elapse per tick.
*/
private double nsPerTick;
/**
* Calls control's initialize() method, adds all input listeners, ensures that
* all events are visible to this component, constructs an unadjustable
* JFrame, links the JFrame to this component, and starts the tick/render
* loop.
*/
public Loader(int width, int height, String title, double tickPerSec, Controller c)
{
nsPerTick = 1000000000 / tickPerSec;
control = c;
control.initialize();
addKeyListener(control);
addMouseListener(control);
addMouseMotionListener(control);
addMouseWheelListener(control);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
JFrame frame = new JFrame(title);
// The buffer of (6, 40) is due to the way
// the screen is drawn on the window.
Dimension d = new Dimension(width, height);
frame.setPreferredSize(d);
frame.setMinimumSize(d);
frame.setMaximumSize(d);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(this);
start();
}
/**
* Executes run() on a new Thread and enables the continual iteration of the
* tick/render loop.
*/
public synchronized void start()
{
thread = new Thread(this);
thread.start();
running = true;
}
/**
* Terminates the current Thread and prevents any further iterations of the
* tick/render loop.
*/
public synchronized void stop()
{
try
{
thread.join();
running = false;
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* Automatically executes on "thread".
*
* ticks:
* the number of ticks that must be called before the program proceeds.
*
* frames:
* The number of times that the program has rendered in this iteration.
*/
public void run()
{
double ticks = 0;
int frames = 0;
long lastNano = System.nanoTime();
long lastMill = System.currentTimeMillis();
while (running)
{
long nowNano = System.nanoTime();
double ticksElapsed = (nowNano - lastNano) / nsPerTick;
lastNano = nowNano;
ticks += ticksElapsed;
while (ticks >= 1)
{
control.tick(frameRate);
ticks--;
}
render();
frames++;
// Restarts the frame count and updates the framerate every second.
long nowMill = System.currentTimeMillis();
if (nowMill - lastMill >= 1000)
{
lastMill += 1000;
frameRate = frames;
frames = 0;
}
}
// Allows "running = false" to immediately terminate this thread.
stop();
}
/**
* Creates a buffer for the program's graphics and passes it, along with the
* frameCount, to central.render. Proceeds to release the system resources
* used by g and make the buffer strategy visible.
*
* Code attributed to:
* RealTutsGML at "https://www.youtube.com/user/RealTutsGML"
*/
private void render()
{
BufferStrategy bs = getBufferStrategy();
if (bs == null)
{
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
control.render(g, frameRate);
g.dispose();
bs.show();
}
}