render

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package render provides the graphics layer for the game.

Camera handles world-to-screen coordinate transformation with zoom and pan. Sprite wraps directional animations loaded from sprite sheets, with automatic horizontal flip to generate left/right variants. TextWriter renders text using loaded fonts. TileSize (16px) defines the base tile dimension used across the rendering pipeline.

Index

Constants

View Source
const (
	// DefaultMaxZoomForText is the default maximum zoom level for text visibility
	// when scaling is disabled. Text will be hidden at zoom levels above this value.
	DefaultMaxZoomForText = 5.0

	// DefaultNameplateGapPixels is the default pixel gap between the bottom
	// of the nameplate and the visible top of the sprite. Constant in pixels
	// at any camera zoom.
	DefaultNameplateGapPixels = 4.0
)
View Source
const (
	TileSize = 16
)

Variables

View Source
var (
	MirroredAnimations = map[AnimationType]AnimationType{
		AnimationIdleLeft:   AnimationIdleRight,
		AnimationMoveLeft:   AnimationMoveRight,
		AnimationAttackLeft: AnimationAttackRight,
	}
)

MirroredAnimations maps a left-facing animation to the right-facing animation it is rendered from by a horizontal flip, so left variants need not be authored separately.

View Source
var (
	TextDefault = NewTextWriter()
)
View Source
var UsePlaceholderSpriteImages bool

UsePlaceholderSpriteImages, when true, makes Sprite.Image return a placeholder for a missing animation type instead of panicking. Set by engine configuration.

Functions

This section is empty.

Types

type Animation

type Animation struct {
	Images   []*ebiten.Image
	Duration time.Duration
}

Animation represents a sequence of images forming an animation.

type AnimationType

type AnimationType int

AnimationType represents different animation states for sprites.

const (
	AnimationDefault     AnimationType = 0
	AnimationMoveUp      AnimationType = 1
	AnimationMoveDown    AnimationType = 2
	AnimationMoveLeft    AnimationType = 3
	AnimationMoveRight   AnimationType = 4
	AnimationIdleUp      AnimationType = 5
	AnimationIdleDown    AnimationType = 6
	AnimationIdleLeft    AnimationType = 7
	AnimationIdleRight   AnimationType = 8
	AnimationAttackUp    AnimationType = 9
	AnimationAttackDown  AnimationType = 10
	AnimationAttackLeft  AnimationType = 11
	AnimationAttackRight AnimationType = 12
)

func AttackAnimation

func AttackAnimation(direction geometry.Vector2) AnimationType

AttackAnimation returns the attack animation matching the facing direction (for actors without a movement direction, defaults to down).

func IdleAnimation

func IdleAnimation(direction geometry.Vector2) AnimationType

func MoveAnimation

func MoveAnimation(direction geometry.Vector2) AnimationType

MoveAnimation returns the appropriate movement animation type for a direction.

func (AnimationType) String

func (i AnimationType) String() string

type Camera

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

Camera represents the game's camera.

func NewCamera

func NewCamera(screenWidth, screenHeight int) *Camera

NewCamera creates and returns a new Camera with default values.

func NewScreenCamera

func NewScreenCamera(screenWidth, screenHeight int) *Camera

NewScreenCamera creates a camera for screen-space rendering. This camera uses an identity transformation (no zoom, no world offset), making it suitable for UI elements that should remain in screen coordinates.

func (*Camera) AddZoom

func (c *Camera) AddZoom(delta float64)

AddZoom adjusts the zoom level by delta, clamped to the camera's limits.

func (*Camera) Adjust

func (c *Camera) Adjust(op *ebiten.DrawImageOptions, p geometry.Vector2)

Adjust applies the camera transform in-place to op for a world position p given in tile units (scaled by TileSize). It is the tile-space counterpart to DrawImageOptions.

func (*Camera) CameraDebugInfo

func (c *Camera) CameraDebugInfo() string

CameraDebugInfo returns a human-readable string of the camera's position and effective zoom, for debug overlays.

