crates/sverigekartan/examples/shots.rs
//! **The proof run, headless on the real GPU**: the same [`App`] the window runs,
//! driven frame by frame through egui_kittest's wgpu renderer (no window, no display),
//! against a live map server, saving a PNG at each stop:
//!
//! 1. `1-country.png` — the whole region;
//! 2. `2-stockholm.png` — central Stockholm, 2D;
//! 3. `3-stockholm-3d.png` — the same place after the 2D → 3D switch;
//! 4. `4-search.png` — a search (`Sveavägen 44 Stockholm`), 2D, the first hit marked;
//! 5. `5-click.png` — a click on the map, the nearest address marked.
//!
//! ```text
//! cargo run --release --example shots -- OUT_DIR [--server URL]
//! ```
use std::path::PathBuf;
use std::time::{Duration, Instant};
use egui_kittest::Harness;
use egui_kittest::wgpu::{WgpuTestRenderer, create_render_state};
use sverigekartan::{App, Cli, Mode, Options, parse_args};
const SIZE: (f32, f32) = (1280.0, 860.0);
fn main() {
let mut args = std::env::args().skip(1);
let out = PathBuf::from(args.next().expect("usage: shots OUT_DIR [--server URL]"));
std::fs::create_dir_all(&out).expect("out dir");
let opts = match parse_args(args).expect("arguments") {
Cli::Run(o) => o,
Cli::Print(t) => return println!("{t}"),
};
let setup = facett_core::render::gpu::facett_wgpu_options().wgpu_setup;
let rs = create_render_state(setup, Default::default());
let info = rs.adapter.get_info();
println!(
"adapter: {} ({:?}, {:?})",
info.name, info.device_type, info.backend
);
sverigekartan::install_gpu(&rs);
let app = App::new(Options { ..opts }, None);
let mut h = Harness::builder()
.with_size(egui::vec2(SIZE.0, SIZE.1))
.with_context_setup(facett_core::fonts::install)
.renderer(WgpuTestRenderer::from_render_state(rs))
.build_ui_state(|ui, app: &mut App| app.ui(ui), app);
let t0 = Instant::now();
settle(&mut h, "country");
shot(&mut h, &out, "1-country", t0);
h.state_mut().go_to(18.0686, 59.3293, 14.0);
settle(&mut h, "stockholm");
shot(&mut h, &out, "2-stockholm", t0);
h.state_mut().set_mode(Mode::ThreeD);
settle(&mut h, "3d");
shot(&mut h, &out, "3-stockholm-3d", t0);
h.state_mut().set_mode(Mode::TwoD);
settle(&mut h, "back to 2d");
h.state_mut().search("Sveavägen 44 Stockholm");
settle(&mut h, "search");
println!(
"hits: {:?}",
h.state_mut()
.hits()
.iter()
.map(|a| a.label())
.collect::<Vec<_>>()
);
shot(&mut h, &out, "4-search", t0);
// A real click, through egui's input: press and release at one point of the map.
let p = egui::pos2(SIZE.0 * 0.70, SIZE.1 * 0.62);
h.hover_at(p);
h.step();
h.event(egui::Event::PointerButton {
pos: p,
button: egui::PointerButton::Primary,
pressed: true,
modifiers: Default::default(),
});
h.step();
h.event(egui::Event::PointerButton {
pos: p,
button: egui::PointerButton::Primary,
pressed: false,
modifiers: Default::default(),
});
h.step();
settle(&mut h, "click");
println!("marked: {:?}", h.state_mut().marked());
shot(&mut h, &out, "5-click", t0);
}
/// Step frames until nothing is on its way (and a few more, for the last scene to
/// paint), at most two minutes.
fn settle(h: &mut Harness<'_, App>, what: &str) {
let start = Instant::now();
let mut quiet = 0;
while quiet < 12 && start.elapsed() < Duration::from_secs(120) {
h.step();
// The window renders every frame; the harness only when asked. The 3D scene's
// upload to the device runs in a paint callback, so render each frame here too.
let _ = h.render();
if h.state_mut().busy() {
quiet = 0;
} else {
quiet += 1;
}
std::thread::sleep(Duration::from_millis(20));
}
println!(
"{what}: settled in {:.1} s, view {:?}",
start.elapsed().as_secs_f64(),
h.state_mut().current_view()
);
}
fn shot(h: &mut Harness<'_, App>, out: &std::path::Path, name: &str, t0: Instant) {
let img = h.render().expect("wgpu render");
let path = out.join(format!("{name}.png"));
img.save(&path).expect("save png");
println!(
"{name}: {} at {:.1} s",
path.display(),
t0.elapsed().as_secs_f64()
);
}