-
Notifications
You must be signed in to change notification settings - Fork 0
/
textureAtlas.go
85 lines (69 loc) · 1.99 KB
/
textureAtlas.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package sparrow
import (
"cmp"
"encoding/xml"
"errors"
"io/fs"
"log/slog"
"slices"
)
var ErrSubTextureDoesNotExist error = errors.New("sub-texture does not exist")
type TextureAtlas struct {
ImagePath string `xml:"imagePath,attr"`
SubTextures []*SubTexture `xml:"SubTexture"`
}
// NewTextureAtlas returns a new TextureAtlas.
func NewTextureAtlas() *TextureAtlas {
return &TextureAtlas{}
}
// ParseTextureAtlas parses a Sparrow v2 texture atlas in XML format.
func ParseTextureAtlas(xmlData []byte) (*TextureAtlas, error) {
var atlas TextureAtlas
if err := xml.Unmarshal(xmlData, &atlas); err != nil {
return nil, err
}
return &atlas, nil
}
// ParseTextureAtlasFromFS parses a Sparrow v2 texture atlas in XML format from a filesystem.
func ParseTextureAtlasFromFS(fsys fs.FS, path string) (*TextureAtlas, error) {
xmlData, err := fs.ReadFile(fsys, path)
if err != nil {
return nil, err
}
atlas, err := ParseTextureAtlas(xmlData)
if err != nil {
return nil, err
}
return atlas, nil
}
// GetSubTexture finds and returns a SubTexture with the specified name.
func (ta *TextureAtlas) GetSubTexture(name string) (*SubTexture, error) {
i, ok := slices.BinarySearchFunc(ta.SubTextures, &SubTexture{Name: name}, func(st1, st2 *SubTexture) int {
return cmp.Compare(st1.Name, st2.Name)
})
if !ok {
return nil, ErrSubTextureDoesNotExist
}
return ta.SubTextures[i], nil
}
// MustGetSubTexture simplay calls GetSubTexture and returns nil if an error occurs.
func (ta *TextureAtlas) MustGetSubTexture(name string) *SubTexture {
st, err := ta.GetSubTexture(name)
if err != nil {
slog.Error(err.Error())
return nil
}
return st
}
// EnumerateSubTextures returns a map that contains all sub-textures.
func (ta *TextureAtlas) EnumerateSubTextures() map[string]*SubTexture {
m := map[string]*SubTexture{}
for _, st := range ta.SubTextures {
m[st.Name] = st
}
return m
}
// Encode encodes a TextureAtlas as XML.
func (ta *TextureAtlas) Encode() ([]byte, error) {
return xml.Marshal(ta)
}