-
Notifications
You must be signed in to change notification settings - Fork 0
/
fps.go
49 lines (40 loc) · 795 Bytes
/
fps.go
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
package hlg
import (
"time"
)
var (
fpsEnabled = false
)
// EnableFPS enables the FPS counter in the window title.
func EnableFPS() {
fpsEnabled = true
}
// DisableFPS disables the FPS counter in the window title.
func DisableFPS() {
fpsEnabled = false
}
type fpsCounter struct {
frameCount int
lastTime time.Time
lastFPS float64
}
func newFPSCounter() *fpsCounter {
return &fpsCounter{
lastTime: time.Now(),
}
}
func (f *fpsCounter) Reset() {
f.frameCount = 0
f.lastTime = time.Now()
}
func (f *fpsCounter) Frame() {
f.frameCount++
elapsedTime := time.Since(f.lastTime)
if elapsedTime >= time.Second {
f.lastFPS = float64(f.frameCount)
f.Reset()
}
}
func (f *fpsCounter) GetFPS() float64 {
return f.lastFPS
}