Compare commits
7
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f65b0354df | ||
|
|
cc196afd2e | ||
|
|
8d9faaaee0 | ||
|
|
41fb0a3d5a | ||
|
|
dcf5ea29b0 | ||
|
|
70784b0f1b | ||
|
|
deef204bd3 |
@@ -118,6 +118,32 @@ set_sprite_clip_switch_and_guards :: proc(t: ^testing.T) {
|
||||
testing.expect_value(t, sprite.time, f32(0.09))
|
||||
}
|
||||
|
||||
@(test)
|
||||
set_sprite_clip_caches_clip_def :: proc(t: ^testing.T) {
|
||||
data := make_test_character()
|
||||
defer destroy_test_character(&data)
|
||||
|
||||
sprite := spawn_sprite(&data, {}, "idle", 0)
|
||||
testing.expect(t, sprite.has_clip, "spawn should cache a valid clip")
|
||||
testing.expect_value(t, sprite.clip, "idle")
|
||||
testing.expect(t, sprite.clip_def.loop, "idle clip loops")
|
||||
testing.expect_value(t, sprite.clip_def.fps, f32(10))
|
||||
testing.expect_value(t, len(sprite.clip_def.frames), 3)
|
||||
|
||||
set_sprite_clip(&sprite, "once")
|
||||
testing.expect(t, sprite.has_clip, "switch should refresh cache")
|
||||
testing.expect_value(t, sprite.clip, "once")
|
||||
testing.expect(t, !sprite.clip_def.loop, "once clip does not loop")
|
||||
testing.expect_value(t, sprite.clip_def.fps, f32(10))
|
||||
testing.expect_value(t, len(sprite.clip_def.frames), 3)
|
||||
|
||||
set_sprite_clip(&sprite, "nope")
|
||||
testing.expect(t, sprite.has_clip, "bad clip must leave cache intact")
|
||||
testing.expect_value(t, sprite.clip, "once")
|
||||
testing.expect(t, !sprite.clip_def.loop, "cached once clip preserved")
|
||||
testing.expect_value(t, len(sprite.clip_def.frames), 3)
|
||||
}
|
||||
|
||||
@(test)
|
||||
spawn_sprite_valid_and_invalid :: proc(t: ^testing.T) {
|
||||
data := make_test_character()
|
||||
|
||||
@@ -27,6 +27,15 @@ choose_shader_runtime_prefers_msl :: proc(t: ^testing.T) {
|
||||
testing.expect_value(t, rt.format, sdl.GPUShaderFormat{.MSL})
|
||||
}
|
||||
|
||||
@(test)
|
||||
choose_shader_runtime_prefers_dxil_without_msl :: proc(t: ^testing.T) {
|
||||
rt, ok := choose_shader_runtime_from_formats({.DXIL, .SPIRV})
|
||||
testing.expect(t, ok, "should pick a runtime when DXIL is available")
|
||||
testing.expect_value(t, rt.backend, Shader_Backend.DSD12_DXIL)
|
||||
testing.expect_value(t, rt.shader_dir, "shaders/d3d12")
|
||||
testing.expect_value(t, rt.format, sdl.GPUShaderFormat{.DXIL})
|
||||
}
|
||||
|
||||
@(test)
|
||||
choose_shader_runtime_spirv_only :: proc(t: ^testing.T) {
|
||||
rt, ok := choose_shader_runtime_from_formats({.SPIRV})
|
||||
|
||||
+26
-23
@@ -5,12 +5,14 @@ import sdl "vendor:sdl3"
|
||||
Vec2 :: [2]f32
|
||||
|
||||
Sprite :: struct {
|
||||
data: ^Character_Data,
|
||||
position: Vec2,
|
||||
clip: string,
|
||||
frame: int,
|
||||
time: f32,
|
||||
flip_x: bool,
|
||||
data: ^Character_Data,
|
||||
position: Vec2,
|
||||
clip: string,
|
||||
clip_def: Clip_Def,
|
||||
has_clip: bool,
|
||||
frame: int,
|
||||
time: f32,
|
||||
flip_x: bool,
|
||||
}
|
||||
|
||||
spawn_sprite :: proc(
|
||||
@@ -24,16 +26,14 @@ spawn_sprite :: proc(
|
||||
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
|
||||
}
|
||||
if frame != 0 && sprite.has_clip {
|
||||
c := sprite.clip_def
|
||||
if frame < 0 {
|
||||
sprite.frame = 0
|
||||
} else if frame >= len(c.frames) {
|
||||
sprite.frame = len(c.frames) - 1
|
||||
} else {
|
||||
sprite.frame = frame
|
||||
}
|
||||
}
|
||||
return sprite
|
||||
@@ -42,9 +42,9 @@ spawn_sprite :: proc(
|
||||
update_sprite :: proc(sprite: ^Sprite, dt: f32) {
|
||||
if sprite == nil || sprite.data == nil do return
|
||||
if dt <= 0 do return
|
||||
if !sprite.has_clip do return
|
||||
|
||||
clip, ok := character_clip(sprite.data, sprite.clip)
|
||||
if !ok do return
|
||||
clip := sprite.clip_def
|
||||
|
||||
frame_count := len(clip.frames)
|
||||
if frame_count <= 0 do return
|
||||
@@ -116,12 +116,13 @@ draw_sprite_batched :: proc(app: ^App, sprite: ^Sprite, batch_group: u32) {
|
||||
if len(app.draw_list) >= MAX_SPRITES {
|
||||
return
|
||||
}
|
||||
|
||||
frame, ok := character_frame(sprite.data, sprite.clip, sprite.frame)
|
||||
if !ok {
|
||||
if !sprite.has_clip do return
|
||||
if sprite.frame < 0 || sprite.frame >= len(sprite.clip_def.frames) {
|
||||
return
|
||||
}
|
||||
|
||||
frame := sprite.clip_def.frames[sprite.frame]
|
||||
|
||||
src_w := f32(frame.source_size[0])
|
||||
src_h := f32(frame.source_size[1])
|
||||
if src_w <= 0 do src_w = f32(frame.rect[2])
|
||||
@@ -177,12 +178,14 @@ draw_sprite_batched :: proc(app: ^App, sprite: ^Sprite, batch_group: u32) {
|
||||
set_sprite_clip :: proc(sprite: ^Sprite, clip: string) {
|
||||
if sprite == nil || sprite.data == nil do return
|
||||
|
||||
if sprite.clip == clip do return
|
||||
if sprite.clip == clip && sprite.has_clip do return
|
||||
|
||||
_, ok := character_clip(sprite.data, clip)
|
||||
def, ok := character_clip(sprite.data, clip)
|
||||
if !ok do return
|
||||
|
||||
sprite.clip = clip
|
||||
sprite.clip_def = def
|
||||
sprite.has_clip = true
|
||||
sprite.frame = 0
|
||||
sprite.time = 0
|
||||
}
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
package main
|
||||
|
||||
import "core:fmt"
|
||||
import eng "pkg:engine"
|
||||
|
||||
// Many sprites sharing one Character_Data (Flyweight) — good batching demo.
|
||||
COUNT :: 24
|
||||
COUNT :: eng.MAX_SPRITES
|
||||
COLS :: 16
|
||||
FRAME_LOG_EVERY :: 60
|
||||
|
||||
main :: proc() {
|
||||
app: eng.App
|
||||
@@ -16,25 +19,45 @@ main :: proc() {
|
||||
|
||||
sprites: [COUNT]eng.Sprite
|
||||
for i in 0 ..< COUNT {
|
||||
col := i % 8
|
||||
row := i / 8
|
||||
col := i % COLS
|
||||
row := i / COLS
|
||||
pos := eng.Vec2 {
|
||||
f32(120 + col * 80),
|
||||
f32(280 + row * 120),
|
||||
f32(40 + col * 48),
|
||||
f32(80 + row * 60),
|
||||
}
|
||||
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()
|
||||
frame_i := 0
|
||||
sum_ms: f64
|
||||
peak_ms: f64
|
||||
|
||||
for eng.events() {
|
||||
now := eng.now_seconds()
|
||||
dt := f32(now - last)
|
||||
last = now
|
||||
frame_ms := f64(dt) * 1000.0
|
||||
sum_ms += frame_ms
|
||||
if frame_ms > peak_ms do peak_ms = frame_ms
|
||||
frame_i += 1
|
||||
|
||||
if frame_i % FRAME_LOG_EVERY == 0 {
|
||||
avg := sum_ms / f64(FRAME_LOG_EVERY)
|
||||
fps := 1000.0 / avg if avg > 0 else 0
|
||||
fmt.printfln(
|
||||
"crowd frame: avg=%.2f ms (%.1f FPS) peak=%.2f ms over %d frames",
|
||||
avg,
|
||||
fps,
|
||||
peak_ms,
|
||||
FRAME_LOG_EVERY,
|
||||
)
|
||||
sum_ms = 0
|
||||
peak_ms = 0
|
||||
}
|
||||
|
||||
for &s in sprites {
|
||||
eng.update_sprite(&s, dt)
|
||||
|
||||
Reference in New Issue
Block a user