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.
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())
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
]
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
ton
entries, wheren
isrows*cols
- Using an underscore
_
skips the sprite. Useful if you have "holes" in your sprite sheet.