Skip to content

Commit

Permalink
Merge branch 'master' into feature/save_load_tileset
Browse files Browse the repository at this point in the history
  • Loading branch information
MetalBlueberry committed Jan 5, 2022
2 parents 67d41bf + 8f6d20a commit c2a3f9b
Show file tree
Hide file tree
Showing 24 changed files with 458 additions and 124 deletions.
21 changes: 21 additions & 0 deletions .changelog.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
repo: lafriks/go-tiled

service: github

groups:
-
name: BREAKING
labels:
- breaking
-
name: ENHANCEMENTS
labels:
- enhancement
-
name: BUGFIXES
labels:
- bug
-
name: TESTING
labels:
- testing
19 changes: 14 additions & 5 deletions .drone.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
---
kind: pipeline
type: docker
name: testing

clone:
depth: 50

trigger:
branch:
- master
event:
- push
- pull_request

steps:
- name: lint
image: golang:1.11
pull: true
image: golang:1.16
pull: always
commands:
- go tool vet -all -shadowstrict .
- go vet -all .
- go get -u github.com/mgechev/revive
- revive -config .revive.toml -exclude=./vendor/... ./...

- name: test
image: golang:1.11
pull: true
image: golang:1.16
pull: always
commands:
- go test -v .
- go vet
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
root = true

[*]
indent_style = space
indent_size = 2
tab_width = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.{go,tmpl}]
indent_style = tab
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
91 changes: 46 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,73 +1,74 @@
# go-tiled

