diff --git a/assetbake/main.odin b/assetbake/main.odin index f96d58a..f6b1b33 100644 --- a/assetbake/main.odin +++ b/assetbake/main.odin @@ -287,16 +287,14 @@ destroy_frames :: proc(frames: []^image.Image) { delete(frames) } -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) - +opaque_bounds_rgba :: proc(pixels: []u8, iw, ih: int, alpha_min: u8 = 1) -> (x, y, w, h: int) { 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 @@ -313,6 +311,12 @@ opaque_bounds :: proc(img: ^image.Image, alpha_min: u8 = 1) -> (x, y, w, h: int) 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 { PADDING :: 1 diff --git a/assetbake/opaque_bounds_test.odin b/assetbake/opaque_bounds_test.odin new file mode 100644 index 0000000..a5e22ba --- /dev/null +++ b/assetbake/opaque_bounds_test.odin @@ -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) +} diff --git a/engine/animation_test.odin b/engine/animation_test.odin index 5905890..40df1bc 100644 --- a/engine/animation_test.odin +++ b/engine/animation_test.odin @@ -74,13 +74,13 @@ character_clip_found_and_missing :: proc(t: ^testing.T) { defer destroy_test_character(&data) clip, ok := character_clip(&data, "idle") - testing.expect(t, ok) + testing.expect(t, ok, "character_clip should find idle") testing.expect_value(t, len(clip.frames), 3) - testing.expect(t, clip.loop) + 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) + testing.expect(t, !ok, "character_clip should miss unknown name") data.def.clips["empty"] = Clip_Def { loop = true, @@ -88,7 +88,7 @@ character_clip_found_and_missing :: proc(t: ^testing.T) { frames = nil, } _, ok = character_clip(&data, "empty") - testing.expect(t, !ok) + testing.expect(t, !ok, "character_clip should reject empty frames") } @(test) @@ -161,7 +161,7 @@ update_sprite_advances_multiple_frames :: proc(t: ^testing.T) { 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) + testing.expect(t, s.time > 0.049 && s.time < 0.051, "leftover time after multi-frame advance should be ~0.05") } @(test) diff --git a/engine/batching_test.odin b/engine/batching_test.odin new file mode 100644 index 0000000..3aaeabf --- /dev/null +++ b/engine/batching_test.odin @@ -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) +} diff --git a/engine/camera_test.odin b/engine/camera_test.odin new file mode 100644 index 0000000..3745ce8 --- /dev/null +++ b/engine/camera_test.odin @@ -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)) +} diff --git a/engine/character_data_test.odin b/engine/character_data_test.odin index 7e91e89..2661258 100644 --- a/engine/character_data_test.odin +++ b/engine/character_data_test.odin @@ -11,7 +11,7 @@ parse_char_def_happy_path :: proc(t: ^testing.T) { def, ok := parse_char_def(src) 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.atlas, "toad.atlas.png") 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)) idle, found := def.clips["idle"] - testing.expect(t, found) - testing.expect(t, idle.loop) + testing.expect(t, found, "expected idle clip in parsed def") + testing.expect(t, idle.loop, "idle clip should loop") testing.expect_value(t, idle.fps, f32(8.0)) testing.expect_value(t, len(idle.frames), 1) 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) parse_char_def_bad_json :: proc(t: ^testing.T) { _, 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) @@ -51,17 +51,66 @@ character_frame_rect_hit_and_miss :: proc(t: ^testing.T) { } 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}) _, 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) - testing.expect(t, !ok) + testing.expect(t, !ok, "character_frame_rect should reject negative index") _, 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) { diff --git a/engine/shader_backend_test.odin b/engine/shader_backend_test.odin index 69cb1fe..68ea4cf 100644 --- a/engine/shader_backend_test.odin +++ b/engine/shader_backend_test.odin @@ -21,7 +21,7 @@ shader_filenames_per_backend :: proc(t: ^testing.T) { @(test) choose_shader_runtime_prefers_msl :: proc(t: ^testing.T) { 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.shader_dir, "shaders/metal") testing.expect_value(t, rt.format, sdl.GPUShaderFormat{.MSL}) @@ -30,7 +30,7 @@ choose_shader_runtime_prefers_msl :: proc(t: ^testing.T) { @(test) choose_shader_runtime_spirv_only :: proc(t: ^testing.T) { 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.shader_dir, "shaders/vulkan") testing.expect_value(t, rt.format, sdl.GPUShaderFormat{.SPIRV}) @@ -39,5 +39,5 @@ choose_shader_runtime_spirv_only :: proc(t: ^testing.T) { @(test) choose_shader_runtime_empty_fails :: proc(t: ^testing.T) { _, ok := choose_shader_runtime_from_formats({}) - testing.expect(t, !ok) + testing.expect(t, !ok, "empty format set should fail") } diff --git a/engine/sprite.odin b/engine/sprite.odin index afa4dfa..12a9dba 100644 --- a/engine/sprite.odin +++ b/engine/sprite.odin @@ -112,19 +112,15 @@ draw_sprite :: proc(app: ^App, sprite: ^Sprite) { 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 + 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) sh := f32(app.swapchain_h) @@ -135,11 +131,7 @@ draw_sprite :: proc(app: ^App, sprite: ^Sprite) { tex_w := f32(sprite.data.width) tex_h := f32(sprite.data.height) - 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 + u0, v0, u1, v1 := frame_uvs(frame.rect, tex_w, tex_h, sprite.flip_x) verts := [SPRITE_VERT_COUNT]Vertex { {pos = p0, uv = {u0, v0}}, @@ -170,6 +162,40 @@ 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]} } +frame_uvs :: proc(rect: [4]int, tex_w, tex_h: f32, flip_x: bool) -> (u0, v0, u1, v1: f32) { + 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 + if flip_x do u0, u1 = u1, u0 + return +} + +sprite_feet_quad :: proc( + feet: Vec2, + src_w, src_h: f32, + trim: Vec2, + size: Vec2, + pivot: [2]f32, + flip_x: bool, +) -> ( + x0, y0, x1, y1: f32, +) { + canvas_top := feet.y - src_h * pivot[1] + if flip_x { + canvas_left := feet.x - (1.0 - pivot[0]) * src_w + x0 = canvas_left + (src_w - trim.x - size.x) + y0 = canvas_top + trim.y + } else { + canvas_left := feet.x - src_w * pivot[0] + x0 = canvas_left + trim.x + 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 diff --git a/engine/sprite_test.odin b/engine/sprite_test.odin index 4135a6a..fd00795 100644 --- a/engine/sprite_test.odin +++ b/engine/sprite_test.odin @@ -33,3 +33,59 @@ sprite_quad_origin_center :: proc(t: ^testing.T) { 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)) +}