forked from ajhager/spine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
atlas.go
391 lines (355 loc) · 9.66 KB
/
atlas.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package spine
import (
"bufio"
"errors"
"io"
"strconv"
"strings"
)
type Atlas struct {
Pages []*AtlasPage
Regions []*AtlasRegion
loader TextureLoader
}
func NewAtlas(r io.Reader, loader TextureLoader) (*Atlas, error) {
if loader == nil {
return nil, errors.New("spine: texture loader cannot be nil")
}
var atlas Atlas
atlas.loader = loader
scanner := bufio.NewScanner(r)
var page *AtlasPage
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if len(line) == 0 {
page = nil
continue
}
if page == nil {
var tuple []string
var terr error
formatName, err := readValue("format", scanner)
if err != nil {
return nil, errors.New("spine: failed to parse format: " + err.Error())
}
format, err := formatFromName(formatName)
if err != nil {
return nil, errors.New("spine: failed to parse format: " + err.Error())
}
tuple, terr = readTuple("filter", scanner)
if terr != nil {
return nil, errors.New("spine: failed to read page: " + err.Error())
}
minFilter, err := filterFromName(tuple[0])
if err != nil {
return nil, errors.New("spine: failed to parse min filter: " + err.Error())
}
magFilter, err := filterFromName(tuple[1])
if err != nil {
return nil, errors.New("spine: failed to parse mag filter: " + err.Error())
}
page = &AtlasPage{
Name: line,
Format: format,
MinFilter: minFilter,
MagFilter: magFilter,
UWrap: ClampToEdge,
VWrap: ClampToEdge,
}
direction, err := readValue("repeat", scanner)
if err != nil {
return nil, errors.New("spine: failed to parse direction: " + err.Error())
}
switch direction {
case "x":
page.UWrap = Repeat
case "y":
page.VWrap = Repeat
case "xy":
page.UWrap = Repeat
page.VWrap = Repeat
}
if err := atlas.loader.Load(page); err != nil {
return nil, errors.New("spine: failed to load texture: " + err.Error())
}
atlas.Pages = append(atlas.Pages, page)
} else {
var tuple []string
var terr error
rotateVal, err := readValue("rotate", scanner)
if err != nil {
return nil, errors.New("spine: failed to parse rotate: " + err.Error())
}
rotate, err := strconv.ParseBool(rotateVal)
if err != nil {
return nil, errors.New("spine: failed to parse rotate: " + err.Error())
}
tuple, terr = readTuple("xy", scanner)
if terr != nil {
return nil, errors.New("spine: failed to read x, y tuple: " + err.Error())
}
x, err := strconv.Atoi(tuple[0])
if err != nil {
return nil, errors.New("spine: failed to parse x: " + err.Error())
}
y, err := strconv.Atoi(tuple[1])
if err != nil {
return nil, errors.New("spine: failed to parse y: " + err.Error())
}
tuple, terr = readTuple("size", scanner)
if terr != nil {
return nil, errors.New("spine: failed to read width, height tuple: " + err.Error())
}
width, err := strconv.Atoi(tuple[0])
if err != nil {
return nil, errors.New("spine: failed to parse width: " + err.Error())
}
height, err := strconv.Atoi(tuple[1])
if err != nil {
return nil, errors.New("spine: failed to parse height: " + err.Error())
}
region := &AtlasRegion{
Name: line,
Page: page,
Rotate: rotate,
X: x,
Y: y,
U: float32(x) / float32(page.Width),
V: float32(y) / float32(page.Height),
}
if region.Rotate {
region.U2 = float32(x+height) / float32(page.Width)
region.V2 = float32(y+width) / float32(page.Height)
} else {
region.U2 = float32(x+width) / float32(page.Width)
region.V2 = float32(y+height) / float32(page.Height)
}
region.X, region.Y = x, y
region.Width, region.Height = width, height
if region.Width < 0 {
region.Width = -region.Width
}
if region.Height < 0 {
region.Height = -region.Height
}
tuple, terr = readTuple("", scanner)
if terr != nil {
return nil, errors.New("spine: failed to read tuple: " + err.Error())
}
if len(tuple) == 4 { // split is optional
for idx, sval := range tuple {
val, err := strconv.Atoi(sval)
if err != nil {
return nil, errors.New("spine: failed to read split: " + err.Error())
}
region.Splits[idx] = val
}
tuple, terr = readTuple("", scanner)
if terr != nil {
return nil, errors.New("spine: failed to read tuple: " + err.Error())
}
if len(tuple) == 4 { // pad is optional, but only present with splits
for idx, sval := range tuple {
val, err := strconv.Atoi(sval)
if err != nil {
return nil, errors.New("spine: failed to read split: " + err.Error())
}
region.Pads[idx] = val
}
tuple, terr = readTuple("orig", scanner)
if terr != nil {
return nil, errors.New("spine: failed to read tuple: " + err.Error())
}
}
}
origWidth, err := strconv.Atoi(tuple[0])
if err != nil {
return nil, errors.New("spine: failed to parse original width: " + err.Error())
}
origHeight, err := strconv.Atoi(tuple[1])
if err != nil {
return nil, errors.New("spine: failed to parse original height: " + err.Error())
}
region.OriginalWidth, region.OriginalHeight = origWidth, origHeight
tuple, terr = readTuple("offset", scanner)
if terr != nil {
return nil, errors.New("spine: failed to read x offset, y offset tuple: " + err.Error())
}
offX, err := strconv.Atoi(tuple[0])
if err != nil {
return nil, errors.New("spine: failed to parse x offset: " + err.Error())
}
offY, err := strconv.Atoi(tuple[1])
if err != nil {
return nil, errors.New("spine: failed to parse y offset: " + err.Error())
}
region.OffsetX, region.OffsetY = float32(offX), float32(offY)
regIdxVal, err := readValue("index", scanner)
if err != nil {
return nil, errors.New("spine: failed to parse region index: " + err.Error())
}
regIdx, err := strconv.Atoi(regIdxVal)
if err != nil {
return nil, errors.New("spine: failed to parse region index: " + err.Error())
}
region.Index = regIdx
atlas.Regions = append(atlas.Regions, region)
}
}
if scanner.Err() != nil {
return nil, errors.New("spine: failed to read atlas file: " + scanner.Err().Error())
}
return &atlas, nil
}
func readValue(expectedKey string, s *bufio.Scanner) (string, error) {
if err := nextLine(s); err != nil {
return "", err
}
line := s.Text()
keyVal := strings.SplitN(line, ":", 2)
if len(keyVal) < 2 {
return "", errors.New("spine: invalid key/value: " + line)
}
key := strings.TrimSpace(keyVal[0])
if expectedKey != "" && expectedKey != key {
return "", errors.New("spine: expected " + expectedKey + ", got " + key)
}
return strings.TrimSpace(keyVal[1]), nil
}
func nextLine(s *bufio.Scanner) error {
if s.Scan() {
return nil
}
if s.Err() != nil {
return errors.New("spine: failed to read file: " + s.Err().Error())
} else {
return errors.New("spine: unexpected EOF encountered")
}
}
/** Returns the number of tuple values read (2 or 4). */
func readTuple(expectedKey string, s *bufio.Scanner) ([]string, error) {
val, err := readValue(expectedKey, s)
if err != nil {
return nil, err
}
tuple := strings.Split(val, ",")
for idx, v := range tuple {
tuple[idx] = strings.TrimSpace(v)
}
return tuple, nil
}
/** Returns the first region found with the specified name. This method uses string comparison to find the region, so the result
* should be cached rather than calling this method multiple times.
* @return The region, or null. */
func (a *Atlas) FindRegion(name string) *AtlasRegion {
for _, region := range a.Regions {
if region.Name == name {
return region
}
}
return nil
}
func (a *Atlas) Dispose() error {
for _, page := range a.Pages {
if err := a.loader.Unload(page); err != nil {
return err
}
}
return nil
}
func formatFromName(name string) (TextureFormat, error) {
var f TextureFormat
switch name {
case "Alpha":
f = Alpha
case "Intensity":
f = Intensity
case "LuminanceAlpha":
f = LuminanceAlpha
case "RGB565":
f = RGB565
case "RGBA4444":
f = RGBA4444
case "RGB888":
f = RGB888
case "RGBA8888":
f = RGBA8888
default:
return 0, errors.New("unknown texture format: " + name)
}
return f, nil
}
func filterFromName(name string) (TextureFilter, error) {
var f TextureFilter
switch name {
case "Nearest":
f = Nearest
case "Linear":
f = Linear
case "MipMap":
f = MipMap
case "MipMapNearestNearest":
f = MipMapNearestNearest
case "MipMapLinearNearest":
f = MipMapLinearNearest
case "MipMapNearestLinear":
f = MipMapNearestLinear
case "MipMapLinearLinear":
f = MipMapLinearLinear
default:
return 0, errors.New("unknown filter: " + name)
}
return f, nil
}
type TextureFormat int
const (
Alpha TextureFormat = iota
Intensity
LuminanceAlpha
RGB565
RGBA4444
RGB888
RGBA8888
)
type TextureFilter int
const (
Nearest TextureFilter = iota
Linear
MipMap
MipMapNearestNearest
MipMapLinearNearest
MipMapNearestLinear
MipMapLinearLinear
)
type TextureWrap int
const (
MirroredRepeat TextureWrap = iota
ClampToEdge
Repeat
)
type AtlasPage struct {
Name string
Format TextureFormat
MinFilter TextureFilter
MagFilter TextureFilter
UWrap TextureWrap
VWrap TextureWrap
RendererObject interface{}
Width, Height int
}
type AtlasRegion struct {
Page *AtlasPage
Name string
X, Y, Width, Height int
U, V, U2, V2 float32
OffsetX, OffsetY float32
OriginalWidth, OriginalHeight int
Index int
Rotate bool
Splits [4]int
Pads [4]int
}
type TextureLoader interface {
Load(page *AtlasPage) error
Unload(page *AtlasPage) error
}