trim atlas of excess transparent pixels
This commit is contained in:
+63
-24
@@ -56,10 +56,12 @@ Char_Def :: struct {
|
|||||||
|
|
||||||
// Packed atlas pixels + per-frame rects (indexed like the combined frames list)
|
// Packed atlas pixels + per-frame rects (indexed like the combined frames list)
|
||||||
Atlas :: struct {
|
Atlas :: struct {
|
||||||
pixels: []u8,
|
pixels: []u8,
|
||||||
width: int,
|
width: int,
|
||||||
height: int,
|
height: int,
|
||||||
rects: [][4]int, // [x, y, w, h] per frame; sizes may differ across clips
|
rects: [][4]int,
|
||||||
|
source_sizes: [][2]int,
|
||||||
|
trim_offsets: [][2]int,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Where one clip's frames sit inside the packed atlas frame list
|
// Where one clip's frames sit inside the packed atlas frame list
|
||||||
@@ -95,6 +97,8 @@ bake_character :: proc(in_dir, out_dir: string) {
|
|||||||
atlas := pack_atlas(frames)
|
atlas := pack_atlas(frames)
|
||||||
defer delete(atlas.pixels)
|
defer delete(atlas.pixels)
|
||||||
defer delete(atlas.rects)
|
defer delete(atlas.rects)
|
||||||
|
defer delete(atlas.source_sizes)
|
||||||
|
defer delete(atlas.trim_offsets)
|
||||||
|
|
||||||
ensure_dir(out_dir)
|
ensure_dir(out_dir)
|
||||||
|
|
||||||
@@ -283,22 +287,56 @@ destroy_frames :: proc(frames: []^image.Image) {
|
|||||||
delete(frames)
|
delete(frames)
|
||||||
}
|
}
|
||||||
|
|
||||||
// pack_atlas packs variable-size frames with stb_rect_pack and blits them into one sheet.
|
opaque_bounds :: proc(img: ^image.Image, alpha_min: u8 = 1) -> (x, y, w, h: int) {
|
||||||
// Caller owns atlas.pixels and atlas.rects.
|
iw, ih := int(img.width), int(img.height)
|
||||||
|
pixels := bytes.buffer_to_bytes(&img.pixels)
|
||||||
|
|
||||||
|
min_x, min_y := iw, ih
|
||||||
|
max_x, max_y := -1, -1
|
||||||
|
|
||||||
|
for py in 0 ..< ih {
|
||||||
|
for px in 0 ..< iw {
|
||||||
|
i := (py * iw + px) * 4
|
||||||
|
a := pixels[i + 3]
|
||||||
|
if a >= alpha_min {
|
||||||
|
if px < min_x do min_x = px
|
||||||
|
if py < min_y do min_y = py
|
||||||
|
if px > max_x do max_x = px
|
||||||
|
if py > max_y do max_y = py
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if max_x < min_x {
|
||||||
|
return 0, 0, 1, 1
|
||||||
|
}
|
||||||
|
return min_x, min_y, max_x - min_x + 1, max_y - min_y + 1
|
||||||
|
}
|
||||||
|
|
||||||
pack_atlas :: proc(frames: []^image.Image) -> Atlas {
|
pack_atlas :: proc(frames: []^image.Image) -> Atlas {
|
||||||
PADDING :: 1 // 1px gap to reduce bleeding if filtered later
|
PADDING :: 1
|
||||||
|
|
||||||
pack_rects := make([]stbrp.Rect, len(frames))
|
pack_rects := make([]stbrp.Rect, len(frames))
|
||||||
defer delete(pack_rects)
|
defer delete(pack_rects)
|
||||||
|
|
||||||
|
trims := make([][4]int, len(frames))
|
||||||
|
defer delete(trims)
|
||||||
|
source_sizes := make([][2]int, len(frames))
|
||||||
|
trim_offsets := make([][2]int, len(frames))
|
||||||
|
|
||||||
total_area := 0
|
total_area := 0
|
||||||
max_w := 0
|
max_w := 0
|
||||||
max_h := 0
|
max_h := 0
|
||||||
for frame, i in frames {
|
for frame, i in frames {
|
||||||
w := frame.width + PADDING
|
tx, ty, tw, th := opaque_bounds(frame)
|
||||||
h := frame.height + PADDING
|
trims[i] = {tx, ty, tw, th}
|
||||||
|
source_sizes[i] = {int(frame.width), int(frame.height)}
|
||||||
|
trim_offsets[i] = {tx, ty}
|
||||||
|
|
||||||
|
w := tw + PADDING
|
||||||
|
h := th + PADDING
|
||||||
pack_rects[i] = stbrp.Rect {
|
pack_rects[i] = stbrp.Rect {
|
||||||
id = c.int(i), // pack_rects may reorder; id maps back to frame index
|
id = c.int(i),
|
||||||
w = stbrp.Coord(w),
|
w = stbrp.Coord(w),
|
||||||
h = stbrp.Coord(h),
|
h = stbrp.Coord(h),
|
||||||
}
|
}
|
||||||
@@ -307,7 +345,6 @@ pack_atlas :: proc(frames: []^image.Image) -> Atlas {
|
|||||||
max_h = max(max_h, h)
|
max_h = max(max_h, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Grow a square-ish atlas until everything fits
|
|
||||||
side := max(max_w, max_h, int(math.ceil(math.sqrt(f64(total_area)))))
|
side := max(max_w, max_h, int(math.ceil(math.sqrt(f64(total_area)))))
|
||||||
atlas_w, atlas_h := 0, 0
|
atlas_w, atlas_h := 0, 0
|
||||||
packed := false
|
packed := false
|
||||||
@@ -326,10 +363,9 @@ pack_atlas :: proc(frames: []^image.Image) -> Atlas {
|
|||||||
os.exit(1)
|
os.exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
pixels := make([]u8, atlas_w * atlas_h * 4) // transparent
|
pixels := make([]u8, atlas_w * atlas_h * 4)
|
||||||
rects := make([][4]int, len(frames))
|
rects := make([][4]int, len(frames))
|
||||||
|
|
||||||
// Blit using each rect's id (array order may have changed during packing)
|
|
||||||
for pr in pack_rects {
|
for pr in pack_rects {
|
||||||
if !pr.was_packed {
|
if !pr.was_packed {
|
||||||
fmt.eprintfln("frame %d was not packed", pr.id)
|
fmt.eprintfln("frame %d was not packed", pr.id)
|
||||||
@@ -337,27 +373,30 @@ pack_atlas :: proc(frames: []^image.Image) -> Atlas {
|
|||||||
}
|
}
|
||||||
idx := int(pr.id)
|
idx := int(pr.id)
|
||||||
frame := frames[idx]
|
frame := frames[idx]
|
||||||
fw := frame.width
|
trim := trims[idx]
|
||||||
fh := frame.height
|
tx, ty, tw, th := trim[0], trim[1], trim[2], trim[3]
|
||||||
dst_x := int(pr.x)
|
dst_x := int(pr.x)
|
||||||
dst_y := int(pr.y)
|
dst_y := int(pr.y)
|
||||||
|
|
||||||
src := bytes.buffer_to_bytes(&frame.pixels)
|
src := bytes.buffer_to_bytes(&frame.pixels)
|
||||||
for y in 0 ..< fh {
|
src_w := int(frame.width)
|
||||||
src_row := src[y * fw * 4:(y + 1) * fw * 4]
|
for y in 0 ..< th {
|
||||||
|
src_row := src[((ty + y) * src_w + tx) * 4:][:tw * 4]
|
||||||
dst_i := ((dst_y + y) * atlas_w + dst_x) * 4
|
dst_i := ((dst_y + y) * atlas_w + dst_x) * 4
|
||||||
copy(pixels[dst_i:], src_row)
|
copy(pixels[dst_i:], src_row)
|
||||||
}
|
}
|
||||||
|
|
||||||
rects[idx] = {dst_x, dst_y, fw, fh}
|
rects[idx] = {dst_x, dst_y, tw, th}
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.printfln("packed atlas %dx%d", atlas_w, atlas_h)
|
fmt.printfln("packed atlas %dx%d", atlas_w, atlas_h)
|
||||||
return Atlas {
|
return Atlas {
|
||||||
pixels = pixels,
|
pixels = pixels,
|
||||||
width = atlas_w,
|
width = atlas_w,
|
||||||
height = atlas_h,
|
height = atlas_h,
|
||||||
rects = rects,
|
rects = rects,
|
||||||
|
source_sizes = source_sizes,
|
||||||
|
trim_offsets = trim_offsets,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -422,8 +461,8 @@ build_char_def :: proc(
|
|||||||
r := atlas.rects[slot]
|
r := atlas.rects[slot]
|
||||||
frame_defs[i] = Frame_Def {
|
frame_defs[i] = Frame_Def {
|
||||||
rect = r,
|
rect = r,
|
||||||
source_size = {r[2], r[3]},
|
source_size = atlas.source_sizes[slot],
|
||||||
trim_offset = {0, 0}, // no trimming in v1
|
trim_offset = atlas.trim_offsets[slot],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 1.8 MiB After Width: | Height: | Size: 1.6 MiB |
@@ -14,274 +14,274 @@
|
|||||||
"frames": [
|
"frames": [
|
||||||
{
|
{
|
||||||
"rect": [
|
"rect": [
|
||||||
0,
|
1253,
|
||||||
0,
|
630,
|
||||||
268,
|
204,
|
||||||
326
|
311
|
||||||
],
|
],
|
||||||
"source_size": [
|
"source_size": [
|
||||||
268,
|
268,
|
||||||
326
|
326
|
||||||
],
|
],
|
||||||
"trim_offset": [
|
"trim_offset": [
|
||||||
0,
|
9,
|
||||||
0
|
14
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"rect": [
|
"rect": [
|
||||||
269,
|
1256,
|
||||||
0,
|
0,
|
||||||
268,
|
218,
|
||||||
326
|
314
|
||||||
],
|
],
|
||||||
"source_size": [
|
"source_size": [
|
||||||
268,
|
268,
|
||||||
326
|
326
|
||||||
],
|
],
|
||||||
"trim_offset": [
|
"trim_offset": [
|
||||||
0,
|
9,
|
||||||
0
|
10
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"rect": [
|
"rect": [
|
||||||
538,
|
1458,
|
||||||
0,
|
630,
|
||||||
268,
|
231,
|
||||||
326
|
310
|
||||||
],
|
],
|
||||||
"source_size": [
|
"source_size": [
|
||||||
268,
|
268,
|
||||||
326
|
326
|
||||||
],
|
],
|
||||||
"trim_offset": [
|
"trim_offset": [
|
||||||
0,
|
9,
|
||||||
0
|
8
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"rect": [
|
"rect": [
|
||||||
807,
|
643,
|
||||||
0,
|
943,
|
||||||
268,
|
243,
|
||||||
326
|
295
|
||||||
],
|
],
|
||||||
"source_size": [
|
"source_size": [
|
||||||
268,
|
268,
|
||||||
326
|
326
|
||||||
],
|
],
|
||||||
"trim_offset": [
|
"trim_offset": [
|
||||||
0,
|
10,
|
||||||
0
|
7
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"rect": [
|
"rect": [
|
||||||
1076,
|
881,
|
||||||
0,
|
1245,
|
||||||
268,
|
254,
|
||||||
326
|
278
|
||||||
],
|
],
|
||||||
"source_size": [
|
"source_size": [
|
||||||
268,
|
268,
|
||||||
326
|
326
|
||||||
],
|
],
|
||||||
"trim_offset": [
|
"trim_offset": [
|
||||||
0,
|
11,
|
||||||
0
|
8
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"rect": [
|
|
||||||
1345,
|
|
||||||
0,
|
|
||||||
268,
|
|
||||||
326
|
|
||||||
],
|
|
||||||
"source_size": [
|
|
||||||
268,
|
|
||||||
326
|
|
||||||
],
|
|
||||||
"trim_offset": [
|
|
||||||
0,
|
|
||||||
0
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"rect": [
|
|
||||||
1614,
|
|
||||||
0,
|
|
||||||
268,
|
|
||||||
326
|
|
||||||
],
|
|
||||||
"source_size": [
|
|
||||||
268,
|
|
||||||
326
|
|
||||||
],
|
|
||||||
"trim_offset": [
|
|
||||||
0,
|
|
||||||
0
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"rect": [
|
"rect": [
|
||||||
0,
|
0,
|
||||||
327,
|
944,
|
||||||
268,
|
247,
|
||||||
326
|
288
|
||||||
],
|
],
|
||||||
"source_size": [
|
"source_size": [
|
||||||
268,
|
268,
|
||||||
326
|
326
|
||||||
],
|
],
|
||||||
"trim_offset": [
|
"trim_offset": [
|
||||||
0,
|
10,
|
||||||
0
|
7
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"rect": [
|
"rect": [
|
||||||
269,
|
1669,
|
||||||
327,
|
941,
|
||||||
268,
|
236,
|
||||||
326
|
305
|
||||||
],
|
],
|
||||||
"source_size": [
|
"source_size": [
|
||||||
268,
|
268,
|
||||||
326
|
326
|
||||||
],
|
],
|
||||||
"trim_offset": [
|
"trim_offset": [
|
||||||
0,
|
9,
|
||||||
0
|
8
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"rect": [
|
|
||||||
538,
|
|
||||||
327,
|
|
||||||
268,
|
|
||||||
326
|
|
||||||
],
|
|
||||||
"source_size": [
|
|
||||||
268,
|
|
||||||
326
|
|
||||||
],
|
|
||||||
"trim_offset": [
|
|
||||||
0,
|
|
||||||
0
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"rect": [
|
|
||||||
807,
|
|
||||||
327,
|
|
||||||
268,
|
|
||||||
326
|
|
||||||
],
|
|
||||||
"source_size": [
|
|
||||||
268,
|
|
||||||
326
|
|
||||||
],
|
|
||||||
"trim_offset": [
|
|
||||||
0,
|
|
||||||
0
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"rect": [
|
|
||||||
1076,
|
|
||||||
327,
|
|
||||||
268,
|
|
||||||
326
|
|
||||||
],
|
|
||||||
"source_size": [
|
|
||||||
268,
|
|
||||||
326
|
|
||||||
],
|
|
||||||
"trim_offset": [
|
|
||||||
0,
|
|
||||||
0
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"rect": [
|
|
||||||
1345,
|
|
||||||
327,
|
|
||||||
268,
|
|
||||||
326
|
|
||||||
],
|
|
||||||
"source_size": [
|
|
||||||
268,
|
|
||||||
326
|
|
||||||
],
|
|
||||||
"trim_offset": [
|
|
||||||
0,
|
|
||||||
0
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"rect": [
|
|
||||||
1614,
|
|
||||||
327,
|
|
||||||
268,
|
|
||||||
326
|
|
||||||
],
|
|
||||||
"source_size": [
|
|
||||||
268,
|
|
||||||
326
|
|
||||||
],
|
|
||||||
"trim_offset": [
|
|
||||||
0,
|
|
||||||
0
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"rect": [
|
"rect": [
|
||||||
0,
|
0,
|
||||||
654,
|
316,
|
||||||
268,
|
223,
|
||||||
326
|
313
|
||||||
],
|
],
|
||||||
"source_size": [
|
"source_size": [
|
||||||
268,
|
268,
|
||||||
326
|
326
|
||||||
],
|
],
|
||||||
"trim_offset": [
|
"trim_offset": [
|
||||||
0,
|
9,
|
||||||
0
|
9
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"rect": [
|
"rect": [
|
||||||
269,
|
1458,
|
||||||
654,
|
941,
|
||||||
268,
|
210,
|
||||||
326
|
310
|
||||||
],
|
],
|
||||||
"source_size": [
|
"source_size": [
|
||||||
268,
|
268,
|
||||||
326
|
326
|
||||||
],
|
],
|
||||||
"trim_offset": [
|
"trim_offset": [
|
||||||
0,
|
9,
|
||||||
0
|
13
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"rect": [
|
"rect": [
|
||||||
538,
|
224,
|
||||||
654,
|
316,
|
||||||
268,
|
211,
|
||||||
326
|
313
|
||||||
],
|
],
|
||||||
"source_size": [
|
"source_size": [
|
||||||
268,
|
268,
|
||||||
326
|
326
|
||||||
],
|
],
|
||||||
"trim_offset": [
|
"trim_offset": [
|
||||||
|
11,
|
||||||
|
11
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"rect": [
|
||||||
|
1032,
|
||||||
|
630,
|
||||||
|
220,
|
||||||
|
311
|
||||||
|
],
|
||||||
|
"source_size": [
|
||||||
|
268,
|
||||||
|
326
|
||||||
|
],
|
||||||
|
"trim_offset": [
|
||||||
|
13,
|
||||||
|
7
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"rect": [
|
||||||
|
1032,
|
||||||
|
942,
|
||||||
|
228,
|
||||||
|
302
|
||||||
|
],
|
||||||
|
"source_size": [
|
||||||
|
268,
|
||||||
|
326
|
||||||
|
],
|
||||||
|
"trim_offset": [
|
||||||
|
16,
|
||||||
|
4
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"rect": [
|
||||||
0,
|
0,
|
||||||
0
|
1233,
|
||||||
|
237,
|
||||||
|
286
|
||||||
|
],
|
||||||
|
"source_size": [
|
||||||
|
268,
|
||||||
|
326
|
||||||
|
],
|
||||||
|
"trim_offset": [
|
||||||
|
18,
|
||||||
|
2
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"rect": [
|
||||||
|
643,
|
||||||
|
1239,
|
||||||
|
237,
|
||||||
|
286
|
||||||
|
],
|
||||||
|
"source_size": [
|
||||||
|
268,
|
||||||
|
326
|
||||||
|
],
|
||||||
|
"trim_offset": [
|
||||||
|
18,
|
||||||
|
2
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"rect": [
|
||||||
|
414,
|
||||||
|
943,
|
||||||
|
228,
|
||||||
|
302
|
||||||
|
],
|
||||||
|
"source_size": [
|
||||||
|
268,
|
||||||
|
326
|
||||||
|
],
|
||||||
|
"trim_offset": [
|
||||||
|
16,
|
||||||
|
4
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"rect": [
|
||||||
|
1690,
|
||||||
|
630,
|
||||||
|
220,
|
||||||
|
310
|
||||||
|
],
|
||||||
|
"source_size": [
|
||||||
|
268,
|
||||||
|
326
|
||||||
|
],
|
||||||
|
"trim_offset": [
|
||||||
|
13,
|
||||||
|
7
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"rect": [
|
||||||
|
436,
|
||||||
|
316,
|
||||||
|
210,
|
||||||
|
313
|
||||||
|
],
|
||||||
|
"source_size": [
|
||||||
|
268,
|
||||||
|
326
|
||||||
|
],
|
||||||
|
"trim_offset": [
|
||||||
|
11,
|
||||||
|
11
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@@ -292,290 +292,290 @@
|
|||||||
"frames": [
|
"frames": [
|
||||||
{
|
{
|
||||||
"rect": [
|
"rect": [
|
||||||
807,
|
414,
|
||||||
654,
|
630,
|
||||||
213,
|
205,
|
||||||
319
|
312
|
||||||
],
|
],
|
||||||
"source_size": [
|
"source_size": [
|
||||||
213,
|
213,
|
||||||
319
|
319
|
||||||
],
|
],
|
||||||
"trim_offset": [
|
"trim_offset": [
|
||||||
0,
|
6,
|
||||||
0
|
7
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"rect": [
|
"rect": [
|
||||||
1021,
|
647,
|
||||||
654,
|
316,
|
||||||
213,
|
206,
|
||||||
319
|
313
|
||||||
],
|
],
|
||||||
"source_size": [
|
"source_size": [
|
||||||
213,
|
213,
|
||||||
319
|
319
|
||||||
],
|
],
|
||||||
"trim_offset": [
|
"trim_offset": [
|
||||||
0,
|
5,
|
||||||
0
|
6
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"rect": [
|
"rect": [
|
||||||
1235,
|
854,
|
||||||
654,
|
316,
|
||||||
213,
|
206,
|
||||||
319
|
313
|
||||||
],
|
],
|
||||||
"source_size": [
|
"source_size": [
|
||||||
213,
|
213,
|
||||||
319
|
319
|
||||||
],
|
],
|
||||||
"trim_offset": [
|
"trim_offset": [
|
||||||
0,
|
5,
|
||||||
0
|
6
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"rect": [
|
"rect": [
|
||||||
1449,
|
1684,
|
||||||
654,
|
0,
|
||||||
213,
|
207,
|
||||||
319
|
314
|
||||||
],
|
],
|
||||||
"source_size": [
|
"source_size": [
|
||||||
213,
|
213,
|
||||||
319
|
319
|
||||||
],
|
],
|
||||||
"trim_offset": [
|
"trim_offset": [
|
||||||
0,
|
4,
|
||||||
0
|
5
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"rect": [
|
"rect": [
|
||||||
1663,
|
1256,
|
||||||
654,
|
315,
|
||||||
213,
|
207,
|
||||||
319
|
314
|
||||||
],
|
],
|
||||||
"source_size": [
|
"source_size": [
|
||||||
213,
|
213,
|
||||||
319
|
319
|
||||||
],
|
],
|
||||||
"trim_offset": [
|
"trim_offset": [
|
||||||
0,
|
4,
|
||||||
0
|
5
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"rect": [
|
"rect": [
|
||||||
807,
|
420,
|
||||||
974,
|
0,
|
||||||
213,
|
208,
|
||||||
319
|
315
|
||||||
],
|
],
|
||||||
"source_size": [
|
"source_size": [
|
||||||
213,
|
213,
|
||||||
319
|
319
|
||||||
],
|
],
|
||||||
"trim_offset": [
|
"trim_offset": [
|
||||||
0,
|
3,
|
||||||
0
|
4
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"rect": [
|
"rect": [
|
||||||
1021,
|
1475,
|
||||||
974,
|
0,
|
||||||
213,
|
208,
|
||||||
319
|
314
|
||||||
],
|
],
|
||||||
"source_size": [
|
"source_size": [
|
||||||
213,
|
213,
|
||||||
319
|
319
|
||||||
],
|
],
|
||||||
"trim_offset": [
|
"trim_offset": [
|
||||||
0,
|
3,
|
||||||
0
|
4
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"rect": [
|
"rect": [
|
||||||
1235,
|
629,
|
||||||
974,
|
0,
|
||||||
213,
|
208,
|
||||||
319
|
315
|
||||||
],
|
],
|
||||||
"source_size": [
|
"source_size": [
|
||||||
213,
|
213,
|
||||||
319
|
319
|
||||||
],
|
],
|
||||||
"trim_offset": [
|
"trim_offset": [
|
||||||
0,
|
3,
|
||||||
0
|
3
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"rect": [
|
|
||||||
1449,
|
|
||||||
974,
|
|
||||||
213,
|
|
||||||
319
|
|
||||||
],
|
|
||||||
"source_size": [
|
|
||||||
213,
|
|
||||||
319
|
|
||||||
],
|
|
||||||
"trim_offset": [
|
|
||||||
0,
|
|
||||||
0
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"rect": [
|
|
||||||
1663,
|
|
||||||
974,
|
|
||||||
213,
|
|
||||||
319
|
|
||||||
],
|
|
||||||
"source_size": [
|
|
||||||
213,
|
|
||||||
319
|
|
||||||
],
|
|
||||||
"trim_offset": [
|
|
||||||
0,
|
|
||||||
0
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"rect": [
|
"rect": [
|
||||||
0,
|
0,
|
||||||
981,
|
0,
|
||||||
213,
|
209,
|
||||||
319
|
315
|
||||||
],
|
],
|
||||||
"source_size": [
|
"source_size": [
|
||||||
213,
|
213,
|
||||||
319
|
319
|
||||||
],
|
],
|
||||||
"trim_offset": [
|
"trim_offset": [
|
||||||
0,
|
2,
|
||||||
0
|
3
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"rect": [
|
"rect": [
|
||||||
214,
|
210,
|
||||||
981,
|
0,
|
||||||
213,
|
209,
|
||||||
319
|
315
|
||||||
],
|
],
|
||||||
"source_size": [
|
"source_size": [
|
||||||
213,
|
213,
|
||||||
319
|
319
|
||||||
],
|
],
|
||||||
"trim_offset": [
|
"trim_offset": [
|
||||||
0,
|
2,
|
||||||
0
|
3
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"rect": [
|
"rect": [
|
||||||
428,
|
838,
|
||||||
981,
|
0,
|
||||||
213,
|
208,
|
||||||
319
|
315
|
||||||
],
|
],
|
||||||
"source_size": [
|
"source_size": [
|
||||||
213,
|
213,
|
||||||
319
|
319
|
||||||
],
|
],
|
||||||
"trim_offset": [
|
"trim_offset": [
|
||||||
0,
|
3,
|
||||||
0
|
3
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"rect": [
|
"rect": [
|
||||||
642,
|
1047,
|
||||||
1294,
|
0,
|
||||||
213,
|
208,
|
||||||
319
|
315
|
||||||
],
|
],
|
||||||
"source_size": [
|
"source_size": [
|
||||||
213,
|
213,
|
||||||
319
|
319
|
||||||
],
|
],
|
||||||
"trim_offset": [
|
"trim_offset": [
|
||||||
0,
|
3,
|
||||||
0
|
4
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"rect": [
|
"rect": [
|
||||||
856,
|
1464,
|
||||||
1294,
|
315,
|
||||||
213,
|
207,
|
||||||
319
|
314
|
||||||
],
|
],
|
||||||
"source_size": [
|
"source_size": [
|
||||||
213,
|
213,
|
||||||
319
|
319
|
||||||
],
|
],
|
||||||
"trim_offset": [
|
"trim_offset": [
|
||||||
0,
|
4,
|
||||||
0
|
5
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"rect": [
|
"rect": [
|
||||||
1070,
|
1672,
|
||||||
1294,
|
315,
|
||||||
213,
|
207,
|
||||||
319
|
314
|
||||||
],
|
],
|
||||||
"source_size": [
|
"source_size": [
|
||||||
213,
|
213,
|
||||||
319
|
319
|
||||||
],
|
],
|
||||||
"trim_offset": [
|
"trim_offset": [
|
||||||
0,
|
4,
|
||||||
0
|
5
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"rect": [
|
"rect": [
|
||||||
1284,
|
0,
|
||||||
1294,
|
630,
|
||||||
213,
|
206,
|
||||||
319
|
313
|
||||||
],
|
],
|
||||||
"source_size": [
|
"source_size": [
|
||||||
213,
|
213,
|
||||||
319
|
319
|
||||||
],
|
],
|
||||||
"trim_offset": [
|
"trim_offset": [
|
||||||
0,
|
5,
|
||||||
0
|
6
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"rect": [
|
"rect": [
|
||||||
1498,
|
207,
|
||||||
1294,
|
630,
|
||||||
213,
|
206,
|
||||||
319
|
313
|
||||||
],
|
],
|
||||||
"source_size": [
|
"source_size": [
|
||||||
213,
|
213,
|
||||||
319
|
319
|
||||||
],
|
],
|
||||||
"trim_offset": [
|
"trim_offset": [
|
||||||
0,
|
5,
|
||||||
0
|
6
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"rect": [
|
||||||
|
620,
|
||||||
|
630,
|
||||||
|
205,
|
||||||
|
312
|
||||||
|
],
|
||||||
|
"source_size": [
|
||||||
|
213,
|
||||||
|
319
|
||||||
|
],
|
||||||
|
"trim_offset": [
|
||||||
|
6,
|
||||||
|
7
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"rect": [
|
||||||
|
826,
|
||||||
|
630,
|
||||||
|
205,
|
||||||
|
312
|
||||||
|
],
|
||||||
|
"source_size": [
|
||||||
|
213,
|
||||||
|
319
|
||||||
|
],
|
||||||
|
"trim_offset": [
|
||||||
|
6,
|
||||||
|
7
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -210,6 +210,22 @@ character_clip :: proc(data: ^Character_Data, clip_name: string) -> (clip: Clip_
|
|||||||
return character, true
|
return character, true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
character_frame :: proc(
|
||||||
|
data: ^Character_Data,
|
||||||
|
clip_name: string,
|
||||||
|
frame_index: int,
|
||||||
|
) -> (
|
||||||
|
frame: Frame_Def,
|
||||||
|
ok: bool,
|
||||||
|
) {
|
||||||
|
if data == nil do return {}, false
|
||||||
|
clip, found := data.def.clips[clip_name]
|
||||||
|
if !found || frame_index < 0 || frame_index >= len(clip.frames) {
|
||||||
|
return {}, false
|
||||||
|
}
|
||||||
|
return clip.frames[frame_index], true
|
||||||
|
}
|
||||||
|
|
||||||
character_frame_rect :: proc(
|
character_frame_rect :: proc(
|
||||||
data: ^Character_Data,
|
data: ^Character_Data,
|
||||||
clip_name: string,
|
clip_name: string,
|
||||||
@@ -218,10 +234,7 @@ character_frame_rect :: proc(
|
|||||||
rect: [4]int,
|
rect: [4]int,
|
||||||
ok: bool,
|
ok: bool,
|
||||||
) {
|
) {
|
||||||
clip, found := data.def.clips[clip_name]
|
frame, found := character_frame(data, clip_name, frame_index)
|
||||||
if !found || frame_index < 0 || frame_index >= len(clip.frames) {
|
if !found do return {}, false
|
||||||
return {}, false
|
return frame.rect, true
|
||||||
}
|
|
||||||
|
|
||||||
return clip.frames[frame_index].rect, true
|
|
||||||
}
|
}
|
||||||
|
|||||||
+30
-16
@@ -84,7 +84,7 @@ to_clip :: proc(px, py, sw, sh: f32) -> [2]f32 {
|
|||||||
|
|
||||||
draw_sprite :: proc(app: ^App, sprite: ^Sprite) {
|
draw_sprite :: proc(app: ^App, sprite: ^Sprite) {
|
||||||
if app.cmd == nil || app.swapchain_texture == nil {
|
if app.cmd == nil || app.swapchain_texture == nil {
|
||||||
return // begin_frame may have skipped (minimized window, etc.)
|
return
|
||||||
}
|
}
|
||||||
if sprite == nil || sprite.data == nil || sprite.data.texture == nil {
|
if sprite == nil || sprite.data == nil || sprite.data.texture == nil {
|
||||||
return
|
return
|
||||||
@@ -93,37 +93,51 @@ draw_sprite :: proc(app: ^App, sprite: ^Sprite) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
rect, ok := character_frame_rect(sprite.data, sprite.clip, sprite.frame)
|
frame, ok := character_frame(sprite.data, sprite.clip, sprite.frame)
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fw := f32(rect[2])
|
src_w := f32(frame.source_size[0])
|
||||||
fh := f32(rect[3])
|
src_h := f32(frame.source_size[1])
|
||||||
|
if src_w <= 0 do src_w = f32(frame.rect[2])
|
||||||
|
if src_h <= 0 do src_h = f32(frame.rect[3])
|
||||||
|
|
||||||
|
fw := f32(frame.rect[2])
|
||||||
|
fh := f32(frame.rect[3])
|
||||||
|
trim_x := f32(frame.trim_offset[0])
|
||||||
|
trim_y := f32(frame.trim_offset[1])
|
||||||
pivot := sprite.data.def.pivot
|
pivot := sprite.data.def.pivot
|
||||||
origin := sprite_quad_origin(sprite.position, {fw, fh}, pivot)
|
|
||||||
x0_px := origin.x
|
canvas_top := sprite.position.y - src_h * pivot[1]
|
||||||
y0_px := origin.y
|
x0_px, y0_px: f32
|
||||||
|
if sprite.flip_x {
|
||||||
|
canvas_left := sprite.position.x - (1.0 - pivot[0]) * src_w
|
||||||
|
x0_px = canvas_left + (src_w - trim_x - fw)
|
||||||
|
y0_px = canvas_top + trim_y
|
||||||
|
} else {
|
||||||
|
canvas_left := sprite.position.x - src_w * pivot[0]
|
||||||
|
x0_px = canvas_left + trim_x
|
||||||
|
y0_px = canvas_top + trim_y
|
||||||
|
}
|
||||||
x1_px := x0_px + fw
|
x1_px := x0_px + fw
|
||||||
y1_px := y0_px + fh
|
y1_px := y0_px + fh
|
||||||
|
|
||||||
sw := f32(app.swapchain_w)
|
sw := f32(app.swapchain_w)
|
||||||
sh := f32(app.swapchain_h)
|
sh := f32(app.swapchain_h)
|
||||||
p0 := to_clip(x0_px, y0_px, sw, sh) // top-left
|
p0 := to_clip(x0_px, y0_px, sw, sh)
|
||||||
p1 := to_clip(x1_px, y0_px, sw, sh) // top-right
|
p1 := to_clip(x1_px, y0_px, sw, sh)
|
||||||
p2 := to_clip(x1_px, y1_px, sw, sh) // bottom-right
|
p2 := to_clip(x1_px, y1_px, sw, sh)
|
||||||
p3 := to_clip(x0_px, y1_px, sw, sh) // bottom-left
|
p3 := to_clip(x0_px, y1_px, sw, sh)
|
||||||
|
|
||||||
tex_w := f32(sprite.data.width)
|
tex_w := f32(sprite.data.width)
|
||||||
tex_h := f32(sprite.data.height)
|
tex_h := f32(sprite.data.height)
|
||||||
u0 := f32(rect[0]) / tex_w
|
u0 := f32(frame.rect[0]) / tex_w
|
||||||
v0 := f32(rect[1]) / tex_h
|
v0 := f32(frame.rect[1]) / tex_h
|
||||||
u1 := f32(rect[0] + rect[2]) / tex_w
|
u1 := f32(frame.rect[0] + frame.rect[2]) / tex_w
|
||||||
v1 := f32(rect[1] + rect[3]) / tex_h
|
v1 := f32(frame.rect[1] + frame.rect[3]) / tex_h
|
||||||
if sprite.flip_x do u0, u1 = u1, u0
|
if sprite.flip_x do u0, u1 = u1, u0
|
||||||
|
|
||||||
// Two triangles: (0,1,2) and (0,2,3)
|
|
||||||
verts := [SPRITE_VERT_COUNT]Vertex {
|
verts := [SPRITE_VERT_COUNT]Vertex {
|
||||||
{pos = p0, uv = {u0, v0}},
|
{pos = p0, uv = {u0, v0}},
|
||||||
{pos = p1, uv = {u1, v0}},
|
{pos = p1, uv = {u1, v0}},
|
||||||
|
|||||||
Reference in New Issue
Block a user