-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfps.go
52 lines (41 loc) · 930 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
50
51
52
package banana
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
accumTime time.Duration
lastFPS float64
updatePeriod time.Duration
}
func newFPSCounter() *fpsCounter {
return &fpsCounter{
lastTime: time.Now(),
updatePeriod: time.Second, // Update FPS every second
}
}
func (f *fpsCounter) Frame() {
f.frameCount++
currentTime := time.Now()
elapsedTime := currentTime.Sub(f.lastTime)
f.accumTime += elapsedTime
f.lastTime = currentTime
if f.accumTime >= f.updatePeriod {
f.lastFPS = float64(f.frameCount) / f.accumTime.Seconds()
f.accumTime = 0
f.frameCount = 0
}
}
func (f *fpsCounter) GetFPS() float64 {
return f.lastFPS
}