-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFPSmeter.java
executable file
·109 lines (88 loc) · 2.92 KB
/
FPSmeter.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
package jToolkit4FixedPipeline.common;
import org.lwjgl.Sys;
/**
*
* @author Astemir Yeleev
*/
public class FPSmeter {
private long lastFrame; // time at last frame
private int fps; // frames per second
private long lastFPS; //last fps time
private long lastFrameTime; // used to calculate delta
private int maxFPS;
private int minFPS;
private int avgFPS;
private long countOfFrames;
private long t0, t1;
private int frames;
public FPSmeter() {
this.maxFPS = -100;
this.minFPS = 100;
}
/**
* Calculate the FPS and set it in the title bar
*/
public void updateFPS() {
if (getTime() - lastFPS > 1000) {
System.out.println("FPS: " + fps + '\t' + "MinFPS: " + this.minFPS + '\t' + "MaxFPS: " + maxFPS +
'\t' + "Total frames: " + this.countOfFrames);
fps = 0;
lastFPS += 1000;
if (fps > this.maxFPS) {
this.maxFPS = fps;
} else if (fps < this.minFPS) {
this.minFPS = fps;
}
}
fps++;
this.countOfFrames++;
}
/**
* Get the accurate system time
* @return The system time in milliseconds
*/
public long getTime() {
return (Sys.getTime() * 1000) / Sys.getTimerResolution();
}
public void fpsMeter () {
this.frames++;
this.countOfFrames++;
this.t1 = System.currentTimeMillis();
if (this.t1 - this.t0 >= 1000) {
this.fps = this.frames;
this.t0 = this.t1;
this.frames = 0;
// this.minFPS = fps;
if (this.fps > this.maxFPS) {
this.maxFPS = this.fps;
}
else if (this.fps < this.minFPS) {
this.minFPS = this.fps;
}
this.avgFPS = (this.maxFPS + this.minFPS + this.fps) / 3;
System.out.println("FPS: " + this.fps + '\t' + "MinFPS: " + this.minFPS +
'\t' + "MaxFPS: " + maxFPS + '\t' + "Avg FPS: " +
this.avgFPS + '\t' + "Total frames: " + this.countOfFrames);
}
}
/**
* Calculate how many milliseconds have passed since last frame.
*
* @return milliseconds passed since last frame
*/
public int getDelta() {
long time = (Sys.getTime() * 1000) / Sys.getTimerResolution();
int delta = (int) (time - lastFrameTime);
lastFrameTime = time;
return delta;
}
public long getLastFPS() {
return lastFPS;
}
public void setLastFPS(long lastFPS) {
this.lastFPS = lastFPS;
}
public int getFps() {
return fps;
}
}