func (*Camera) DrawImageOptions

func (c *Camera) DrawImageOptions(p geometry.Vector2) *ebiten.DrawImageOptions

DrawImageOptions returns draw options that map a pixel-space position p into screen space under the current camera transform. Unlike Adjust, p is in pixels (not tile units) and a fresh options value is returned.

func (*Camera) EffectiveZoom

func (c *Camera) EffectiveZoom() float64

EffectiveZoom returns the screen-adjusted zoom actually applied to the world-to-screen transform: the user zoom scaled by a screen-size normalization factor so that a given user zoom frames the same number of tiles on any resolution.

func (*Camera) MaxZoom

func (c *Camera) MaxZoom() float64

MaxZoom returns the maximum user-level zoom the camera clamps to.

func (*Camera) MinZoom

func (c *Camera) MinZoom() float64

MinZoom returns the minimum user-level zoom the camera clamps to.

func (*Camera) Move

func (c *Camera) Move(delta geometry.Vector2)

Move moves the camera by the given delta.

func (*Camera) Position

func (c *Camera) Position() geometry.Vector2

Position returns the camera's position.

func (*Camera) ScreenHeight

func (c *Camera) ScreenHeight() int

ScreenHeight returns the camera's screen height.

func (*Camera) ScreenToWorld

func (c *Camera) ScreenToWorld(screenPos geometry.Vector2) geometry.Vector2

ScreenToWorld converts screen coordinates to world coordinates.

func (*Camera) ScreenWidth

func (c *Camera) ScreenWidth() int

ScreenWidth returns the camera's screen width.

func (*Camera) SetPosition

func (c *Camera) SetPosition(pos geometry.Vector2)

SetPosition sets the camera's position.

func (*Camera) SetZeroAsCenter

func (c *Camera) SetZeroAsCenter()

SetZeroAsCenter sets the camera's position so that (0,0) in world space is at the center of the screen.

func (*Camera) SetZeroAsTopLeft

func (c *Camera) SetZeroAsTopLeft()

SetZeroAsTopLeft sets the camera's position so that (0,0) in world space is at the top-left of the screen.

func (*Camera) SetZoom

func (c *Camera) SetZoom(zoom float64)

SetZoom sets the camera's zoom level, clamped to the camera's limits.

func (*Camera) WorldToScreen

func (c *Camera) WorldToScreen(worldPos geometry.Vector2) geometry.Vector2

WorldToScreen converts world coordinates to screen coordinates.

func (*Camera) Zoom

func (c *Camera) Zoom() float64

Zoom returns the camera's user-level zoom. This is the value passed to SetZoom/AddZoom and is independent of screen size; 1.0 is the default framing.

type CameraController

type CameraController struct {
	// Camera is the camera this controller drives.
	Camera *Camera
	// MoveSpeed is the pan speed in world units per frame, before zoom scaling.
	MoveSpeed float64
	// ZoomSpeed is the zoom increment applied per input step.
	ZoomSpeed float64
	// contains filtered or unexported fields
}

CameraController drives a Camera from user input. It implements the engine's default pan/zoom control scheme: WASD keyboard panning, Q/E and mouse-wheel zoom, and middle-mouse-button drag panning. Games wanting a different scheme can drive the Camera directly instead of attaching a controller.

func NewCameraController

func NewCameraController(camera *Camera) *CameraController

NewCameraController returns a controller driving the given camera with the engine's default pan and zoom speeds.

func (*CameraController) CursorWorldPosition

func (cc *CameraController) CursorWorldPosition() geometry.Vector2

CursorWorldPosition returns the OS cursor position converted to world coordinates through the controller's camera.

func (*CameraController) HandleInput

func (cc *CameraController) HandleInput()

HandleInput reads input for the current frame and pans/zooms the camera.

type Drawable

type Drawable interface {
	Draw(screen *ebiten.Image, c *Camera)
}

Drawable is implemented by anything that can render itself to screen using a Camera for the world-to-screen transform.

