tmx

package module
v0.0.0-...-ef401b7 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2016 License: MIT Imports: 20 Imported by: 0

README

GoDoc Build Status Coverage Status Go Report Card

TMX Map File Loader

This repository aims to provide go support for maps that are saved according the the TMX Map Format

Support

This library currently supports loading of base64 encoded tile maps with either gzip, zlip or no compression.

Usage

Currently the library only provides functionality to load maps, in the future it should provide utility functions to make using tmx files even more convenient.

Renderer

To generate a preview image of your tilemap you can use the Render function:

  testfile := "example.tmx"

  reader, err := os.Open(testfile)

  m, err := tmx.NewMap(reader)
  if err != nil {
    log.Fatal(err)
  }

  canvas := tmx.NewImageCanvasFromMap(*m)

  renderer := tmx.NewRenderer(*m, canvas)
  err = renderer.Render()
  if err != nil {
    log.Fatal(err)
  }

  target, err := os.Create("result.png")
  if err != nil {
    target, err = os.Open("result.png")
    if err != nil {
      log.Fatal(err)
    }
  }
  defer target.Close()

  err = png.Encode(target, canvas.Image())
  if err != nil {
    log.Fatal(err)
  }

The renderer is still a work in progress and currently only renders tiles and layers.

Documentation

Overview

Package tmx provides an TMX file loader specification: http://doc.mapeditor.org/reference/tmx-map-format/

Index

Constants

