Documentation
¶
Overview ¶
Package extract connects the ecs World to the renderer. Its systems run during Update, read components and write render.Items into the render.Queue resource. Package render then draws that queue in Draw, so Draw never touches the World.
The systems expect these resources in the World (ecs.SetResource):
- *render.Queue: filled by the Extract systems
- *render.Camera: written by the game, read for culling
- *extract.Screen: the logical screen size, for culling
- *render.LayerConfig and *render.Stores: optional, read by the SortQueue runner (Stores gives it the sprite store for texture batching)
- *asset.Server: read by the AssetSystem runner, which also publishes the tick's *asset.Events as a resource
Components hold asset handles (gfx.SpriteHandle, gfx.FontHandle); the extract systems convert them to render handles and the Renderer resolves them, so assets that are missing or still loading draw placeholders.
A frame's Schedule looks like this (see doc/guide/engine/rendering.md, "Without game.Engine"):
s.AddStage("assets") // first: commits loaded assets
s.AddRunner(extract.NewAssetSystem(w)) // a Runner, so its Init runs
s.AddStage("pre", extract.NewSnapshotTransforms(w).Run) // before anything moves entities
// ... game systems ...
s.Add(extract.ResetQueue)
s.AddRunner(extract.NewSpriteExtract(w), extract.NewNineSliceExtract(w), extract.NewLabelExtract(w))
s.AddRunner(extract.NewSortQueue(w))
Example ¶
The render stage of a game's schedule: snapshot first, extract and sort last. (A game also adds extract.NewAssetSystem as the first stage and sets the *asset.Server and *render.Stores resources.) The renderer would then draw the queue in Ebiten's Draw.
package main
import (
"fmt"
"image"
"time"
"github.com/weakpixel/ecs"
"github.com/weakpixel/ecs/system"
"github.com/weakpixel/engine/extract"
"github.com/weakpixel/engine/gfx"
"github.com/weakpixel/engine/render"
)
func main() {
w := ecs.NewWorld()
queue := &render.Queue{}
ecs.SetResource(w, queue)
ecs.SetResource(w, &render.Camera{Zoom: 1})
ecs.SetResource(w, &extract.Screen{Size: image.Pt(320, 180)})
layers := &render.LayerConfig{}
layers.Set(1, render.LayerOptions{YSort: true})
ecs.SetResource(w, layers)
ecs.NewEntity2(w, gfx.Transform{Pos: gfx.Vec2{X: 10, Y: 5}}, gfx.Sprite{Layer: 1})
var s system.Schedule
s.AddStage("pre", extract.NewSnapshotTransforms(w).Run)
// ... input, simulation and camera stages go here ...
s.AddStage("render-prepare", extract.ResetQueue)
s.AddRunner(extract.NewSpriteExtract(w), extract.NewLabelExtract(w))
s.AddRunner(extract.NewSortQueue(w))
s.Run(w, time.Second/60)
fmt.Println(queue.Len(), queue.At(0).Pos)
}
Output: 1 {10 5}
Index ¶
Examples ¶
Constants ¶
const DebugLayer int16 = 32767
DebugLayer is the layer debug shapes are drawn on.
Variables ¶
This section is empty.
Functions ¶
func ResetQueue ¶
ResetQueue empties the render.Queue. Run it first in the render stage.
Types ¶
type AssetSystem ¶
type AssetSystem struct {
// contains filtered or unexported fields
}
AssetSystem advances the asset.Server once per tick: it applies finished loads, so new assets become Ready and their handles stop drawing placeholders. Run it first in the tick, before systems that read assets.
It needs the *asset.Server resource and publishes the tick's *asset.Events as a resource after its first Run, for systems that react to loads and failures (the value is reused and valid until the next tick).
func NewAssetSystem ¶
func NewAssetSystem(w *ecs.World) *AssetSystem
NewAssetSystem returns the system for w.
func (*AssetSystem) Init ¶
func (s *AssetSystem) Init(ctx *system.Context)
Init implements system.Initializer.
func (*AssetSystem) Run ¶
func (s *AssetSystem) Run(ctx *system.Context)
Run implements system.Runner.
type DebugExtract ¶
type DebugExtract struct {
// Margin grows the culling rectangle, in world units.
Margin float32
// contains filtered or unexported fields
}
DebugExtract queues a world-space shape item for every entity with a Transform and a DebugShape, culled like sprites (and counted in render.Queue.Culled). Shapes draw on layer DebugLayer, above ordinary content.
It also draws the outlines a shape package publishes as the World's gfx.DebugOutlines resource: collision publishes its colliders as the last run placed them (nothing without a collision system), colored by the collider's lowest layer (collision.ColliderColor). Custom shapes draw as their bounds.
func NewDebugExtract ¶
func NewDebugExtract(w *ecs.World) *DebugExtract
NewDebugExtract returns the system for w.
func (*DebugExtract) Init ¶
func (s *DebugExtract) Init(ctx *system.Context)
Init implements system.Initializer.
func (*DebugExtract) Run ¶
func (s *DebugExtract) Run(ctx *system.Context)
Run implements system.Runner.
type LabelExtract ¶
type LabelExtract struct {
// contains filtered or unexported fields
}
LabelExtract queues a screen-space text item for every entity with a Transform and a Label. Transform.Pos is in logical screen pixels.
func NewLabelExtract ¶
func NewLabelExtract(w *ecs.World) *LabelExtract
NewLabelExtract returns the system for w.
func (*LabelExtract) Init ¶
func (s *LabelExtract) Init(ctx *system.Context)
Init implements system.Initializer.
func (*LabelExtract) Run ¶
func (s *LabelExtract) Run(ctx *system.Context)
Run implements system.Runner.
type NineSliceExtract ¶
type NineSliceExtract struct {
// Margin grows the culling rectangle on every side, in world units.
Margin float32
// contains filtered or unexported fields
}
NineSliceExtract queues a world-space item for every visible entity with a Transform and a NineSlice. Hidden panels are skipped. A panel is culled when it is outside the camera bounds (plus Margin) by more than its own size: the test is conservative, so a panel whose pivot is off-screen but whose body is visible is kept, whatever its Origin and rotation. A panel with a zero Size has no size to add, so grow Margin for those. Culled panels count in render.Queue.Culled. A panel with a hierarchy.Parent sorts by its root's Y, as for SpriteExtract.
func NewNineSliceExtract ¶
func NewNineSliceExtract(w *ecs.World) *NineSliceExtract
NewNineSliceExtract returns the system for w.
func (*NineSliceExtract) Init ¶
func (s *NineSliceExtract) Init(ctx *system.Context)
Init implements system.Initializer.
func (*NineSliceExtract) Run ¶
func (s *NineSliceExtract) Run(ctx *system.Context)
Run implements system.Runner.
type Screen ¶
Screen is the logical screen size resource. Set it from Ebiten's Layout (see render.Layout) and update it when the size changes.
type ScreenNineSliceExtract ¶
type ScreenNineSliceExtract struct {
// contains filtered or unexported fields
}
ScreenNineSliceExtract queues a screen-space item for every entity with a Transform and a ScreenNineSlice. There is no culling: HUD elements are few.
func NewScreenNineSliceExtract ¶
func NewScreenNineSliceExtract(w *ecs.World) *ScreenNineSliceExtract
NewScreenNineSliceExtract returns the system for w.
func (*ScreenNineSliceExtract) Init ¶
func (s *ScreenNineSliceExtract) Init(ctx *system.Context)
Init implements system.Initializer.
func (*ScreenNineSliceExtract) Run ¶
func (s *ScreenNineSliceExtract) Run(ctx *system.Context)
Run implements system.Runner.
type ScreenSpriteExtract ¶
type ScreenSpriteExtract struct {
// contains filtered or unexported fields
}
ScreenSpriteExtract queues a screen-space item for every entity with a Transform and a ScreenSprite. There is no culling: HUD elements are few.
func NewScreenSpriteExtract ¶
func NewScreenSpriteExtract(w *ecs.World) *ScreenSpriteExtract
NewScreenSpriteExtract returns the system for w.
func (*ScreenSpriteExtract) Init ¶
func (s *ScreenSpriteExtract) Init(ctx *system.Context)
Init implements system.Initializer.
func (*ScreenSpriteExtract) Run ¶
func (s *ScreenSpriteExtract) Run(ctx *system.Context)
Run implements system.Runner.
type SnapshotTransforms ¶
type SnapshotTransforms struct {
// contains filtered or unexported fields
}
SnapshotTransforms records the pose of every Transform in its Prev (Transform.Snapshot), so the renderer can interpolate the movement of the tick that follows. It must run before any system that moves entities: the first stage of the tick.
An entity created after this system ran in a tick has no Prev until the next tick, and is drawn at its current pose meanwhile.
func NewSnapshotTransforms ¶
func NewSnapshotTransforms(w *ecs.World) *SnapshotTransforms
NewSnapshotTransforms returns the system for w.
func (*SnapshotTransforms) Run ¶
func (s *SnapshotTransforms) Run(ctx *system.Context)
Run implements system.Runner.
type SortQueue ¶
type SortQueue struct {
// contains filtered or unexported fields
}
SortQueue sorts the render.Queue by layer, Y and texture. Run it last in the render stage. Build it with NewSortQueue.
func NewSortQueue ¶
NewSortQueue returns the system for w.
type SpriteExtract ¶
type SpriteExtract struct {
// Margin grows the culling rectangle on every side, in world units.
// Set it to about half of the largest sprite so that sprites at the
// edge do not pop. The default zero culls exactly at the view edge.
Margin float32
// contains filtered or unexported fields
}
SpriteExtract queues a world-space item for every visible entity with a Transform and a Sprite. Hidden sprites and sprites whose position is outside the camera bounds (plus Margin) are skipped; the latter count in render.Queue.Culled.
A sprite with a hierarchy.Parent sorts by its root's Y, just in front of or behind it (see childSortY), so it stays glued to its root in a Y-sorted layer.
func NewSpriteExtract ¶
func NewSpriteExtract(w *ecs.World) *SpriteExtract
NewSpriteExtract returns the system for w.
func (*SpriteExtract) Init ¶
func (s *SpriteExtract) Init(ctx *system.Context)
Init implements system.Initializer.
func (*SpriteExtract) Run ¶
func (s *SpriteExtract) Run(ctx *system.Context)
Run implements system.Runner.