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
|
|
|
|
2018-10-19 20:31:58 +00:00
|
|
|
"git.kirsle.net/apps/doodle/balance"
|
2018-07-25 03:57:22 +00:00
|
|
|
"git.kirsle.net/apps/doodle/doodads"
|
2018-06-17 02:59:23 +00:00
|
|
|
"git.kirsle.net/apps/doodle/render"
|
2018-10-19 20:31:58 +00:00
|
|
|
"git.kirsle.net/apps/doodle/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 (
|
2018-09-23 22:20:45 +00:00
|
|
|
DebugOverlay = true
|
2018-07-26 02:38:54 +00:00
|
|
|
DebugCollision = true
|
2018-10-19 20:31:58 +00:00
|
|
|
|
|
|
|
DebugTextPadding int32 = 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
|
2018-10-19 20:31:58 +00:00
|
|
|
|
Play: Autoscrolling and Bounded Level Support
Implement scrolling behavior in Play Mode by allowing the Canvas to
follow a specific actor and keep it in view. The Canvas has a
FollowActor property which holds an ID of the actor to follow (if blank,
no actor is being followed).
In Play Mode the Player is followed and when they get too close to the
left or right edges of the screen, the level will scroll to try and
catch them. If the player is moving very fast they can outrun the
camera.
The bounded levels are enforced in Play Mode and the camera won't scroll
to view pixels out-of-bounds and the Doodad actors inside the level
aren't allowed to exit its boundaries. This is global, not only for the
Player doodad but any Doodad that came with the level as well.
Other changes:
- Restructured Canvas widget code into many new files. The Canvas widget
is shaping up to be where most of the magic happens, which is okay
because it's close to the action and pulling the strings from outside
would be harder, even tho as a UI element you think it should be
lightweight.
- Debug Overlay: added room for Scenes to insert their own custom Debug
Overlay key/value pairs (the values are string pointers so the Scene
can update them freely):
- The core labels are FPS, Scene and Mouse. The Pixel (world
coordinate under cursor) is removed from the core labels.
- Edit Scene provides Pixel, Tool and Swatch
- Play Scene provides Pixel, Player, Viewport, Scroll
2018-10-29 00:33:24 +00:00
|
|
|
// Custom labels for scenes to add to the debug overlay view.
|
|
|
|
customDebugLabels []debugLabel
|
2018-06-17 02:59:23 +00:00
|
|
|
)
|
|
|
|
|
Play: Autoscrolling and Bounded Level Support
Implement scrolling behavior in Play Mode by allowing the Canvas to
follow a specific actor and keep it in view. The Canvas has a
FollowActor property which holds an ID of the actor to follow (if blank,
no actor is being followed).
In Play Mode the Player is followed and when they get too close to the
left or right edges of the screen, the level will scroll to try and
catch them. If the player is moving very fast they can outrun the
camera.
The bounded levels are enforced in Play Mode and the camera won't scroll
to view pixels out-of-bounds and the Doodad actors inside the level
aren't allowed to exit its boundaries. This is global, not only for the
Player doodad but any Doodad that came with the level as well.
Other changes:
- Restructured Canvas widget code into many new files. The Canvas widget
is shaping up to be where most of the magic happens, which is okay
because it's close to the action and pulling the strings from outside
would be harder, even tho as a UI element you think it should be
lightweight.
- Debug Overlay: added room for Scenes to insert their own custom Debug
Overlay key/value pairs (the values are string pointers so the Scene
can update them freely):
- The core labels are FPS, Scene and Mouse. The Pixel (world
coordinate under cursor) is removed from the core labels.
- Edit Scene provides Pixel, Tool and Swatch
- Play Scene provides Pixel, Player, Viewport, Scroll
2018-10-29 00:33:24 +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() {
|
2018-10-21 02:49:59 +00:00
|
|
|
if !DebugOverlay {
|
2018-06-17 02:59:23 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-19 20:31:58 +00:00
|
|
|
var (
|
|
|
|
darken = balance.DebugStrokeDarken
|
|
|
|
Yoffset int32 = 20 // leave room for the menu bar
|
|
|
|
Xoffset int32 = 5
|
|
|
|
keys = []string{
|
Play: Autoscrolling and Bounded Level Support
Implement scrolling behavior in Play Mode by allowing the Canvas to
follow a specific actor and keep it in view. The Canvas has a
FollowActor property which holds an ID of the actor to follow (if blank,
no actor is being followed).
In Play Mode the Player is followed and when they get too close to the
left or right edges of the screen, the level will scroll to try and
catch them. If the player is moving very fast they can outrun the
camera.
The bounded levels are enforced in Play Mode and the camera won't scroll
to view pixels out-of-bounds and the Doodad actors inside the level
aren't allowed to exit its boundaries. This is global, not only for the
Player doodad but any Doodad that came with the level as well.
Other changes:
- Restructured Canvas widget code into many new files. The Canvas widget
is shaping up to be where most of the magic happens, which is okay
because it's close to the action and pulling the strings from outside
would be harder, even tho as a UI element you think it should be
lightweight.
- Debug Overlay: added room for Scenes to insert their own custom Debug
Overlay key/value pairs (the values are string pointers so the Scene
can update them freely):
- The core labels are FPS, Scene and Mouse. The Pixel (world
coordinate under cursor) is removed from the core labels.
- Edit Scene provides Pixel, Tool and Swatch
- Play Scene provides Pixel, Player, Viewport, Scroll
2018-10-29 00:33:24 +00:00
|
|
|
"FPS:",
|
2018-10-19 20:31:58 +00:00
|
|
|
"Scene:",
|
|
|
|
"Mouse:",
|
|
|
|
}
|
|
|
|
values = []string{
|
|
|
|
fmt.Sprintf("%d (skip: %dms)", fpsCurrent, fpsSkipped),
|
|
|
|
d.Scene.Name(),
|
|
|
|
fmt.Sprintf("%d,%d", d.event.CursorX.Now, d.event.CursorY.Now),
|
|
|
|
}
|
2018-06-17 02:59:23 +00:00
|
|
|
)
|
2018-07-22 00:12:22 +00:00
|
|
|
|
Play: Autoscrolling and Bounded Level Support
Implement scrolling behavior in Play Mode by allowing the Canvas to
follow a specific actor and keep it in view. The Canvas has a
FollowActor property which holds an ID of the actor to follow (if blank,
no actor is being followed).
In Play Mode the Player is followed and when they get too close to the
left or right edges of the screen, the level will scroll to try and
catch them. If the player is moving very fast they can outrun the
camera.
The bounded levels are enforced in Play Mode and the camera won't scroll
to view pixels out-of-bounds and the Doodad actors inside the level
aren't allowed to exit its boundaries. This is global, not only for the
Player doodad but any Doodad that came with the level as well.
Other changes:
- Restructured Canvas widget code into many new files. The Canvas widget
is shaping up to be where most of the magic happens, which is okay
because it's close to the action and pulling the strings from outside
would be harder, even tho as a UI element you think it should be
lightweight.
- Debug Overlay: added room for Scenes to insert their own custom Debug
Overlay key/value pairs (the values are string pointers so the Scene
can update them freely):
- The core labels are FPS, Scene and Mouse. The Pixel (world
coordinate under cursor) is removed from the core labels.
- Edit Scene provides Pixel, Tool and Swatch
- Play Scene provides Pixel, Player, Viewport, Scroll
2018-10-29 00:33:24 +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.
|
|
|
|
var longest int
|
|
|
|
for _, key := range keys {
|
|
|
|
if len(key) > longest {
|
|
|
|
longest = len(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Space pad the keys to align them.
|
|
|
|
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.
|
2018-09-26 17:04:46 +00:00
|
|
|
func (d *Doodle) DrawCollisionBox(actor doodads.Actor) {
|
2018-10-21 02:49:59 +00:00
|
|
|
if !DebugCollision {
|
2018-07-26 02:38:54 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-25 03:57:22 +00:00
|
|
|
var (
|
|
|
|
rect = doodads.GetBoundingRect(actor)
|
|
|
|
box = doodads.GetCollisionBox(rect)
|
|
|
|
)
|
|
|
|
|
|
|
|
d.Engine.DrawLine(render.DarkGreen, box.Top[0], box.Top[1])
|
|
|
|
d.Engine.DrawLine(render.DarkBlue, box.Bottom[0], box.Bottom[1])
|
|
|
|
d.Engine.DrawLine(render.DarkYellow, box.Left[0], box.Left[1])
|
|
|
|
d.Engine.DrawLine(render.Red, box.Right[0], box.Right[1])
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
fpsLastTime = fpsCurrentTicks
|
|
|
|
fpsCurrent = fpsFrames
|
|
|
|
fpsFrames = 0
|
|
|
|
fpsSkipped = skipped
|
|
|
|
}
|
|
|
|
}
|