View Source
const (
	//GIDHorizontalFlip for horizontal flipped tiles
	GIDHorizontalFlip = 0x80000000
	//GIDVerticalFlip for vertical flipped tiles
	GIDVerticalFlip = 0x40000000
	//GIDDiagonalFlip for diagonally flipped tile
	GIDDiagonalFlip = 0x20000000
	//GIDFlips removes all flipping informations
	GIDFlips = GIDHorizontalFlip | GIDVerticalFlip | GIDDiagonalFlip
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Animation

type Animation struct {
	Frames []*Frame `xml:"frame"`
	// contains filtered or unexported fields
}

Animation references an animated tile

func (Animation) GetFrame

func (a Animation) GetFrame() *Frame

GetFrame returns the frame that will be drawn

func (*Animation) Update

func (a *Animation) Update(elapsedTime int64)

Update the animation with elapsedTime since last update

type Canvas

type Canvas interface {
	FillRect(what color.Color, where image.Rectangle)
	Bounds() image.Rectangle
}

Canvas to draw on

type Data

type Data struct {
	Encoding    string     `xml:"encoding,attr"`
	Compression string     `xml:"compression,attr"`
	RawData     []byte     `xml:",innerxml"`
	DataTiles   []DataTile `xml:"tile"`
}

Data contains raw loaded tmx data

type DataTile

type DataTile struct {
	GID GID `xml:"gid,attr"`
	//HorizontalFlip true if the tile should be rendered with flip in x dir
	HorizontalFlip bool
	//VerticalFlip true if the tile should be rendered with flip in y dir
	VerticalFlip bool
	//DiagonalFlip true if it should be rendered flipped diagonally, can be combined
	//with `HorizontalFlip` or `VerticalFlip`
	DiagonalFlip bool
}

DataTile datatile

type FilesystemLocator

type FilesystemLocator struct {
}

FilesystemLocator loads files simply from the filesystem it supports png, jpeg and non animated gifs

func (FilesystemLocator) LocateResource

func (f FilesystemLocator) LocateResource(filepath string) (image.Image, error)

LocateResource to implement ResourceLocator interface

type FlipMode

type FlipMode uint8

FlipMode gives the mode to flip

const (
	//FlipDiagonal if the tile is diagonally flipped
	FlipDiagonal FlipMode = iota
	//FlipHorizontal if the tile is horizontally flipped
	FlipHorizontal
	//FlipVertical if the tile is vertically flipped
	FlipVertical
	//FlipNone if the tile is not flipped
	FlipNone
)

func (FlipMode) String

func (f FlipMode) String() string

type Frame

type Frame struct {
	//Duration is given in milliseconds
	Duration int64 `xml:"duration,attr"`
	TileID   int   `xml:"tileid,attr"`
	// contains filtered or unexported fields
}

Frame is one frame of an animation

type GID

type GID uint32

GID is a global tile id

type Image

type Image struct {
	Source string `xml:"source,attr"`
	Trans  string `xml:"trans,attr"`
	Width  int    `xml:"width,attr"`
	Height int    `xml:"height,attr"`
}

Image refers to the image of one tile or the tileset

type ImageCanvas

type ImageCanvas interface {
	Canvas
	Draw(what image.Image, where image.Rectangle)
}

ImageCanvas will always draw the exact image to where

type ImgCanvas

type ImgCanvas struct {
	// contains filtered or unexported fields
}

ImgCanvas is a sample renderer that renders on a image.RGBA to generate snapshots

func NewImageCanvasFromMap

func NewImageCanvasFromMap(m Map) *ImgCanvas

NewImageCanvasFromMap returns an image canvas with correct bounds

func (ImgCanvas) Bounds

func (i ImgCanvas) Bounds() image.Rectangle

Bounds returns the canvas' bounds

func (ImgCanvas) Draw

func (i ImgCanvas) Draw(what image.Image, where image.Rectangle)

Draw renders on the image.RGBA surface

func (ImgCanvas) FillRect

func (i ImgCanvas) FillRect(what color.Color, where image.Rectangle)

FillRect draws a rectangle on the canvas

func (ImgCanvas) Image

func (i ImgCanvas) Image() *image.RGBA

Image returns the image that has been drawn

type Layer

type Layer struct {
	Name       string        `xml:"name,attr"`
	Opacity    float32       `xml:"opacity,attr"`
	Visible    *visibleValue `xml:"visible,attr"`
	Properties []Property    `xml:"properties>property"`
	Data       Data          `xml:"data"`
	Width      int           `xml:"width,attr"`
	Height     int           `xml:"height,attr"`
}

Layer represents one layer of the map.

func (Layer) IsVisible

func (l Layer) IsVisible() bool

IsVisible returns true if the layer is visible, false otherwise

type Map

type Map struct {
	Version         string        `xml:"title,attr"`
	Orientation     string        `xml:"orientation,attr"`
	Width           int           `xml:"width,attr"`
	Height          int           `xml:"height,attr"`
	TileWidth       int           `xml:"tilewidth,attr"`
	TileHeight      int           `xml:"tileheight,attr"`
	BackgroundColor hexcolor      `xml:"backgroundcolor,attr"`
	RenderOrder     string        `xml:"renderorder,attr"`
	Properties      []Property    `xml:"properties>property"`
	Tilesets        []Tileset     `xml:"tileset"`
	Layers          []Layer       `xml:"layer"`
	ObjectGroups    []ObjectGroup `xml:"objectgroup"`
	// contains filtered or unexported fields
}

Map contains map information

func NewMap

func NewMap(f io.Reader) (*Map, error)

NewMap creates a new map from a given io.Reader

func (Map) GetTilesetForGID

func (m Map) GetTilesetForGID(gid GID) (*Tileset, error)

GetTilesetForGID returns the correct tileset for a given gid

type Object

type Object struct {
	Name       string        `xml:"name,attr"`
	Type       string        `xml:"type,attr"`
	X          int           `xml:"x,attr"`
	Y          int           `xml:"y,attr"`
	Width      int           `xml:"width,attr"`
	Height     int           `xml:"height,attr"`
	GID        int           `xml:"gid,attr"`
	Visible    *visibleValue `xml:"visible,attr"`
	Polygons   []Polygon     `xml:"polygon"`
	PolyLines  []PolyLine    `xml:"polyline"`
	Properties []Property    `xml:"properties>property"`
}

Object is an object

func (Object) IsVisible

func (o Object) IsVisible() bool

IsVisible returns true if the object is visible, false otherwise

type ObjectGroup

type ObjectGroup struct {
	Name       string        `xml:"name,attr"`
	Color      string        `xml:"color,attr"`
	Opacity    float32       `xml:"opacity,attr"`
	Visible    *visibleValue `xml:"visible,attr"`
	Properties []Property    `xml:"properties>property"`
	Objects    []Object      `xml:"object"`
}

ObjectGroup is a group of objects

func (ObjectGroup) IsVisible

func (o ObjectGroup) IsVisible() bool

IsVisible returns true if the object group is visible, false otherwise

type PolyLine

type PolyLine struct {
	Points string `xml:"points,attr"`
}

PolyLine loads a polyline from tmx

type Polygon

type Polygon struct {
	Points string `xml:"points,attr"`
}

Polygon loads a polygon from tmx

type Property

type Property struct {
	Name  string `xml:"name,attr"`
	Value string `xml:"value,attr"`
}

Property can be set on tiles

type RelativeCanvas

type RelativeCanvas interface {
	Draw(tile image.Rectangle, where image.Rectangle, f FlipMode, tileset string)
}

RelativeCanvas allows custom cropping of tiles

type Renderer

type Renderer interface {
	Render(elapsedTime int64) error
}

Renderer renders the given Map on a provided Canvas elapsedTime in milliseconds this is necessary to update animated tiles accordingly

func NewRenderer

func NewRenderer(m Map, c Canvas) Renderer

NewRenderer lets you draw the map on a custom canvas with a default FilesystemLocator

func NewRendererWithResourceLocator

func NewRendererWithResourceLocator(m Map, c Canvas, locator ResourceLocator) Renderer

NewRendererWithResourceLocator return a new renderer

func NewRendererWithResourceLocatorAndTileFlipper

func NewRendererWithResourceLocatorAndTileFlipper(
	m Map,
	c Canvas,
	locator ResourceLocator,
	tf TileFlipper,
) Renderer

NewRendererWithResourceLocatorAndTileFlipper allows you to specify a custom canvas, locator and TileFlipper

type ResourceLocator

type ResourceLocator interface {
	LocateResource(filepath string) (image.Image, error)
}

ResourceLocator can be implemented to load resources differently than from filesystem

func NewLazyResourceLocator

func NewLazyResourceLocator(l ResourceLocator) ResourceLocator

NewLazyResourceLocator wraps a ResourceLocator and caches results

type ResourceManager

type ResourceManager interface {
	UnsetResource(filepath string)
}

ResourceManager allows better memory handling and cleanup of resources

type Tile

type Tile struct {
	ID        uint32     `xml:"id,attr"`
	Image     Image      `xml:"image"`
	Animation *Animation `xml:"animation"`
}

Tile refers to one tile in the tileset

type TileFlipper

type TileFlipper interface {
	FlipHorizontal(image.Image) image.Image
	FlipVertical(image.Image) image.Image
	FlipDiagonal(image.Image) image.Image
}

TileFlipper allows a custom implementation of flipping techniques

type Tileset

type Tileset struct {
	FirstGID   GID        `xml:"firstgid,attr"`
	Source     string     `xml:"source,attr"`
	Name       string     `xml:"name,attr"`
	TileWidth  int        `xml:"tilewidth,attr"`
	TileHeight int        `xml:"tileheight,attr"`
	Spacing    int        `xml:"spacing,attr"`
	Margin     int        `xml:"margin,attr"`
	Properties []Property `xml:"properties>property"`
	Image      Image      `xml:"image"`
	Tiles      []Tile     `xml:"tile"`
}

Tileset entry describes a complete tileset

func (Tileset) GetFilename

func (t Tileset) GetFilename() string

GetFilename returns the filename for this tileset

func (Tileset) GetNumTiles

func (t Tileset) GetNumTiles() int

GetNumTiles returns the number of tiles of this tileset

func (Tileset) GetNumTilesX

func (t Tileset) GetNumTilesX() int

GetNumTilesX returns the number of tiles in x direction

func (Tileset) GetNumTilesY

func (t Tileset) GetNumTilesY() int

GetNumTilesY returns the number of tiles in y direction

func (Tileset) GetTileByID

func (t Tileset) GetTileByID(tileID uint32) *Tile

GetTileByID returns special tile information

type Timer

type Timer interface {
	GetElapsedTime() int64
	Start()
	UpdateTime()
}

Timer can be used to measure time delta between two snapshots

func CreateTimer

func CreateTimer() Timer

CreateTimer creates a new timer

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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