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
|
package assets_gen
import os "core:os/os2"
import "core:fmt"
import "core:strings"
import "core:path/filepath"
import "core:image"
import "core:image/png"
import "core:image/qoi"
import ase "aseprite"
import aseutil "aseprite/utils"
load_world :: proc(path: string, file: ^os.File, _: ^os.File) {
load_json_world(path)
}
load_room :: proc(path: string, file: ^os.File, _: ^os.File) {
load_json_room(path, file)
paths_to_res_type[path] = "Room_Id"
}
load_tileset :: proc(path: string, _: ^os.File, _: ^os.File) {
tileset_name := filepath.stem(path)
if tileset_name not_in tileset_gids {
load_json_tileset(path)
}
}
load_qoi :: proc(path: string, qoi: ^os.File, output: ^os.File) {
rel_path, rel_err := filepath.rel(
output_dir,
path,
context.temp_allocator,
)
if rel_err != nil {
die(
"Could not find relative path from %v to %v (%v)",
output_dir,
path,
rel_err,
)
}
line := fmt.aprintf("{{data = #load(%w), anim = .None}}", rel_path)
images[filepath.stem(path)] = line
paths_to_res_type[path] = "Image_Id"
}
load_png :: proc(path: string, png_file: ^os.File, output: ^os.File) {
// Convert all PNG files to QOI and store those
png_bytes, read_err := os.read_entire_file(png_file, context.allocator)
if read_err != nil {
die("Could not read file (%v)", read_err)
}
defer delete(png_bytes)
img, png_err := png.load_from_bytes(png_bytes)
if png_err != nil {
die("Could not parse file (%v)", png_err)
}
defer image.destroy(img)
info, fi_err := os.fstat(png_file, allocator = context.temp_allocator)
if fi_err != nil {
die("Could not load file info (%v)", fi_err)
}
compiled_path := strings.concatenate(
{COMPILED_DIR, filepath.stem(info.name), ".qoi"},
allocator = context.temp_allocator,
)
qoi_err := qoi.save_to_file(compiled_path, img)
if qoi_err != nil {
die("Could not convert PNG to QOI (%v)", qoi_err)
}
abs_path, found_abs := filepath.abs(
compiled_path,
context.temp_allocator
)
if !found_abs {
die("Could not find absolute path for %v", compiled_path)
}
rel_path, rel_err := filepath.rel(
output_dir,
abs_path,
context.temp_allocator,
)
if rel_err != nil {
die(
"Could not find relative path from %v to %v (%v)",
output_dir,
compiled_path,
rel_err,
)
}
line := fmt.aprintf("{{data = #load(%w), anim = .None}}", rel_path)
images[filepath.stem(path)] = line
paths_to_res_type[path] = "Image_Id"
}
@(private="file")
_load_sprite_sheet :: proc(
path: string,
doc: ^ase.Document
) {
sprite_sheet, ss_err := aseutil.create_sprite_sheet(doc, {
size = {int(doc.header.width), int(doc.header.height)},
count = int(doc.header.frames),
})
if ss_err != nil {
die(
"Could not create sprite sheet from aseprite file %v (%v)",
path,
ss_err,
)
}
defer aseutil.destroy(sprite_sheet)
pixels := make(
[]image.RGBA_Pixel,
sprite_sheet.width * sprite_sheet.height,
allocator = context.temp_allocator,
)
i := 0
for i < sprite_sheet.width * sprite_sheet.height {
channel := i * 4
pixels[i].r = sprite_sheet.data[channel + 0]
pixels[i].g = sprite_sheet.data[channel + 1]
pixels[i].b = sprite_sheet.data[channel + 2]
pixels[i].a = sprite_sheet.data[channel + 3]
i += 1
}
img, img_ok := image.pixels_to_image(
pixels,
sprite_sheet.width,
sprite_sheet.height,
)
if !img_ok {
die("Could not create sprite sheet image %v", path)
}
compiled_path := strings.concatenate(
{COMPILED_DIR, filepath.stem(path), "-sheet.qoi"},
allocator = context.temp_allocator,
)
qoi_err := qoi.save_to_file(compiled_path, &img)
if qoi_err != nil {
die("Could not save spritesheet %v (%v)", path, qoi_err)
}
abs_path, found_abs := filepath.abs(
compiled_path,
context.temp_allocator
)
if !found_abs {
die("Could not find absolute path for %v", compiled_path)
}
rel_path, rel_err := filepath.rel(
output_dir,
abs_path,
context.temp_allocator,
)
if rel_err != nil {
die(
"Could not find relative path from %v to %v (%v)",
output_dir,
compiled_path,
rel_err,
)
}
anim_name := strings.to_ada_case(
filepath.stem(path),
allocator = context.temp_allocator,
)
line := fmt.aprintf("{{data = #load(%w), anim = .%v}}", rel_path, anim_name)
images[filepath.stem(path)] = line
paths_to_res_type[path] = "Image_Id"
}
@(private="file")
_load_animation :: proc(path: string, doc: ^ase.Document) {
tags: map[string]struct{
from: i32,
to: i32,
}
defer delete(tags)
for frame in doc.frames {
for chunk in frame.chunks {
bin_tags, is_tags := chunk.(ase.Tags_Chunk)
if !is_tags {
continue
}
for bin_tag in bin_tags {
tags[bin_tag.name] = {
from = i32(bin_tag.from_frame),
to = i32(bin_tag.to_frame),
}
}
}
}
frame_durations := make([]i32, doc.header.frames)
defer delete(frame_durations)
i := 0
for frame in doc.frames {
frame_durations[i] = i32(frame.header.duration)
i += 1
}
line := fmt.aprintf(
"{{frame_count = %w, frame_durations = %w, tags = %w}}",
len(frame_durations),
frame_durations[:],
tags,
)
animations[filepath.stem(path)] = line
}
load_ase :: proc(path: string, ase_file: ^os.File, output: ^os.File) {
doc: ase.Document
defer ase.destroy_doc(&doc)
unmarshal_err := ase.unmarshal(&doc, path, alloc = context.temp_allocator)
if unmarshal_err != nil {
die("Could not unmarshal aseprite file %v (%v)", path, unmarshal_err)
}
// Load animation
_load_animation(path, &doc)
// Load sprite sheet
_load_sprite_sheet(path, &doc)
}
|