render

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 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 camera state and collects sprites into ordered DrawList values for a renderer to draw.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewCamera added in v0.4.0

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

NewCamera 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 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
	// contains filtered or unexported fields
}

Camera defines the framing of a camera entity.

Its core.Transform supplies the position, rotation, and uniform zoom; zero scale components mean 1. Collector.Collect writes the resolved framing to the selected camera component. Framing methods report false until the component has received a framing; copies retain their previous framing.

func (Camera) Center added in v0.4.0

func (c Camera) Center() (geom.Vector2, bool)

Center returns the world-space center of the visible rectangle.

func (Camera) Position added in v0.4.0

func (c Camera) Position() (geom.Vector2, bool)

Position returns the framing's world-space position after interpolation and bounds clamping.

func (Camera) Rotation added in v0.4.0

func (c Camera) Rotation() (float64, bool)

Rotation returns the framing's rotation in radians.

func (Camera) ScreenToWorld added in v0.4.0

func (c Camera) ScreenToWorld(screen geom.Vector2) (geom.Vector2, bool)

ScreenToWorld converts a point on the collected viewport to world coordinates without rounding.

func (Camera) Translate added in v0.4.0

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

Translate 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 the camera bounds, accounting for the logical viewport dimensions in pixels, zoom, rotation, and anchor. A zero displacement only constrains the position. If the view cannot fit on an axis, its center is pinned to the bounds center on that axis.

Translate returns an error for a nonpositive viewport size, invalid anchor or zoom, non-finite position, displacement, or rotation, or a non-finite result. Scale components of zero mean 1; the resulting scale must be positive, finite, and uniform.

func (Camera) Validate

func (c Camera) Validate() error

Validate returns an error if either anchor coordinate is NaN or outside [-0.5, 0.5].

func (Camera) VisibleRect added in v0.4.0

func (c Camera) VisibleRect() (geom.Rect, bool)

VisibleRect returns the axis-aligned world-space rectangle enclosing the view. For a rotated view, the rectangle includes areas outside the view.

func (Camera) WorldToScreen added in v0.4.0

func (c Camera) WorldToScreen(world geom.Vector2) (geom.Vector2, bool)

WorldToScreen projects a world-space point onto the collected viewport, rounded to the nearest pixel.

func (Camera) Zoom

func (c Camera) Zoom() (float64, bool)

Zoom returns the framing's zoom.

type CameraResolver added in v0.4.0

type CameraResolver struct {
	// contains filtered or unexported fields
}

CameraResolver selects and resolves the world's primary camera.

func NewCameraResolver added in v0.4.0

func NewCameraResolver(world *core.World) *CameraResolver

NewCameraResolver creates a resolver for world. Eligible cameras must have core.Transform and core.PrevTransform components.

func (*CameraResolver) FoldedCamera added in v0.4.0

func (r *CameraResolver) FoldedCamera() (Camera, bool)

FoldedCamera returns the primary camera component with its most recently collected framing. It reports false if no primary camera is available or the selected component has not received a framing.

func (*CameraResolver) Primary added in v0.4.0

func (r *CameraResolver) Primary() (core.Entity, bool)

Primary returns the world's primary camera entity, preferring user cameras over the engine camera. Among equally preferred cameras, the first in query order wins. It reports false if no eligible primary camera is available.

type Collector

type Collector struct {

	// SortedItems is the storage behind the items returned by [Collector.Collect].
	SortedItems []DrawItem
	// contains filtered or unexported fields
}

Collector collects draw items for a world and can be reused across frames.

func NewCollector

func NewCollector(world *core.World) *Collector

NewCollector creates a Collector for world.

func (*Collector) Collect

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

Collect resolves the draw list for the current frame using alpha to interpolate transforms. An alpha of 1 uses current transforms.

Items outside the camera view are culled; the rest are sorted by layer, sort order, and world Y. Ties preserve class order: sprites, primitives, text, then animators. An animator contributes one item per weighted frame layer.

If no primary camera is available, Collect returns an empty list without an error. Otherwise the resolved framing is folded into the primary camera's Camera component for the framing getters, before items are collected. It returns an error if camera, resource, asset, or animation resolution fails.

func (*Collector) FoldedCamera added in v0.4.0

func (c *Collector) FoldedCamera() (Camera, bool)

FoldedCamera returns the primary camera's component with the framing folded by the most recent successful Collector.Collect with a camera.

type DrawItem

type DrawItem struct {
	// 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
	// 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
	// contains filtered or unexported fields
}

DrawItem describes one drawable for a frame.

func (DrawItem) Payload added in v0.4.0

func (d DrawItem) Payload() DrawPayload

Payload returns the draw data for the item, or nil for a zero DrawItem.

type DrawList

type DrawList struct {
	// Items is the sorted set of drawables.
	// When returned by [Collector.Collect], the slice is collector-owned; copy it
	// before modifying it or retaining it across later collection calls.
	Items []DrawItem
}

DrawList contains the sorted draw items for one frame.

type DrawPayload added in v0.4.0

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

DrawPayload holds the kind-specific data for a DrawItem. The collector emits TextureDraw, TextDraw, or ShapeDraw values.

type ShapeDraw added in v0.4.0

type ShapeDraw struct {
	// Shape supplies the geometry in local space.
	Shape sprite.DrawShape
	// Outline draws the shape's border instead of filling it; it has no effect on lines.
	Outline bool
	// StrokeWidth is the line or outline width in world units.
	StrokeWidth float64
}

ShapeDraw describes a geometric shape to draw.

type StaticWindow added in v0.4.0

type StaticWindow struct {
	// contains filtered or unexported fields
}

StaticWindow is a Window for headless runs and tests that build a bare world. Its controls do nothing. [castrum.New] provides one as the default window until a runner replaces it.

