1 | import * as fpsNative from './fps-native';
|
2 | const callbacks = {};
|
3 | let idCounter = 0;
|
4 | let _minFps = 1000;
|
5 | let framesRendered = 0;
|
6 | let frameStartTime = 0;
|
7 | function doFrame(currentTimeMillis) {
|
8 | let fps = 0;
|
9 | if (frameStartTime > 0) {
|
10 |
|
11 | const timeSpan = currentTimeMillis - frameStartTime;
|
12 | framesRendered++;
|
13 | if (timeSpan > 1000) {
|
14 | fps = (framesRendered * 1000) / timeSpan;
|
15 | if (fps < _minFps) {
|
16 | _minFps = fps;
|
17 | }
|
18 | notify(fps);
|
19 | frameStartTime = currentTimeMillis;
|
20 | framesRendered = 0;
|
21 | }
|
22 | }
|
23 | else {
|
24 | frameStartTime = currentTimeMillis;
|
25 | }
|
26 | }
|
27 | let native;
|
28 | function ensureNative() {
|
29 | if (!native) {
|
30 | native = new fpsNative.FPSCallback(doFrame);
|
31 | }
|
32 | }
|
33 | export function reset() {
|
34 | _minFps = 1000;
|
35 | frameStartTime = 0;
|
36 | framesRendered = 0;
|
37 | }
|
38 | export function running() {
|
39 | if (!native) {
|
40 | return false;
|
41 | }
|
42 | return native.running;
|
43 | }
|
44 | export function minFps() {
|
45 | return _minFps;
|
46 | }
|
47 | export function start() {
|
48 | ensureNative();
|
49 | native.start();
|
50 | }
|
51 | export function stop() {
|
52 | if (!native) {
|
53 | return;
|
54 | }
|
55 | native.stop();
|
56 | reset();
|
57 | }
|
58 | export function addCallback(callback) {
|
59 | const id = idCounter;
|
60 |
|
61 | callbacks[id] = zonedCallback(callback);
|
62 | idCounter++;
|
63 | return id;
|
64 | }
|
65 | export function removeCallback(id) {
|
66 | if (id in callbacks) {
|
67 | delete callbacks[id];
|
68 | }
|
69 | }
|
70 | function notify(fps) {
|
71 | let callback;
|
72 | for (const id in callbacks) {
|
73 | callback = callbacks[id];
|
74 | callback(fps, _minFps);
|
75 | }
|
76 | }
|
77 |
|
\ | No newline at end of file |