-
Notifications
You must be signed in to change notification settings - Fork 5
/
fps.js
36 lines (30 loc) · 923 Bytes
/
fps.js
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
var gamejs = require('gamejs');
var conf = require('./config');
exports.FpsDisplay = function() {
// fps counter
var lastDurations = [];
var fpsFont = new gamejs.font.Font();
var fpsWarningFont = new gamejs.font.Font('20px monospace');
var fpsAvg = 99;
this.lowCPU = false;
this.update = function(msDuration) {
// fps
lastDurations.push(msDuration);
if (lastDurations.length > 60) {
var sum = 0;
lastDurations = lastDurations.splice(0, 10);
lastDurations.forEach(function(ld) {
sum += ld;
});
fpsAvg = Math.ceil(1000 / (sum / lastDurations.length));
}
};
this.draw = function(display) {
if (fpsAvg < 14) this.lowCPU = true;
if (this.lowCPU) {
display.blit(fpsFont.render('Low CPU mode! ' + fpsAvg, '#ff33ff'), [conf.SCREEN_WIDTH - 100, 5]);
}
return;
};
return this;
};