2020-05-09 13:32:43 +00:00
|
|
|
package tins2020
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/veandco/go-sdl2/sdl"
|
|
|
|
)
|
|
|
|
|
|
|
|
type FPS struct {
|
|
|
|
ControlBase
|
|
|
|
|
2020-05-11 09:44:50 +00:00
|
|
|
Show *bool
|
2020-05-09 13:32:43 +00:00
|
|
|
start time.Time
|
|
|
|
stamp time.Duration
|
|
|
|
slot int
|
|
|
|
ticks []int
|
|
|
|
total int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FPS) Init(*Context) error {
|
|
|
|
f.start = time.Now()
|
|
|
|
f.stamp = 0
|
|
|
|
f.ticks = make([]int, 51)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FPS) Render(ctx *Context) {
|
2020-05-11 09:44:50 +00:00
|
|
|
if f.Show == nil || !*f.Show {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-09 13:32:43 +00:00
|
|
|
elapsed := time.Since(f.start)
|
|
|
|
stamp := elapsed / (20 * time.Millisecond)
|
|
|
|
for f.stamp < stamp {
|
|
|
|
f.total += f.ticks[f.slot]
|
|
|
|
f.slot = (f.slot + 1) % len(f.ticks)
|
|
|
|
f.total -= f.ticks[f.slot]
|
|
|
|
f.ticks[f.slot] = 0
|
|
|
|
f.stamp++
|
|
|
|
}
|
|
|
|
f.ticks[f.slot]++
|
|
|
|
|
|
|
|
font := ctx.Fonts.Font("debug")
|
2020-05-10 15:16:18 +00:00
|
|
|
font.RenderCopy(ctx.Renderer, fmt.Sprintf("FPS: %d", f.total), Pt(5, 17), sdl.Color{R: 255, G: 255, B: 255, A: 255})
|
2020-05-09 13:32:43 +00:00
|
|
|
}
|