Skip to content

Commit c730a1d

Browse files
committed
Add helper class ValueAverageOverTime
1 parent 0666815 commit c730a1d

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

src/label-draw.ts

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,39 @@ export class IdLabelDrawClass implements LabelDrawClass {
1616
}
1717
}
1818

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+
1940
export class FpsLabelDrawClass implements LabelDrawClass {
41+
private avg = new ValueAverageOverTime(60)
2042
private lastDrawTime: number = 0
21-
private frameAvgCount: number = 60
22-
private lastFramesAvg: number = 0
23-
private lastFrames: number[] = []
2443

2544
draw(y: number) {
2645
if (!instanceinator.displayFps) return y
2746
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()
3652

3753
const text = new ig.TextBlock(sc.fontsystem.font, `${fps.round(0)} fps`, {})
3854

0 commit comments

Comments
 (0)