-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFrameCounter.js
More file actions
87 lines (70 loc) · 1.69 KB
/
FrameCounter.js
File metadata and controls
87 lines (70 loc) · 1.69 KB
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
import * as Pop from './PopEngine.js'
export class Clock
{
constructor(TimeMs,GetLiveTimeMs=Pop.GetTimeNowMs)
{
this.GetLiveTimeMs = GetLiveTimeMs;
this.ClockTimeMs = TimeMs;
this.AppTimeMs = this.GetLiveTimeMs();
this.PausedAppTime = null;
}
get TimeMs()
{
if ( this.PausedAppTime )
return this.ClockTimeMs;
const Elapsed = this.GetLiveTimeMs() - this.AppTimeMs;
return this.ClockTimeMs + Elapsed;
}
Pause()
{
if ( this.PausedAppTime )
return;
// reset our origin time to the current time and we can resume from that again later
this.ClockTimeMs = this.TimeMs;
this.PausedAppTime = this.GetLiveTimeMs();
this.AppTimeMs = null;
}
Resume()
{
if ( !this.PausedAppTime )
return;
this.PausedAppTime = null;
this.AppTimeMs = this.GetLiveTimeMs();
}
}
export default class FrameCounter
{
constructor(CounterName="",LapTimeMs=1000)
{
this.CounterName = CounterName;
this.LapTimeMs = LapTimeMs;
this.LastLapTime = null;
this.Count = 0;
// this can be overloaded, so is a member
this.Report = this.ReportDefault.bind(this);
}
ReportDefault(CountPerSec)
{
Pop.Debug( this.CounterName + " " + CountPerSec.toFixed(2) + "/sec");
}
OnLap()
{
let TimeElapsed = Pop.GetTimeNowMs() - this.LastLapTime;
let Scalar = TimeElapsed / this.LapTimeMs;
let CountPerSec = this.Count / Scalar;
this.Report( CountPerSec );
this.LastLapTime = Pop.GetTimeNowMs();
this.Count = 0;
}
Add(Increment=1)
{
this.Count += Increment;
if ( this.LastLapTime === null )
this.LastLapTime = Pop.GetTimeNowMs();
let TimeElapsed = Pop.GetTimeNowMs() - this.LastLapTime;
if ( TimeElapsed > this.LapTimeMs )
{
this.OnLap();
}
}
}