-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathJavaGameEngine.java
More file actions
120 lines (97 loc) · 3.46 KB
/
Copy pathJavaGameEngine.java
File metadata and controls
120 lines (97 loc) · 3.46 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
package javagameengine;
import javagameengine.components.Component;
import javagameengine.input.Input;
import javagameengine.input.Keys;
import javagameengine.msc.Vector2;
import javax.swing.*;
import java.awt.*;
import java.util.LinkedList;
public class JavaGameEngine{
public static JavaGameEngine gameInstance;
private static boolean newScene = false;
public static int DELAY = 5;
/**this is the JPanel that is rendering our scenes and retrieving inputs*/
public static final GameWorld gameWorld = new GameWorld();
/** the scene that is renderned and updated*/
static Scene selectedScene = new Scene();
public static Vector2 g = new Vector2(0,0.03982f);
public static Vector2 size = new Vector2(720,500);
public static JFrame gameWindow = new JFrame();
/**return selected scene*/
public static Scene getSelectedScene() {
return selectedScene;
}
/** sets the scene that is inputed and sets it to the game world*/
public static void setSelectedScene(Scene selectedScene) {
newScene = true;
gameWorld.remove(getSelectedScene());
selectedScene.startScene();
gameWorld.add(selectedScene);
JavaGameEngine.selectedScene = selectedScene;
}
public JavaGameEngine() {
}
/**
* Time since last update (ms)
*/
public static double deltaTime = 0;
private static double prevTime = ((double)System.currentTimeMillis());
private static double time = System.currentTimeMillis();
/**
* this is the amount of frames drawn every second
*/
public static float fps = 5;
private static float counter = 5;
/**
* This caps the amount of frames drawn in a second (0 = uncapped)
*/
public static float fpsCap = 0;
public static Vector2 getWindowSize(){
return new Vector2(gameWindow.getSize().width,gameWindow.getSize().height);
}
private static void update(){
double now = ((double)System.currentTimeMillis());
//Increases counter every tick but when a 1/10 of a second we reset the counter
//and sets the fps to the counter
// To cap the fps we just increase the delay if our fps is too high
// and decrease it when it is too low
if(now-time>=100){
fps = counter*10;
gameWindow.setTitle("FPS "+String.valueOf(fps));
if(fpsCap > 0 && fps>fpsCap) DELAY++;
if(fpsCap > 0 && fps<fpsCap && DELAY>5) DELAY--;
counter = 0;
time = now;
}
counter++;
// delta time is the time from previous frame (tick speed)
deltaTime = (now-prevTime)/10;
prevTime = now;
try {
Thread.sleep(DELAY);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
selectedScene.update();
gameWindow.repaint();
//For linux
Toolkit.getDefaultToolkit().sync();
Input.setScrollValue(0);
if(newScene){
gameWindow.validate();
selectedScene.start();
newScene = false;
}
}
public static void start(){
//Set som basic properties
gameWindow.setSize((int) size.getX(), (int) size.getY());
gameWindow.setContentPane(gameWorld);
gameWindow.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
gameWindow.setVisible(true);
selectedScene.start();
while(true){
update();
}
}
}