spritesheet

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2021 License: MIT Imports: 10 Imported by: 0

README

go-spritesheet

Go Reference

Use YAML to describe your sprite sheet and give your sprites names. go-spritesheet will take care of the math for each sprite location, as well as provide a map for easy sprite lookups.

Using With Ebiten

go-spritesheet pairs nicely with a library like ebiten.

// Load the config
sheet, err := spritesheet.OpenAndRead("spritesheet.yml")

if err != nil {
    panic(err)
}

// Load the image
img, _, err := ebitenutil.NewImageFromFile(sheet.Image)

if err != nil {
    panic(err)
}

sprites := sheet.Sprites()

// Get the sprite
s := img.SubImage(sprites["mySprite"].Rect())

YAML Example

image: hero.png

rows: 3
cols: 4
size: 16

sprites: [
    idle_1, idle_2, idle_3, idle_4,
    run_1, run_2, run_3, run_4,
    atk_1, atk_2, atk_3, atk_4
]

Config Format

  • image: The path to your sprite sheet image.
  • rows: The number of rows in the sprite sheet.
  • cols: The number of columns in the sprite sheet.
  • size: The size of each sprite, in pixels.
  • sprites: A list of sprite names.
    • Names must be unique.
    • Must contain 0 to n entries, where n is rows*cols
    • Using an underscore _ skips the sprite. Useful if you have "holes" in your sprite sheet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Sprite

type Sprite struct {
	Name  string
	Row   int
	Col   int
	Sheet *SpriteSheet
}

Sprite represents a single sprite within a sprite sheet.

func (*Sprite) Rect

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

Rect returns the area where this sprite is in the sprite sheet.

type SpriteSheet

type SpriteSheet struct {
	Rows, Cols int
	Size       int
	Image      string
	Names      []string `yaml:"sprites"`
}

SpriteSheet represents a sprite sheet config file loaded from YAML.

func OpenAndRead

func OpenAndRead(path string) (*SpriteSheet, error)

OpenAndRead reads and returns the sprite sheet config file at the given path.

func Read

func Read(r io.Reader) (*SpriteSheet, error)

Read reads a sprite sheet config file, parses it, and returns it.

func (*SpriteSheet) Sprites

func (ss *SpriteSheet) Sprites() map[string]*Sprite

Sprites returns a map of all the sprites declared in the sprite sheet. The map keys are the sprite names.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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