diff --git a/cmd/tmx2img/main.go b/cmd/tmx2img/main.go index ae6316b..cb2e840 100644 --- a/cmd/tmx2img/main.go +++ b/cmd/tmx2img/main.go @@ -43,5 +43,8 @@ func main() { return } defer w.Close() - rend.SaveAsPng(w) + if err := rend.SaveAsPng(w); err != nil { + fmt.Println(err) + return + } } diff --git a/go.mod b/go.mod index a99dbc6..f89d3a9 100644 --- a/go.mod +++ b/go.mod @@ -4,5 +4,5 @@ go 1.16 require ( github.com/disintegration/imaging v1.6.2 - github.com/stretchr/testify v1.8.0 + github.com/stretchr/testify v1.8.1 ) diff --git a/go.sum b/go.sum index 47d8531..0e16bdc 100644 --- a/go.sum +++ b/go.sum @@ -7,9 +7,11 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb 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/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= 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= diff --git a/tiled_test.go b/tiled_test.go index 6a47421..1a36769 100644 --- a/tiled_test.go +++ b/tiled_test.go @@ -377,7 +377,7 @@ func TestParseHexColor(t *testing.T) { for _, c := range cases { color := &HexColor{} - color.UnmarshalXMLAttr(c.attr) + assert.NoError(t, color.UnmarshalXMLAttr(c.attr)) assert.Equal(t, c.color, color.c, c.name) } } diff --git a/tmx_data.go b/tmx_data.go index 0dbb0f6..ed4dbe6 100644 --- a/tmx_data.go +++ b/tmx_data.go @@ -29,15 +29,12 @@ import ( "encoding/base64" "errors" "io" - "io/ioutil" "strconv" "strings" ) -var ( - // ErrUnknownCompression error is returned when file contains invalid compression method - ErrUnknownCompression = errors.New("tiled: invalid compression method") -) +// ErrUnknownCompression error is returned when file contains invalid compression method +var ErrUnknownCompression = errors.New("tiled: invalid compression method") // Data is raw data type Data struct { @@ -82,7 +79,7 @@ func (d *Data) decodeBase64() (data []byte, err error) { return } - return ioutil.ReadAll(comr) + return io.ReadAll(comr) } func (d *Data) decodeCSV() ([]uint32, error) { diff --git a/tmx_map.go b/tmx_map.go index d3b0f48..e5f68d0 100644 --- a/tmx_map.go +++ b/tmx_map.go @@ -203,7 +203,9 @@ func (m *Map) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { // Decode object groups. for _, g := range item.ObjectGroups { - g.DecodeObjectGroup((*Map)(&item)) + if err := g.DecodeObjectGroup((*Map)(&item)); err != nil { + return err + } } *m = (Map)(item) diff --git a/tmx_object.go b/tmx_object.go index c054861..22fb9d3 100644 --- a/tmx_object.go +++ b/tmx_object.go @@ -30,10 +30,8 @@ import ( "strings" ) -var ( - // ErrInvalidObjectPoint error is returned if there is error parsing object points - ErrInvalidObjectPoint = errors.New("tiled: invalid object point") -) +// ErrInvalidObjectPoint error is returned if there is error parsing object points +var ErrInvalidObjectPoint = errors.New("tiled: invalid object point") // ObjectGroup is in fact a map layer, and is hence called "object layer" in Tiled Qt type ObjectGroup struct { @@ -64,18 +62,23 @@ type ObjectGroup struct { } // DecodeObjectGroup decodes object group data -func (g *ObjectGroup) DecodeObjectGroup(m *Map) { +func (g *ObjectGroup) DecodeObjectGroup(m *Map) error { for _, object := range g.Objects { if object.GID > 0 { // Initialize all tilesets that are referenced by tile objects. Otherwise, // if a tileset is used by an object tile but not used by any layer it // won't be loaded. - m.TileGIDToTile(object.GID) + if _, err := m.TileGIDToTile(object.GID); err != nil { + return err + } } if len(object.TemplateSource) > 0 { - object.initTemplate(m) + if err := object.initTemplate(m); err != nil { + return err + } } } + return nil } // UnmarshalXML decodes a single XML element beginning with the given start element. @@ -181,8 +184,7 @@ func (o *Object) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { } // Ellipse is used to mark an object as an ellipse. -type Ellipse struct { -} +type Ellipse struct{} // Polygon object is made up of a space-delimited list of x,y coordinates. The origin for these coordinates is the location of the parent object. // By default, the first point is created as 0,0 denoting that the point will originate exactly where the object is placed. diff --git a/tmx_tileset.go b/tmx_tileset.go index 8599d9f..9bb53ed 100644 --- a/tmx_tileset.go +++ b/tmx_tileset.go @@ -123,17 +123,12 @@ type AnimationFrame struct { // GetTileRect returns a rectangle that contains the tile in the tileset.Image func (ts *Tileset) GetTileRect(tileID uint32) image.Rectangle { - tilesetTileCount := ts.TileCount tilesetColumns := ts.Columns if tilesetColumns == 0 { tilesetColumns = ts.Image.Width / (ts.TileWidth + ts.Spacing) } - if tilesetTileCount == 0 { - tilesetTileCount = (ts.Image.Height / (ts.TileHeight + ts.Spacing)) * tilesetColumns - } - x := int(tileID) % tilesetColumns y := int(tileID) / tilesetColumns