graphics

package
v0.0.0-...-4776544 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2025 License: MIT Imports: 8 Imported by: 0

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) Destroy

func (am *AssetManager) Destroy()

Destroy unloads all textures.

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

func (c *Camera) Follow(targetX, targetY float64, smoothing float64)

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

func (c *Camera) ScreenToWorld(screenX, screenY int) (worldX, worldY float64)

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

func (c *Camera) SetScreenSize(width, height int)

SetScreenSize updates the camera's screen dimensions (called by engine on resize).

func (*Camera) WorldToScreen

func (c *Camera) WorldToScreen(worldX, worldY float64) (screenX, screenY int)

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

func LoadFont(path string, size int) (*Font, error)

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) Close

func (f *Font) Close()

Close closes the font and frees resources.

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

func NewRenderer(sdlRenderer *sdl.Renderer) *Renderer

NewRenderer creates a renderer from an SDL renderer.

func (*Renderer) Clear

func (r *Renderer) Clear(color gamemath.Color) error

Clear clears the screen with the specified color.

func (*Renderer) Destroy

func (r *Renderer) Destroy() error

Destroy releases renderer resources.

func (*Renderer) DrawSprite

func (r *Renderer) DrawSprite(sprite *Sprite, transform gamemath.Transform, camera *Camera) error

DrawSprite renders a sprite at the specified transform with camera transform applied.

func (*Renderer) GetSDLRenderer

func (r *Renderer) GetSDLRenderer() *sdl.Renderer

GetSDLRenderer returns the underlying SDL renderer (for internal use).

func (*Renderer) Present

func (r *Renderer) Present()

Present presents the rendered frame to the screen.

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

func NewSprite(texture *Texture) *Sprite

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

func (s *Sprite) SetColor(color gamemath.Color)

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

func (s *Sprite) SetSourceRect(x, y, width, height int)

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

func (tr *TextRenderer) DrawText(text string, x, y int, color gamemath.Color) error

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

func NewTexture(sdlTexture *sdl.Texture, width, height int, path string) *Texture

NewTexture creates a new texture wrapper around an SDL texture.

func (*Texture) Destroy

func (t *Texture) Destroy() error

Destroy releases the SDL texture resources.

func (*Texture) GetSDLTexture

func (t *Texture) GetSDLTexture() *sdl.Texture

GetSDLTexture returns the underlying SDL texture (for internal use).

Jump to

Keyboard shortcuts

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