-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtextures.go
124 lines (106 loc) · 2.97 KB
/
textures.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
package main
import (
"encoding/json"
"image"
"os"
"regexp"
"github.com/faiface/pixel"
)
// Textures loaded from packed texture atlas and JSON descr.
type textures struct {
batch *pixel.Batch
image *pixel.PictureData
sprites map[string]*pixel.Sprite
objects []*sprite
}
// Sprite contains an object where position is volatile
type sprite struct {
name string
pos pixel.Vec
scale float64
}
// JSON parsing for texture packed information
type frames struct {
Frames []files `json:"frames"`
Meta meta `json:"meta"`
}
type meta struct {
App string `json:"app"`
Version string `json:"version"`
Image string `json:"image"`
Format string `json:"format"`
Size frame `json:"size"`
Scale string `json:"scale"`
Smartupdate string `json:"smartupdate"`
}
type files struct {
Filename string `json:"filename"`
Frame frame `json:"frame"`
Rotated bool `json:"rotated"`
Trimmed bool `json:"trimmed"`
SpriteSourceSize frame `json:"spriteSourceSize"`
SourceSize frame `json:"sourceSize"`
}
type frame struct {
X float64 `json:"x"`
Y float64 `json:"y"`
W float64 `json:"w"`
H float64 `json:"h"`
}
// Load and create textures
func (t *textures) load(jsonfile string) {
t.sprites = make(map[string]*pixel.Sprite)
// Read json file to names (tr .png from name)
jfile, err := os.Open(jsonfile)
defer jfile.Close()
if err != nil {
Error("Read JSON file failed:", err.Error())
}
jsonParser := json.NewDecoder(jfile)
result := frames{}
err = jsonParser.Decode(&result)
if err != nil {
Error("Error decoding JSON:", err)
}
// Load the image
file, err := os.Open(result.Meta.Image)
if err != nil {
Error("Error loading texture file:", err)
}
defer file.Close()
img, _, err := image.Decode(file)
if err != nil {
Error("Error decoding texture file:", err)
}
t.image = pixel.PictureDataFromImage(img)
// Create a batch from image
t.batch = pixel.NewBatch(&pixel.TrianglesData{}, t.image)
// Create sprite for each frame t.sprites.
reg := regexp.MustCompile(`\..*`)
for _, f := range result.Frames {
name := reg.ReplaceAllString(f.Filename, "${1}")
Debug("F:", f.Filename, pixel.R(f.Frame.X, f.Frame.Y, f.Frame.X+f.Frame.W, f.Frame.Y+f.Frame.H))
t.sprites[name] = pixel.NewSprite(t.image, pixel.R(f.Frame.X, f.Frame.Y, f.Frame.X+f.Frame.W, f.Frame.Y+f.Frame.H))
}
}
// Add new sprite object to draw
func (t *textures) addObject(o *sprite) {
t.objects = append(t.objects, o)
}
// Remove object from drawing list
func (t *textures) removeObject(o sprite) {
// TBD
}
// Get info for sprite
func (t *textures) spriteInfo(name string) (int, int) {
f := t.sprites[name].Frame()
return int(f.Max.X - f.Min.X), int(f.Max.Y - f.Min.Y)
}
// Draw the batch based on the packed texture atlas
func (t *textures) update(dt float64) {
t.batch.Clear()
for _, o := range t.objects {
t.sprites[o.name].Draw(t.batch, pixel.IM.Scaled(pixel.ZV, o.scale).Moved(o.pos))
}
t.batch.Draw(global.gWin)
}