2018-06-17 02:59:23 +00:00
|
|
|
package doodle
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-10-19 20:31:58 +00:00
|
|
|
"strings"
|
2018-06-17 02:59:23 +00:00
|
|
|
|
2019-04-10 00:35:44 +00:00
|
|
|
"git.kirsle.net/apps/doodle/pkg/balance"
|
2019-04-16 02:12:25 +00:00
|
|
|
"git.kirsle.net/apps/doodle/pkg/collision"
|
2021-06-03 03:41:53 +00:00
|
|
|
"git.kirsle.net/apps/doodle/pkg/drawtool"
|
|
|
|
"git.kirsle.net/apps/doodle/pkg/uix"
|
2019-12-28 03:16:34 +00:00
|
|
|
"git.kirsle.net/go/render"
|
|
|
|
"git.kirsle.net/go/ui"
|
2018-06-17 02:59:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Frames to cache for FPS calculation.
|
|
|
|
const maxSamples = 100
|
|
|
|
|
2018-07-26 02:38:54 +00:00
|
|
|
// Debug mode options, these can be enabled in the dev console
|
|
|
|
// like: boolProp DebugOverlay true
|
|
|
|
var (
|
2019-04-19 23:21:04 +00:00
|
|
|
DebugOverlay = false
|
2019-04-19 01:15:05 +00:00
|
|
|
DebugCollision = false
|
2018-10-19 20:31:58 +00:00
|
|
|
|
2019-12-28 03:16:34 +00:00
|
|
|
DebugTextPadding = 8
|
|
|
|
DebugTextSize = 24
|
|
|
|
DebugTextColor = render.SkyBlue
|
|
|
|
DebugTextStroke = render.Grey
|
|
|
|
DebugTextShadow = render.Black
|
2018-07-26 02:38:54 +00:00
|
|
|
)
|
|
|
|
|
2018-06-17 02:59:23 +00:00
|
|
|
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
|
2019-04-10 01:28:08 +00:00
|
|
|
fpsDoNotCap bool // remove the FPS delay cap in main loop
|
2018-10-19 20:31:58 +00:00
|
|
|
|
2019-04-10 01:28:08 +00:00
|
|
|
// Custom labels for individual Scenes to add debug info.
|
|
|
|
customDebugLabels []debugLabel
|
2018-06-17 02:59:23 +00:00
|
|
|
)
|
|
|
|
|
2019-04-10 01:28:08 +00:00
|
|
|
type debugLabel struct {
|
|
|
|
key string
|
|
|
|
variable *string
|
|
|
|
}
|
|
|
|
|
2018-06-17 02:59:23 +00:00
|
|
|
// DrawDebugOverlay draws the debug FPS text on the SDL canvas.
|
|
|
|
func (d *Doodle) DrawDebugOverlay() {
|
2019-04-10 01:28:08 +00:00
|
|
|
if !DebugOverlay {
|
2018-06-17 02:59:23 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-10 01:28:08 +00:00
|
|
|
var framesSkipped = fmt.Sprintf("(skip: %dms)", fpsSkipped)
|
|
|
|
if fpsDoNotCap {
|
|
|
|
framesSkipped = "uncapped"
|
|
|
|
}
|
|
|
|
|
2018-10-19 20:31:58 +00:00
|
|
|
var (
|
2019-12-28 03:16:34 +00:00
|
|
|
darken = balance.DebugStrokeDarken
|
|
|
|
Yoffset = 20 // leave room for the menu bar
|
|
|
|
Xoffset = 20
|
|
|
|
keys = []string{
|
2019-04-10 01:28:08 +00:00
|
|
|
"FPS:",
|
2018-10-19 20:31:58 +00:00
|
|
|
"Scene:",
|
|
|
|
"Mouse:",
|
|
|
|
}
|
|
|
|
values = []string{
|
2019-04-10 01:28:08 +00:00
|
|
|
fmt.Sprintf("%d %s", fpsCurrent, framesSkipped),
|
2018-10-19 20:31:58 +00:00
|
|
|
d.Scene.Name(),
|
2019-12-22 22:11:01 +00:00
|
|
|
fmt.Sprintf("%d,%d", d.event.CursorX, d.event.CursorY),
|
2018-10-19 20:31:58 +00:00
|
|
|
}
|
2018-06-17 02:59:23 +00:00
|
|
|
)
|
2018-07-22 00:12:22 +00:00
|
|
|
|
2019-04-10 01:28:08 +00:00
|
|
|
// Insert custom keys.
|
|
|
|
for _, custom := range customDebugLabels {
|
|
|
|
keys = append(keys, custom.key)
|
|
|
|
if custom.variable == nil {
|
|
|
|
values = append(values, "<nil>")
|
|
|
|
} else if len(*custom.variable) == 0 {
|
|
|
|
values = append(values, `""`)
|
|
|
|
} else {
|
|
|
|
values = append(values, *custom.variable)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the longest key to align the labels up.
|
|
|
|
var longest int
|
|
|
|
for _, key := range keys {
|
|
|
|
if len(key) > longest {
|
|
|
|
longest = len(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Space pad the keys for alignment.
|
|
|
|
for i, key := range keys {
|
|
|
|
if len(key) < longest {
|
|
|
|
key = strings.Repeat(" ", longest-len(key)) + key
|
|
|
|
keys[i] = key
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-19 20:31:58 +00:00
|
|
|
key := ui.NewLabel(ui.Label{
|
|
|
|
Text: strings.Join(keys, "\n"),
|
|
|
|
Font: render.Text{
|
|
|
|
Size: balance.DebugFontSize,
|
|
|
|
FontFilename: balance.ShellFontFilename,
|
|
|
|
Color: balance.DebugLabelColor,
|
|
|
|
Stroke: balance.DebugLabelColor.Darken(darken),
|
2018-07-22 00:12:22 +00:00
|
|
|
},
|
2018-10-19 20:31:58 +00:00
|
|
|
})
|
|
|
|
key.Compute(d.Engine)
|
|
|
|
key.Present(d.Engine, render.NewPoint(
|
|
|
|
DebugTextPadding+Xoffset,
|
|
|
|
DebugTextPadding+Yoffset,
|
|
|
|
))
|
|
|
|
|
|
|
|
value := ui.NewLabel(ui.Label{
|
|
|
|
Text: strings.Join(values, "\n"),
|
|
|
|
Font: render.Text{
|
|
|
|
Size: balance.DebugFontSize,
|
|
|
|
FontFilename: balance.DebugFontFilename,
|
|
|
|
Color: balance.DebugValueColor,
|
|
|
|
Stroke: balance.DebugValueColor.Darken(darken),
|
2018-07-22 00:12:22 +00:00
|
|
|
},
|
2018-10-19 20:31:58 +00:00
|
|
|
})
|
|
|
|
value.Compute(d.Engine)
|
|
|
|
value.Present(d.Engine, render.NewPoint(
|
|
|
|
DebugTextPadding+Xoffset+key.Size().W+DebugTextPadding,
|
|
|
|
DebugTextPadding+Yoffset, // padding to not overlay menu bar
|
|
|
|
))
|
2018-06-17 02:59:23 +00:00
|
|
|
}
|
|
|
|
|
2018-07-25 03:57:22 +00:00
|
|
|
// DrawCollisionBox draws the collision box around a Doodad.
|
2019-04-14 22:25:03 +00:00
|
|
|
//
|
2021-06-03 03:41:53 +00:00
|
|
|
// The canvas will be the level Canvas, and the collision box is drawn in world
|
|
|
|
// space using the canvas.DrawStrokes function.
|
|
|
|
func (d *Doodle) DrawCollisionBox(canvas *uix.Canvas, actor *uix.Actor) {
|
2019-04-10 01:28:08 +00:00
|
|
|
if !DebugCollision {
|
2018-07-26 02:38:54 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-25 03:57:22 +00:00
|
|
|
var (
|
2022-01-09 02:27:37 +00:00
|
|
|
rect = collision.GetBoundingRect(actor)
|
|
|
|
box = collision.GetCollisionBox(rect)
|
2021-06-03 03:41:53 +00:00
|
|
|
hitbox = actor.Hitbox()
|
2018-07-25 03:57:22 +00:00
|
|
|
)
|
|
|
|
|
2021-06-03 03:41:53 +00:00
|
|
|
// Adjust the actor's bounding rect by its stated Hitbox from its script.
|
|
|
|
rect = collision.SizePlusHitbox(rect, hitbox)
|
|
|
|
|
|
|
|
box = collision.GetCollisionBox(rect)
|
|
|
|
|
|
|
|
// The stroke data for drawing the collision box "inside" the level Canvas,
|
|
|
|
// so it scrolls and works in world units not screen units.
|
2022-01-09 02:27:37 +00:00
|
|
|
var strokes = []struct {
|
|
|
|
Color render.Color
|
2021-06-03 03:41:53 +00:00
|
|
|
PointA render.Point
|
|
|
|
PointB render.Point
|
|
|
|
}{
|
|
|
|
{render.DarkGreen, box.Top[0], box.Top[1]},
|
|
|
|
{render.DarkBlue, box.Bottom[0], box.Bottom[1]},
|
|
|
|
{render.DarkYellow, box.Left[0], box.Left[1]},
|
|
|
|
{render.Red, box.Right[0], box.Right[1]},
|
|
|
|
}
|
|
|
|
for _, cfg := range strokes {
|
|
|
|
stroke := drawtool.NewStroke(drawtool.Line, cfg.Color)
|
|
|
|
stroke.PointA = cfg.PointA
|
|
|
|
stroke.PointB = cfg.PointB
|
|
|
|
canvas.DrawStrokes(d.Engine, []*drawtool.Stroke{stroke})
|
|
|
|
}
|
2018-07-25 03:57:22 +00:00
|
|
|
}
|
|
|
|
|
2018-06-17 02:59:23 +00:00
|
|
|
// TrackFPS shows the current FPS once per second.
|
2019-06-25 21:57:11 +00:00
|
|
|
//
|
|
|
|
// In debug mode, changes the window title to include the FPS counter.
|
2018-06-17 02:59:23 +00:00
|
|
|
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 {
|
|
|
|
fpsLastTime = fpsCurrentTicks
|
|
|
|
fpsCurrent = fpsFrames
|
|
|
|
fpsFrames = 0
|
|
|
|
fpsSkipped = skipped
|
|
|
|
}
|
2019-06-25 21:57:11 +00:00
|
|
|
|
2022-01-09 02:27:37 +00:00
|
|
|
// FPS in the title bar.
|
|
|
|
d.Engine.SetTitle(fmt.Sprintf("%s (%d FPS)", d.Title(), fpsCurrent))
|
2018-06-17 02:59:23 +00:00
|
|
|
}
|