Package texturepacker reads sprite sheets created and exported as JSON by TexturePacker.
Add it to a module as a dependency via:
go get github.com/fzipp/texturepacker
Package documentation is available on pkg.go.dev.
Create a sprite sheet with TexturePacker and and save it in "JSON (Hash)" format.
package main
import (
"image"
_ "image/png"
"log"
"os"
"github.com/fzipp/texturepacker"
)
func main() {
sheet, err := texturepacker.SheetFromFile("ExampleSheet.json", texturepacker.FormatJSONHash{})
if err != nil {
log.Fatal(err)
}
imageFile, err := os.Open(sheet.Meta.Image)
if err != nil {
log.Fatal(err)
}
defer imageFile.Close()
img, _, err := image.Decode(imageFile)
sheetImage, ok := img.(image.RGBA)
if !ok {
log.Fatal("expected RGBA image")
}
for name, sprite := range sheet.Sprites {
spriteImage := sheetImage.SubImage(sprite.Frame)
// ...
}
}
This project is free and open source software licensed under the BSD 3-Clause License.