func NewStaticWindow added in v0.4.0

func NewStaticWindow(v Viewport) *StaticWindow

NewStaticWindow returns a StaticWindow reporting v. It panics if either the logical or the window dimensions are not positive.

func (*StaticWindow) SetBordered added in v0.4.0

func (w *StaticWindow) SetBordered(bordered bool)

SetBordered does nothing.

func (*StaticWindow) SetCursorVisible added in v0.4.0

func (w *StaticWindow) SetCursorVisible(visible bool)

SetCursorVisible does nothing.

func (*StaticWindow) SetFullscreen added in v0.4.0

func (w *StaticWindow) SetFullscreen(fullscreen bool)

SetFullscreen does nothing.

func (*StaticWindow) SetLogicalSize added in v0.4.0

func (w *StaticWindow) SetLogicalSize(width, height int)

SetLogicalSize does nothing; the viewport is fixed at construction.

func (*StaticWindow) SetResizable added in v0.4.0

func (w *StaticWindow) SetResizable(resizable bool)

SetResizable does nothing.

func (*StaticWindow) SetSize added in v0.4.0

func (w *StaticWindow) SetSize(width, height int)

SetSize does nothing.

func (*StaticWindow) SetTitle added in v0.4.0

func (w *StaticWindow) SetTitle(title string)

SetTitle does nothing.

func (*StaticWindow) SetVSync added in v0.4.0

func (w *StaticWindow) SetVSync(enabled bool)

SetVSync does nothing.

func (*StaticWindow) Viewport added in v0.4.0

func (w *StaticWindow) Viewport() Viewport

Viewport returns the current viewport.

func (*StaticWindow) WindowState added in v0.4.0

func (w *StaticWindow) WindowState() WindowState

WindowState returns the zero state; the controls do nothing.

type TextDraw added in v0.4.0

type TextDraw struct {
	// Text is the text to draw.
	Text string
	// Font is the font asset for the text.
	Font asset.ID
	// Size is the font size in pixels.
	Size float64
	// Bounds is the measured width and height in pixels before applying scale.
	Bounds geom.Vector2
}

TextDraw describes measured text ready to draw.

type TextureDraw added in v0.4.0

type TextureDraw struct {
	// Texture identifies the source texture.
	Texture asset.ID
	// Rect selects the source region in pixels; an empty rectangle selects the whole texture.
	Rect image.Rectangle
	// FlipH mirrors the image horizontally without changing the item transform.
	FlipH bool
	// FlipV mirrors the image vertically without changing the item transform.
	FlipV bool
}

TextureDraw describes a texture or a region of a texture to draw.

type Viewport added in v0.4.0

type Viewport struct {
	// LogicalWidth is the internal render resolution's width in pixels.
	LogicalWidth int
	// LogicalHeight is the internal render resolution's height in pixels.
	LogicalHeight int
	// WindowWidth is the window's current width in pixels.
	WindowWidth int
	// WindowHeight is the window's current height in pixels.
	WindowHeight int
}

Viewport describes the logical render target and the window that presents it. The runner owns the current value, publishes it through Window.Viewport, and updates it when the window is resized.

func (Viewport) Center added in v0.4.0

func (v Viewport) Center() geom.Vector2

Center returns the center of the logical render target, in logical pixels.

func (Viewport) ScaleX added in v0.4.0

func (v Viewport) ScaleX() float64

ScaleX returns the horizontal scale from logical to window pixels, in window pixels per logical pixel. It returns infinity or NaN when LogicalWidth is zero.

func (Viewport) ScaleY added in v0.4.0

func (v Viewport) ScaleY() float64

ScaleY returns the vertical scale from logical to window pixels, in window pixels per logical pixel. It returns infinity or NaN when LogicalHeight is zero.

func (Viewport) Size added in v0.4.0

func (v Viewport) Size() geom.Vector2

Size returns the logical render size as a vector, in logical pixels.

func (Viewport) ToLogical added in v0.4.0

func (v Viewport) ToLogical(p geom.Vector2) geom.Vector2

ToLogical converts a point from window coordinates to logical coordinates, for example a raw cursor position to render coordinates.

func (Viewport) ToWindow added in v0.4.0

func (v Viewport) ToWindow(p geom.Vector2) geom.Vector2

ToWindow converts a point from logical coordinates to window coordinates.

type Window added in v0.4.0

type Window interface {
	// Viewport returns the current viewport. Read-only.
	Viewport() Viewport
	// WindowState returns the current window state. Read-only.
	WindowState() WindowState
	// SetTitle sets the window title.
	SetTitle(title string)
	// SetSize sets the window size in pixels.
	SetSize(width, height int)
	// SetFullscreen switches the window between fullscreen and windowed.
	SetFullscreen(fullscreen bool)
	// SetBordered controls whether the window has decorations such as a title
	// bar and border.
	SetBordered(bordered bool)
	// SetResizable controls whether the window can be resized.
	SetResizable(resizable bool)
	// SetVSync enables or disables vertical synchronization.
	SetVSync(enabled bool)
	// SetCursorVisible controls whether the cursor is visible over the window.
	SetCursorVisible(visible bool)
	// SetLogicalSize sets the logical render size in pixels.
	SetLogicalSize(width, height int)
}

Window is the presentation surface for a game. The runner provides it as a world resource; systems read the current Viewport and request changes through the Set methods.

Implementations must be safe to call from the game loop's thread, which is where systems run. Changes take effect on the runner's next window update.

type WindowState added in v0.4.0

type WindowState struct {
	Title                           string
	FullScreen, Bordered, Resizable bool
	Vsync, CursorVisible            bool
}

WindowState describes the current state of the window, including title, fullscreen, bordered, resizable, vsync, and cursor visibility.

Jump to

Keyboard shortcuts

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