[![godoc reference](https://img.shields.io/badge/godoc-reference-5272B4)](https://pkg.go.dev/github.com/lafriks/go-tiled?tab=doc)
[![Build Status](https://cloud.drone.io/api/badges/lafriks/go-tiled/status.svg)](https://cloud.drone.io/lafriks/go-tiled)
[![PkgGoDev](https://pkg.go.dev/badge/github.com/lafriks/go-tiled)](https://pkg.go.dev/github.com/lafriks/go-tiled)
[![Build Status](https://cloud.drone.io/api/badges/lafriks/go-tiled/status.svg?ref=refs/heads/master)](https://cloud.drone.io/lafriks/go-tiled)

Go library to parse Tiled map editor file format (TMX) and render map to image. Currently supports only orthogonal rendering out-of-the-box.

## Installing

$ go get github.com/lafriks/go-tiled
```sh
go get github.com/lafriks/go-tiled
```

You can use `go get -u` to update the package. You can also just import and start using the package directly if you're using Go modules, and Go will then download the package on first compilation.

## Basic Usage:
## Basic Usage

```go
package main

import (
"fmt"
"os"
"fmt"
"os"

"github.com/lafriks/go-tiled"
"github.com/lafriks/go-tiled/render"
"github.com/lafriks/go-tiled"
"github.com/lafriks/go-tiled/render"
)

const mapPath = "maps/map.tmx" // Path to your Tiled Map.

func main() {
// Parse .tmx file.
gameMap, err := tiled.LoadFromFile(mapPath)
if err != nil {
fmt.Printf("error parsing map: %s", err.Error()
os.Exit(2)
}

fmt.Println(gameMap)

// You can also render the map to an in-memory image for direct
// use with the default Renderer, or by making your own.
renderer, err := render.NewRenderer(gameMap)
if err != nil {
fmt.Printf("map unsupported for rendering: %s", err.Error()
os.Exit(2)
}

// Render just layer 0 to the Renderer.
err = renderer.RenderLayer(0)
if err != nil {
fmt.Printf("layer unsupported for rendering: %s", err.Error()
os.Exit(2)
}

// Get a reference to the Renderer's output, an image.NRGBA struct.
img := renderer.Result

// Clear the render result after copying the output if separation of
// layers is desired.
renderer.Clear()

// And so on. You can also export the image to a file by using the
// Renderer's Save functions.

gameMap, err := tiled.LoadFromFile(mapPath)
if err != nil {
fmt.Printf("error parsing map: %s", err.Error()
os.Exit(2)
}

fmt.Println(gameMap)

// You can also render the map to an in-memory image for direct
// use with the default Renderer, or by making your own.
renderer, err := render.NewRenderer(gameMap)
if err != nil {
fmt.Printf("map unsupported for rendering: %s", err.Error()
os.Exit(2)
}

// Render just layer 0 to the Renderer.
err = renderer.RenderLayer(0)
if err != nil {
fmt.Printf("layer unsupported for rendering: %s", err.Error()
os.Exit(2)
}

// Get a reference to the Renderer's output, an image.NRGBA struct.
img := renderer.Result

// Clear the render result after copying the output if separation of
// layers is desired.
renderer.Clear()

// And so on. You can also export the image to a file by using the
// Renderer's Save functions.
}

```

## Documentation

For further documentation, see https://pkg.go.dev/github.com/lafriks/go-tiled or run:

$ godoc github.com/lafriks/go-tiled
For further documentation, see <https://pkg.go.dev/github.com/lafriks/go-tiled> or run:

```sh
godoc github.com/lafriks/go-tiled
```
2 changes: 1 addition & 1 deletion assets/groups.tmx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<layer id="3" name="Tile Layer 2" width="20" height="20">
<data encoding="base64" compression="zlib">
eJxjYBgFo2AUjIJRMApIBwAGQAAB
</data>
</data>
</layer>
<group id="8" name="C">
<objectgroup id="7" name="Object Layer 1"/>
Expand Down
9 changes: 9 additions & 0 deletions assets/test3.tmx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.2" tiledversion="1.2.4" orientation="orthogonal" renderorder="right-down" width="10" height="10" tilewidth="128" tileheight="128" infinite="0" nextlayerid="2" nextobjectid="1">
<tileset firstgid="1" source="tilesets/kenny-racing/kenny-racing-tileset-grass.tsx"/>
<layer id="1" name="Tile Layer 1" width="10" height="10">
<data encoding="base64" compression="zlib">
eJxjYWBgYEHCjEDMhCaGDbMBMTsOOVxmoIvjMgOXODXcRipGtxOXG9DtJNcNADh3AZE=
</data>
</layer>
</map>
8 changes: 8 additions & 0 deletions assets/test_tileobject.tmx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.2" tiledversion="1.2.3" orientation="orthogonal" renderorder="right-down" width="10" height="10" tilewidth="32" tileheight="32" infinite="0" nextlayerid="4" nextobjectid="2">
<tileset firstgid="1" source="tilesets/test2.tsx"/>
<objectgroup id="3" name="Off-tile objects">
<object id="1" gid="8" x="146.667" y="108" width="32" height="32">
</object>
</objectgroup>
</map>
4 changes: 2 additions & 2 deletions assets/tilesets/testLoadTilesetTile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<tileset version="1.2" tiledversion="1.2.3" name="ProjectUtumno_full" tilewidth="32" tileheight="32" tilecount="6080" columns="64">
<image source="ProjectUtumno_full.png" width="2048" height="3040"/>
<tile id="464">
<objectgroup draworder="index">
<object id="1" x="-0.25" y="17.75" width="32.375" height="6.125"/>
<objectgroup opacity="1" visible="true" draworder="index">
<object id="1" x="-0.25" y="17.75" width="32.375" height="6.125" visible="true"/>
</objectgroup>
<animation>
<frame tileid="75" duration="500"/>
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module github.com/lafriks/go-tiled

go 1.11
go 1.16

require (
github.com/disintegration/imaging v1.6.0
github.com/stretchr/testify v1.3.0
github.com/disintegration/imaging v1.6.2
github.com/stretchr/testify v1.7.0
)
17 changes: 11 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/disintegration/imaging v1.6.0 h1:nVPXRUUQ36Z7MNf0O77UzgnOb1mkMMor7lmJMJXc/mA=
github.com/disintegration/imaging v1.6.0/go.mod h1:xuIt+sRxDFrHS0drzXUlCJthkJ8k7lkkUojDSR247MQ=
github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c=
github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81 h1:00VmoueYNlNz/aHIilyyQz/MHSqGoWJzpFv/HW8xpzI=
golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 h1:hVwzHzIUGRjiF7EcUjqNxk3NCfkPxbDKRdnNE1Rpg0U=
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
6 changes: 1 addition & 5 deletions render/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ type Renderer struct {
engine RendererEngine
}

type subImager interface {
SubImage(r image.Rectangle) image.Image
}

// NewRenderer creates new rendering engine instance.
func NewRenderer(m *tiled.Map) (*Renderer, error) {
r := &Renderer{m: m, tileCache: make(map[uint32]image.Image)}
Expand Down Expand Up @@ -118,7 +114,7 @@ func (r *Renderer) getTileImage(tile *tiled.LayerTile) (image.Image, error) {
rect := tile.Tileset.GetTileRect(i)
r.tileCache[i+tile.Tileset.FirstGID] = imaging.Crop(img, rect)
if tile.ID == i {
timg = r.tileCache[i]
timg = r.tileCache[i+tile.Tileset.FirstGID]
}
}
}
Expand Down
12 changes: 4 additions & 8 deletions tiled.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ package tiled
import (
"encoding/xml"
"io"
"net/http"
"io/fs"
"os"
"path/filepath"
)
Expand All @@ -47,12 +47,12 @@ type Loader struct {
// resources it may reference.
//
// A nil FileSystem uses the local file system.
FileSystem http.FileSystem
FileSystem fs.FS
}

// open opens the given file using the Loader's FileSystem, or uses os.Open
// if l or l.FileSystem is nil.
func (l *Loader) open(name string) (http.File, error) {
func (l *Loader) open(name string) (fs.File, error) {
if l == nil || l.FileSystem == nil {
return os.Open(name)
}
Expand Down Expand Up @@ -83,11 +83,7 @@ func (l *Loader) LoadFromFile(fileName string) (*Map, error) {
}
defer f.Close()

dir, err := filepath.Abs(filepath.Dir(fileName))
if err != nil {
return nil, err
}

dir := filepath.Dir(fileName)
return l.LoadFromReader(dir, f)
}

Expand Down
Loading

0 comments on commit c2a3f9b

Please sign in to comment.