/
loader.go
310 lines (275 loc) · 8.21 KB
/
loader.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
package render
import (
"encoding/json"
"errors"
"image"
"image/color"
"image/gif"
"image/jpeg"
"image/png"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"sync"
"github.com/oakmound/oak/dlog"
"github.com/oakmound/oak/fileutil"
)
var (
regexpSingleNumber, _ = regexp.Compile(`^\d+$`)
regexpTwoNumbers, _ = regexp.Compile(`^\d+x\d+$`)
)
var (
// Form ...main/core.go/assets/images,
// the image directory.
wd, _ = os.Getwd()
dir = filepath.Join(
wd,
"assets",
"images")
loadedImages = make(map[string]*image.RGBA)
loadedSheets = make(map[string]*Sheet)
// move to some batch load settings
defaultPad = 0
loadLock = sync.Mutex{}
)
func loadImage(directory, fileName string) (*image.RGBA, error) {
loadLock.Lock()
if _, ok := loadedImages[fileName]; !ok {
imgFile, err := fileutil.Open(filepath.Join(directory, fileName))
if err != nil {
loadLock.Unlock()
return nil, err
}
defer func() {
err = imgFile.Close()
if err != nil {
dlog.Error(err)
}
}()
ext := fileName[len(fileName)-4:]
var img image.Image
switch ext {
case ".png":
img, err = png.Decode(imgFile)
case ".gif":
img, err = gif.Decode(imgFile)
case "jpeg", ".jpg":
img, err = jpeg.Decode(imgFile)
}
if err != nil {
loadLock.Unlock()
return nil, err
}
bounds := img.Bounds()
rgba := image.NewRGBA(bounds)
for x := 0; x < bounds.Max.X; x++ {
for y := 0; y < bounds.Max.Y; y++ {
rgba.Set(x, y, color.RGBAModel.Convert(img.At(x, y)))
}
}
loadedImages[fileName] = rgba
dlog.Verb("Loaded filename: ", fileName)
}
r := loadedImages[fileName]
loadLock.Unlock()
return r, nil
}
// LoadSprite loads the input fileName into a Sprite
func LoadSprite(fileName string) *Sprite {
r, err := loadImage(dir, fileName)
if err != nil {
dlog.Error(err)
return nil
}
return NewSprite(0, 0, r)
}
// GetSheet tries to find the given file in the set of loaded sheets.
// If it fails, it will panic unhelpfully. Todo: fix this
// If it succeeds, it will return the sheet (a 2d array of sprites)
func GetSheet(fileName string) [][]*Sprite {
sprites := make([][]*Sprite, 0)
dlog.Verb(loadedSheets, fileName, loadedSheets[fileName])
sheet, _ := LoadSheet(dir, fileName, 0, 0, 0)
for x, row := range *sheet {
sprites = append(sprites, make([]*Sprite, 0))
for y := range row {
sprites[x] = append(sprites[x], sheet.SubSprite(x, y))
}
}
return sprites
}
// LoadSheet loads a file in some directory with sheets of (w,h) sized sprites,
// where there is pad pixels of vertical/horizontal pad between each sprite
func LoadSheet(directory, fileName string, w, h, pad int) (*Sheet, error) {
if _, ok := loadedImages[fileName]; !ok {
dlog.Verb("Missing file in loaded images: ", fileName)
r, err := loadImage(directory, fileName)
if err != nil {
return nil, err
}
loadedImages[fileName] = r
}
if sheetP, ok := loadedSheets[fileName]; ok {
return sheetP, nil
}
dlog.Verb("Loading sheet: ", fileName)
rgba := loadedImages[fileName]
bounds := rgba.Bounds()
sheetW := bounds.Max.X / w
remainderW := bounds.Max.X % w
sheetH := bounds.Max.Y / h
remainderH := bounds.Max.Y % h
var widthBuffers, heightBuffers int
if pad != 0 {
widthBuffers = remainderW / pad
heightBuffers = remainderH / pad
} else {
widthBuffers = sheetW - 1
heightBuffers = sheetH - 1
}
if sheetW < 1 || sheetH < 1 ||
widthBuffers != sheetW-1 ||
heightBuffers != sheetH-1 {
dlog.Error("Bad dimensions given to load sheet")
return nil, errors.New("Bad dimensions given to load sheet")
}
sheet := make(Sheet, sheetW)
i := 0
for x := 0; x < bounds.Max.X; x += (w + pad) {
sheet[i] = make([]*image.RGBA, sheetH)
j := 0
for y := 0; y < bounds.Max.Y; y += (h + pad) {
sheet[i][j] = subImage(rgba, x, y, w, h)
j++
}
i++
}
dlog.Verb("Loaded sheet into map")
loadedSheets[fileName] = &sheet
return loadedSheets[fileName], nil
}
// LoadSheetAnimation loads a sheet and then calls LoadAnimation on that sheet
func LoadSheetAnimation(fileName string, w, h, pad int, fps float64, frames []int) (*Animation, error) {
sheet, err := LoadSheet(dir, fileName, w, h, pad)
if err != nil {
return nil, err
}
return LoadAnimation(sheet, w, h, pad, fps, frames)
}
// LoadAnimation takes in a sheet with sheet dimensions, a frame rate and a list of frames where
// frames are in x,y pairs ([0,0,1,0,2,0] for (0,0) (1,0) (2,0)) and returns an animation from that
func LoadAnimation(sheet *Sheet, w, h, pad int, fps float64, frames []int) (*Animation, error) {
animation, err := NewAnimation(sheet, fps, frames)
if err != nil {
return nil, err
}
return animation, nil
}
func subImage(rgba *image.RGBA, x, y, w, h int) *image.RGBA {
out := image.NewRGBA(image.Rect(0, 0, w, h))
for i := 0; i < w; i++ {
for j := 0; j < h; j++ {
out.Set(i, j, rgba.At(x+i, y+j))
}
}
return out
}
// BatchLoad loads subdirectories from the given base folder and imports all files,
// using alias rules to automatically determine the size of sprites and sheets in
// subfolders.
// A folder named 16x8 will have its images split into sheets where each sprite is
// 16x8, for example. 16 is a shorter way of writing 16x16.
// An alias.json file can be included that can indicate what dimensions named folders
// represent, so a "tiles": "32" field in the json would indicate that sprite sheets
// in the /tiles folder should be read as 32x32
func BatchLoad(baseFolder string) error {
folders, err := fileutil.ReadDir(baseFolder)
if err != nil {
dlog.Error(err)
return err
}
aliasFile, err := fileutil.ReadFile(filepath.Join(baseFolder, "alias.json"))
aliases := make(map[string]string)
if err == nil {
err = json.Unmarshal(aliasFile, &aliases)
if err != nil {
dlog.Error("Alias file unparseable: ", err)
}
}
for i, folder := range folders {
dlog.Verb("folder ", i, folder.Name())
if folder.IsDir() {
var frameW int
var frameH int
if folder.Name() == "raw" {
frameW = 0
frameH = 0
} else if result := regexpTwoNumbers.Find([]byte(folder.Name())); result != nil {
vals := strings.Split(string(result), "x")
dlog.Verb("Extracted dimensions: ", vals)
frameW, _ = strconv.Atoi(vals[0])
frameH, _ = strconv.Atoi(vals[1])
} else if result := regexpSingleNumber.Find([]byte(folder.Name())); result != nil {
val, _ := strconv.Atoi(string(result))
frameW = val
frameH = val
} else {
if aliased, ok := aliases[folder.Name()]; ok {
if result := regexpTwoNumbers.Find([]byte(aliased)); result != nil {
vals := strings.Split(string(result), "x")
dlog.Verb("Extracted dimensions: ", vals)
frameW, _ = strconv.Atoi(vals[0])
frameH, _ = strconv.Atoi(vals[1])
} else if result := regexpSingleNumber.Find([]byte(aliased)); result != nil {
val, _ := strconv.Atoi(string(result))
frameW = val
frameH = val
} else {
return errors.New("Alias value not parseable as a frame width and height pair")
}
} else {
return errors.New("Alias name not found in alias file")
}
}
files, _ := fileutil.ReadDir(filepath.Join(baseFolder, folder.Name()))
for _, file := range files {
if !file.IsDir() {
n := file.Name()
switch n[len(n)-4:] {
case ".png":
dlog.Verb("loading file ", n)
buff, err := loadImage(baseFolder, filepath.Join(folder.Name(), n))
if err != nil {
dlog.Error(err)
continue
}
w := buff.Bounds().Max.X
h := buff.Bounds().Max.Y
dlog.Verb("buffer: ", w, h, " frame: ", frameW, frameH)
if frameW == 0 || frameH == 0 {
continue
} else if w < frameW || h < frameH {
dlog.Error("File ", n, " in folder", folder.Name(), " is too small for these folder dimensions")
return errors.New("File in folder is too small for these folder dimensions")
// Load this as a sheet if it is greater
// than the folder size's frame size
} else if w != frameW || h != frameH {
dlog.Verb("Loading as sprite sheet")
_, err = LoadSheet(baseFolder, filepath.Join(folder.Name(), n), frameW, frameH, defaultPad)
if err != nil {
dlog.Error(err)
}
}
default:
dlog.Error("Unsupported file ending for batchLoad: ", n)
}
}
}
} else {
dlog.Verb("Not Folder")
}
}
return nil
}