Compare commits

...
Author SHA1 Message Date
codegirl007 5040446ec1 movement and sprite flipping 2026-08-01 12:01:17 -07:00
codegirl007 269578548a Determine pivot based on manifest 2026-08-01 11:48:55 -07:00
codegirl007 90da5934d7 determine pivot based on manifest 2026-08-01 10:37:36 -07:00
codegirl007 4aa3ed6ed8 Merge pull request #5 from Codegirl-Games/animation
Add sprite clip animation player + unit tests
2026-08-01 08:40:32 -07:00
codegirl007 fbae54aef0 animation + test 2026-08-01 08:27:44 -07:00
codegirl007 eea672023f Set up github to run tests 2026-08-01 01:13:12 -07:00
codegirl007 3a8c2ee830 change the way we install sdl 2026-08-01 01:10:37 -07:00
codegirl007 a1fa9863b6 Set up github to run tests 2026-08-01 01:06:05 -07:00
codegirl007 d5ef7f478e Add initial unit tests
- Pulled pure logic out of GPU-facing code so it can be tested without a window: parse_char_def (JSON → Char_Def) and choose_shader_runtime_from_formats (MSL → DXIL → SPIR-V preference)
- Added eight Odin @(test) cases in engine/ covering happy/bad JSON parse, frame-rect hit/miss/OOB, clip-space conversion corners, shader filename mapping, and backend selection
- Added make test to run odin test engine
2026-08-01 01:04:31 -07:00
codegirl007 22cca621f7 Add shader backend + support for multiple shader formats- #2
Adds a runtime GPU shader-backend selection layer so the engine is no longer hardcoded to #load SPIR-V only.

- Introduce Shader_Runtime / choose_shader_runtime (engine/shader_backend.odin)
- Create the GPU device with {.SPIRV, .DXIL, .MSL}, then pick a supported format + shaders/<backend>/ directory
- Load shader blobs from disk and build the sprite pipeline with the chosen format/entrypoint
- Add shader build scripts + Makefile targets (shaders-vulkan / d3d12 / metal / all)
2026-08-01 00:57:32 -07:00
14 changed files with 569 additions and 104 deletions
+55
View File
@@ -0,0 +1,55 @@
name: CI
on:
pull_request:
push:
branches: [master, main]
jobs:
test:
name: Engine tests
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- name: Checkout
uses: actions/checkout@v4
# Ubuntu 24.04 does not ship libsdl3-dev; build/install SDL3 from source (cached).
- name: Setup SDL3
id: sdl
uses: libsdl-org/setup-sdl@main
with:
version: 3-latest
install-linux-dependencies: true
add-to-environment: true
token: ${{ secrets.GITHUB_TOKEN }}
- name: Export SDL3 paths for the linker
run: |
PREFIX="${{ steps.sdl.outputs.prefix }}"
echo "PKG_CONFIG_PATH=${PREFIX}/lib/pkgconfig:${PKG_CONFIG_PATH:-}" >> "$GITHUB_ENV"
echo "LIBRARY_PATH=${PREFIX}/lib:${LIBRARY_PATH:-}" >> "$GITHUB_ENV"
echo "LD_LIBRARY_PATH=${PREFIX}/lib:${LD_LIBRARY_PATH:-}" >> "$GITHUB_ENV"
echo "C_INCLUDE_PATH=${PREFIX}/include:${C_INCLUDE_PATH:-}" >> "$GITHUB_ENV"
# Help some linkers find the shared lib without rpath surprises
if [ -d "${PREFIX}/lib" ]; then
echo "${PREFIX}/lib" | sudo tee /etc/ld.so.conf.d/sdl3-ci.conf >/dev/null
sudo ldconfig
fi
- name: Setup Odin
uses: laytan/setup-odin@v2
with:
token: ${{ secrets.GITHUB_TOKEN }}
# Match local toolchain: odin version dev-2026-05
release: dev-2026-05
- name: Odin version
run: odin version
- name: Unit tests
run: make test
- name: Typecheck toad example
run: make check
+5 -1
View File
@@ -1,7 +1,8 @@
.PHONY: shaders-vulkan shaders-d3d12 shaders-metal shaders-all toad check test help
.PHONY: shaders-vulkan shaders-d3d12 shaders-metal shaders-all bake toad check test help
help:
@echo "Targets:"
@echo " bake Bake all characters under content/characters/"
@echo " shaders-vulkan Compile SPIR-V into shaders/vulkan/"
@echo " shaders-d3d12 Compile DXIL into shaders/d3d12/ (needs shadercross)"
@echo " shaders-metal Compile MSL into shaders/metal/ (needs shadercross)"
@@ -10,6 +11,9 @@ help:
@echo " check Typecheck examples/toad"
@echo " toad Run the toad example"
bake:
./scripts/bake_all.sh
shaders-vulkan:
./scripts/shaders_vulkan.sh
Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.9 MiB

