ci-platforms / darwin amd64 smoke (push) Skipped
GitHub / linux amd64 native GitHub-hosted native CI: success
ci-platforms / windows amd64 smoke (push) Skipped
GitHub / windows amd64 native GitHub-hosted native CI: success
GitHub / darwin arm64 native GitHub-hosted native CI: success
GitHub / darwin amd64 native GitHub-hosted native CI: success
ci / checks (push) Failing after 1h21m34s
ci / native-authoring (push) Skipped
ci-platforms / darwin arm64 smoke (push) Skipped
github-native / gitea-pending (push) Skipped
github-native / darwin amd64 native (push) Skipped
github-native / linux amd64 native (push) Skipped
github-native / windows amd64 native (push) Skipped
github-native / darwin arm64 native (push) Skipped
github-native / gitea-result (push) Skipped
ci / build (amd64, linux, ubuntu-24.04) (push) Successful in 12m4s
ci / release (push) Skipped
Add dockable Art tools, animation and previews, guarded asset workflows, and the Web Asset Studio built on the shared Art Core. Expand English and Russian documentation, interactive Try scenes, examples and editor icons. Add audio waveform navigation, source analysis, guarded previews and a browser Worker mixer. Preserve source changes and recovery with bounded checks, and strengthen native, browser and interoperability CI. Prepare version 0.12.5 and manually built native artifact candidates. Fix the production site image demo build dependency. Reuse immutable scene indices when capturing keyframes. Wait for real Persist save/load completion in component tests. Stabilize browser Game View captures outside pointer hover. Aseprite interchange and persistent Audio EDL recovery remain limited; the artifact workflow does not publish releases or change stable.
447 lines
11 KiB
Go
447 lines
11 KiB
Go
package playground
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"slices"
|
|
"strings"
|
|
|
|
playmods "aego/app/assembly/playground"
|
|
|
|
"aego/asset"
|
|
"aego/asset/loader"
|
|
"aego/contracts/persistence"
|
|
"aego/core/ids"
|
|
"aego/core/log"
|
|
"aego/core/mathx"
|
|
"aego/engine"
|
|
"aego/engine/base"
|
|
"aego/engine/module"
|
|
"aego/engine/problems"
|
|
"aego/engine/recorder"
|
|
"aego/platform"
|
|
"aego/render"
|
|
renderhost "aego/render/host"
|
|
"aego/render/material"
|
|
"aego/render/sprite"
|
|
"aego/render/view"
|
|
"aego/scene"
|
|
"aego/scene/frame"
|
|
"aego/scene/sceneio"
|
|
"aego/script/bind"
|
|
"aego/script/bytecode"
|
|
scriptruntime "aego/script/runtime"
|
|
)
|
|
|
|
const (
|
|
maxLines = 500
|
|
gameWidth = 960
|
|
gameHeight = 540
|
|
maxSprites = 16384
|
|
statsAlpha = 0.1
|
|
benchLabel = "playground.bench"
|
|
benchName = "bench"
|
|
gameAppID = "aego-playground"
|
|
recordDepth = 1024
|
|
)
|
|
|
|
type GameConfig struct {
|
|
Headless bool
|
|
FrameTime float64
|
|
}
|
|
|
|
type Stats struct {
|
|
Running bool `json:"running"`
|
|
Example string `json:"example,omitempty"`
|
|
Frames uint64 `json:"frames"`
|
|
FrameMs float64 `json:"frameMs"`
|
|
Calls uint64 `json:"calls"`
|
|
DrawCalls uint32 `json:"drawCalls"`
|
|
Sprites uint32 `json:"sprites"`
|
|
Lost bool `json:"lost"`
|
|
Restores int `json:"restores"`
|
|
Renderer string `json:"renderer"`
|
|
}
|
|
|
|
type Poll struct {
|
|
Log []Line `json:"log"`
|
|
Faults []Fault `json:"faults"`
|
|
Stats Stats `json:"stats"`
|
|
}
|
|
|
|
type gpuCalls interface {
|
|
Calls() uint64
|
|
}
|
|
|
|
type gpuLoss interface {
|
|
Lost() bool
|
|
Restores() int
|
|
SimulateLoss(lose bool) bool
|
|
}
|
|
|
|
type bench struct {
|
|
count int
|
|
animate bool
|
|
t float64
|
|
mat *material.Material
|
|
}
|
|
|
|
type Game struct {
|
|
cfg GameConfig
|
|
types *scene.Types
|
|
reg *bind.Registry
|
|
man *module.Manifest
|
|
e *engine.Engine
|
|
renderer *render.Renderer
|
|
rstats func() render.Stats
|
|
loader *loader.Loader
|
|
problems *problems.Service
|
|
runner *frame.Runner
|
|
rt *scriptruntime.Runtime
|
|
scene *scene.Scene
|
|
detach func()
|
|
log *log.Logger
|
|
clog *log.ChannelLogger
|
|
lines []Line
|
|
faults int
|
|
example string
|
|
frames uint64
|
|
frameMs float64
|
|
bench bench
|
|
source []byte
|
|
modules []*bytecode.Module
|
|
running bool
|
|
looping bool
|
|
overflow bool
|
|
}
|
|
|
|
type driver struct {
|
|
g *Game
|
|
}
|
|
|
|
func (d driver) Init(*engine.Engine) error { return nil }
|
|
func (d driver) Event(platform.Event) {}
|
|
func (d driver) FixedUpdate(float64) {}
|
|
func (d driver) Update(dt float64) { d.g.update(dt) }
|
|
func (d driver) Render(float64) { d.g.render() }
|
|
func (d driver) Shutdown() {}
|
|
|
|
func newGame(cfg GameConfig, types *scene.Types, reg *bind.Registry, man *module.Manifest, sections *module.Sections) (*Game, error) {
|
|
g := &Game{cfg: cfg, types: types, reg: reg, man: man, runner: frame.New()}
|
|
g.log = log.New(log.FuncSink(g.record))
|
|
g.log.SetLevel(log.Info)
|
|
g.clog = g.log.With(log.Engine)
|
|
rmod := renderhost.New(render.Options{MaxSprites: maxSprites})
|
|
b := engine.NewBuilder(engine.Config{
|
|
Title: "Aego Playground",
|
|
Width: gameWidth,
|
|
Height: gameHeight,
|
|
Headless: cfg.Headless,
|
|
HeadlessFrameTime: cfg.FrameTime,
|
|
VSync: platform.VSyncOn,
|
|
AppID: gameAppID,
|
|
Seed: 1,
|
|
}).WithLogger(g.log).Use(rmod)
|
|
if err := frame.Register(b.Registry()); err != nil {
|
|
return nil, err
|
|
}
|
|
if ts, ok := module.SectionValue[base.Settings](sections, base.Section); ok {
|
|
b.Configure(ts.Apply)
|
|
}
|
|
b.Use(man.Engine())
|
|
e, err := b.Build()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
g.e = e
|
|
g.renderer = rmod.Renderer()
|
|
g.rstats = rmod.Stats
|
|
g.loader = loader.New(e.GPU(), g.renderer.Textures(), g.log)
|
|
g.loader.SetMaterials(g.renderer.Materials())
|
|
if err := g.loader.Mount(asset.NewIndex(), e.VFS()); err != nil {
|
|
e.Close()
|
|
return nil, err
|
|
}
|
|
e.SetService(loader.ServiceID, g.loader)
|
|
playmods.UseMemorySaves(e)
|
|
e.SetService(renderhost.ServiceID, g.renderer)
|
|
e.SetView(frame.ServiceID, g.runner)
|
|
e.SetView(persistence.HostID, g)
|
|
e.MainWorld().Recorder = recorder.New(recordDepth)
|
|
g.problems = problems.New()
|
|
if err := man.Init(&module.InitContext{Engine: e, Assets: g.loader, Log: g.log, Sections: sections, Problems: g.problems}); err != nil {
|
|
e.Close()
|
|
return nil, err
|
|
}
|
|
return g, nil
|
|
}
|
|
|
|
// Problems returns the modules' current aggregate, including diagnostics from
|
|
// presentation/extraction that are intentionally not logged every frame.
|
|
func (g *Game) Problems() []problems.Diagnostic {
|
|
if g.problems == nil {
|
|
return nil
|
|
}
|
|
list, _ := g.problems.Collect()
|
|
return list
|
|
}
|
|
|
|
func (g *Game) record(r log.Record) {
|
|
if strings.HasPrefix(r.Msg, "script stopped: ") {
|
|
return
|
|
}
|
|
if len(g.lines) >= maxLines {
|
|
g.lines = append(g.lines[:0], g.lines[len(g.lines)-maxLines/2:]...)
|
|
}
|
|
g.lines = append(g.lines, record(&g.frames, r))
|
|
}
|
|
|
|
func (g *Game) start() {
|
|
if g.looping || g.cfg.Headless {
|
|
return
|
|
}
|
|
g.looping = true
|
|
go func() {
|
|
if err := g.e.Run(driver{g: g}); err != nil {
|
|
g.clog.Error("the game loop stopped", log.F("err", err.Error()))
|
|
}
|
|
}()
|
|
}
|
|
|
|
func (g *Game) Step(n int) error {
|
|
for i := 0; i < n; i++ {
|
|
if !g.e.Step(driver{g: g}) {
|
|
return g.e.Err()
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (g *Game) Engine() *engine.Engine {
|
|
return g.e
|
|
}
|
|
|
|
// Scene is the scene Play loaded, nil before the first Play or after Stop.
|
|
func (g *Game) Scene() *scene.Scene {
|
|
return g.scene
|
|
}
|
|
|
|
// Playground scenes are ephemeral, not project assets. A save may reload
|
|
// the currently running source only; it must not select arbitrary asset IDs.
|
|
func (g *Game) SceneID() ids.AssetID { return ids.AssetID{} }
|
|
|
|
func (g *Game) LoadScene(id ids.AssetID) error {
|
|
if !id.IsZero() {
|
|
return fmt.Errorf("playground cannot load a different scene asset")
|
|
}
|
|
if !g.running || g.source == nil {
|
|
return fmt.Errorf("playground has no running scene to reload")
|
|
}
|
|
return g.Play(g.example, g.source, g.modules)
|
|
}
|
|
|
|
func (g *Game) Play(name string, src []byte, mods []*bytecode.Module) error {
|
|
g.Stop()
|
|
rt := scriptruntime.New(scriptruntime.Options{Bindings: g.reg, Schema: g.types.Schema(), Log: g.log, VM: playmods.SandboxVM()})
|
|
if err := rt.Link(mods); err != nil {
|
|
rt.Close()
|
|
return err
|
|
}
|
|
s, rep, err := sceneio.Load(src, ScenePrefix+name+sceneExt, g.types, g.loader, sceneio.LoadOptions{KeepIDs: true})
|
|
if err != nil {
|
|
rt.Close()
|
|
return err
|
|
}
|
|
for _, w := range rep.Warnings {
|
|
g.clog.Warn("scene warning", log.F("path", w.Path), log.F("line", w.Pos.Line), log.F("msg", w.Msg))
|
|
}
|
|
for _, u := range rep.Unknown {
|
|
g.clog.Warn("unknown component preserved", log.F("type", u))
|
|
}
|
|
g.rt, g.scene = rt, s
|
|
g.e.SetService(scriptruntime.ServiceID, rt)
|
|
g.e.SetService(scene.ServiceID, s)
|
|
g.runner.Bind(s, g.renderer, g.loader)
|
|
if err := rt.Instantiate(s); err != nil {
|
|
g.Stop()
|
|
return err
|
|
}
|
|
d, err := g.man.Attach(&module.AttachContext{World: g.e.MainWorld(), Scene: s, Log: g.log, Play: true})
|
|
if err != nil {
|
|
g.Stop()
|
|
return err
|
|
}
|
|
g.detach = d
|
|
g.source, g.modules = bytes.Clone(src), slices.Clone(mods)
|
|
g.example, g.running, g.faults, g.overflow = name, true, 0, false
|
|
g.start()
|
|
return nil
|
|
}
|
|
|
|
func (g *Game) Bench(count int, animate, withMaterial bool) error {
|
|
g.Stop()
|
|
g.bench = bench{count: min(max(count, 1), maxSprites), animate: animate}
|
|
if withMaterial {
|
|
m, err := BenchMaterial()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
g.bench.mat = m
|
|
g.renderer.PinClock(&BenchClock)
|
|
}
|
|
g.example, g.running = benchName, true
|
|
g.start()
|
|
return nil
|
|
}
|
|
|
|
func (g *Game) Stop() {
|
|
if g.detach != nil {
|
|
g.detach()
|
|
g.detach = nil
|
|
}
|
|
w := g.e.MainWorld()
|
|
if g.rt != nil {
|
|
g.rt.Close()
|
|
g.rt = nil
|
|
w.RemoveService(scriptruntime.ServiceID)
|
|
}
|
|
if g.scene != nil {
|
|
g.scene = nil
|
|
g.runner.SetScene(nil)
|
|
w.RemoveService(scene.ServiceID)
|
|
}
|
|
g.bench = bench{}
|
|
g.renderer.PinClock(nil)
|
|
g.running = false
|
|
g.example = ""
|
|
g.source, g.modules = nil, nil
|
|
}
|
|
|
|
func (g *Game) LoseContext(lose bool) bool {
|
|
l, ok := g.e.GPU().(gpuLoss)
|
|
return ok && l.SimulateLoss(lose)
|
|
}
|
|
|
|
func (g *Game) update(dt float64) {
|
|
g.frames++
|
|
ft := g.e.Stats().FrameTime * 1000
|
|
if g.frameMs == 0 {
|
|
g.frameMs = ft
|
|
} else {
|
|
g.frameMs += (ft - g.frameMs) * statsAlpha
|
|
}
|
|
g.bench.t += dt
|
|
if g.scene != nil && !g.overflow && g.scene.Stats().Alive > playmods.SandboxNodes {
|
|
g.overflow = true
|
|
g.clog.Error(fmt.Sprintf("the scene grew past %d nodes, the game was stopped", playmods.SandboxNodes))
|
|
}
|
|
}
|
|
|
|
func hue(i, n int) mathx.Color {
|
|
h := float32(i) / float32(max(n, 1)) * 6
|
|
f := h - float32(int(h))
|
|
q, t := 1-f, f
|
|
switch int(h) % 6 {
|
|
case 0:
|
|
return mathx.Color{R: 1, G: t, A: 1}
|
|
case 1:
|
|
return mathx.Color{R: q, G: 1, A: 1}
|
|
case 2:
|
|
return mathx.Color{G: 1, B: t, A: 1}
|
|
case 3:
|
|
return mathx.Color{G: q, B: 1, A: 1}
|
|
case 4:
|
|
return mathx.Color{R: t, B: 1, A: 1}
|
|
}
|
|
return mathx.Color{R: 1, B: q, A: 1}
|
|
}
|
|
|
|
func (g *Game) render() {
|
|
b := &g.bench
|
|
if b.count == 0 || g.renderer == nil {
|
|
return
|
|
}
|
|
m := g.e.Metrics()
|
|
if m.PixelW <= 0 || m.PixelH <= 0 {
|
|
return
|
|
}
|
|
scale := m.Density()
|
|
v := view.ScreenView(m.PixelW, m.PixelH, scale)
|
|
v.DoClear, v.Clear, v.Label = true, mathx.Color{R: 0.07, G: 0.08, B: 0.1, A: 1}, benchLabel
|
|
f := g.renderer.View(v)
|
|
w, h := float32(m.PixelW)/scale, float32(m.PixelH)/scale
|
|
cols := max(1, int(mathx.Sqrt(float32(b.count)*w/h)+0.5))
|
|
rows := (b.count + cols - 1) / cols
|
|
cw, ch := w/float32(cols), h/float32(rows)
|
|
size := min(cw, ch) * 0.7
|
|
white := g.renderer.White()
|
|
for i := 0; i < b.count; i++ {
|
|
var rot float32
|
|
if b.animate {
|
|
rot = float32(b.t)*1.5 + float32(i)*0.05
|
|
}
|
|
f.Sprite(sprite.Draw{
|
|
Region: white,
|
|
Position: mathx.Vec2{X: (float32(i%cols) + 0.5) * cw, Y: (float32(i/cols) + 0.5) * ch},
|
|
Size: mathx.Vec2{X: size, Y: size},
|
|
Origin: mathx.Vec2{X: 0.5, Y: 0.5},
|
|
Rotation: rot,
|
|
Tint: hue(i, b.count),
|
|
Material: b.mat,
|
|
})
|
|
}
|
|
f.End()
|
|
}
|
|
|
|
func (g *Game) newFaults() []Fault {
|
|
if g.rt == nil {
|
|
return nil
|
|
}
|
|
all := g.rt.Faults()
|
|
if len(all) < g.faults {
|
|
g.faults = 0
|
|
}
|
|
var out []Fault
|
|
for _, f := range all[g.faults:] {
|
|
x := Fault{Script: f.Type, Node: f.Path, Code: f.Fault.Code, Message: f.Fault.Message}
|
|
if len(f.Fault.Stack) > 0 {
|
|
x.File = strings.TrimPrefix(f.Fault.Stack[0].File, ScriptsDir+"/")
|
|
x.Line = int(f.Fault.Stack[0].Line)
|
|
}
|
|
out = append(out, x)
|
|
}
|
|
g.faults = len(all)
|
|
return out
|
|
}
|
|
|
|
func (g *Game) Poll() Poll {
|
|
if g.overflow {
|
|
g.overflow = false
|
|
g.Stop()
|
|
}
|
|
p := Poll{Log: g.lines, Faults: g.newFaults()}
|
|
g.lines = nil
|
|
s := &p.Stats
|
|
s.Running, s.Example, s.Frames, s.FrameMs = g.running, g.example, g.frames, g.frameMs
|
|
s.DrawCalls = g.e.Stats().DrawCalls
|
|
s.Renderer = g.e.Caps().Renderer
|
|
if g.rstats != nil {
|
|
s.Sprites = g.rstats().Instances
|
|
}
|
|
if c, ok := g.e.GPU().(gpuCalls); ok {
|
|
s.Calls = c.Calls()
|
|
}
|
|
if l, ok := g.e.GPU().(gpuLoss); ok {
|
|
s.Lost, s.Restores = l.Lost(), l.Restores()
|
|
}
|
|
return p
|
|
}
|
|
|
|
func (g *Game) Close() {
|
|
g.Stop()
|
|
g.man.Shutdown()
|
|
g.runner.Close()
|
|
g.e.Close()
|
|
g.loader.Close()
|
|
}
|