@@ -16,23 +16,39 @@ export class IdLabelDrawClass implements LabelDrawClass {
16
16
}
17
17
}
18
18
19
+ export class ValueAverageOverTime {
20
+ private values : number [ ] = [ ]
21
+ private avg : number = 0
22
+
23
+ constructor ( public valueInterval : number ) { }
24
+
25
+ pushValue ( v : number ) {
26
+ this . avg += v / this . valueInterval
27
+ if ( this . values . length >= this . valueInterval ) {
28
+ this . avg -= this . values [ 0 ] / this . valueInterval
29
+ this . values . splice ( 0 , 1 )
30
+ }
31
+ this . values . push ( v )
32
+ }
33
+
34
+ getAverage ( ) : number {
35
+ if ( this . values . length == this . valueInterval ) return this . avg
36
+ return this . avg * ( this . valueInterval / this . values . length )
37
+ }
38
+ }
39
+
19
40
export class FpsLabelDrawClass implements LabelDrawClass {
41
+ private avg = new ValueAverageOverTime ( 60 )
20
42
private lastDrawTime : number = 0
21
- private frameAvgCount : number = 60
22
- private lastFramesAvg : number = 0
23
- private lastFrames : number [ ] = [ ]
24
43
25
44
draw ( y : number ) {
26
45
if ( ! instanceinator . displayFps ) return y
27
46
const time = Date . now ( )
28
- const timeDiff = time - this . lastDrawTime
29
- this . lastFramesAvg += timeDiff / this . frameAvgCount
30
- if ( this . lastFrames . length >= this . frameAvgCount ) {
31
- this . lastFramesAvg -= this . lastFrames [ 0 ] / this . frameAvgCount
32
- this . lastFrames . splice ( 0 , 1 )
33
- }
34
- this . lastFrames . push ( timeDiff )
35
- const fps = 1000 / this . lastFramesAvg
47
+
48
+ let timeDiff = time - this . lastDrawTime
49
+ if ( this . lastDrawTime == 0 ) timeDiff = 0
50
+ this . avg . pushValue ( timeDiff )
51
+ const fps = 1000 / this . avg . getAverage ( )
36
52
37
53
const text = new ig . TextBlock ( sc . fontsystem . font , `${ fps . round ( 0 ) } fps` , { } )
38
54
0 commit comments