type Sprite

type Sprite struct {
	Animations   map[AnimationType]*Animation
	ZeroPosition geometry.Vector2
	Scale        float64
	Type         SpriteType
	// contains filtered or unexported fields
}

Sprite represents a game sprite with animations.

func LoadSprite

func LoadSprite(img *ebiten.Image, width, height int, indexes map[AnimationType][]int, durations map[AnimationType]time.Duration) (*Sprite, error)

LoadSprite slices img into a grid of width columns by height rows, assigns the frame indexes in indexes to each AnimationType, and sets per-animation durations (defaulting to one second when unspecified). Note: width and height are column/row counts, not pixel dimensions.

func MustLoadSprite

func MustLoadSprite(img *ebiten.Image, width, height int, indexes map[AnimationType][]int, durations map[AnimationType]time.Duration) *Sprite

MustLoadSprite is like LoadSprite but panics on error.

func NewSprite

func NewSprite() *Sprite

NewSprite creates and returns a new Sprite with default values.

func (*Sprite) AddImage

func (s *Sprite) AddImage(animationType AnimationType, img *ebiten.Image)

AddImage adds an image to the specified animation type.

func (*Sprite) AllAnimations

func (s *Sprite) AllAnimations() []AnimationType

AllAnimations returns a slice of all animation types the sprite can draw.

func (*Sprite) CanAnimate

func (s *Sprite) CanAnimate(a AnimationType) bool

CanAnimate checks if the sprite can perform a specific animation, either directly or by flipping an existing animation.

func (*Sprite) Draw

func (s *Sprite) Draw(screen *ebiten.Image, c *Camera, p geometry.Vector2, a AnimationType)

Draw draws the sprite at the given position with the specified animation type.

func (*Sprite) DrawAnimation

func (s *Sprite) DrawAnimation(screen *ebiten.Image, c *Camera, p geometry.Vector2, a AnimationType, duration time.Duration)

DrawAnimation draws the sprite at the given position with the specified animation type.

func (*Sprite) HasAnimation

func (s *Sprite) HasAnimation(a AnimationType) bool

HasAnimation checks if the sprite has a specific animation defined.

func (*Sprite) Image

func (s *Sprite) Image(animationType AnimationType) *ebiten.Image

Image returns the first image of the specified animation type. It logs a fatal error if the animation type does not exist.

func (*Sprite) SetScale

func (s *Sprite) SetScale(scale float64) *Sprite

SetScale sets the scale of the sprite.

func (*Sprite) SetType

func (s *Sprite) SetType(spriteType SpriteType) *Sprite

SetType sets the type of the sprite.

func (*Sprite) SetZeroPosition

func (s *Sprite) SetZeroPosition(pos geometry.Vector2) *Sprite

SetZeroPosition sets the zero position of the sprite.

func (*Sprite) VisibleBounds

func (s *Sprite) VisibleBounds() image.Rectangle

VisibleBounds returns the bounding rectangle of non-transparent pixels in one frame, expressed in frame-local pixel coordinates. Cached after first call. Returns an empty rectangle if no visible content is found.

func (*Sprite) VisibleTopAboveZero

func (s *Sprite) VisibleTopAboveZero() float64

VisibleTopAboveZero returns the number of pixels the visible sprite content (non-transparent pixels) extends above ZeroPosition in one frame. Frames across animations share the same size and layout, so the result is computed once from any available frame and cached.

Use this instead of the raw frame height when placing UI elements (like nameplates) above a sprite, so transparent padding at the top of the frame is not counted as part of the visible sprite.

type SpriteType

type SpriteType int

SpriteType represents different types of sprites in the game.

const (
	SpriteTypeUnknown SpriteType = 0
	SpriteTypeActor   SpriteType = 1
	SpriteTypeTerrain SpriteType = 2
)

func (SpriteType) String

func (i SpriteType) String() string

type TextAlignment

type TextAlignment int

TextAlignment represents horizontal text alignment options.

