render

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 30, 2026 License: Apache-2.0 Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package render defines drawable components and camera state, then resolves them into ordered DrawList values for a renderer to draw.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SpawnCamera added in v0.2.0

func SpawnCamera(w *core.World, cam Camera, position geom.Vector2, zoom float64) (*core.Entity, error)

SpawnCamera creates a camera entity in w at position with the given zoom. A zoom of zero means 1. It returns an error for an invalid anchor or a negative or non-finite zoom.

func SpawnEngineCamera

func SpawnEngineCamera(w *core.World) (*core.Entity, error)

SpawnEngineCamera creates the engine's default primary camera at the world origin with zoom 1. Collector uses it when no user-spawned primary camera is available.

Types

type AtlasSource

type AtlasSource struct {
	// Atlas is the ID of the registered atlas.
	Atlas asset.AtlasID
	// Region is the name of the region to draw.
	Region string
}

AtlasSource draws a named region of a registered atlas.

type Camera

type Camera struct {
	// Primary marks this camera as eligible for rendering.
	Primary bool
	// Anchor is the viewport point the camera's position maps to: (0,0)
	// is the center and (-0.5,-0.5) is the top-left corner.
	Anchor geom.Vector2
	// Bounds limits the camera view to a world-space rectangle. An empty
	// rectangle means the view is unbounded.
	Bounds geom.Rect
}

Camera defines the framing of a camera entity.

Its core.Transform supplies the position, rotation, and uniform zoom; zero scale components mean 1. The Collector selects a primary camera, preferring user-spawned cameras over the engine camera.

func (Camera) MovePosition added in v0.2.0

func (c Camera) MovePosition(transform core.Transform, displacement geom.Vector2, width, height int) (geom.Vector2, error)

MovePosition returns the position reached by moving transform by a world-space displacement, without modifying transform.

It constrains both the starting position and the destination to Camera.Bounds, accounting for the logical viewport width and height in pixels, zoom, rotation, and anchor. A zero displacement only constrains the position. Empty bounds leave movement unbounded. If the view cannot fit on an axis, its center is pinned to the bounds center on that axis.

MovePosition returns an error for a nonpositive viewport size, invalid anchor or zoom, non-finite position, displacement, or rotation, or a non-finite result.

func (Camera) Validate

func (c Camera) Validate() error

Validate reports an error if the camera's anchor lies outside the viewport.

type CameraView

type CameraView struct {
	// Position is the camera position in world space after interpolation and bounds.
	Position geom.Vector2
	// Zoom is the scale used to project between world and screen coordinates.
	Zoom float64
	// Rotation is the interpolated camera rotation in radians. Positive values
	// rotate the view counterclockwise; sprites with the same rotation appear
	// unrotated.
	Rotation float64
	// Anchor is the viewport point the camera's position maps to: (0,0)
	// is the center and (-0.5,-0.5) is the top-left corner.
	Anchor geom.Vector2
	// contains filtered or unexported fields
}

CameraView is the resolved framing of a camera for one collected frame.

A Collector publishes it as the world's *CameraView resource after successful collection. Systems see the most recently collected view.

func (CameraView) ScreenToWorld

func (v CameraView) ScreenToWorld(screen geom.Vector2, screenWidth, screenHeight int) geom.Vector2

ScreenToWorld converts a screen-space point to world coordinates without rounding, preserving fractional positions.

func (CameraView) WorldToScreen

func (v CameraView) WorldToScreen(world geom.Vector2, screenWidth, screenHeight int) geom.Vector2

WorldToScreen projects a world-space point to pixel coordinates for a viewport of the given size. The camera's position maps to its anchor, and each coordinate is rounded to the nearest pixel.

type CircleShape

type CircleShape struct {
	// Radii are the circle's horizontal and vertical radii in world units.
	Radii geom.Vector2
}

CircleShape is a circle centered on the sprite's position, or an ellipse when its radii differ. Its fill or outline comes from the sprite's style.

type Collector

type Collector struct {

	// SortedItems backs [DrawList.Items]. It is reused on the next
	// [Collector.Collect] call; callers must not modify or retain it.
	SortedItems []DrawItem
	// contains filtered or unexported fields
}

Collector resolves drawable assets, culls against the camera viewport, and sorts draw items for each frame. Reuse a Collector across frames.

func NewCollector

func NewCollector(world *core.World) *Collector

NewCollector creates a Collector for world. A user primary camera takes precedence over the engine camera.

func (*Collector) Collect

func (c *Collector) Collect(ctx *core.Context) (DrawList, error)

Collect resolves the draw list for the current frame. It interpolates transforms using core.Context.Alpha, clamps the camera to its bounds, culls to the view, and sorts items by layer, sort order, then world Y. Text bounds use asset.FontData.Measure.

