Compare commits
22
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4dced062a5 | ||
|
|
cf888f26cd | ||
|
|
248e57d3f0 | ||
|
|
209e774230 | ||
|
|
4dc173ecee | ||
|
|
5041753bad | ||
|
|
9e680c62fe | ||
|
|
589f2bd45c | ||
|
|
c720774c79 | ||
|
|
c1f3d0e61b | ||
|
|
a85380fb71 | ||
|
|
21d2a2c0e4 | ||
|
|
d488161433 | ||
|
|
49a3b8a682 | ||
|
|
35420b7344 | ||
|
|
2c209da3b2 | ||
|
|
5040446ec1 | ||
|
|
269578548a | ||
|
|
90da5934d7 | ||
|
|
4aa3ed6ed8 | ||
|
|
fbae54aef0 | ||
|
|
eea672023f |
@@ -1,14 +1,22 @@
|
|||||||
.PHONY: shaders-vulkan shaders-d3d12 shaders-metal shaders-all toad check test help
|
.PHONY: shaders-vulkan shaders-d3d12 shaders-metal shaders-all bake toad hello_sprite crowd camera_sandbox clips check test help
|
||||||
|
|
||||||
help:
|
help:
|
||||||
@echo "Targets:"
|
@echo "Targets:"
|
||||||
@echo " shaders-vulkan Compile SPIR-V into shaders/vulkan/"
|
@echo " bake Bake all characters under content/characters/"
|
||||||
@echo " shaders-d3d12 Compile DXIL into shaders/d3d12/ (needs shadercross)"
|
@echo " shaders-vulkan Compile SPIR-V into shaders/vulkan/"
|
||||||
@echo " shaders-metal Compile MSL into shaders/metal/ (needs shadercross)"
|
@echo " shaders-d3d12 Compile DXIL into shaders/d3d12/ (needs shadercross)"
|
||||||
@echo " shaders-all Build all shader backends"
|
@echo " shaders-metal Compile MSL into shaders/metal/ (needs shadercross)"
|
||||||
@echo " test Run engine unit tests"
|
@echo " shaders-all Build all shader backends"
|
||||||
@echo " check Typecheck examples/toad"
|
@echo " test Run engine unit tests"
|
||||||
@echo " toad Run the toad example"
|
@echo " check Typecheck all examples"
|
||||||
|
@echo " toad Run the toad example (full demo)"
|
||||||
|
@echo " hello_sprite Minimal load + draw"
|
||||||
|
@echo " crowd Many sprites, one Character_Data"
|
||||||
|
@echo " camera_sandbox Pan camera / Space toggles follow"
|
||||||
|
@echo " clips Keys 1/2 switch idle/walk"
|
||||||
|
|
||||||
|
bake:
|
||||||
|
./scripts/bake_all.sh
|
||||||
|
|
||||||
shaders-vulkan:
|
shaders-vulkan:
|
||||||
./scripts/shaders_vulkan.sh
|
./scripts/shaders_vulkan.sh
|
||||||
@@ -26,6 +34,22 @@ test:
|
|||||||
|
|
||||||
check:
|
check:
|
||||||
odin check examples/toad -collection:pkg=.
|
odin check examples/toad -collection:pkg=.
|
||||||
|
odin check examples/hello_sprite -collection:pkg=.
|
||||||
|
odin check examples/crowd -collection:pkg=.
|
||||||
|
odin check examples/camera_sandbox -collection:pkg=.
|
||||||
|
odin check examples/clips -collection:pkg=.
|
||||||
|
|
||||||
toad:
|
toad:
|
||||||
odin run examples/toad -collection:pkg=.
|
odin run examples/toad -collection:pkg=.
|
||||||
|
|
||||||
|
hello_sprite:
|
||||||
|
odin run examples/hello_sprite -collection:pkg=.
|
||||||
|
|
||||||
|
crowd:
|
||||||
|
odin run examples/crowd -collection:pkg=.
|
||||||
|
|
||||||
|
camera_sandbox:
|
||||||
|
odin run examples/camera_sandbox -collection:pkg=.
|
||||||
|
|
||||||
|
clips:
|
||||||
|
odin run examples/clips -collection:pkg=.
|
||||||
|
|||||||
+67
-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,60 @@ 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_rgba :: proc(pixels: []u8, iw, ih: int, alpha_min: u8 = 1) -> (x, y, w, h: int) {
|
||||||
// Caller owns atlas.pixels and atlas.rects.
|
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
|
||||||
|
if i + 3 >= len(pixels) do continue
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
return opaque_bounds_rgba(pixels, iw, ih, alpha_min)
|
||||||
|
}
|
||||||
|
|
||||||
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 +349,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 +367,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 +377,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 +465,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],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,46 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "core:testing"
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
opaque_bounds_inset :: proc(t: ^testing.T) {
|
||||||
|
// 4x4, opaque 2x2 at (1,1)
|
||||||
|
pixels := make([]u8, 4 * 4 * 4)
|
||||||
|
defer delete(pixels)
|
||||||
|
for y in 1 ..= 2 {
|
||||||
|
for x in 1 ..= 2 {
|
||||||
|
i := (y * 4 + x) * 4
|
||||||
|
pixels[i + 3] = 255
|
||||||
|
}
|
||||||
|
}
|
||||||
|
x, y, w, h := opaque_bounds_rgba(pixels, 4, 4)
|
||||||
|
testing.expect_value(t, x, 1)
|
||||||
|
testing.expect_value(t, y, 1)
|
||||||
|
testing.expect_value(t, w, 2)
|
||||||
|
testing.expect_value(t, h, 2)
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
opaque_bounds_full :: proc(t: ^testing.T) {
|
||||||
|
pixels := make([]u8, 2 * 2 * 4)
|
||||||
|
defer delete(pixels)
|
||||||
|
for i := 3; i < len(pixels); i += 4 {
|
||||||
|
pixels[i] = 255
|
||||||
|
}
|
||||||
|
x, y, w, h := opaque_bounds_rgba(pixels, 2, 2)
|
||||||
|
testing.expect_value(t, x, 0)
|
||||||
|
testing.expect_value(t, y, 0)
|
||||||
|
testing.expect_value(t, w, 2)
|
||||||
|
testing.expect_value(t, h, 2)
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
opaque_bounds_empty :: proc(t: ^testing.T) {
|
||||||
|
pixels := make([]u8, 3 * 3 * 4)
|
||||||
|
defer delete(pixels)
|
||||||
|
x, y, w, h := opaque_bounds_rgba(pixels, 3, 3)
|
||||||
|
testing.expect_value(t, x, 0)
|
||||||
|
testing.expect_value(t, y, 0)
|
||||||
|
testing.expect_value(t, w, 1)
|
||||||
|
testing.expect_value(t, h, 1)
|
||||||
|
}
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 1.9 MiB After Width: | Height: | Size: 1.6 MiB |
@@ -10,588 +10,572 @@
|
|||||||
"clips": {
|
"clips": {
|
||||||
"walk": {
|
"walk": {
|
||||||
"loop": true,
|
"loop": true,
|
||||||
"fps": 10.00000000,
|
"fps": 15.00000000,
|
||||||
"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": [
|
||||||
0,
|
11,
|
||||||
0
|
11
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"rect": [
|
"rect": [
|
||||||
807,
|
1032,
|
||||||
654,
|
630,
|
||||||
268,
|
220,
|
||||||
326
|
311
|
||||||
],
|
],
|
||||||
"source_size": [
|
"source_size": [
|
||||||
268,
|
268,
|
||||||
326
|
326
|
||||||
],
|
],
|
||||||
"trim_offset": [
|
"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
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"idle": {
|
"idle": {
|
||||||
"loop": true,
|
"loop": true,
|
||||||
"fps": 10.00000000,
|
"fps": 15.00000000,
|
||||||
"frames": [
|
"frames": [
|
||||||
{
|
{
|
||||||
"rect": [
|
"rect": [
|
||||||
1883,
|
414,
|
||||||
0,
|
630,
|
||||||
213,
|
205,
|
||||||
319
|
312
|
||||||
],
|
],
|
||||||
"source_size": [
|
"source_size": [
|
||||||
213,
|
213,
|
||||||
319
|
319
|
||||||
],
|
],
|
||||||
"trim_offset": [
|
"trim_offset": [
|
||||||
0,
|
6,
|
||||||
0
|
7
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"rect": [
|
"rect": [
|
||||||
1883,
|
647,
|
||||||
320,
|
316,
|
||||||
213,
|
206,
|
||||||
319
|
313
|
||||||
],
|
],
|
||||||
"source_size": [
|
"source_size": [
|
||||||
213,
|
213,
|
||||||
319
|
319
|
||||||
],
|
],
|
||||||
"trim_offset": [
|
"trim_offset": [
|
||||||
0,
|
5,
|
||||||
0
|
6
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"rect": [
|
"rect": [
|
||||||
1883,
|
854,
|
||||||
640,
|
316,
|
||||||
213,
|
206,
|
||||||
319
|
313
|
||||||
],
|
],
|
||||||
"source_size": [
|
"source_size": [
|
||||||
213,
|
213,
|
||||||
319
|
319
|
||||||
],
|
],
|
||||||
"trim_offset": [
|
"trim_offset": [
|
||||||
0,
|
5,
|
||||||
0
|
6
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"rect": [
|
"rect": [
|
||||||
1076,
|
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": [
|
||||||
1290,
|
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": [
|
||||||
1504,
|
420,
|
||||||
654,
|
0,
|
||||||
213,
|
208,
|
||||||
319
|
315
|
||||||
],
|
],
|
||||||
"source_size": [
|
"source_size": [
|
||||||
213,
|
213,
|
||||||
319
|
319
|
||||||
],
|
],
|
||||||
"trim_offset": [
|
"trim_offset": [
|
||||||
0,
|
3,
|
||||||
0
|
4
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"rect": [
|
"rect": [
|
||||||
1718,
|
1475,
|
||||||
960,
|
0,
|
||||||
213,
|
208,
|
||||||
319
|
314
|
||||||
],
|
],
|
||||||
"source_size": [
|
"source_size": [
|
||||||
213,
|
213,
|
||||||
319
|
319
|
||||||
],
|
],
|
||||||
"trim_offset": [
|
"trim_offset": [
|
||||||
0,
|
3,
|
||||||
0
|
4
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"rect": [
|
"rect": [
|
||||||
1076,
|
629,
|
||||||
974,
|
0,
|
||||||
213,
|
208,
|
||||||
319
|
315
|
||||||
],
|
],
|
||||||
"source_size": [
|
"source_size": [
|
||||||
213,
|
213,
|
||||||
319
|
319
|
||||||
],
|
],
|
||||||
"trim_offset": [
|
"trim_offset": [
|
||||||
0,
|
3,
|
||||||
0
|
3
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"rect": [
|
|
||||||
1290,
|
|
||||||
974,
|
|
||||||
213,
|
|
||||||
319
|
|
||||||
],
|
|
||||||
"source_size": [
|
|
||||||
213,
|
|
||||||
319
|
|
||||||
],
|
|
||||||
"trim_offset": [
|
|
||||||
0,
|
|
||||||
0
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"rect": [
|
|
||||||
1504,
|
|
||||||
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,
|
||||||
981,
|
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,
|
||||||
981,
|
315,
|
||||||
213,
|
207,
|
||||||
319
|
314
|
||||||
],
|
],
|
||||||
"source_size": [
|
"source_size": [
|
||||||
213,
|
213,
|
||||||
319
|
319
|
||||||
],
|
],
|
||||||
"trim_offset": [
|
"trim_offset": [
|
||||||
0,
|
4,
|
||||||
0
|
5
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"rect": [
|
"rect": [
|
||||||
1718,
|
1672,
|
||||||
1280,
|
315,
|
||||||
213,
|
207,
|
||||||
319
|
314
|
||||||
],
|
],
|
||||||
"source_size": [
|
"source_size": [
|
||||||
213,
|
213,
|
||||||
319
|
319
|
||||||
],
|
],
|
||||||
"trim_offset": [
|
"trim_offset": [
|
||||||
0,
|
4,
|
||||||
0
|
5
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"rect": [
|
"rect": [
|
||||||
1070,
|
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": [
|
||||||
1284,
|
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
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 42 KiB |
@@ -5,12 +5,12 @@
|
|||||||
"clips": {
|
"clips": {
|
||||||
"idle": {
|
"idle": {
|
||||||
"folder": "Idle",
|
"folder": "Idle",
|
||||||
"fps": 10,
|
"fps": 15,
|
||||||
"loop": true
|
"loop": true
|
||||||
},
|
},
|
||||||
"walk": {
|
"walk": {
|
||||||
"folder": "Walk",
|
"folder": "Walk",
|
||||||
"fps": 10,
|
"fps": 15,
|
||||||
"loop": true
|
"loop": true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,232 @@
|
|||||||
|
package engine
|
||||||
|
|
||||||
|
import "core:testing"
|
||||||
|
|
||||||
|
make_test_character :: proc(allocator := context.allocator) -> Character_Data {
|
||||||
|
data: Character_Data
|
||||||
|
data.def.clips = make(map[string]Clip_Def, allocator)
|
||||||
|
|
||||||
|
idle_frames := make([]Frame_Def, 3, allocator)
|
||||||
|
idle_frames[0] = {
|
||||||
|
rect = {0, 0, 10, 10},
|
||||||
|
}
|
||||||
|
idle_frames[1] = {
|
||||||
|
rect = {10, 0, 10, 10},
|
||||||
|
}
|
||||||
|
idle_frames[2] = {
|
||||||
|
rect = {20, 0, 10, 10},
|
||||||
|
}
|
||||||
|
data.def.clips["idle"] = Clip_Def {
|
||||||
|
loop = true,
|
||||||
|
fps = 10,
|
||||||
|
frames = idle_frames,
|
||||||
|
}
|
||||||
|
|
||||||
|
once_frames := make([]Frame_Def, 3, allocator)
|
||||||
|
once_frames[0] = {
|
||||||
|
rect = {0, 10, 10, 10},
|
||||||
|
}
|
||||||
|
once_frames[1] = {
|
||||||
|
rect = {10, 10, 10, 10},
|
||||||
|
}
|
||||||
|
once_frames[2] = {
|
||||||
|
rect = {20, 10, 10, 10},
|
||||||
|
}
|
||||||
|
data.def.clips["once"] = Clip_Def {
|
||||||
|
loop = false,
|
||||||
|
fps = 10,
|
||||||
|
frames = once_frames,
|
||||||
|
}
|
||||||
|
|
||||||
|
hold_frames := make([]Frame_Def, 2, allocator)
|
||||||
|
hold_frames[0] = {
|
||||||
|
rect = {0, 20, 10, 10},
|
||||||
|
}
|
||||||
|
hold_frames[1] = {
|
||||||
|
rect = {10, 20, 10, 10},
|
||||||
|
}
|
||||||
|
data.def.clips["hold"] = Clip_Def {
|
||||||
|
loop = true,
|
||||||
|
fps = 0,
|
||||||
|
frames = hold_frames,
|
||||||
|
}
|
||||||
|
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
|
destroy_test_character :: proc(data: ^Character_Data) {
|
||||||
|
if data == nil do return
|
||||||
|
keys := make([dynamic]string, context.temp_allocator)
|
||||||
|
for key, clip in data.def.clips {
|
||||||
|
delete(clip.frames)
|
||||||
|
append(&keys, key)
|
||||||
|
}
|
||||||
|
for key in keys {
|
||||||
|
delete_key(&data.def.clips, key)
|
||||||
|
}
|
||||||
|
delete(data.def.clips)
|
||||||
|
data^ = {}
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
character_clip_found_and_missing :: proc(t: ^testing.T) {
|
||||||
|
data := make_test_character()
|
||||||
|
defer destroy_test_character(&data)
|
||||||
|
|
||||||
|
clip, ok := character_clip(&data, "idle")
|
||||||
|
testing.expect(t, ok, "character_clip should find idle")
|
||||||
|
testing.expect_value(t, len(clip.frames), 3)
|
||||||
|
testing.expect(t, clip.loop, "idle clip should loop")
|
||||||
|
testing.expect_value(t, clip.fps, f32(10))
|
||||||
|
|
||||||
|
_, ok = character_clip(&data, "missing")
|
||||||
|
testing.expect(t, !ok, "character_clip should miss unknown name")
|
||||||
|
|
||||||
|
data.def.clips["empty"] = Clip_Def {
|
||||||
|
loop = true,
|
||||||
|
fps = 10,
|
||||||
|
frames = nil,
|
||||||
|
}
|
||||||
|
_, ok = character_clip(&data, "empty")
|
||||||
|
testing.expect(t, !ok, "character_clip should reject empty frames")
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
set_sprite_clip_switch_and_guards :: proc(t: ^testing.T) {
|
||||||
|
data := make_test_character()
|
||||||
|
defer destroy_test_character(&data)
|
||||||
|
|
||||||
|
sprite := spawn_sprite(&data, {0, 0}, "idle", 0)
|
||||||
|
testing.expect_value(t, sprite.clip, "idle")
|
||||||
|
|
||||||
|
sprite.frame = 2
|
||||||
|
sprite.time = 0.05
|
||||||
|
set_sprite_clip(&sprite, "once")
|
||||||
|
testing.expect_value(t, sprite.clip, "once")
|
||||||
|
testing.expect_value(t, sprite.frame, 0)
|
||||||
|
testing.expect_value(t, sprite.time, f32(0))
|
||||||
|
|
||||||
|
sprite.frame = 1
|
||||||
|
sprite.time = 0.09
|
||||||
|
set_sprite_clip(&sprite, "once")
|
||||||
|
testing.expect_value(t, sprite.frame, 1)
|
||||||
|
testing.expect_value(t, sprite.time, f32(0.09))
|
||||||
|
|
||||||
|
set_sprite_clip(&sprite, "nope")
|
||||||
|
testing.expect_value(t, sprite.clip, "once")
|
||||||
|
testing.expect_value(t, sprite.frame, 1)
|
||||||
|
testing.expect_value(t, sprite.time, f32(0.09))
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
spawn_sprite_valid_and_invalid :: proc(t: ^testing.T) {
|
||||||
|
data := make_test_character()
|
||||||
|
defer destroy_test_character(&data)
|
||||||
|
|
||||||
|
s := spawn_sprite(&data, {1, 2}, "idle", 0)
|
||||||
|
testing.expect_value(t, s.clip, "idle")
|
||||||
|
testing.expect_value(t, s.frame, 0)
|
||||||
|
testing.expect_value(t, s.time, f32(0))
|
||||||
|
testing.expect_value(t, s.position, Vec2{1, 2})
|
||||||
|
|
||||||
|
s2 := spawn_sprite(&data, {}, "idle", 2)
|
||||||
|
testing.expect_value(t, s2.clip, "idle")
|
||||||
|
testing.expect_value(t, s2.frame, 2)
|
||||||
|
|
||||||
|
s3 := spawn_sprite(&data, {}, "idle", 99)
|
||||||
|
testing.expect_value(t, s3.frame, 2)
|
||||||
|
|
||||||
|
bad := spawn_sprite(&data, {}, "missing", 0)
|
||||||
|
testing.expect_value(t, bad.clip, "")
|
||||||
|
testing.expect_value(t, bad.frame, 0)
|
||||||
|
testing.expect_value(t, bad.time, f32(0))
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
update_sprite_advances_one_frame :: proc(t: ^testing.T) {
|
||||||
|
data := make_test_character()
|
||||||
|
defer destroy_test_character(&data)
|
||||||
|
|
||||||
|
s := spawn_sprite(&data, {}, "idle", 0)
|
||||||
|
update_sprite(&s, 0.1)
|
||||||
|
testing.expect_value(t, s.frame, 1)
|
||||||
|
testing.expect_value(t, s.time, f32(0))
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
update_sprite_advances_multiple_frames :: proc(t: ^testing.T) {
|
||||||
|
data := make_test_character()
|
||||||
|
defer destroy_test_character(&data)
|
||||||
|
|
||||||
|
s := spawn_sprite(&data, {}, "idle", 0)
|
||||||
|
update_sprite(&s, 0.25)
|
||||||
|
testing.expect_value(t, s.frame, 2)
|
||||||
|
testing.expect(t, s.time > 0.049 && s.time < 0.051, "leftover time after multi-frame advance should be ~0.05")
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
update_sprite_loops :: proc(t: ^testing.T) {
|
||||||
|
data := make_test_character()
|
||||||
|
defer destroy_test_character(&data)
|
||||||
|
|
||||||
|
s := spawn_sprite(&data, {}, "idle", 2)
|
||||||
|
update_sprite(&s, 0.1)
|
||||||
|
testing.expect_value(t, s.frame, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
update_sprite_non_loop_holds_last :: proc(t: ^testing.T) {
|
||||||
|
data := make_test_character()
|
||||||
|
defer destroy_test_character(&data)
|
||||||
|
|
||||||
|
s := spawn_sprite(&data, {}, "once", 2)
|
||||||
|
s.time = 0.05
|
||||||
|
update_sprite(&s, 0.1)
|
||||||
|
testing.expect_value(t, s.frame, 2)
|
||||||
|
testing.expect_value(t, s.time, f32(0))
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
update_sprite_fps_zero_holds :: proc(t: ^testing.T) {
|
||||||
|
data := make_test_character()
|
||||||
|
defer destroy_test_character(&data)
|
||||||
|
|
||||||
|
s := spawn_sprite(&data, {}, "hold", 0)
|
||||||
|
update_sprite(&s, 1.0)
|
||||||
|
testing.expect_value(t, s.frame, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
update_sprite_dt_non_positive_noop :: proc(t: ^testing.T) {
|
||||||
|
data := make_test_character()
|
||||||
|
defer destroy_test_character(&data)
|
||||||
|
|
||||||
|
s := spawn_sprite(&data, {}, "idle", 0)
|
||||||
|
update_sprite(&s, 0)
|
||||||
|
update_sprite(&s, -0.1)
|
||||||
|
testing.expect_value(t, s.frame, 0)
|
||||||
|
testing.expect_value(t, s.time, f32(0))
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
update_sprite_nil_and_missing_clip_safe :: proc(t: ^testing.T) {
|
||||||
|
update_sprite(nil, 0.1)
|
||||||
|
|
||||||
|
data := make_test_character()
|
||||||
|
defer destroy_test_character(&data)
|
||||||
|
|
||||||
|
s := Sprite {
|
||||||
|
data = &data,
|
||||||
|
clip = "missing",
|
||||||
|
frame = 0,
|
||||||
|
time = 0,
|
||||||
|
}
|
||||||
|
update_sprite(&s, 0.1)
|
||||||
|
testing.expect_value(t, s.frame, 0)
|
||||||
|
|
||||||
|
s2 := Sprite {
|
||||||
|
data = nil,
|
||||||
|
clip = "idle",
|
||||||
|
}
|
||||||
|
update_sprite(&s2, 0.1)
|
||||||
|
}
|
||||||
+123
-20
@@ -1,6 +1,7 @@
|
|||||||
package engine
|
package engine
|
||||||
|
|
||||||
import "core:fmt"
|
import "core:fmt"
|
||||||
|
import "core:mem"
|
||||||
import "core:path/filepath"
|
import "core:path/filepath"
|
||||||
import sdl "vendor:sdl3"
|
import sdl "vendor:sdl3"
|
||||||
|
|
||||||
@@ -10,7 +11,14 @@ Vertex :: struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
SPRITE_VERT_COUNT :: 6
|
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 {
|
App :: struct {
|
||||||
window: ^sdl.Window,
|
window: ^sdl.Window,
|
||||||
@@ -25,6 +33,9 @@ App :: struct {
|
|||||||
swapchain_h: u32,
|
swapchain_h: u32,
|
||||||
vertex_buffer: ^sdl.GPUBuffer,
|
vertex_buffer: ^sdl.GPUBuffer,
|
||||||
transfer_buffer: ^sdl.GPUTransferBuffer,
|
transfer_buffer: ^sdl.GPUTransferBuffer,
|
||||||
|
draw_list: [dynamic]Queued_Sprite,
|
||||||
|
clear_color: sdl.FColor,
|
||||||
|
camera: Camera,
|
||||||
}
|
}
|
||||||
|
|
||||||
Shader_Backend :: enum {
|
Shader_Backend :: enum {
|
||||||
@@ -112,10 +123,16 @@ init :: proc(app: ^App, title: cstring, width, height: i32) -> bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
app.draw_list = make([dynamic]Queued_Sprite)
|
||||||
|
|
||||||
|
app.camera = camera_default()
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
shutdown :: proc(app: ^App) {
|
shutdown :: proc(app: ^App) {
|
||||||
|
delete(app.draw_list)
|
||||||
|
|
||||||
if app.device != nil {
|
if app.device != nil {
|
||||||
ok := sdl.WaitForGPUIdle(app.device)
|
ok := sdl.WaitForGPUIdle(app.device)
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -164,9 +181,13 @@ events :: proc() -> bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
begin_frame :: proc(app: ^App, clear: sdl.FColor = {0.12, 0.12, 0.16, 1}) {
|
begin_frame :: proc(app: ^App, clear_color: sdl.FColor = {0.12, 0.12, 0.16, 1}) {
|
||||||
app.cmd = sdl.AcquireGPUCommandBuffer(app.device)
|
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 {
|
if app.cmd == nil {
|
||||||
fmt.eprintfln("AcquireGPUCommandBuffer failed: %s", sdl.GetError())
|
fmt.eprintfln("AcquireGPUCommandBuffer failed: %s", sdl.GetError())
|
||||||
return
|
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 {
|
if !ok || app.swapchain_texture == nil {
|
||||||
|
app.swapchain_texture = nil
|
||||||
return
|
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 {
|
color_info := sdl.GPUColorTargetInfo {
|
||||||
texture = app.swapchain_texture,
|
texture = app.swapchain_texture,
|
||||||
clear_color = clear,
|
clear_color = app.clear_color,
|
||||||
load_op = .CLEAR,
|
load_op = .CLEAR,
|
||||||
store_op = .STORE,
|
store_op = .STORE,
|
||||||
}
|
}
|
||||||
|
app.render_pass = sdl.BeginGPURenderPass(cmd, &color_info, 1, nil)
|
||||||
app.render_pass = sdl.BeginGPURenderPass(app.cmd, &color_info, 1, nil)
|
|
||||||
|
|
||||||
sdl.BindGPUGraphicsPipeline(app.render_pass, app.pipeline)
|
sdl.BindGPUGraphicsPipeline(app.render_pass, app.pipeline)
|
||||||
}
|
|
||||||
|
|
||||||
end_frame :: proc(app: ^App) {
|
i := 0
|
||||||
if app.render_pass != nil {
|
for i < n {
|
||||||
sdl.EndGPURenderPass(app.render_pass)
|
run := texture_run_len(app.draw_list[:], i)
|
||||||
app.render_pass = nil
|
q0 := app.draw_list[i]
|
||||||
}
|
|
||||||
|
|
||||||
if app.cmd != nil {
|
sampler_binding := sdl.GPUTextureSamplerBinding {
|
||||||
ok := sdl.SubmitGPUCommandBuffer(app.cmd)
|
texture = q0.texture,
|
||||||
if !ok {
|
sampler = app.sampler,
|
||||||
fmt.eprintfln("SubmitGPUCommandBuffer failed")
|
|
||||||
}
|
}
|
||||||
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(
|
load_gpu_shader :: proc(
|
||||||
@@ -338,3 +427,17 @@ create_sprite_pipeline :: proc(app: ^App) -> ^sdl.GPUGraphicsPipeline {
|
|||||||
|
|
||||||
return pipeline
|
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,64 @@
|
|||||||
|
package engine
|
||||||
|
|
||||||
|
import "core:testing"
|
||||||
|
import sdl "vendor:sdl3"
|
||||||
|
|
||||||
|
fake_tex :: proc(id: uintptr) -> ^sdl.GPUTexture {
|
||||||
|
return cast(^sdl.GPUTexture)id
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
texture_run_len_empty_or_oob :: proc(t: ^testing.T) {
|
||||||
|
testing.expect_value(t, texture_run_len(nil, 0), 0)
|
||||||
|
list := []Queued_Sprite{}
|
||||||
|
testing.expect_value(t, texture_run_len(list, 0), 0)
|
||||||
|
list = make([]Queued_Sprite, 1)
|
||||||
|
defer delete(list)
|
||||||
|
list[0] = {texture = fake_tex(1)}
|
||||||
|
testing.expect_value(t, texture_run_len(list, -1), 0)
|
||||||
|
testing.expect_value(t, texture_run_len(list, 1), 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
texture_run_len_single :: proc(t: ^testing.T) {
|
||||||
|
list := make([]Queued_Sprite, 1)
|
||||||
|
defer delete(list)
|
||||||
|
list[0] = {texture = fake_tex(1)}
|
||||||
|
testing.expect_value(t, texture_run_len(list, 0), 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
texture_run_len_same_texture :: proc(t: ^testing.T) {
|
||||||
|
tex := fake_tex(1)
|
||||||
|
list := make([]Queued_Sprite, 3)
|
||||||
|
defer delete(list)
|
||||||
|
list[0] = {texture = tex}
|
||||||
|
list[1] = {texture = tex}
|
||||||
|
list[2] = {texture = tex}
|
||||||
|
testing.expect_value(t, texture_run_len(list, 0), 3)
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
texture_run_len_breaks_on_change :: proc(t: ^testing.T) {
|
||||||
|
a := fake_tex(1)
|
||||||
|
b := fake_tex(2)
|
||||||
|
list := make([]Queued_Sprite, 3)
|
||||||
|
defer delete(list)
|
||||||
|
list[0] = {texture = a}
|
||||||
|
list[1] = {texture = a}
|
||||||
|
list[2] = {texture = b}
|
||||||
|
testing.expect_value(t, texture_run_len(list, 0), 2)
|
||||||
|
testing.expect_value(t, texture_run_len(list, 2), 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
texture_run_len_all_different :: proc(t: ^testing.T) {
|
||||||
|
list := make([]Queued_Sprite, 3)
|
||||||
|
defer delete(list)
|
||||||
|
list[0] = {texture = fake_tex(1)}
|
||||||
|
list[1] = {texture = fake_tex(2)}
|
||||||
|
list[2] = {texture = fake_tex(3)}
|
||||||
|
testing.expect_value(t, texture_run_len(list, 0), 1)
|
||||||
|
testing.expect_value(t, texture_run_len(list, 1), 1)
|
||||||
|
testing.expect_value(t, texture_run_len(list, 2), 1)
|
||||||
|
}
|
||||||
@@ -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],
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package engine
|
||||||
|
|
||||||
|
import "core:testing"
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
camera_default_values :: proc(t: ^testing.T) {
|
||||||
|
cam := camera_default()
|
||||||
|
testing.expect_value(t, cam.position, Vec2{0, 0})
|
||||||
|
testing.expect_value(t, cam.anchor, Vec2{0.5, 0.5})
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
world_to_screen_centered_identity :: proc(t: ^testing.T) {
|
||||||
|
cam := Camera {
|
||||||
|
position = {400, 500},
|
||||||
|
anchor = {0.5, 0.5},
|
||||||
|
}
|
||||||
|
viewport := Vec2{800, 600}
|
||||||
|
screen := world_to_screen(cam, cam.position, viewport)
|
||||||
|
testing.expect_value(t, screen.x, f32(400))
|
||||||
|
testing.expect_value(t, screen.y, f32(300))
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
world_to_screen_origin_cam :: proc(t: ^testing.T) {
|
||||||
|
cam := Camera {
|
||||||
|
position = {0, 0},
|
||||||
|
anchor = {0.5, 0.5},
|
||||||
|
}
|
||||||
|
screen := world_to_screen(cam, {10, 20}, {200, 100})
|
||||||
|
testing.expect_value(t, screen.x, f32(110))
|
||||||
|
testing.expect_value(t, screen.y, f32(70))
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
world_to_screen_top_left_anchor :: proc(t: ^testing.T) {
|
||||||
|
cam := Camera {
|
||||||
|
position = {5, 7},
|
||||||
|
anchor = {0, 0},
|
||||||
|
}
|
||||||
|
screen := world_to_screen(cam, {15, 27}, {800, 600})
|
||||||
|
testing.expect_value(t, screen.x, f32(10))
|
||||||
|
testing.expect_value(t, screen.y, f32(20))
|
||||||
|
}
|
||||||
@@ -170,7 +170,10 @@ load_character_data :: proc(app: ^App, character_json_path: string) -> (Characte
|
|||||||
sdl.EndGPUCopyPass(copy_pass)
|
sdl.EndGPUCopyPass(copy_pass)
|
||||||
fence := sdl.SubmitGPUCommandBufferAndAcquireFence(cmd)
|
fence := sdl.SubmitGPUCommandBufferAndAcquireFence(cmd)
|
||||||
if fence == nil {
|
if fence == nil {
|
||||||
fmt.eprintfln("SubmitGPUCommandBufferAndAcquireFence (texture upload) failed: %s", sdl.GetError())
|
fmt.eprintfln(
|
||||||
|
"SubmitGPUCommandBufferAndAcquireFence (texture upload) failed: %s",
|
||||||
|
sdl.GetError(),
|
||||||
|
)
|
||||||
sdl.ReleaseGPUTransferBuffer(app.device, tbuf)
|
sdl.ReleaseGPUTransferBuffer(app.device, tbuf)
|
||||||
sdl.ReleaseGPUTexture(app.device, out.texture)
|
sdl.ReleaseGPUTexture(app.device, out.texture)
|
||||||
return {}, false
|
return {}, false
|
||||||
@@ -200,6 +203,29 @@ destroy_character_data :: proc(app: ^App, data: ^Character_Data) {
|
|||||||
data^ = {}
|
data^ = {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
character_clip :: proc(data: ^Character_Data, clip_name: string) -> (clip: Clip_Def, ok: bool) {
|
||||||
|
if data == nil do return {}, false
|
||||||
|
character, found := data.def.clips[clip_name]
|
||||||
|
if !found || len(character.frames) == 0 do return {}, false
|
||||||
|
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,
|
||||||
@@ -208,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
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ parse_char_def_happy_path :: proc(t: ^testing.T) {
|
|||||||
def, ok := parse_char_def(src)
|
def, ok := parse_char_def(src)
|
||||||
defer destroy_char_def(&def)
|
defer destroy_char_def(&def)
|
||||||
|
|
||||||
testing.expect(t, ok)
|
testing.expect(t, ok, "parse_char_def should succeed on valid JSON")
|
||||||
testing.expect_value(t, def.id, "toad")
|
testing.expect_value(t, def.id, "toad")
|
||||||
testing.expect_value(t, def.atlas, "toad.atlas.png")
|
testing.expect_value(t, def.atlas, "toad.atlas.png")
|
||||||
testing.expect_value(t, def.version, 1)
|
testing.expect_value(t, def.version, 1)
|
||||||
@@ -20,8 +20,8 @@ parse_char_def_happy_path :: proc(t: ^testing.T) {
|
|||||||
testing.expect_value(t, def.pivot[1], f32(1.0))
|
testing.expect_value(t, def.pivot[1], f32(1.0))
|
||||||
|
|
||||||
idle, found := def.clips["idle"]
|
idle, found := def.clips["idle"]
|
||||||
testing.expect(t, found)
|
testing.expect(t, found, "expected idle clip in parsed def")
|
||||||
testing.expect(t, idle.loop)
|
testing.expect(t, idle.loop, "idle clip should loop")
|
||||||
testing.expect_value(t, idle.fps, f32(8.0))
|
testing.expect_value(t, idle.fps, f32(8.0))
|
||||||
testing.expect_value(t, len(idle.frames), 1)
|
testing.expect_value(t, len(idle.frames), 1)
|
||||||
testing.expect_value(t, idle.frames[0].rect, [4]int{1, 2, 3, 4})
|
testing.expect_value(t, idle.frames[0].rect, [4]int{1, 2, 3, 4})
|
||||||
@@ -30,7 +30,7 @@ parse_char_def_happy_path :: proc(t: ^testing.T) {
|
|||||||
@(test)
|
@(test)
|
||||||
parse_char_def_bad_json :: proc(t: ^testing.T) {
|
parse_char_def_bad_json :: proc(t: ^testing.T) {
|
||||||
_, ok := parse_char_def(transmute([]u8)string("{ not json"))
|
_, ok := parse_char_def(transmute([]u8)string("{ not json"))
|
||||||
testing.expect(t, !ok)
|
testing.expect(t, !ok, "parse_char_def should fail on invalid JSON")
|
||||||
}
|
}
|
||||||
|
|
||||||
@(test)
|
@(test)
|
||||||
@@ -51,17 +51,66 @@ character_frame_rect_hit_and_miss :: proc(t: ^testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
rect, ok := character_frame_rect(&data, "idle", 1)
|
rect, ok := character_frame_rect(&data, "idle", 1)
|
||||||
testing.expect(t, ok)
|
testing.expect(t, ok, "character_frame_rect should find idle frame 1")
|
||||||
testing.expect_value(t, rect, [4]int{50, 60, 70, 80})
|
testing.expect_value(t, rect, [4]int{50, 60, 70, 80})
|
||||||
|
|
||||||
_, ok = character_frame_rect(&data, "missing", 0)
|
_, ok = character_frame_rect(&data, "missing", 0)
|
||||||
testing.expect(t, !ok)
|
testing.expect(t, !ok, "character_frame_rect should miss unknown clip")
|
||||||
|
|
||||||
_, ok = character_frame_rect(&data, "idle", -1)
|
_, ok = character_frame_rect(&data, "idle", -1)
|
||||||
testing.expect(t, !ok)
|
testing.expect(t, !ok, "character_frame_rect should reject negative index")
|
||||||
|
|
||||||
_, ok = character_frame_rect(&data, "idle", 2)
|
_, ok = character_frame_rect(&data, "idle", 2)
|
||||||
testing.expect(t, !ok)
|
testing.expect(t, !ok, "character_frame_rect should reject out-of-range index")
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
character_frame_returns_full_def :: proc(t: ^testing.T) {
|
||||||
|
data: Character_Data
|
||||||
|
data.def.clips = make(map[string]Clip_Def)
|
||||||
|
defer delete(data.def.clips)
|
||||||
|
|
||||||
|
frames := make([]Frame_Def, 1)
|
||||||
|
frames[0] = Frame_Def {
|
||||||
|
rect = {10, 20, 30, 40},
|
||||||
|
source_size = {100, 200},
|
||||||
|
trim_offset = {5, 7},
|
||||||
|
}
|
||||||
|
defer delete(frames)
|
||||||
|
|
||||||
|
data.def.clips["idle"] = Clip_Def {
|
||||||
|
loop = true,
|
||||||
|
fps = 10,
|
||||||
|
frames = frames,
|
||||||
|
}
|
||||||
|
|
||||||
|
frame, ok := character_frame(&data, "idle", 0)
|
||||||
|
testing.expect(t, ok, "character_frame should find idle frame 0")
|
||||||
|
testing.expect_value(t, frame.rect, [4]int{10, 20, 30, 40})
|
||||||
|
testing.expect_value(t, frame.source_size, [2]int{100, 200})
|
||||||
|
testing.expect_value(t, frame.trim_offset, [2]int{5, 7})
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
character_frame_nil_and_bad_index :: proc(t: ^testing.T) {
|
||||||
|
_, ok := character_frame(nil, "idle", 0)
|
||||||
|
testing.expect(t, !ok, "character_frame should fail on nil data")
|
||||||
|
|
||||||
|
data: Character_Data
|
||||||
|
data.def.clips = make(map[string]Clip_Def)
|
||||||
|
defer delete(data.def.clips)
|
||||||
|
|
||||||
|
frames := make([]Frame_Def, 1)
|
||||||
|
frames[0] = {rect = {1, 2, 3, 4}}
|
||||||
|
defer delete(frames)
|
||||||
|
data.def.clips["idle"] = Clip_Def{frames = frames}
|
||||||
|
|
||||||
|
_, ok = character_frame(&data, "idle", -1)
|
||||||
|
testing.expect(t, !ok, "character_frame should reject negative index")
|
||||||
|
_, ok = character_frame(&data, "idle", 1)
|
||||||
|
testing.expect(t, !ok, "character_frame should reject out-of-range index")
|
||||||
|
_, ok = character_frame(&data, "missing", 0)
|
||||||
|
testing.expect(t, !ok, "character_frame should miss unknown clip")
|
||||||
}
|
}
|
||||||
|
|
||||||
destroy_char_def :: proc(def: ^Char_Def) {
|
destroy_char_def :: proc(def: ^Char_Def) {
|
||||||
|
|||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package engine
|
||||||
|
|
||||||
|
import sdl "vendor:sdl3"
|
||||||
|
|
||||||
|
Key :: enum {
|
||||||
|
A,
|
||||||
|
D,
|
||||||
|
Left,
|
||||||
|
Right,
|
||||||
|
Up,
|
||||||
|
Down,
|
||||||
|
Space,
|
||||||
|
N1,
|
||||||
|
N2,
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
case .Up:
|
||||||
|
scan_code = .UP
|
||||||
|
case .Down:
|
||||||
|
scan_code = .DOWN
|
||||||
|
case .Space:
|
||||||
|
scan_code = .SPACE
|
||||||
|
case .N1:
|
||||||
|
scan_code = ._1
|
||||||
|
case .N2:
|
||||||
|
scan_code = ._2
|
||||||
|
}
|
||||||
|
return keys[scan_code]
|
||||||
|
}
|
||||||
@@ -21,7 +21,7 @@ shader_filenames_per_backend :: proc(t: ^testing.T) {
|
|||||||
@(test)
|
@(test)
|
||||||
choose_shader_runtime_prefers_msl :: proc(t: ^testing.T) {
|
choose_shader_runtime_prefers_msl :: proc(t: ^testing.T) {
|
||||||
rt, ok := choose_shader_runtime_from_formats({.MSL, .SPIRV, .DXIL})
|
rt, ok := choose_shader_runtime_from_formats({.MSL, .SPIRV, .DXIL})
|
||||||
testing.expect(t, ok)
|
testing.expect(t, ok, "should pick a runtime when MSL is available")
|
||||||
testing.expect_value(t, rt.backend, Shader_Backend.Metal_MSL)
|
testing.expect_value(t, rt.backend, Shader_Backend.Metal_MSL)
|
||||||
testing.expect_value(t, rt.shader_dir, "shaders/metal")
|
testing.expect_value(t, rt.shader_dir, "shaders/metal")
|
||||||
testing.expect_value(t, rt.format, sdl.GPUShaderFormat{.MSL})
|
testing.expect_value(t, rt.format, sdl.GPUShaderFormat{.MSL})
|
||||||
@@ -30,7 +30,7 @@ choose_shader_runtime_prefers_msl :: proc(t: ^testing.T) {
|
|||||||
@(test)
|
@(test)
|
||||||
choose_shader_runtime_spirv_only :: proc(t: ^testing.T) {
|
choose_shader_runtime_spirv_only :: proc(t: ^testing.T) {
|
||||||
rt, ok := choose_shader_runtime_from_formats({.SPIRV})
|
rt, ok := choose_shader_runtime_from_formats({.SPIRV})
|
||||||
testing.expect(t, ok)
|
testing.expect(t, ok, "should pick SPIR-V when it is the only format")
|
||||||
testing.expect_value(t, rt.backend, Shader_Backend.Vulkan_SPIRV)
|
testing.expect_value(t, rt.backend, Shader_Backend.Vulkan_SPIRV)
|
||||||
testing.expect_value(t, rt.shader_dir, "shaders/vulkan")
|
testing.expect_value(t, rt.shader_dir, "shaders/vulkan")
|
||||||
testing.expect_value(t, rt.format, sdl.GPUShaderFormat{.SPIRV})
|
testing.expect_value(t, rt.format, sdl.GPUShaderFormat{.SPIRV})
|
||||||
@@ -39,5 +39,5 @@ choose_shader_runtime_spirv_only :: proc(t: ^testing.T) {
|
|||||||
@(test)
|
@(test)
|
||||||
choose_shader_runtime_empty_fails :: proc(t: ^testing.T) {
|
choose_shader_runtime_empty_fails :: proc(t: ^testing.T) {
|
||||||
_, ok := choose_shader_runtime_from_formats({})
|
_, ok := choose_shader_runtime_from_formats({})
|
||||||
testing.expect(t, !ok)
|
testing.expect(t, !ok, "empty format set should fail")
|
||||||
}
|
}
|
||||||
|
|||||||
+144
-73
@@ -1,6 +1,5 @@
|
|||||||
package engine
|
package engine
|
||||||
|
|
||||||
import "core:mem"
|
|
||||||
import sdl "vendor:sdl3"
|
import sdl "vendor:sdl3"
|
||||||
|
|
||||||
Vec2 :: [2]f32
|
Vec2 :: [2]f32
|
||||||
@@ -10,6 +9,8 @@ Sprite :: struct {
|
|||||||
position: Vec2,
|
position: Vec2,
|
||||||
clip: string,
|
clip: string,
|
||||||
frame: int,
|
frame: int,
|
||||||
|
time: f32,
|
||||||
|
flip_x: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
spawn_sprite :: proc(
|
spawn_sprite :: proc(
|
||||||
@@ -18,13 +19,60 @@ spawn_sprite :: proc(
|
|||||||
clip: string = "idle",
|
clip: string = "idle",
|
||||||
frame: int = 0,
|
frame: int = 0,
|
||||||
) -> Sprite {
|
) -> Sprite {
|
||||||
return Sprite{data = data, position = position, clip = clip, frame = frame}
|
sprite := Sprite {
|
||||||
|
data = data,
|
||||||
|
position = position,
|
||||||
|
}
|
||||||
|
set_sprite_clip(&sprite, clip)
|
||||||
|
if frame != 0 {
|
||||||
|
c, ok := character_clip(sprite.data, sprite.clip)
|
||||||
|
if ok {
|
||||||
|
if frame < 0 {
|
||||||
|
sprite.frame = 0
|
||||||
|
} else if frame >= len(c.frames) {
|
||||||
|
sprite.frame = len(c.frames) - 1
|
||||||
|
} else {
|
||||||
|
sprite.frame = frame
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sprite
|
||||||
}
|
}
|
||||||
|
|
||||||
// stubbed for later
|
|
||||||
update_sprite :: proc(sprite: ^Sprite, dt: f32) {
|
update_sprite :: proc(sprite: ^Sprite, dt: f32) {
|
||||||
_ = sprite
|
if sprite == nil || sprite.data == nil do return
|
||||||
_ = dt
|
if dt <= 0 do return
|
||||||
|
|
||||||
|
clip, ok := character_clip(sprite.data, sprite.clip)
|
||||||
|
if !ok do return
|
||||||
|
|
||||||
|
frame_count := len(clip.frames)
|
||||||
|
if frame_count <= 0 do return
|
||||||
|
|
||||||
|
if sprite.frame < 0 do sprite.frame = 0
|
||||||
|
if sprite.frame >= frame_count do sprite.frame = frame_count - 1
|
||||||
|
|
||||||
|
if clip.fps <= 0 do return
|
||||||
|
|
||||||
|
sprite.time += dt
|
||||||
|
frame_duration := 1.0 / clip.fps
|
||||||
|
|
||||||
|
for sprite.time >= frame_duration {
|
||||||
|
sprite.time -= frame_duration
|
||||||
|
next := sprite.frame + 1
|
||||||
|
|
||||||
|
if next >= frame_count {
|
||||||
|
if clip.loop {
|
||||||
|
sprite.frame = 0
|
||||||
|
} else {
|
||||||
|
sprite.frame = frame_count - 1
|
||||||
|
sprite.time = 0
|
||||||
|
break
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
sprite.frame = next
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
to_clip :: proc(px, py, sw, sh: f32) -> [2]f32 {
|
to_clip :: proc(px, py, sw, sh: f32) -> [2]f32 {
|
||||||
@@ -35,43 +83,57 @@ 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.render_pass == nil || 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
|
||||||
}
|
}
|
||||||
|
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 {
|
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])
|
||||||
|
|
||||||
// Feet-centered placement, same as renderer version
|
fw := f32(frame.rect[2])
|
||||||
x0_px := sprite.position.x - fw * 0.5
|
fh := f32(frame.rect[3])
|
||||||
y0_px := sprite.position.y - fh
|
trim_x := f32(frame.trim_offset[0])
|
||||||
x1_px := x0_px + fw
|
trim_y := f32(frame.trim_offset[1])
|
||||||
y1_px := y0_px + fh
|
pivot := sprite.data.def.pivot
|
||||||
|
|
||||||
|
viewport := Vec2{f32(app.swapchain_w), f32(app.swapchain_h)}
|
||||||
|
feet := world_to_screen(app.camera, sprite.position, viewport)
|
||||||
|
|
||||||
|
x0_px, y0_px, x1_px, y1_px := sprite_feet_quad(
|
||||||
|
feet,
|
||||||
|
src_w,
|
||||||
|
src_h,
|
||||||
|
{trim_x, trim_y},
|
||||||
|
{fw, fh},
|
||||||
|
pivot,
|
||||||
|
sprite.flip_x,
|
||||||
|
)
|
||||||
|
|
||||||
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, v0, u1, v1 := frame_uvs(frame.rect, tex_w, tex_h, sprite.flip_x)
|
||||||
v0 := f32(rect[1]) / tex_h
|
|
||||||
u1 := f32(rect[0] + rect[2]) / tex_w
|
|
||||||
v1 := f32(rect[1] + rect[3]) / tex_h
|
|
||||||
|
|
||||||
// Two triangles: (0,1,2) and (0,2,3)
|
verts := [SPRITE_VERT_COUNT]Vertex {
|
||||||
verts := [6]Vertex {
|
|
||||||
{pos = p0, uv = {u0, v0}},
|
{pos = p0, uv = {u0, v0}},
|
||||||
{pos = p1, uv = {u1, v0}},
|
{pos = p1, uv = {u1, v0}},
|
||||||
{pos = p2, uv = {u1, v1}},
|
{pos = p2, uv = {u1, v1}},
|
||||||
@@ -80,52 +142,61 @@ draw_sprite :: proc(app: ^App, sprite: ^Sprite) {
|
|||||||
{pos = p3, uv = {u0, v1}},
|
{pos = p3, uv = {u0, v1}},
|
||||||
}
|
}
|
||||||
|
|
||||||
map_ptr := sdl.MapGPUTransferBuffer(app.device, app.transfer_buffer, false)
|
append(&app.draw_list, Queued_Sprite{texture = sprite.data.texture, verts = verts})
|
||||||
if map_ptr == nil {
|
}
|
||||||
return
|
|
||||||
}
|
set_sprite_clip :: proc(sprite: ^Sprite, clip: string) {
|
||||||
mem.copy(map_ptr, raw_data(verts[:]), size_of(verts))
|
if sprite == nil || sprite.data == nil do return
|
||||||
sdl.UnmapGPUTransferBuffer(app.device, app.transfer_buffer)
|
|
||||||
|
if sprite.clip == clip do return
|
||||||
// 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.
|
_, ok := character_clip(sprite.data, clip)
|
||||||
copy_cmd := sdl.AcquireGPUCommandBuffer(app.device)
|
if !ok do return
|
||||||
if copy_cmd == nil {
|
|
||||||
return
|
sprite.clip = clip
|
||||||
}
|
sprite.frame = 0
|
||||||
copy_pass := sdl.BeginGPUCopyPass(copy_cmd)
|
sprite.time = 0
|
||||||
|
}
|
||||||
src := sdl.GPUTransferBufferLocation {
|
|
||||||
transfer_buffer = app.transfer_buffer,
|
sprite_quad_origin :: proc(position: Vec2, size: Vec2, pivot: [2]f32) -> Vec2 {
|
||||||
offset = 0,
|
return {position.x - size.x * pivot[0], position.y - size.y * pivot[1]}
|
||||||
}
|
}
|
||||||
dst := sdl.GPUBufferRegion {
|
|
||||||
buffer = app.vertex_buffer,
|
frame_uvs :: proc(rect: [4]int, tex_w, tex_h: f32, flip_x: bool) -> (u0, v0, u1, v1: f32) {
|
||||||
offset = 0,
|
u0 = f32(rect[0]) / tex_w
|
||||||
size = u32(size_of(verts)),
|
v0 = f32(rect[1]) / tex_h
|
||||||
}
|
u1 = f32(rect[0] + rect[2]) / tex_w
|
||||||
sdl.UploadToGPUBuffer(copy_pass, src, dst, false)
|
v1 = f32(rect[1] + rect[3]) / tex_h
|
||||||
sdl.EndGPUCopyPass(copy_pass)
|
if flip_x do u0, u1 = u1, u0
|
||||||
fence := sdl.SubmitGPUCommandBufferAndAcquireFence(copy_cmd)
|
return
|
||||||
if fence == nil {
|
}
|
||||||
return
|
|
||||||
}
|
sprite_feet_quad :: proc(
|
||||||
defer sdl.ReleaseGPUFence(app.device, fence)
|
feet: Vec2,
|
||||||
if !sdl.WaitForGPUFences(app.device, true, &fence, 1) {
|
src_w, src_h: f32,
|
||||||
return
|
trim: Vec2,
|
||||||
}
|
size: Vec2,
|
||||||
|
pivot: [2]f32,
|
||||||
sampler_binding := sdl.GPUTextureSamplerBinding {
|
flip_x: bool,
|
||||||
texture = sprite.data.texture,
|
) -> (
|
||||||
sampler = app.sampler,
|
x0, y0, x1, y1: f32,
|
||||||
}
|
) {
|
||||||
sdl.BindGPUFragmentSamplers(app.render_pass, 0, &sampler_binding, 1)
|
canvas_top := feet.y - src_h * pivot[1]
|
||||||
|
if flip_x {
|
||||||
vb_binding := sdl.GPUBufferBinding {
|
canvas_left := feet.x - (1.0 - pivot[0]) * src_w
|
||||||
buffer = app.vertex_buffer,
|
x0 = canvas_left + (src_w - trim.x - size.x)
|
||||||
offset = 0,
|
y0 = canvas_top + trim.y
|
||||||
}
|
} else {
|
||||||
sdl.BindGPUVertexBuffers(app.render_pass, 0, &vb_binding, 1)
|
canvas_left := feet.x - src_w * pivot[0]
|
||||||
|
x0 = canvas_left + trim.x
|
||||||
sdl.DrawGPUPrimitives(app.render_pass, 6, 1, 0, 0)
|
y0 = canvas_top + trim.y
|
||||||
|
}
|
||||||
|
x1 = x0 + size.x
|
||||||
|
y1 = y0 + size.y
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
set_sprite_flip_x :: proc(sprite: ^Sprite, flip: bool) {
|
||||||
|
if sprite == nil do return
|
||||||
|
sprite.flip_x = flip
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,3 +19,73 @@ to_clip_corners :: proc(t: ^testing.T) {
|
|||||||
testing.expect_value(t, p[0], f32(0))
|
testing.expect_value(t, p[0], f32(0))
|
||||||
testing.expect_value(t, p[1], f32(0))
|
testing.expect_value(t, p[1], f32(0))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
sprite_quad_origin_feet :: proc(t: ^testing.T) {
|
||||||
|
o := sprite_quad_origin({400, 500}, {100, 200}, {0.5, 1.0})
|
||||||
|
testing.expect_value(t, o.x, f32(350))
|
||||||
|
testing.expect_value(t, o.y, f32(300))
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
sprite_quad_origin_center :: proc(t: ^testing.T) {
|
||||||
|
o := sprite_quad_origin({400, 500}, {100, 200}, {0.5, 0.5})
|
||||||
|
testing.expect_value(t, o.x, f32(350))
|
||||||
|
testing.expect_value(t, o.y, f32(400))
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
frame_uvs_unflipped :: proc(t: ^testing.T) {
|
||||||
|
u0, v0, u1, v1 := frame_uvs({10, 20, 30, 40}, 100, 200, false)
|
||||||
|
testing.expect_value(t, u0, f32(0.1))
|
||||||
|
testing.expect_value(t, v0, f32(0.1))
|
||||||
|
testing.expect_value(t, u1, f32(0.4))
|
||||||
|
testing.expect_value(t, v1, f32(0.3))
|
||||||
|
testing.expect(t, u0 < u1, "unflipped UVs should have u0 < u1")
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
frame_uvs_flipped :: proc(t: ^testing.T) {
|
||||||
|
a0, _, a1, _ := frame_uvs({10, 20, 30, 40}, 100, 200, false)
|
||||||
|
b0, v0, b1, v1 := frame_uvs({10, 20, 30, 40}, 100, 200, true)
|
||||||
|
testing.expect_value(t, b0, a1)
|
||||||
|
testing.expect_value(t, b1, a0)
|
||||||
|
testing.expect_value(t, v0, f32(0.1))
|
||||||
|
testing.expect_value(t, v1, f32(0.3))
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
sprite_feet_quad_no_flip :: proc(t: ^testing.T) {
|
||||||
|
// feet (400,500), source 100x200, pivot feet, trim (10,20), packed 80x160
|
||||||
|
x0, y0, x1, y1 := sprite_feet_quad(
|
||||||
|
{400, 500},
|
||||||
|
100,
|
||||||
|
200,
|
||||||
|
{10, 20},
|
||||||
|
{80, 160},
|
||||||
|
{0.5, 1.0},
|
||||||
|
false,
|
||||||
|
)
|
||||||
|
testing.expect_value(t, x0, f32(360)) // 400 - 50 + 10
|
||||||
|
testing.expect_value(t, y0, f32(320)) // 500 - 200 + 20
|
||||||
|
testing.expect_value(t, x1, f32(440))
|
||||||
|
testing.expect_value(t, y1, f32(480))
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
sprite_feet_quad_flip_x :: proc(t: ^testing.T) {
|
||||||
|
x0, y0, x1, y1 := sprite_feet_quad(
|
||||||
|
{400, 500},
|
||||||
|
100,
|
||||||
|
200,
|
||||||
|
{5, 20},
|
||||||
|
{80, 160},
|
||||||
|
{0.5, 1.0},
|
||||||
|
true,
|
||||||
|
)
|
||||||
|
// canvas_left = 350; trim_x_draw = 100 - 5 - 80 = 15 → x0 = 365
|
||||||
|
testing.expect_value(t, x0, f32(365))
|
||||||
|
testing.expect_value(t, y0, f32(320))
|
||||||
|
testing.expect_value(t, x1, f32(445))
|
||||||
|
testing.expect_value(t, y1, f32(480))
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package engine
|
||||||
|
|
||||||
|
import sdl "vendor:sdl3"
|
||||||
|
|
||||||
|
// now_seconds returns seconds since SDL init (monotonic for frame timing).
|
||||||
|
now_seconds :: proc() -> f64 {
|
||||||
|
return f64(sdl.GetTicksNS()) / 1_000_000_000.0
|
||||||
|
}
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import eng "pkg:engine"
|
||||||
|
|
||||||
|
// Camera sandbox: toad stays put; arrows pan the camera. Space toggles follow mode.
|
||||||
|
main :: proc() {
|
||||||
|
app: eng.App
|
||||||
|
if !eng.init(&app, "camera sandbox", 800, 600) do return
|
||||||
|
defer eng.shutdown(&app)
|
||||||
|
|
||||||
|
data, ok := eng.load_character_data(&app, "assets_baked/characters/toad/toad.char.json")
|
||||||
|
if !ok do return
|
||||||
|
defer eng.destroy_character_data(&app, &data)
|
||||||
|
|
||||||
|
toad := eng.spawn_sprite(&data, {0, 0}, "idle", 0)
|
||||||
|
app.camera.position = {0, -100}
|
||||||
|
|
||||||
|
follow := false
|
||||||
|
space_was_down := false
|
||||||
|
CAM_SPEED :: f32(300)
|
||||||
|
|
||||||
|
last := eng.now_seconds()
|
||||||
|
|
||||||
|
for eng.events() {
|
||||||
|
now := eng.now_seconds()
|
||||||
|
dt := f32(now - last)
|
||||||
|
last = now
|
||||||
|
|
||||||
|
space := eng.key_down(.Space)
|
||||||
|
if space && !space_was_down {
|
||||||
|
follow = !follow
|
||||||
|
}
|
||||||
|
space_was_down = space
|
||||||
|
|
||||||
|
if follow {
|
||||||
|
app.camera.position = toad.position - {0, 100}
|
||||||
|
} else {
|
||||||
|
if eng.key_down(.Left) || eng.key_down(.A) {
|
||||||
|
app.camera.position.x -= CAM_SPEED * dt
|
||||||
|
}
|
||||||
|
if eng.key_down(.Right) || eng.key_down(.D) {
|
||||||
|
app.camera.position.x += CAM_SPEED * dt
|
||||||
|
}
|
||||||
|
if eng.key_down(.Up) {
|
||||||
|
app.camera.position.y -= CAM_SPEED * dt
|
||||||
|
}
|
||||||
|
if eng.key_down(.Down) {
|
||||||
|
app.camera.position.y += CAM_SPEED * dt
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
eng.update_sprite(&toad, dt)
|
||||||
|
|
||||||
|
eng.begin_frame(&app)
|
||||||
|
eng.draw_sprite(&app, &toad)
|
||||||
|
eng.end_frame(&app)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import eng "pkg:engine"
|
||||||
|
|
||||||
|
// Clip switcher: 1 = idle, 2 = walk. No movement — animation policy only.
|
||||||
|
main :: proc() {
|
||||||
|
app: eng.App
|
||||||
|
if !eng.init(&app, "clips", 800, 600) do return
|
||||||
|
defer eng.shutdown(&app)
|
||||||
|
|
||||||
|
data, ok := eng.load_character_data(&app, "assets_baked/characters/toad/toad.char.json")
|
||||||
|
if !ok do return
|
||||||
|
defer eng.destroy_character_data(&app, &data)
|
||||||
|
|
||||||
|
toad := eng.spawn_sprite(&data, {400, 500}, "idle", 0)
|
||||||
|
app.camera.position = toad.position - {0, 100}
|
||||||
|
|
||||||
|
last := eng.now_seconds()
|
||||||
|
|
||||||
|
for eng.events() {
|
||||||
|
now := eng.now_seconds()
|
||||||
|
dt := f32(now - last)
|
||||||
|
last = now
|
||||||
|
|
||||||
|
if eng.key_down(.N1) {
|
||||||
|
eng.set_sprite_clip(&toad, "idle")
|
||||||
|
}
|
||||||
|
if eng.key_down(.N2) {
|
||||||
|
eng.set_sprite_clip(&toad, "walk")
|
||||||
|
}
|
||||||
|
|
||||||
|
eng.update_sprite(&toad, dt)
|
||||||
|
|
||||||
|
eng.begin_frame(&app)
|
||||||
|
eng.draw_sprite(&app, &toad)
|
||||||
|
eng.end_frame(&app)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import eng "pkg:engine"
|
||||||
|
|
||||||
|
// Many sprites sharing one Character_Data (Flyweight) — good batching demo.
|
||||||
|
COUNT :: 24
|
||||||
|
|
||||||
|
main :: proc() {
|
||||||
|
app: eng.App
|
||||||
|
if !eng.init(&app, "crowd", 800, 600) do return
|
||||||
|
defer eng.shutdown(&app)
|
||||||
|
|
||||||
|
data, ok := eng.load_character_data(&app, "assets_baked/characters/toad/toad.char.json")
|
||||||
|
if !ok do return
|
||||||
|
defer eng.destroy_character_data(&app, &data)
|
||||||
|
|
||||||
|
sprites: [COUNT]eng.Sprite
|
||||||
|
for i in 0 ..< COUNT {
|
||||||
|
col := i % 8
|
||||||
|
row := i / 8
|
||||||
|
pos := eng.Vec2 {
|
||||||
|
f32(120 + col * 80),
|
||||||
|
f32(280 + row * 120),
|
||||||
|
}
|
||||||
|
clip := "idle" if (i % 2) == 0 else "walk"
|
||||||
|
sprites[i] = eng.spawn_sprite(&data, pos, clip, i % 5)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Look at the middle of the grid
|
||||||
|
app.camera.position = {400, 400}
|
||||||
|
|
||||||
|
last := eng.now_seconds()
|
||||||
|
|
||||||
|
for eng.events() {
|
||||||
|
now := eng.now_seconds()
|
||||||
|
dt := f32(now - last)
|
||||||
|
last = now
|
||||||
|
|
||||||
|
for &s in sprites {
|
||||||
|
eng.update_sprite(&s, dt)
|
||||||
|
}
|
||||||
|
|
||||||
|
eng.begin_frame(&app)
|
||||||
|
for &s in sprites {
|
||||||
|
eng.draw_sprite(&app, &s)
|
||||||
|
}
|
||||||
|
eng.end_frame(&app)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import eng "pkg:engine"
|
||||||
|
|
||||||
|
// Minimal: load one baked character and draw idle. No input, no camera follow.
|
||||||
|
main :: proc() {
|
||||||
|
app: eng.App
|
||||||
|
if !eng.init(&app, "hello sprite", 800, 600) do return
|
||||||
|
defer eng.shutdown(&app)
|
||||||
|
|
||||||
|
data, ok := eng.load_character_data(&app, "assets_baked/characters/toad/toad.char.json")
|
||||||
|
if !ok do return
|
||||||
|
defer eng.destroy_character_data(&app, &data)
|
||||||
|
|
||||||
|
sprite := eng.spawn_sprite(&data, {400, 500}, "idle", 0)
|
||||||
|
|
||||||
|
// Screen-centered framing without gameplay camera follow:
|
||||||
|
// treat spawn position as world; look slightly above feet.
|
||||||
|
app.camera.position = sprite.position - {0, 100}
|
||||||
|
|
||||||
|
last := eng.now_seconds()
|
||||||
|
|
||||||
|
for eng.events() {
|
||||||
|
now := eng.now_seconds()
|
||||||
|
dt := f32(now - last)
|
||||||
|
last = now
|
||||||
|
|
||||||
|
eng.update_sprite(&sprite, dt)
|
||||||
|
|
||||||
|
eng.begin_frame(&app)
|
||||||
|
eng.draw_sprite(&app, &sprite)
|
||||||
|
eng.end_frame(&app)
|
||||||
|
}
|
||||||
|
}
|
||||||
+29
-2
@@ -4,7 +4,7 @@ import eng "pkg:engine"
|
|||||||
|
|
||||||
main :: proc() {
|
main :: proc() {
|
||||||
app: eng.App
|
app: eng.App
|
||||||
if !eng.init(&app, "toad", 800, 600) do return
|
if !eng.init(&app, "toad game", 800, 600) do return
|
||||||
defer eng.shutdown(&app)
|
defer eng.shutdown(&app)
|
||||||
|
|
||||||
data, ok := eng.load_character_data(&app, "assets_baked/characters/toad/toad.char.json")
|
data, ok := eng.load_character_data(&app, "assets_baked/characters/toad/toad.char.json")
|
||||||
@@ -14,10 +14,37 @@ main :: proc() {
|
|||||||
|
|
||||||
toad := eng.spawn_sprite(&data, {400, 500}, "idle", 0)
|
toad := eng.spawn_sprite(&data, {400, 500}, "idle", 0)
|
||||||
|
|
||||||
|
last := eng.now_seconds()
|
||||||
|
SPEED :: f32(200)
|
||||||
|
|
||||||
for eng.events() {
|
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)
|
eng.begin_frame(&app)
|
||||||
eng.draw_sprite(&app, &toad)
|
eng.draw_sprite(&app, &toad)
|
||||||
eng.end_frame(&app)
|
eng.end_frame(&app)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Executable
+31
@@ -0,0 +1,31 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
|
||||||
|
cd "$ROOT"
|
||||||
|
|
||||||
|
CONTENT_ROOT="${CONTENT_ROOT:-content/characters}"
|
||||||
|
OUT_ROOT="${OUT_ROOT:-assets_baked/characters}"
|
||||||
|
|
||||||
|
if [[ ! -d "$CONTENT_ROOT" ]]; then
|
||||||
|
echo "missing content root: $CONTENT_ROOT" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
shopt -s nullglob
|
||||||
|
manifests=("$CONTENT_ROOT"/*/manifest.json)
|
||||||
|
if [[ ${#manifests[@]} -eq 0 ]]; then
|
||||||
|
echo "no character manifests under $CONTENT_ROOT" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
for manifest in "${manifests[@]}"; do
|
||||||
|
char_dir="$(dirname "$manifest")"
|
||||||
|
name="$(basename "$char_dir")"
|
||||||
|
out_dir="$OUT_ROOT/$name"
|
||||||
|
mkdir -p "$out_dir"
|
||||||
|
echo "baking $name -> $out_dir"
|
||||||
|
odin run assetbake -- "$char_dir" "$out_dir"
|
||||||
|
done
|
||||||
|
|
||||||
|
echo "baked ${#manifests[@]} character(s)"
|
||||||
Reference in New Issue
Block a user