const (
	// AlignLeft aligns text to the left.
	AlignLeft TextAlignment = iota
	// AlignCenter centers text horizontally.
	AlignCenter
	// AlignRight aligns text to the right.
	AlignRight
)

type TextWriter

type TextWriter struct {
	Size              int
	Font              *text.GoTextFaceSource
	Color             color.Color
	Background        *color.Color  // Optional background color
	BackgroundPadding int           // Padding around background (default: 2)
	Scaling           bool          // Whether text scales with camera zoom
	MaxZoom           float64       // Max zoom level for text visibility (0 = use default)
	Align             TextAlignment // Horizontal alignment
	// contains filtered or unexported fields
}

TextWriter provides a fluent API for rendering text with various styling options.

func NewTextWriter

func NewTextWriter() *TextWriter

func (*TextWriter) Clear

func (t *TextWriter) Clear() *TextWriter

Clear removes all built text segments, allowing the builder to be reused with different text content while keeping the same styling configuration.

func (*TextWriter) ColoredText

func (t *TextWriter) ColoredText(msg string, c color.Color) *TextWriter

ColoredText adds a text segment with a specific color to the builder. This allows creating multi-colored text without string templating overhead.

func (*TextWriter) Draw

func (t *TextWriter) Draw(screen *ebiten.Image, camera *Camera, position geometry.Vector2)

Draw renders the built text segments to the screen at the specified position. The position is in world coordinates, which are converted to screen coordinates using the camera's WorldToScreen transformation.

When scaling is disabled (WithScaling(false)), text maintains fixed pixel size and is hidden at zoom levels above the configured maximum zoom threshold.

The text segments are rendered with the configured alignment and optional background.

func (*TextWriter) Print

func (t *TextWriter) Print(screen *ebiten.Image, x, y int, msg string)

func (*TextWriter) Printf

func (t *TextWriter) Printf(screen *ebiten.Image, x, y int, format string, a ...interface{})

func (*TextWriter) RenderedHeight

func (t *TextWriter) RenderedHeight() float64

RenderedHeight returns the pixel height of the text as it would be drawn without scaling. Scaling-enabled TextWriters multiply this by camera zoom at draw time; this method does not apply that multiplier.

func (*TextWriter) Text

func (t *TextWriter) Text(msg string) *TextWriter

Text adds a text segment with the current color to the builder. This allows building multi-segment text where each segment can have different styling.

func (*TextWriter) WithAlignment

func (t *TextWriter) WithAlignment(align TextAlignment) *TextWriter

WithAlignment sets the horizontal alignment of the text.

func (*TextWriter) WithBackground

func (t *TextWriter) WithBackground(bg color.Color) *TextWriter

WithBackground sets an optional background color for the text. The background is rendered as a rectangle behind the text with padding.

func (*TextWriter) WithBackgroundPadding

func (t *TextWriter) WithBackgroundPadding(padding int) *TextWriter

WithBackgroundPadding sets the padding around the background rectangle. Default padding is 2 pixels if not specified.

func (*TextWriter) WithColor

func (t *TextWriter) WithColor(color color.Color) *TextWriter

func (*TextWriter) WithFont

func (t *TextWriter) WithFont(font *text.GoTextFaceSource) *TextWriter

func (*TextWriter) WithMaxZoom

func (t *TextWriter) WithMaxZoom(maxZoom float64) *TextWriter

WithMaxZoom sets the maximum effective zoom at which fixed-size text remains visible; above it the text is hidden. It only applies when scaling is disabled (see WithScaling). A zero value uses DefaultMaxZoomForText.

func (*TextWriter) WithScaling

func (t *TextWriter) WithScaling(enabled bool) *TextWriter

WithScaling configures whether text scales with camera zoom. When disabled, text maintains a fixed pixel size regardless of zoom (and is hidden above the MaxZoom threshold; see WithMaxZoom).

func (*TextWriter) WithSize

func (t *TextWriter) WithSize(size int) *TextWriter

Jump to

Keyboard shortcuts

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