-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
60 lines (53 loc) · 2.05 KB
/
Copy pathMain.java
File metadata and controls
60 lines (53 loc) · 2.05 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
import blackjack.game.GameStatistics;
import blackjack.ui.BlackjackFrame;
import javax.swing.SwingUtilities;
import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* Entry point: starts the render loop on a background thread and shows the Swing UI on the EDT.
*/
public final class Main implements Runnable {
private static final int TARGET_FPS = 100;
private static final long NANOS_PER_SECOND = 1_000_000_000L;
private final BlackjackFrame frame;
private final AtomicBoolean applicationRunning;
private long lastFrameNanos = System.nanoTime();
private Main(BlackjackFrame frame, AtomicBoolean applicationRunning) {
this.frame = frame;
this.applicationRunning = applicationRunning;
}
public static void main(String[] args) {
GameStatistics statistics = new GameStatistics();
AtomicBoolean applicationRunning = new AtomicBoolean(true);
BlackjackFrame[] frameHolder = new BlackjackFrame[1];
try {
SwingUtilities.invokeAndWait(() ->
frameHolder[0] = new BlackjackFrame(statistics, applicationRunning));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
} catch (InvocationTargetException e) {
Throwable cause = e.getCause();
if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
}
if (cause instanceof Error) {
throw (Error) cause;
}
throw new RuntimeException(cause);
}
Main loop = new Main(frameHolder[0], applicationRunning);
new Thread(loop, "blackjack-render-loop").start();
}
@Override
public void run() {
while (applicationRunning.get()) {
long now = System.nanoTime();
if (now - lastFrameNanos >= NANOS_PER_SECOND / TARGET_FPS) {
frame.tick();
frame.repaint();
lastFrameNanos = now;
}
}
}
}