Documentation
¶
Overview ¶
Package graphics provides rendering functionality including sprites, textures, cameras, and asset management.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AssetManager ¶
type AssetManager struct {
// contains filtered or unexported fields
}
AssetManager manages texture loading and caching.
func NewAssetManager ¶
func NewAssetManager(renderer *sdl.Renderer) *AssetManager
NewAssetManager creates a new asset manager.
func (*AssetManager) LoadTexture ¶
func (am *AssetManager) LoadTexture(path string) (*Texture, error)
LoadTexture loads a texture from disk or returns cached
Parameters:
path: File path (PNG or JPEG)
Returns:
*Texture: Loaded texture error: Non-nil if file not found or decode fails
Behavior:
- Returns existing texture if already loaded
- Increments reference count
- Caches texture
Example:
texture, err := assets.LoadTexture("assets/player.png")
if err != nil {
log.Fatal(err)
}
func (*AssetManager) UnloadTexture ¶
func (am *AssetManager) UnloadTexture(path string)
UnloadTexture decrements reference count
Parameters:
path: File path of texture to unload
Behavior:
- Decrements reference count
- Unloads if count reaches zero
- Safe to call multiple times
- No-op if texture not loaded
Example:
assets.UnloadTexture("assets/player.png")
type Camera ¶
type Camera struct {
Position gamemath.Vector2 // Camera center in world space
Zoom float64 // Zoom factor (1.0 = normal, >1.0 = zoomed in)
// contains filtered or unexported fields
}
Camera defines view transformation from world to screen space.
func NewCamera ¶
func NewCamera() *Camera
NewCamera creates a camera at origin with no zoom
Returns:
*Camera: Camera at (0,0) with zoom 1.0
func (*Camera) Follow ¶
Follow smoothly moves camera toward target
Parameters:
targetX, targetY: Target world position smoothing: Interpolation factor (0.0 = instant, 1.0 = no follow)
Example:
camera.Follow(player.Transform.Position.X, player.Transform.Position.Y, 0.1)
func (*Camera) ScreenToWorld ¶
ScreenToWorld transforms screen pixels to world coordinates
Parameters:
screenX, screenY: Screen pixel coordinates
Returns:
worldX, worldY: World coordinates
Example:
worldX, worldY := camera.ScreenToWorld(mouseX, mouseY) entities := scene.GetEntitiesAt(worldX, worldY)
func (*Camera) SetScreenSize ¶
SetScreenSize updates the camera's screen dimensions (called by engine on resize).
func (*Camera) WorldToScreen ¶
WorldToScreen transforms world coordinates to screen pixels
Parameters:
worldX, worldY: World coordinates
Returns:
screenX, screenY: Screen pixel coordinates
Example:
screenX, screenY := camera.WorldToScreen(entity.Transform.Position.X, entity.Transform.Position.Y)
type Font ¶
type Font struct {
// contains filtered or unexported fields
}
Font represents a loaded TTF font.
func LoadFont ¶
LoadFont loads a TTF font from file.
Parameters:
path: Path to TTF font file size: Font size in points
Returns:
*Font: Loaded font error: Non-nil if font loading fails
Example:
font, err := graphics.LoadFont("/System/Library/Fonts/Helvetica.ttc", 24)
func (*Font) RenderText ¶
func (f *Font) RenderText(renderer *sdl.Renderer, text string, color gamemath.Color) (*sdl.Texture, int32, int32, error)
RenderText renders text to a texture.
Parameters:
renderer: SDL renderer text: Text to render color: Text color
Returns:
*sdl.Texture: Rendered text texture int32: Texture width int32: Texture height error: Non-nil if rendering fails
type Renderer ¶
type Renderer struct {
// contains filtered or unexported fields
}
Renderer wraps SDL2 rendering operations.
func NewRenderer ¶
NewRenderer creates a renderer from an SDL renderer.
func (*Renderer) DrawSprite ¶
DrawSprite renders a sprite at the specified transform with camera transform applied.
func (*Renderer) GetSDLRenderer ¶
GetSDLRenderer returns the underlying SDL renderer (for internal use).
type Sprite ¶
type Sprite struct {
Texture *Texture // Loaded texture (via AssetManager)
SourceRect gamemath.Rectangle // Region of texture to render (for sprite sheets)
Color gamemath.Color // Tint color (white = no tint)
Alpha float64 // Opacity (0.0 = transparent, 1.0 = opaque)
FlipH bool // Flip horizontally
FlipV bool // Flip vertically
}
Sprite represents a visual representation attached to entities.
func NewSprite ¶
NewSprite creates a sprite from a texture
Parameters:
texture: Loaded texture
Returns:
*Sprite: Sprite rendering full texture
Example:
texture, _ := assets.LoadTexture("player.png")
sprite := graphics.NewSprite(texture)
func (*Sprite) SetColor ¶
SetColor sets the tint color
Parameters:
color: RGBA tint (white = no tint)
Example:
sprite.SetColor(math.Color{R: 255, G: 0, B: 0, A: 255}) // Red tint
func (*Sprite) SetSourceRect ¶
SetSourceRect sets the sprite sheet region
Parameters:
x, y: Top-left corner in texture width, height: Region dimensions
Example:
// Extract 32x32 sprite from sprite sheet sprite.SetSourceRect(64, 0, 32, 32)
type TextRenderer ¶
type TextRenderer struct {
// contains filtered or unexported fields
}
TextRenderer provides high-level text rendering with caching.
func NewTextRenderer ¶
func NewTextRenderer(renderer *sdl.Renderer, font *Font) *TextRenderer
NewTextRenderer creates a new text renderer.
func (*TextRenderer) DrawText ¶
DrawText renders text at a position.
Parameters:
text: Text to render x, y: Screen position (top-left corner) color: Text color
Returns:
error: Non-nil if rendering fails
Example:
err := textRenderer.DrawText("Score: 100", 10, 10, gamemath.White)
func (*TextRenderer) MeasureText ¶
func (tr *TextRenderer) MeasureText(text string) (int, int, error)
MeasureText returns the dimensions of rendered text.
Parameters:
text: Text to measure
Returns:
width: Text width in pixels height: Text height in pixels error: Non-nil if measurement fails
type Texture ¶
type Texture struct {
Width int // Texture width in pixels
Height int // Texture height in pixels
Path string // Source file path
// contains filtered or unexported fields
}
Texture represents a loaded image texture.
func NewTexture ¶
NewTexture creates a new texture wrapper around an SDL texture.
func (*Texture) GetSDLTexture ¶
GetSDLTexture returns the underlying SDL texture (for internal use).