texturepacker

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2021 License: BSD-3-Clause Imports: 4 Imported by: 0

README

texturepacker

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

Documentation

Package documentation is available on pkg.go.dev.

Example usage

Create a sprite sheet with TexturePacker and and save it in "JSON (Hash)" format.

TexturePacker format selection dialog

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)
		// ...
	}
}

License

This project is free and open source software licensed under the BSD 3-Clause License.

Documentation

Overview

Package texturepacker reads sprite sheets created and exported as JSON by TexturePacker: https://www.codeandweb.com/texturepacker

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FloatPoint

type FloatPoint struct{ X, Y float64 }

A FloatPoint is an X, Y coordinate pair.

type Format

type Format interface {
	SpriteSheetFrom(data []byte) (*SpriteSheet, error)
}

Format represents a generic parser for a sprite sheet data format.

type FormatJSONHash

type FormatJSONHash struct{}

FormatJSONHash implements the Format interface and is a parser for the TexturePacker output data format "JSON (Hash)".

func (FormatJSONHash) SpriteSheetFrom

func (f FormatJSONHash) SpriteSheetFrom(data []byte) (*SpriteSheet, error)

SpriteSheetFrom parses the given JSON data in the TexturePacker output data format "JSON (Hash)" and returns a sprite sheet.

type Metadata

type Metadata struct {
	App         string
	Version     string
	Image       string
	Format      string
	Size        image.Point
	Scale       string
	SmartUpdate string
}

The Metadata structure holds information about the sprite sheet image file and the application with which the sprite sheet was created.

type Sprite

type Sprite struct {
	Frame            image.Rectangle
	Rotated          bool
	Trimmed          bool
	SpriteSourceSize image.Rectangle
	SourceSize       image.Point
	Pivot            FloatPoint
}

A Sprite is a rectangular area within a sprite sheet image.

type SpriteSheet

type SpriteSheet struct {
	Sprites map[string]*Sprite
	Meta    *Metadata
}

A SpriteSheet is a collection of sprites packed into a single image. Each sprite has a name by which it can be looked up and a rectangular area within the sheet image. The sheet image file is referenced by its file name in the metadata structure.

func SheetFromData

func SheetFromData(data []byte, f Format) (*SpriteSheet, error)

SheetFromData parses sprite sheet information from data with the specified format.

func SheetFromFile

func SheetFromFile(path string, f Format) (*SpriteSheet, error)

SheetFromFile loads and parses sprite sheet information from a file with the specified format.

Jump to

Keyboard shortcuts

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