2018-06-17 02:59:23 +00:00
|
|
|
package doodle
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.kirsle.net/apps/doodle/render"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Frames to cache for FPS calculation.
|
|
|
|
const maxSamples = 100
|
|
|
|
|
|
|
|
var (
|
|
|
|
fpsCurrentTicks uint32 // current time we get sdl.GetTicks()
|
|
|
|
fpsLastTime uint32 // last time we printed the fpsCurrentTicks
|
|
|
|
fpsCurrent int
|
|
|
|
fpsFrames int
|
|
|
|
fpsSkipped uint32
|
|
|
|
fpsInterval uint32 = 1000
|
|
|
|
)
|
|
|
|
|
|
|
|
// DrawDebugOverlay draws the debug FPS text on the SDL canvas.
|
|
|
|
func (d *Doodle) DrawDebugOverlay() {
|
|
|
|
if !d.Debug {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-22 00:12:22 +00:00
|
|
|
label := fmt.Sprintf(
|
2018-06-21 02:00:46 +00:00
|
|
|
"FPS: %d (%dms) (%d,%d) S:%s F12=screenshot",
|
2018-06-17 02:59:23 +00:00
|
|
|
fpsCurrent,
|
|
|
|
fpsSkipped,
|
|
|
|
d.events.CursorX.Now,
|
|
|
|
d.events.CursorY.Now,
|
2018-06-21 02:00:46 +00:00
|
|
|
d.scene.Name(),
|
2018-06-17 02:59:23 +00:00
|
|
|
)
|
2018-07-22 00:12:22 +00:00
|
|
|
|
|
|
|
err := d.Engine.DrawText(
|
|
|
|
render.Text{
|
|
|
|
Text: label,
|
|
|
|
Size: 24,
|
|
|
|
Color: DebugTextColor,
|
|
|
|
Stroke: DebugTextStroke,
|
|
|
|
Shadow: DebugTextShadow,
|
|
|
|
},
|
|
|
|
render.Rect{
|
|
|
|
X: DebugTextPadding,
|
|
|
|
Y: DebugTextPadding,
|
|
|
|
W: d.width,
|
|
|
|
H: d.height,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("DrawDebugOverlay: text error: %s", err.Error())
|
|
|
|
}
|
2018-06-17 02:59:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TrackFPS shows the current FPS once per second.
|
|
|
|
func (d *Doodle) TrackFPS(skipped uint32) {
|
|
|
|
fpsFrames++
|
2018-07-22 00:12:22 +00:00
|
|
|
fpsCurrentTicks = d.Engine.GetTicks()
|
2018-06-17 02:59:23 +00:00
|
|
|
|
|
|
|
// Skip the first second.
|
|
|
|
if fpsCurrentTicks < fpsInterval {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if fpsLastTime < fpsCurrentTicks-fpsInterval {
|
|
|
|
log.Debug("Uptime: %s FPS: %d deltaTicks: %d skipped: %dms",
|
|
|
|
time.Now().Sub(d.startTime),
|
|
|
|
fpsCurrent,
|
|
|
|
fpsCurrentTicks-fpsLastTime,
|
|
|
|
skipped,
|
|
|
|
)
|
|
|
|
|
|
|
|
fpsLastTime = fpsCurrentTicks
|
|
|
|
fpsCurrent = fpsFrames
|
|
|
|
fpsFrames = 0
|
|
|
|
fpsSkipped = skipped
|
|
|
|
}
|
|
|
|
}
|