krampus19/gut/fps.go
Sander Schobers b85ac17d8a Added sprites & tried out splash animation.
Moveds fonts to separate directory & added corresponding license texts.
Moved console & fps to separte gut (game utility) package.
Added console (view only).
2019-12-20 21:05:23 +01:00

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)
}