-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
56 lines (46 loc) · 1.77 KB
/
Copy pathMain.java
File metadata and controls
56 lines (46 loc) · 1.77 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
import static java.lang.System.currentTimeMillis;
import javax.swing.JFrame;
public class Main {
public static void main(String[] args) throws InterruptedException {
int frameCount = 0;
JFrame frame = new JFrame();
frame.setSize(1280, 1280);
frame.setTitle("Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setIgnoreRepaint(true);
Engine engine = new Engine();
frame.add(engine);
double physicsStepLength = 0.01;
double currentTime = millisToSeconds(currentTimeMillis());
double accumulator = 0.0;
// note, the program *will* take up 20% of your CPU if you don't have this at a reasonable value
double maxFPS = 120.0;
frame.setVisible(true);
try {
for (int i = 0; i < 0; i++) {
engine.update(physicsStepLength);
}
while (true) {
double newTime = millisToSeconds(currentTimeMillis());
double frameTime = newTime - currentTime;
if ( frameTime > 0.25 )
frameTime = 0.25;
currentTime = newTime;
accumulator += frameTime;
while (accumulator >= physicsStepLength) {
engine.update(physicsStepLength);
accumulator -= physicsStepLength;
//accumulator = 0; // one physics step per frame good for debugging
}
frame.repaint();
frameCount++;
Thread.sleep((long)(1000.0/maxFPS - frameTime));
}
} catch (Exception e) {
System.out.println(e);
}
}
public static double millisToSeconds(long time) {
return time/1000.0;
}
}