-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofiler.go
42 lines (36 loc) · 891 Bytes
/
profiler.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
package main
import (
glfw "github.com/go-gl/glfw3"
"github.com/jimarnold/gltext"
)
type Profiler struct {
frameSpeed float64
frameTimes []float64
frame int
lastTime float64
font *gltext.Font
}
func NewProfiler(font *gltext.Font) *Profiler {
return &Profiler{frameTimes: make([]float64, 256), frameSpeed: 0, frame: 0, font: font}
}
func (this *Profiler) start() {
this.lastTime = glfw.GetTime()
}
func (this *Profiler) update() {
frameCount := len(this.frameTimes)
now := glfw.GetTime()
this.frameTimes[this.frame%frameCount] = now - this.lastTime
movingAverage := 0.0
for _, f := range this.frameTimes {
movingAverage += f
}
movingAverage /= float64(frameCount)
if this.frame%frameCount == 0 {
this.frameSpeed = movingAverage
}
this.lastTime = now
this.frame++
}
func (this *Profiler) render() {
this.font.Printf(1.7, 0, "%f", this.frameSpeed)
}