Sander Schobers
b85ac17d8a
Moveds fonts to separate directory & added corresponding license texts. Moved console & fps to separte gut (game utility) package. Added console (view only).
61 lines
967 B
Go
61 lines
967 B
Go
package gut
|
|
|
|
import "time"
|
|
|
|
type FPS struct {
|
|
done chan struct{}
|
|
cnt chan struct{}
|
|
|
|
curr int
|
|
|
|
total int
|
|
frame int
|
|
frames []int
|
|
i int
|
|
}
|
|
|
|
func NewFPS() *FPS {
|
|
fps := &FPS{
|
|
done: make(chan struct{}),
|
|
cnt: make(chan struct{}, 100),
|
|
curr: 0, // to display
|
|
total: 0, // sum of frames
|
|
frame: 0, // current frame
|
|
frames: make([]int, 20), // all frames
|
|
i: 0, // frame index
|
|
}
|
|
go fps.count()
|
|
return fps
|
|
}
|
|
|
|
func (f *FPS) count() {
|
|
ticker := time.NewTicker(50 * time.Millisecond)
|
|
for {
|
|
select {
|
|
case <-f.done:
|
|
return
|
|
case <-f.cnt:
|
|
f.frame++
|
|
case <-ticker.C:
|
|
f.total -= f.frames[f.i]
|
|
f.frames[f.i] = f.frame
|
|
f.total += f.frames[f.i]
|
|
f.frame = 0
|
|
f.i = (f.i + 1) % len(f.frames)
|
|
f.curr = f.total
|
|
}
|
|
}
|
|
}
|
|
|
|
func (f *FPS) Count() {
|
|
f.cnt <- struct{}{}
|
|
}
|
|
|
|
func (f *FPS) Current() int {
|
|
return f.curr
|
|
}
|
|
|
|
func (f *FPS) Destroy() {
|
|
close(f.done)
|
|
}
|