After

Width:  |  Height:  |  Size: 1.8 MiB

+76 -92
View File
@@ -10,7 +10,7 @@
"clips": {
"walk": {
"loop": true,
"fps": 10.00000000,
"fps": 15.00000000,
"frames": [
{
"rect": [
@@ -283,80 +283,16 @@
0,
0
]
},
{
"rect": [
807,
654,
268,
326
],
"source_size": [
268,
326
],
"trim_offset": [
0,
0
]
}
]
},
"idle": {
"loop": true,
"fps": 10.00000000,
"fps": 15.00000000,
"frames": [
{
"rect": [
1883,
0,
213,
319
],
"source_size": [
213,
319
],
"trim_offset": [
0,
0
]
},
{
"rect": [
1883,
320,
213,
319
],
"source_size": [
213,
319
],
"trim_offset": [
0,
0
]
},
{
"rect": [
1883,
640,
213,
319
],
"source_size": [
213,
319
],
"trim_offset": [
0,
0
]
},
{
"rect": [
1076,
807,
654,
213,
319
@@ -372,7 +308,7 @@
},
{
"rect": [
1290,
1021,
654,
213,
319
@@ -388,7 +324,7 @@
},
{
"rect": [
1504,
1235,
654,
213,
319
@@ -404,8 +340,8 @@
},
{
"rect": [
1718,
960,
1449,
654,
213,
319
],
@@ -420,7 +356,23 @@
},
{
"rect": [
1076,
1663,
654,
213,
319
],
"source_size": [
213,
319
],
"trim_offset": [
0,
0
]
},
{
"rect": [
807,
974,
213,
319
@@ -436,7 +388,7 @@
},
{
"rect": [
1290,
1021,
974,
213,
319
@@ -452,7 +404,39 @@
},
{
"rect": [
1504,
1235,
974,
213,
319
],
"source_size": [
213,
319
],
"trim_offset": [
0,
0
]
},
{
"rect": [
1449,
974,
213,
319
],
"source_size": [
213,
319
],
"trim_offset": [
0,
0
]
},
{
"rect": [
1663,
974,
213,
319
@@ -517,7 +501,7 @@
{
"rect": [
642,
981,
1294,
213,
319
],
@@ -533,23 +517,7 @@
{
"rect": [
856,
981,
213,
319
],
"source_size": [
213,
319
],
"trim_offset": [
0,
0
]
},
{
"rect": [
1718,
1280,
1294,
213,
319
],
@@ -593,6 +561,22 @@
0,
0
]
},
{
"rect": [
1498,
1294,
213,
319
],
"source_size": [
213,
319
],
"trim_offset": [
0,
0
]
}
]
}
Binary file not shown.

Before

Width:  |  Height:  |  Size: 42 KiB

+2 -2
View File
@@ -5,12 +5,12 @@
"clips": {
"idle": {
"folder": "Idle",
"fps": 10,
"fps": 15,
"loop": true
},
"walk": {
"folder": "Walk",
"fps": 10,
"fps": 15,
"loop": true
}
}
+232
View File
@@ -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)
testing.expect_value(t, len(clip.frames), 3)
testing.expect(t, clip.loop)
testing.expect_value(t, clip.fps, f32(10))
_, ok = character_clip(&data, "missing")
testing.expect(t, !ok)
data.def.clips["empty"] = Clip_Def {
loop = true,
fps = 10,
frames = nil,
}
_, ok = character_clip(&data, "empty")
testing.expect(t, !ok)
}
@(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)
}
@(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)
}
+11 -1
View File
@@ -170,7 +170,10 @@ load_character_data :: proc(app: ^App, character_json_path: string) -> (Characte
sdl.EndGPUCopyPass(copy_pass)
fence := sdl.SubmitGPUCommandBufferAndAcquireFence(cmd)
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.ReleaseGPUTexture(app.device, out.texture)
return {}, false
@@ -200,6 +203,13 @@ destroy_character_data :: proc(app: ^App, data: ^Character_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_rect :: proc(
data: ^Character_Data,
clip_name: string,
+28
View File
@@ -0,0 +1,28 @@
package engine
import sdl "vendor:sdl3"
Key :: enum {
A,
D,
Left,
Right,
}
key_down :: proc(key: Key) -> bool {
keys := sdl.GetKeyboardState(nil)
if keys == nil do return false
scan_code: sdl.Scancode
switch key {
case .A:
scan_code = .A
case .D:
scan_code = .D
case .Left:
scan_code = .LEFT
case .Right:
scan_code = .RIGHT
}
return keys[scan_code]
}
+80 -6
View File
@@ -10,6 +10,8 @@ Sprite :: struct {
position: Vec2,
clip: string,
frame: int,
time: f32,
flip_x: bool,
}
spawn_sprite :: proc(
@@ -18,13 +20,61 @@ spawn_sprite :: proc(
clip: string = "idle",
frame: int = 0,
) -> 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) {
_ = sprite
_ = dt
if sprite == nil || sprite.data == nil do return
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 {
@@ -50,9 +100,10 @@ draw_sprite :: proc(app: ^App, sprite: ^Sprite) {
fw := f32(rect[2])
fh := f32(rect[3])
// Feet-centered placement, same as renderer version
x0_px := sprite.position.x - fw * 0.5
y0_px := sprite.position.y - fh
pivot := sprite.data.def.pivot
origin := sprite_quad_origin(sprite.position, {fw, fh}, pivot)
x0_px := origin.x
y0_px := origin.y
x1_px := x0_px + fw
y1_px := y0_px + fh
@@ -69,6 +120,7 @@ draw_sprite :: proc(app: ^App, sprite: ^Sprite) {
v0 := f32(rect[1]) / tex_h
u1 := f32(rect[0] + rect[2]) / tex_w
v1 := f32(rect[1] + rect[3]) / tex_h
if sprite.flip_x do u0, u1 = u1, u0
// Two triangles: (0,1,2) and (0,2,3)
verts := [6]Vertex {
@@ -129,3 +181,25 @@ draw_sprite :: proc(app: ^App, sprite: ^Sprite) {
sdl.DrawGPUPrimitives(app.render_pass, 6, 1, 0, 0)
}
set_sprite_clip :: proc(sprite: ^Sprite, clip: string) {
if sprite == nil || sprite.data == nil do return
if sprite.clip == clip do return
_, ok := character_clip(sprite.data, clip)
if !ok do return
sprite.clip = clip
sprite.frame = 0
sprite.time = 0
}
sprite_quad_origin :: proc(position: Vec2, size: Vec2, pivot: [2]f32) -> Vec2 {
return {position.x - size.x * pivot[0], position.y - size.y * pivot[1]}
}
set_sprite_flip_x :: proc(sprite: ^Sprite, flip: bool) {
if sprite == nil do return
sprite.flip_x = flip
}
+14
View File
@@ -19,3 +19,17 @@ to_clip_corners :: proc(t: ^testing.T) {
testing.expect_value(t, p[0], 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))
}
+8
View File
@@ -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
}
+27 -2
View File
@@ -4,7 +4,7 @@ import eng "pkg:engine"
main :: proc() {
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)
data, ok := eng.load_character_data(&app, "assets_baked/characters/toad/toad.char.json")
@@ -14,10 +14,35 @@ main :: proc() {
toad := eng.spawn_sprite(&data, {400, 500}, "idle", 0)
last := eng.now_seconds()
SPEED :: f32(200)
for eng.events() {
now := eng.now_seconds()
dt := f32(now - last)
last = now
left := eng.key_down(.A) || eng.key_down(.Left)
right := eng.key_down(.D) || eng.key_down(.Right)
if left || right {
eng.set_sprite_clip(&toad, "walk")
if left {
toad.position.x -= SPEED * dt
toad.flip_x = true
}
if right {
toad.position.x += SPEED * dt
toad.flip_x = false
}
} else {
eng.set_sprite_clip(&toad, "idle")
}
eng.update_sprite(&toad, dt)
eng.begin_frame(&app)
eng.draw_sprite(&app, &toad)
eng.end_frame(&app)
}
}
+31
View File
@@ -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)"