Skip to content

Commit ac149a9

Browse files
committed
added right fps
1 parent b8c657d commit ac149a9

File tree

4 files changed

+13
-18
lines changed

4 files changed

+13
-18
lines changed
-340 Bytes
Binary file not shown.

src/JavaGameEngine/Backend/GameWorld.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public class GameWorld extends JPanel{
2323
*/
2424
public static LinkedList<Component> layerList = new LinkedList<>();
2525
public static String fps = "0";
26+
private long last;
27+
2628
public GameWorld() {
2729
/*
2830
Key keyboard inputs
@@ -76,16 +78,21 @@ public void mouseMoved(MouseEvent e) {
7678
a.start();
7779
}
7880
}
79-
81+
float fpsecund = 0;
8082

8183
/**
8284
* Here is the main drawing function
8385
* */
8486
@Override
8587
protected void paintComponent(Graphics g) {
8688
super.paintComponent(g);
87-
8889
drawComponents(g);
90+
if(System.nanoTime()-last>1000000000){
91+
fps = Float.toString(fpsecund);
92+
fpsecund = 0;
93+
last = System.nanoTime();
94+
}
95+
fpsecund+=1;
8996
}
9097
private void drawUi(Graphics g){
9198

src/JavaGameEngine/JavaGameEngine.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public static void start(JFrame frame) {
4949
public static float deltaTime = 0f;
5050

5151
private static int fps = 0;
52+
static float last=0;
53+
5254

5355
private static void startGame(){
5456
frame.setVisible(true);
@@ -73,17 +75,8 @@ public void run() {
7375
// Debug.log(totalElapsed);
7476
GAMEWORLD.repaint();
7577
Toolkit.getDefaultToolkit().sync(); // so it does not lag on linux
76-
float current = System.nanoTime();
77-
deltaTime = current - previous;
78-
previous = current;
79-
totalElapsed += deltaTime;
80-
81-
if((totalElapsed/1000000000)>1){
82-
GameWorld.fps = String.valueOf(fps);
83-
totalElapsed = 0;
84-
fps = 0;
85-
}
86-
fps++;
78+
//System.out.println(System.nanoTime()-last);
79+
8780
}
8881
}, DELAY,DELAY);
8982

src/Testing/Main.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ public static void main(String[] args){
3333

3434
ComponentHandler.addObject(new T());
3535

36-
for(int i = 0;i<10;i++){
37-
Coin coin = new Coin();
38-
coin.setPosition(new Vector2(i*100,420));
39-
ComponentHandler.addObject(coin);
40-
}
4136

4237

4338
start();

0 commit comments

Comments
 (0)