-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
127 lines (102 loc) · 2.31 KB
/
main.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
package main
import (
"flag"
"fmt"
"image"
"image/png"
"io/ioutil"
"log"
"os"
"strconv"
"strings"
"github.com/zan-xhipe/atlas-packer/packer"
)
func main() {
space := flag.Int("space", 1, "space added between images")
dim := flag.String("dimensions", "1024x1024", "atlas size")
verbose := flag.Bool("v", false, "verbose output")
forceRebuild := flag.Bool("force", false, "force rebuild")
flag.Parse()
args := flag.Args()
inputDir := args[0]
outputName := args[1]
imgName := outputName + ".png"
dataName := outputName + ".json"
// check if it is necessary to rebuild the atlas
if !*forceRebuild {
imgInfo, imgErr := os.Stat(imgName)
datInfo, datErr := os.Stat(dataName)
dirInfo, dirErr := os.Stat(inputDir)
if imgErr == nil && datErr == nil && dirErr == nil &&
dirInfo.ModTime().Before(imgInfo.ModTime()) &&
dirInfo.ModTime().Before(datInfo.ModTime()) {
if *verbose {
log.Println("already up to date")
}
os.Exit(0)
}
}
dimX, dimY, err := parseDimensions(*dim)
if err != nil {
log.Fatal(err)
}
sprites, err := packer.ReadSprites(inputDir)
if err != nil {
log.Fatal(err)
}
if *verbose {
log.Println("read sprite directory")
}
atlas := packer.Atlas{DimX: dimX, DimY: dimY, Space: *space}
img, data, err := packer.Pack(sprites, atlas)
if err != nil {
log.Fatal(err)
}
if *verbose {
log.Println("packed sprites in atlas")
}
err = ioutil.WriteFile(dataName, data, 0666)
if err != nil {
log.Fatal(err)
}
if *verbose {
log.Printf("wrote data to file")
}
err = writeImage(img, imgName)
if err != nil {
log.Fatal(err)
}
if *verbose {
log.Printf("wrote texture to file")
}
}
// parseDimensions takes a string of two numbers separated with an x
// and returns two ints representing a width and a height
func parseDimensions(dim string) (dimX, dimY int, err error) {
dims := strings.Split(dim, "x")
if len(dims) != 2 {
err = fmt.Errorf("couldn't parse dimension %s\n", dims)
return
}
dimX, err = strconv.Atoi(dims[0])
if err != nil {
return
}
dimY, err = strconv.Atoi(dims[1])
if err != nil {
return
}
return
}
func writeImage(image *image.RGBA, filename string) error {
writer, err := os.Create(filename)
if err != nil {
return err
}
defer writer.Close()
err = png.Encode(writer, image)
if err != nil {
return err
}
return nil
}