When a primary camera is available, Collect publishes the resolved view as the world's *CameraView resource once collection succeeds: the resource always describes a drawn frame. A failed Collect publishes nothing - the resource keeps the previous frame's view, and a first Collect that fails registers no resource. Calls without a primary camera leave the resource unchanged.

If no primary camera is available, Collect returns an empty list without an error. Asset lookup and loading errors are returned; shapes require no assets.

type DrawItem

type DrawItem struct {
	// Texture is the source texture for a texture item; empty for text and shapes.
	Texture asset.ID
	// Rect is the source region in Texture, in pixels. An empty rectangle
	// selects the whole texture.
	Rect image.Rectangle
	// Shape is the geometry of a shape item; nil for texture and text items.
	Shape Shape
	// Text is the text to draw; empty for non-text items.
	Text string
	// Font is the font asset for a text item; empty for non-text items.
	Font asset.ID
	// TextSize is the font size in pixels before applying Scale.
	TextSize float64
	// TextWidth and TextHeight are the text dimensions measured by the font
	// before applying Scale.
	TextWidth, TextHeight float64
	// Position is the interpolated position in world space.
	Position geom.Vector2
	// Rotation is the rotation in world space, in radians.
	Rotation float64
	// Scale is the drawable's scale in world space.
	Scale geom.Vector2
	// FlipH and FlipV mirror texture items horizontally and vertically.
	FlipH, FlipV bool
	// Color tints texture pixels or sets the text or shape color. Its alpha
	// controls opacity; nil means no tint on a texture item.
	Color color.Color
	// Outline strokes a shape's border instead of filling it.
	Outline bool
	// StrokeWidth is the outline width in world units.
	StrokeWidth float64
}

DrawItem is one drawable resolved from a Sprite for a frame.

type DrawList

type DrawList struct {
	// Camera is the resolved primary camera view.
	Camera CameraView
	// Items is the sorted set of drawables. When returned by [Collector.Collect],
	// the collector owns the slice and overwrites it on its next call.
	Items []DrawItem
}

DrawList contains the camera view and sorted draw items for one frame.

type Drawable

type Drawable interface {
	// contains filtered or unexported methods
}

Drawable is the image, text, or shape rendered by a Sprite. Its implementations are AtlasSource, TextureSource, TextSource, RectShape, CircleShape, and LineShape.

type LineShape

type LineShape struct {
	// From is the segment's start point relative to the sprite's position.
	From geom.Vector2
	// To is the segment's end point relative to the sprite's position.
	To geom.Vector2
}

LineShape is a segment whose endpoints are relative to the sprite's position.

type RectShape

type RectShape struct {
	// Size is the rectangle's width and height in world units.
	Size geom.Vector2
}

RectShape is a rectangle centered on the sprite's position. Its fill or outline comes from the sprite's style.

type Shape

type Shape interface {
	Drawable
	// contains filtered or unexported methods
}

Shape is a Drawable defined by geometry rather than an image. Implementations are RectShape, CircleShape, and LineShape.

type Sprite

type Sprite struct {
	// Layer orders sprites from back to front, from 0 through 31.
	Layer uint8
	// SortOrder orders sprites within a layer; higher values draw on top.
	// Equal values are ordered by world Y.
	SortOrder int8
	// Hidden excludes the sprite from collection.
	Hidden bool
	// FlipH and FlipV mirror texture-backed drawables horizontally and
	// vertically; shapes are not flipped.
	FlipH, FlipV bool
	// Drawable is the image, text, or shape to draw. A nil value draws nothing.
	Drawable Drawable
	// Outline strokes a shape's border instead of filling it. It requires a
	// positive StrokeWidth and has no effect on non-shape drawables.
	Outline bool
	// StrokeWidth is the outline width in world units. It must not be negative
	// and must be positive when Outline is set.
	StrokeWidth float64
	// Color tints texture pixels or sets the text or shape color. Its alpha
	// controls opacity. A nil value leaves textures untinted, defaults text
	// to white, and defaults shapes to black.
	Color color.Color
}

Sprite is a drawable component with shared style and an optional drawable.

The zero value is visible but draws nothing until Sprite.Drawable is set.

func (Sprite) Validate

func (s Sprite) Validate() error

Validate reports whether the sprite's style and drawable parameters are valid. It does not resolve referenced assets.

type TextSource

type TextSource struct {
	// Font is the ID of the font asset to render with.
	Font asset.ID
	// Text is the single line to draw. Empty text draws nothing.
	Text string
	// Size is the font size in pixels before sprite scaling.
	Size float64
}

TextSource draws one line of text centered on the sprite's position. The sprite's scale applies to the font size.

type TextureSource

type TextureSource struct {
	// Texture is the ID of the texture asset to draw.
	Texture asset.ID
}

TextureSource draws a whole standalone texture.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL