-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprite_test.go
112 lines (91 loc) · 2.16 KB
/
sprite_test.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
package spritenik
import (
"fmt"
"image"
"image/color"
"image/draw"
"image/png"
"log"
"math/rand"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
"golang.org/x/image/font"
"golang.org/x/image/font/basicfont"
"golang.org/x/image/math/fixed"
)
func TestGenerateSprite(t *testing.T) {
textures := make([]Texture, 0)
for i := 0; i < 20; i++ {
size := rand.Intn(32) + 32
pixelRatio := i%2 + 1
name := fmt.Sprintf("%d", i)
img := image.NewRGBA(image.Rect(0, 0, size, size))
c := color.RGBA{uint8(rand.Intn(255)), uint8(rand.Intn(255)), uint8(rand.Intn(255)), 255}
draw.Draw(img, img.Bounds(), &image.Uniform{c}, image.ZP, draw.Src)
addLabel(img, 2, 10, fmt.Sprintf("%s@%dx", name, pixelRatio))
addLabel(img, 2, 20, fmt.Sprintf("%d,%d", size, size))
textures = append(textures, &TextureTest{
SymbolName: name,
Image: img,
PixelRatio: pixelRatio,
})
}
{
_, img, err := GenerateSprite(textures, 2, true)
require.NoError(t, err)
SavePNG("./testdata/test@2x.png", img)
}
{
_, img, err := GenerateSprite(textures, 1, true)
require.NoError(t, err)
SavePNG("./testdata/test.png", img)
}
}
func addLabel(img *image.RGBA, x, y int, label string) {
col := color.RGBA{0, 0, 0, 255}
point := fixed.Point26_6{X: fixed.Int26_6(x * 64), Y: fixed.Int26_6(y * 64)}
d := &font.Drawer{
Dst: img,
Src: image.NewUniform(col),
Face: basicfont.Face7x13,
Dot: point,
}
d.DrawString(label)
}
type TextureTest struct {
SymbolName string
PixelRatio int
image.Image
}
func (m *TextureTest) TexturePixelRatio() int {
return m.PixelRatio
}
func (m *TextureTest) TextureName() string {
return m.SymbolName
}
func (m *TextureTest) TextureImage() image.Image {
return m.Image
}
func (m *TextureTest) TextureWidth() int {
return m.Image.Bounds().Size().X
}
func (m *TextureTest) TextureHeight() int {
return m.Image.Bounds().Size().Y
}
func SavePNG(path string, img image.Image) {
err := os.MkdirAll(filepath.Dir(path), os.ModePerm)
if err != nil {
log.Fatal(err)
}
f, err := os.Create(path)
if err != nil {
log.Fatal(err)
}
defer f.Close()
err = png.Encode(f, img)
if err != nil {
log.Fatal(err)
}
}