42 lines
744 B
Go
42 lines
744 B
Go
package tins2020
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/veandco/go-sdl2/sdl"
|
|
)
|
|
|
|
type FPS struct {
|
|
ControlBase
|
|
|
|
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) {
|
|
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")
|
|
font.RenderCopy(ctx.Renderer, fmt.Sprintf("FPS: %d", f.total), Pt(5, 5), sdl.Color{R: 255, G: 255, B: 255, A: 255})
|
|
}
|