Compare commits
11
Commits
sprite_pivot
...
camera
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
589f2bd45c | ||
|
|
c720774c79 | ||
|
|
c1f3d0e61b | ||
|
|
a85380fb71 | ||
|
|
21d2a2c0e4 | ||
|
|
d488161433 | ||
|
|
49a3b8a682 | ||
|
|
35420b7344 | ||
|
|
2c209da3b2 | ||
|
|
5040446ec1 | ||
|
|
269578548a |
+63
-24
@@ -56,10 +56,12 @@ Char_Def :: struct {
|
||||
|
||||
// Packed atlas pixels + per-frame rects (indexed like the combined frames list)
|
||||
Atlas :: struct {
|
||||
pixels: []u8,
|
||||
width: int,
|
||||
height: int,
|
||||
rects: [][4]int, // [x, y, w, h] per frame; sizes may differ across clips
|
||||
pixels: []u8,
|
||||
width: int,
|
||||
height: int,
|
||||
rects: [][4]int,
|
||||
source_sizes: [][2]int,
|
||||
trim_offsets: [][2]int,
|
||||
}
|
||||
|
||||
// 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)
|
||||
defer delete(atlas.pixels)
|
||||
defer delete(atlas.rects)
|
||||
defer delete(atlas.source_sizes)
|
||||
defer delete(atlas.trim_offsets)
|
||||
|
||||
ensure_dir(out_dir)
|
||||
|
||||
@@ -283,22 +287,56 @@ destroy_frames :: proc(frames: []^image.Image) {
|
||||
delete(frames)
|
||||
}
|
||||
|
||||
// pack_atlas packs variable-size frames with stb_rect_pack and blits them into one sheet.
|
||||
// Caller owns atlas.pixels and atlas.rects.
|
||||
opaque_bounds :: proc(img: ^image.Image, alpha_min: u8 = 1) -> (x, y, w, h: int) {
|
||||
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 {
|
||||
PADDING :: 1 // 1px gap to reduce bleeding if filtered later
|
||||
PADDING :: 1
|
||||
|
||||
pack_rects := make([]stbrp.Rect, len(frames))
|
||||
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
|
||||
max_w := 0
|
||||
max_h := 0
|
||||
for frame, i in frames {
|
||||
w := frame.width + PADDING
|
||||
h := frame.height + PADDING
|
||||
tx, ty, tw, th := opaque_bounds(frame)
|
||||
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 {
|
||||
id = c.int(i), // pack_rects may reorder; id maps back to frame index
|
||||
id = c.int(i),
|
||||
w = stbrp.Coord(w),
|
||||
h = stbrp.Coord(h),
|
||||
}
|
||||
@@ -307,7 +345,6 @@ pack_atlas :: proc(frames: []^image.Image) -> Atlas {
|
||||
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)))))
|
||||
atlas_w, atlas_h := 0, 0
|
||||
packed := false
|
||||
@@ -326,10 +363,9 @@ pack_atlas :: proc(frames: []^image.Image) -> Atlas {
|
||||
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))
|
||||
|
||||
// Blit using each rect's id (array order may have changed during packing)
|
||||
for pr in pack_rects {
|
||||
if !pr.was_packed {
|
||||
fmt.eprintfln("frame %d was not packed", pr.id)
|
||||
@@ -337,27 +373,30 @@ pack_atlas :: proc(frames: []^image.Image) -> Atlas {
|
||||
}
|
||||
idx := int(pr.id)
|
||||
frame := frames[idx]
|
||||
fw := frame.width
|
||||
fh := frame.height
|
||||
trim := trims[idx]
|
||||
tx, ty, tw, th := trim[0], trim[1], trim[2], trim[3]
|
||||
dst_x := int(pr.x)
|
||||
dst_y := int(pr.y)
|
||||
|
||||
src := bytes.buffer_to_bytes(&frame.pixels)
|
||||
for y in 0 ..< fh {
|
||||
src_row := src[y * fw * 4:(y + 1) * fw * 4]
|
||||
src_w := int(frame.width)
|
||||
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
|
||||
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)
|
||||
return Atlas {
|
||||
pixels = pixels,
|
||||
width = atlas_w,
|
||||
height = atlas_h,
|
||||
rects = rects,
|
||||
pixels = pixels,
|
||||
width = atlas_w,
|
||||
height = atlas_h,
|
||||
rects = rects,
|
||||
source_sizes = source_sizes,
|
||||
trim_offsets = trim_offsets,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -422,8 +461,8 @@ build_char_def :: proc(
|
||||
r := atlas.rects[slot]
|
||||
frame_defs[i] = Frame_Def {
|
||||
rect = r,
|
||||
source_size = {r[2], r[3]},
|
||||
trim_offset = {0, 0}, // no trimming in v1
|
||||
source_size = atlas.source_sizes[slot],
|
||||
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": [
|
||||
{
|
||||
"rect": [
|
||||
0,
|
||||
0,
|
||||
268,
|
||||
326
|
||||
1253,
|
||||
630,
|
||||
204,
|
||||
311
|
||||
],
|
||||
"source_size": [
|
||||
268,
|
||||
326
|
||||
],
|
||||
"trim_offset": [
|
||||
0,
|
||||
0
|
||||
9,
|
||||
14
|
||||
]
|
||||
},
|
||||
{
|
||||
"rect": [
|
||||
269,
|
||||
1256,
|
||||
0,
|
||||
268,
|
||||
326
|
||||
218,
|
||||
314
|
||||
],
|
||||
"source_size": [
|
||||
268,
|
||||
326
|
||||
],
|
||||
"trim_offset": [
|
||||
0,
|
||||
0
|
||||
9,
|
||||
10
|
||||
]
|
||||
},
|
||||
{
|
||||
"rect": [
|
||||
538,
|
||||
0,
|
||||
268,
|
||||
326
|
||||
1458,
|
||||
630,
|
||||
231,
|
||||
310
|
||||
],
|
||||
"source_size": [
|
||||
268,
|
||||
326
|
||||
],
|
||||
"trim_offset": [
|
||||
0,
|
||||
0
|
||||
9,
|
||||
8
|
||||
]
|
||||
},
|
||||
{
|
||||
"rect": [
|
||||
807,
|
||||
0,
|
||||
268,
|
||||
326
|
||||
643,
|
||||
943,
|
||||
243,
|
||||
295
|
||||
],
|
||||
"source_size": [
|
||||
268,
|
||||
326
|
||||
],
|
||||
"trim_offset": [
|
||||
0,
|
||||
0
|
||||
10,
|
||||
7
|
||||
]
|
||||
},
|
||||
{
|
||||
"rect": [
|
||||
1076,
|
||||
0,
|
||||
268,
|
||||
326
|
||||
881,
|
||||
1245,
|
||||
254,
|
||||
278
|
||||
],
|
||||
"source_size": [
|
||||
268,
|
||||
326
|
||||
],
|
||||
"trim_offset": [
|
||||
0,
|
||||
0
|
||||
]
|
||||
},
|
||||
{
|
||||
"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
|
||||
11,
|
||||
8
|
||||
]
|
||||
},
|
||||
{
|
||||
"rect": [
|
||||
0,
|
||||
327,
|
||||
268,
|
||||
326
|
||||
944,
|
||||
247,
|
||||
288
|
||||
],
|
||||
"source_size": [
|
||||
268,
|
||||
326
|
||||
],
|
||||
"trim_offset": [
|
||||
0,
|
||||
0
|
||||
10,
|
||||
7
|
||||
]
|
||||
},
|
||||
{
|
||||
"rect": [
|
||||
269,
|
||||
327,
|
||||
268,
|
||||
326
|
||||
1669,
|
||||
941,
|
||||
236,
|
||||
305
|
||||
],
|
||||
"source_size": [
|
||||
268,
|
||||
326
|
||||
],
|
||||
"trim_offset": [
|
||||
0,
|
||||
0
|
||||
]
|
||||
},
|
||||
{
|
||||
"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
|
||||
9,
|
||||
8
|
||||
]
|
||||
},
|
||||
{
|
||||
"rect": [
|
||||
0,
|
||||
654,
|
||||
268,
|
||||
326
|
||||
316,
|
||||
223,
|
||||
313
|
||||
],
|
||||
"source_size": [
|
||||
268,
|
||||
326
|
||||
],
|
||||
"trim_offset": [
|
||||
0,
|
||||
0
|
||||
9,
|
||||
9
|
||||
]
|
||||
},
|
||||
{
|
||||
"rect": [
|
||||
269,
|
||||
654,
|
||||
268,
|
||||
326
|
||||
1458,
|
||||
941,
|
||||
210,
|
||||
310
|
||||
],
|
||||
"source_size": [
|
||||
268,
|
||||
326
|
||||
],
|
||||
"trim_offset": [
|
||||
0,
|
||||
0
|
||||
9,
|
||||
13
|
||||
]
|
||||
},
|
||||
{
|
||||
"rect": [
|
||||
538,
|
||||
654,
|
||||
268,
|
||||
326
|
||||
224,
|
||||
316,
|
||||
211,
|
||||
313
|
||||
],
|
||||
"source_size": [
|
||||
268,
|
||||
326
|
||||
],
|
||||
"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
|
||||
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": [
|
||||
{
|
||||
"rect": [
|
||||
807,
|
||||
654,
|
||||
213,
|
||||
319
|
||||
414,
|
||||
630,
|
||||
205,
|
||||
312
|
||||
],
|
||||
"source_size": [
|
||||
213,
|
||||
319
|
||||
],
|
||||
"trim_offset": [
|
||||
0,
|
||||
0
|
||||
6,
|
||||
7
|
||||
]
|
||||
},
|
||||
{
|
||||
"rect": [
|
||||
1021,
|
||||
654,
|
||||
213,
|
||||
319
|
||||
647,
|
||||
316,
|
||||
206,
|
||||
313
|
||||
],
|
||||
"source_size": [
|
||||
213,
|
||||
319
|
||||
],
|
||||
"trim_offset": [
|
||||
0,
|
||||
0
|
||||
5,
|
||||
6
|
||||
]
|
||||
},
|
||||
{
|
||||
"rect": [
|
||||
1235,
|
||||
654,
|
||||
213,
|
||||
319
|
||||
854,
|
||||
316,
|
||||
206,
|
||||
313
|
||||
],
|
||||
"source_size": [
|
||||
213,
|
||||
319
|
||||
],
|
||||
"trim_offset": [
|
||||
0,
|
||||
0
|
||||
5,
|
||||
6
|
||||
]
|
||||
},
|
||||
{
|
||||
"rect": [
|
||||
1449,
|
||||
654,
|
||||
213,
|
||||
319
|
||||
1684,
|
||||
0,
|
||||
207,
|
||||
314
|
||||
],
|
||||
"source_size": [
|
||||
213,
|
||||
319
|
||||
],
|
||||
"trim_offset": [
|
||||
0,
|
||||
0
|
||||
4,
|
||||
5
|
||||
]
|
||||
},
|
||||
{
|
||||
"rect": [
|
||||
1663,
|
||||
654,
|
||||
213,
|
||||
319
|
||||
1256,
|
||||
315,
|
||||
207,
|
||||
314
|
||||
],
|
||||
"source_size": [
|
||||
213,
|
||||
319
|
||||
],
|
||||
"trim_offset": [
|
||||
0,
|
||||
0
|
||||
4,
|
||||
5
|
||||
]
|
||||
},
|
||||
{
|
||||
"rect": [
|
||||
807,
|
||||
974,
|
||||
213,
|
||||
319
|
||||
420,
|
||||
0,
|
||||
208,
|
||||
315
|
||||
],
|
||||
"source_size": [
|
||||
213,
|
||||
319
|
||||
],
|
||||
"trim_offset": [
|
||||
0,
|
||||
0
|
||||
3,
|
||||
4
|
||||
]
|
||||
},
|
||||
{
|
||||
"rect": [
|
||||
1021,
|
||||
974,
|
||||
213,
|
||||
319
|
||||
1475,
|
||||
0,
|
||||
208,
|
||||
314
|
||||
],
|
||||
"source_size": [
|
||||
213,
|
||||
319
|
||||
],
|
||||
"trim_offset": [
|
||||
0,
|
||||
0
|
||||
3,
|
||||
4
|
||||
]
|
||||
},
|
||||
{
|
||||
"rect": [
|
||||
1235,
|
||||
974,
|
||||
213,
|
||||
319
|
||||
629,
|
||||
0,
|
||||
208,
|
||||
315
|
||||
],
|
||||
"source_size": [
|
||||
213,
|
||||
319
|
||||
],
|
||||
"trim_offset": [
|
||||
0,
|
||||
0
|
||||
]
|
||||
},
|
||||
{
|
||||
"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
|
||||
3,
|
||||
3
|
||||
]
|
||||
},
|
||||
{
|
||||
"rect": [
|
||||
0,
|
||||
981,
|
||||
213,
|
||||
319
|
||||
0,
|
||||
209,
|
||||
315
|
||||
],
|
||||
"source_size": [
|
||||
213,
|
||||
319
|
||||
],
|
||||
"trim_offset": [
|
||||
0,
|
||||
0
|
||||
2,
|
||||
3
|
||||
]
|
||||
},
|
||||
{
|
||||
"rect": [
|
||||
214,
|
||||
981,
|
||||
213,
|
||||
319
|
||||
210,
|
||||
0,
|
||||
209,
|
||||
315
|
||||
],
|
||||
"source_size": [
|
||||
213,
|
||||
319
|
||||
],
|
||||
"trim_offset": [
|
||||
0,
|
||||
0
|
||||
2,
|
||||
3
|
||||
]
|
||||
},
|
||||
{
|
||||
"rect": [
|
||||
428,
|
||||
981,
|
||||
213,
|
||||
319
|
||||
838,
|
||||
0,
|
||||
208,
|
||||
315
|
||||
],
|
||||
"source_size": [
|
||||
213,
|
||||
319
|
||||
],
|
||||
"trim_offset": [
|
||||
0,
|
||||
0
|
||||
3,
|
||||
3
|
||||
]
|
||||
},
|
||||
{
|
||||
"rect": [
|
||||
642,
|
||||
1294,
|
||||
213,
|
||||
319
|
||||
1047,
|
||||
0,
|
||||
208,
|
||||
315
|
||||
],
|
||||
"source_size": [
|
||||
213,
|
||||
319
|
||||
],
|
||||
"trim_offset": [
|
||||
0,
|
||||
0
|
||||
3,
|
||||
4
|
||||
]
|
||||
},
|
||||
{
|
||||
"rect": [
|
||||
856,
|
||||
1294,
|
||||
213,
|
||||
319
|
||||
1464,
|
||||
315,
|
||||
207,
|
||||
314
|
||||
],
|
||||
"source_size": [
|
||||
213,
|
||||
319
|
||||
],
|
||||
"trim_offset": [
|
||||
0,
|
||||
0
|
||||
4,
|
||||
5
|
||||
]
|
||||
},
|
||||
{
|
||||
"rect": [
|
||||
1070,
|
||||
1294,
|
||||
213,
|
||||
319
|
||||
1672,
|
||||
315,
|
||||
207,
|
||||
314
|
||||
],
|
||||
"source_size": [
|
||||
213,
|
||||
319
|
||||
],
|
||||
"trim_offset": [
|
||||
0,
|
||||
0
|
||||
4,
|
||||
5
|
||||
]
|
||||
},
|
||||
{
|
||||
"rect": [
|
||||
1284,
|
||||
1294,
|
||||
213,
|
||||
319
|
||||
0,
|
||||
630,
|
||||
206,
|
||||
313
|
||||
],
|
||||
"source_size": [
|
||||
213,
|
||||
319
|
||||
],
|
||||
"trim_offset": [
|
||||
0,
|
||||
0
|
||||
5,
|
||||
6
|
||||
]
|
||||
},
|
||||
{
|
||||
"rect": [
|
||||
1498,
|
||||
1294,
|
||||
213,
|
||||
319
|
||||
207,
|
||||
630,
|
||||
206,
|
||||
313
|
||||
],
|
||||
"source_size": [
|
||||
213,
|
||||
319
|
||||
],
|
||||
"trim_offset": [
|
||||
0,
|
||||
0
|
||||
5,
|
||||
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
|
||||
]
|
||||
}
|
||||
]
|
||||
|
||||
+123
-20
@@ -1,6 +1,7 @@
|
||||
package engine
|
||||
|
||||
import "core:fmt"
|
||||
import "core:mem"
|
||||
import "core:path/filepath"
|
||||
import sdl "vendor:sdl3"
|
||||
|
||||
@@ -10,7 +11,14 @@ Vertex :: struct {
|
||||
}
|
||||
|
||||
SPRITE_VERT_COUNT :: 6
|
||||
VERTEX_BUFFER_SIZE :: SPRITE_VERT_COUNT * size_of(Vertex)
|
||||
MAX_SPRITES :: 128
|
||||
SPRITE_VERTS_SIZE :: SPRITE_VERT_COUNT * size_of(Vertex)
|
||||
VERTEX_BUFFER_SIZE :: MAX_SPRITES * SPRITE_VERTS_SIZE
|
||||
|
||||
Queued_Sprite :: struct {
|
||||
texture: ^sdl.GPUTexture,
|
||||
verts: [SPRITE_VERT_COUNT]Vertex,
|
||||
}
|
||||
|
||||
App :: struct {
|
||||
window: ^sdl.Window,
|
||||
@@ -25,6 +33,9 @@ App :: struct {
|
||||
swapchain_h: u32,
|
||||
vertex_buffer: ^sdl.GPUBuffer,
|
||||
transfer_buffer: ^sdl.GPUTransferBuffer,
|
||||
draw_list: [dynamic]Queued_Sprite,
|
||||
clear_color: sdl.FColor,
|
||||
camera: Camera,
|
||||
}
|
||||
|
||||
Shader_Backend :: enum {
|
||||
@@ -112,10 +123,16 @@ init :: proc(app: ^App, title: cstring, width, height: i32) -> bool {
|
||||
return false
|
||||
}
|
||||
|
||||
app.draw_list = make([dynamic]Queued_Sprite)
|
||||
|
||||
app.camera = camera_default()
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
shutdown :: proc(app: ^App) {
|
||||
delete(app.draw_list)
|
||||
|
||||
if app.device != nil {
|
||||
ok := sdl.WaitForGPUIdle(app.device)
|
||||
if !ok {
|
||||
@@ -164,9 +181,13 @@ events :: proc() -> bool {
|
||||
return true
|
||||
}
|
||||
|
||||
begin_frame :: proc(app: ^App, clear: sdl.FColor = {0.12, 0.12, 0.16, 1}) {
|
||||
app.cmd = sdl.AcquireGPUCommandBuffer(app.device)
|
||||
begin_frame :: proc(app: ^App, clear_color: sdl.FColor = {0.12, 0.12, 0.16, 1}) {
|
||||
clear(&app.draw_list)
|
||||
app.clear_color = clear_color
|
||||
app.render_pass = nil
|
||||
app.swapchain_texture = nil
|
||||
|
||||
app.cmd = sdl.AcquireGPUCommandBuffer(app.device)
|
||||
if app.cmd == nil {
|
||||
fmt.eprintfln("AcquireGPUCommandBuffer failed: %s", sdl.GetError())
|
||||
return
|
||||
@@ -181,36 +202,104 @@ begin_frame :: proc(app: ^App, clear: sdl.FColor = {0.12, 0.12, 0.16, 1}) {
|
||||
)
|
||||
|
||||
if !ok || app.swapchain_texture == nil {
|
||||
app.swapchain_texture = nil
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
end_frame :: proc(app: ^App) {
|
||||
if app.cmd == nil {
|
||||
clear(&app.draw_list)
|
||||
return
|
||||
}
|
||||
|
||||
cmd := app.cmd
|
||||
defer {
|
||||
app.render_pass = nil
|
||||
app.swapchain_texture = nil
|
||||
app.cmd = nil
|
||||
clear(&app.draw_list)
|
||||
}
|
||||
|
||||
if app.swapchain_texture == nil {
|
||||
if !sdl.SubmitGPUCommandBuffer(cmd) {
|
||||
fmt.eprintfln("SubmitGPUCommandBuffer failed")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
n := len(app.draw_list)
|
||||
if n > 0 {
|
||||
map_ptr := sdl.MapGPUTransferBuffer(app.device, app.transfer_buffer, false)
|
||||
if map_ptr == nil {
|
||||
fmt.eprintfln("MapGPUTransferBuffer failed: %s", sdl.GetError())
|
||||
if !sdl.SubmitGPUCommandBuffer(cmd) {
|
||||
fmt.eprintfln("SubmitGPUCommandBuffer failed")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
for i in 0 ..< n {
|
||||
q := &app.draw_list[i]
|
||||
offset := i * SPRITE_VERTS_SIZE
|
||||
mem.copy(
|
||||
rawptr(uintptr(map_ptr) + uintptr(offset)),
|
||||
raw_data(q.verts[:]),
|
||||
SPRITE_VERTS_SIZE,
|
||||
)
|
||||
}
|
||||
sdl.UnmapGPUTransferBuffer(app.device, app.transfer_buffer)
|
||||
|
||||
copy_pass := sdl.BeginGPUCopyPass(cmd)
|
||||
src := sdl.GPUTransferBufferLocation {
|
||||
transfer_buffer = app.transfer_buffer,
|
||||
offset = 0,
|
||||
}
|
||||
dst := sdl.GPUBufferRegion {
|
||||
buffer = app.vertex_buffer,
|
||||
offset = 0,
|
||||
size = u32(n * SPRITE_VERTS_SIZE),
|
||||
}
|
||||
sdl.UploadToGPUBuffer(copy_pass, src, dst, false)
|
||||
sdl.EndGPUCopyPass(copy_pass)
|
||||
}
|
||||
|
||||
color_info := sdl.GPUColorTargetInfo {
|
||||
texture = app.swapchain_texture,
|
||||
clear_color = clear,
|
||||
clear_color = app.clear_color,
|
||||
load_op = .CLEAR,
|
||||
store_op = .STORE,
|
||||
}
|
||||
|
||||
app.render_pass = sdl.BeginGPURenderPass(app.cmd, &color_info, 1, nil)
|
||||
|
||||
app.render_pass = sdl.BeginGPURenderPass(cmd, &color_info, 1, nil)
|
||||
sdl.BindGPUGraphicsPipeline(app.render_pass, app.pipeline)
|
||||
}
|
||||
|
||||
end_frame :: proc(app: ^App) {
|
||||
if app.render_pass != nil {
|
||||
sdl.EndGPURenderPass(app.render_pass)
|
||||
app.render_pass = nil
|
||||
}
|
||||
i := 0
|
||||
for i < n {
|
||||
run := texture_run_len(app.draw_list[:], i)
|
||||
q0 := app.draw_list[i]
|
||||
|
||||
if app.cmd != nil {
|
||||
ok := sdl.SubmitGPUCommandBuffer(app.cmd)
|
||||
if !ok {
|
||||
fmt.eprintfln("SubmitGPUCommandBuffer failed")
|
||||
sampler_binding := sdl.GPUTextureSamplerBinding {
|
||||
texture = q0.texture,
|
||||
sampler = app.sampler,
|
||||
}
|
||||
app.cmd = nil
|
||||
}
|
||||
sdl.BindGPUFragmentSamplers(app.render_pass, 0, &sampler_binding, 1)
|
||||
|
||||
app.swapchain_texture = nil
|
||||
vb_binding := sdl.GPUBufferBinding {
|
||||
buffer = app.vertex_buffer,
|
||||
offset = u32(i * SPRITE_VERTS_SIZE),
|
||||
}
|
||||
sdl.BindGPUVertexBuffers(app.render_pass, 0, &vb_binding, 1)
|
||||
|
||||
sdl.DrawGPUPrimitives(app.render_pass, u32(run * SPRITE_VERT_COUNT), 1, 0, 0)
|
||||
|
||||
i += run
|
||||
}
|
||||
sdl.EndGPURenderPass(app.render_pass)
|
||||
app.render_pass = nil
|
||||
|
||||
if !sdl.SubmitGPUCommandBuffer(cmd) {
|
||||
fmt.eprintfln("SubmitGPUCommandBuffer failed")
|
||||
}
|
||||
}
|
||||
|
||||
load_gpu_shader :: proc(
|
||||
@@ -338,3 +427,17 @@ create_sprite_pipeline :: proc(app: ^App) -> ^sdl.GPUGraphicsPipeline {
|
||||
|
||||
return pipeline
|
||||
}
|
||||
|
||||
texture_run_len :: proc(list: []Queued_Sprite, start: int) -> int {
|
||||
if start < 0 || start >= len(list) do return 0
|
||||
|
||||
tex := list[start].texture
|
||||
n := 1
|
||||
|
||||
for i in start + 1 ..< len(list) {
|
||||
if list[i].texture != tex do break
|
||||
n += 1
|
||||
}
|
||||
|
||||
return n
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package engine
|
||||
|
||||
Camera :: struct {
|
||||
position: Vec2,
|
||||
anchor: Vec2,
|
||||
}
|
||||
|
||||
camera_default :: proc() -> Camera {
|
||||
return Camera{position = {0, 0}, anchor = {0.5, 0.5}}
|
||||
}
|
||||
|
||||
world_to_screen :: proc(cam: Camera, world: Vec2, viewport: Vec2) -> Vec2 {
|
||||
return {
|
||||
world.x - cam.position.x + viewport.x * cam.anchor[0],
|
||||
world.y - cam.position.y + viewport.y * cam.anchor[1],
|
||||
}
|
||||
}
|
||||
@@ -210,6 +210,22 @@ character_clip :: proc(data: ^Character_Data, clip_name: string) -> (clip: Clip_
|
||||
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(
|
||||
data: ^Character_Data,
|
||||
clip_name: string,
|
||||
@@ -218,10 +234,7 @@ character_frame_rect :: proc(
|
||||
rect: [4]int,
|
||||
ok: bool,
|
||||
) {
|
||||
clip, found := data.def.clips[clip_name]
|
||||
if !found || frame_index < 0 || frame_index >= len(clip.frames) {
|
||||
return {}, false
|
||||
}
|
||||
|
||||
return clip.frames[frame_index].rect, true
|
||||
frame, found := character_frame(data, clip_name, frame_index)
|
||||
if !found do return {}, false
|
||||
return frame.rect, true
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package engine
|
||||
|
||||
import sdl "vendor:sdl3"
|
||||
|
||||
Key :: enum {
|
||||
A,
|
||||
D,
|
||||
Left,
|
||||
Right,
|
||||
}
|
||||
|
||||
key_down :: proc(key: Key) -> bool {
|
||||
keys := sdl.GetKeyboardState(nil)
|
||||
if keys == nil do return false
|
||||
|
||||
scan_code: sdl.Scancode
|
||||
switch key {
|
||||
case .A:
|
||||
scan_code = .A
|
||||
case .D:
|
||||
scan_code = .D
|
||||
case .Left:
|
||||
scan_code = .LEFT
|
||||
case .Right:
|
||||
scan_code = .RIGHT
|
||||
}
|
||||
return keys[scan_code]
|
||||
}
|
||||
+46
-68
@@ -1,6 +1,5 @@
|
||||
package engine
|
||||
|
||||
import "core:mem"
|
||||
import sdl "vendor:sdl3"
|
||||
|
||||
Vec2 :: [2]f32
|
||||
@@ -11,6 +10,7 @@ Sprite :: struct {
|
||||
clip: string,
|
||||
frame: int,
|
||||
time: f32,
|
||||
flip_x: bool,
|
||||
}
|
||||
|
||||
spawn_sprite :: proc(
|
||||
@@ -39,7 +39,6 @@ spawn_sprite :: proc(
|
||||
return sprite
|
||||
}
|
||||
|
||||
// stubbed for later
|
||||
update_sprite :: proc(sprite: ^Sprite, dt: f32) {
|
||||
if sprite == nil || sprite.data == nil do return
|
||||
if dt <= 0 do return
|
||||
@@ -84,44 +83,65 @@ to_clip :: proc(px, py, sw, sh: f32) -> [2]f32 {
|
||||
}
|
||||
|
||||
draw_sprite :: proc(app: ^App, sprite: ^Sprite) {
|
||||
if app.render_pass == nil || app.cmd == nil || app.swapchain_texture == nil {
|
||||
return // begin_frame may have skipped (minimized window, etc.)
|
||||
if app.cmd == nil || app.swapchain_texture == nil {
|
||||
return
|
||||
}
|
||||
if sprite == nil || sprite.data == nil || sprite.data.texture == nil {
|
||||
return
|
||||
}
|
||||
if len(app.draw_list) >= MAX_SPRITES {
|
||||
return
|
||||
}
|
||||
|
||||
rect, ok := character_frame_rect(sprite.data, sprite.clip, sprite.frame)
|
||||
frame, ok := character_frame(sprite.data, sprite.clip, sprite.frame)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
fw := f32(rect[2])
|
||||
fh := f32(rect[3])
|
||||
src_w := f32(frame.source_size[0])
|
||||
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
|
||||
origin := sprite_quad_origin(sprite.position, {fw, fh}, pivot)
|
||||
x0_px := origin.x
|
||||
y0_px := origin.y
|
||||
|
||||
viewport := Vec2{f32(app.swapchain_w), f32(app.swapchain_h)}
|
||||
feet := world_to_screen(app.camera, sprite.position, viewport)
|
||||
|
||||
canvas_top := feet.y - src_h * pivot[1]
|
||||
x0_px, y0_px: f32
|
||||
if sprite.flip_x {
|
||||
canvas_left := feet.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 := feet.x - src_w * pivot[0]
|
||||
x0_px = canvas_left + trim_x
|
||||
y0_px = canvas_top + trim_y
|
||||
}
|
||||
x1_px := x0_px + fw
|
||||
y1_px := y0_px + fh
|
||||
|
||||
sw := f32(app.swapchain_w)
|
||||
sh := f32(app.swapchain_h)
|
||||
p0 := to_clip(x0_px, y0_px, sw, sh) // top-left
|
||||
p1 := to_clip(x1_px, y0_px, sw, sh) // top-right
|
||||
p2 := to_clip(x1_px, y1_px, sw, sh) // bottom-right
|
||||
p3 := to_clip(x0_px, y1_px, sw, sh) // bottom-left
|
||||
p0 := to_clip(x0_px, y0_px, sw, sh)
|
||||
p1 := to_clip(x1_px, y0_px, sw, sh)
|
||||
p2 := to_clip(x1_px, y1_px, sw, sh)
|
||||
p3 := to_clip(x0_px, y1_px, sw, sh)
|
||||
|
||||
tex_w := f32(sprite.data.width)
|
||||
tex_h := f32(sprite.data.height)
|
||||
u0 := f32(rect[0]) / tex_w
|
||||
v0 := f32(rect[1]) / tex_h
|
||||
u1 := f32(rect[0] + rect[2]) / tex_w
|
||||
v1 := f32(rect[1] + rect[3]) / tex_h
|
||||
u0 := f32(frame.rect[0]) / tex_w
|
||||
v0 := f32(frame.rect[1]) / tex_h
|
||||
u1 := f32(frame.rect[0] + frame.rect[2]) / tex_w
|
||||
v1 := f32(frame.rect[1] + frame.rect[3]) / tex_h
|
||||
if sprite.flip_x do u0, u1 = u1, u0
|
||||
|
||||
// Two triangles: (0,1,2) and (0,2,3)
|
||||
verts := [6]Vertex {
|
||||
verts := [SPRITE_VERT_COUNT]Vertex {
|
||||
{pos = p0, uv = {u0, v0}},
|
||||
{pos = p1, uv = {u1, v0}},
|
||||
{pos = p2, uv = {u1, v1}},
|
||||
@@ -130,54 +150,7 @@ draw_sprite :: proc(app: ^App, sprite: ^Sprite) {
|
||||
{pos = p3, uv = {u0, v1}},
|
||||
}
|
||||
|
||||
map_ptr := sdl.MapGPUTransferBuffer(app.device, app.transfer_buffer, false)
|
||||
if map_ptr == nil {
|
||||
return
|
||||
}
|
||||
mem.copy(map_ptr, raw_data(verts[:]), size_of(verts))
|
||||
sdl.UnmapGPUTransferBuffer(app.device, app.transfer_buffer)
|
||||
|
||||
// IMPORTANT: SDL does not allow beginning a copy pass while a render pass
|
||||
// is active on the same command buffer. Upload on a separate command buffer.
|
||||
copy_cmd := sdl.AcquireGPUCommandBuffer(app.device)
|
||||
if copy_cmd == nil {
|
||||
return
|
||||
}
|
||||
copy_pass := sdl.BeginGPUCopyPass(copy_cmd)
|
||||
|
||||
src := sdl.GPUTransferBufferLocation {
|
||||
transfer_buffer = app.transfer_buffer,
|
||||
offset = 0,
|
||||
}
|
||||
dst := sdl.GPUBufferRegion {
|
||||
buffer = app.vertex_buffer,
|
||||
offset = 0,
|
||||
size = u32(size_of(verts)),
|
||||
}
|
||||
sdl.UploadToGPUBuffer(copy_pass, src, dst, false)
|
||||
sdl.EndGPUCopyPass(copy_pass)
|
||||
fence := sdl.SubmitGPUCommandBufferAndAcquireFence(copy_cmd)
|
||||
if fence == nil {
|
||||
return
|
||||
}
|
||||
defer sdl.ReleaseGPUFence(app.device, fence)
|
||||
if !sdl.WaitForGPUFences(app.device, true, &fence, 1) {
|
||||
return
|
||||
}
|
||||
|
||||
sampler_binding := sdl.GPUTextureSamplerBinding {
|
||||
texture = sprite.data.texture,
|
||||
sampler = app.sampler,
|
||||
}
|
||||
sdl.BindGPUFragmentSamplers(app.render_pass, 0, &sampler_binding, 1)
|
||||
|
||||
vb_binding := sdl.GPUBufferBinding {
|
||||
buffer = app.vertex_buffer,
|
||||
offset = 0,
|
||||
}
|
||||
sdl.BindGPUVertexBuffers(app.render_pass, 0, &vb_binding, 1)
|
||||
|
||||
sdl.DrawGPUPrimitives(app.render_pass, 6, 1, 0, 0)
|
||||
append(&app.draw_list, Queued_Sprite{texture = sprite.data.texture, verts = verts})
|
||||
}
|
||||
|
||||
set_sprite_clip :: proc(sprite: ^Sprite, clip: string) {
|
||||
@@ -196,3 +169,8 @@ set_sprite_clip :: proc(sprite: ^Sprite, clip: string) {
|
||||
sprite_quad_origin :: proc(position: Vec2, size: Vec2, pivot: [2]f32) -> Vec2 {
|
||||
return {position.x - size.x * pivot[0], position.y - size.y * pivot[1]}
|
||||
}
|
||||
|
||||
set_sprite_flip_x :: proc(sprite: ^Sprite, flip: bool) {
|
||||
if sprite == nil do return
|
||||
sprite.flip_x = flip
|
||||
}
|
||||
|
||||
@@ -15,12 +15,32 @@ main :: proc() {
|
||||
toad := eng.spawn_sprite(&data, {400, 500}, "idle", 0)
|
||||
|
||||
last := eng.now_seconds()
|
||||
SPEED :: f32(200)
|
||||
|
||||
for eng.events() {
|
||||
now := eng.now_seconds()
|
||||
dt := f32(now - last)
|
||||
last = now
|
||||
|
||||
left := eng.key_down(.A) || eng.key_down(.Left)
|
||||
right := eng.key_down(.D) || eng.key_down(.Right)
|
||||
|
||||
if left || right {
|
||||
eng.set_sprite_clip(&toad, "walk")
|
||||
if left {
|
||||
toad.position.x -= SPEED * dt
|
||||
toad.flip_x = true
|
||||
}
|
||||
|
||||
if right {
|
||||
toad.position.x += SPEED * dt
|
||||
toad.flip_x = false
|
||||
}
|
||||
} else {
|
||||
eng.set_sprite_clip(&toad, "idle")
|
||||
}
|
||||
|
||||
app.camera.position = toad.position - {0, 100}
|
||||
eng.update_sprite(&toad, dt)
|
||||
|
||||
eng.begin_frame(&app)
|
||||
|
||||
Reference in New Issue
Block a user