render

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2026 License: Apache-2.0 Imports: 10 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 SpawnEngineCamera

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

SpawnEngineCamera adds the engine's default primary camera at the world origin with zoom 1. The Collector uses it when no user 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 {
	// Zoom is the world-to-screen scale and must be positive.
	Zoom float64
	// Primary marks this camera as eligible for rendering.
	Primary bool
}

Camera defines the framing for a camera entity. Pair it with a core.Transform to set its position. The renderer uses a primary camera and interpolates its position like any other entity. A user-spawned primary takes precedence over the engine's default camera.

The runner owns the logical resolution; keeping a camera within level bounds is the responsibility of game systems.

func (Camera) Validate

func (c Camera) Validate() error

Validate reports an error when the camera's zoom is zero or negative.

type CameraView

type CameraView struct {
	// Position is the interpolated camera position in world space.
	Position geom.Vector2
	// Zoom is the scale used to project between world and screen coordinates.
	Zoom float64
}

CameraView is a camera's resolved position and zoom for a rendered frame.

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 relative to the center of a render target of the given size. It rounds each coordinate to the nearest whole 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, culls to the camera viewport, and sorts items by layer, sort order, then world Y. Text bounds use asset.FontData.Measure.

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