ponko 0.0.1

A fast 2D game engine for simulating millions of things: ECS on all cores, hot reload, an in-game inspector, AI-agent control, and a C++ API.
Documentation
//! Profiling: frame statistics, the performance overlay and traces.

use ponko::*;

// ANCHOR: profiling
/// F3 shows the performance overlay; F4 records the next 300 frames as a
/// trace to open in Perfetto. Slow stretches are logged with their cause.
fn perf_keys(ctx: &mut Ctx) {
    if ctx.key_pressed(Key::F3) {
        let on = !ctx.perf_hud();
        ctx.set_perf_hud(on);
    }
    if ctx.key_pressed(Key::F4) {
        ctx.record_trace("trace.json", 300);
    }
    let stats = ctx.frame_stats();
    if stats.frames > 0 && stats.p99_ms > 20.0 && ctx.frame().is_multiple_of(600) {
        // Waiting on the display means the GPU (or vsync) is the limit.
        let gpu_bound = stats.display_wait_ms > stats.systems_ms;
        println!(
            "slow: p99 {:.1} ms, gpu {:?} ms, gpu bound: {gpu_bound}",
            stats.p99_ms, stats.gpu_ms
        );
        for pass in ctx.gpu_passes() {
            println!("  {}: {:.2} ms", pass.name, pass.ms);
        }
    }
}
// ANCHOR_END: profiling

#[test]
fn headless_runs_have_no_frame_stats() {
    Game::new("t", 1, 1).system(perf_keys).run_headless(10, |ctx| {
        assert_eq!(ctx.frame_stats().frames, 0);
        assert!(ctx.gpu_passes().is_empty());
        ctx.set_perf_hud(true);
        assert!(ctx.perf_hud());
        ctx.record_trace("never-written.json", 10); // no frames: nothing
    });
    assert!(!std::path::Path::new("never-written.json").exists());
}