//! hello: one avatar, waving, saved as avatar.png. //! cargo run -- [out.png] [--dir ] //! Credit: show "Powered by 360sona" where your users see the avatar. use std::process::ExitCode; // Getting started's steps 3 to 6, for a program with a window of its own #[allow(dead_code)] mod window; fn run() -> Result { let mut args: Vec = std::env::args().skip(1).collect(); let mut dir = None; if args.len() >= 4 && args[args.len() - 2] == "--dir" { dir = args.pop(); args.pop(); } if args.len() < 2 { return Err("usage: hello [out.png] [--dir ]".into()); } let path = args.get(2).cloned().unwrap_or_else(|| "avatar.png".into()); // A context: the licence and where the avatar files come from (the gate: item files from 360sona). let licence = std::fs::read(&args[0]) .map_err(|e| format!("cannot read {}: {}", args[0], e))?; let mut cfg = sona::Config::new().licence(licence); if let Ok(app) = std::env::var("SONA_APP_ID") { cfg = cfg.app_id(&app); // unset: the app id your download is made for } cfg = match &dir { Some(d) => cfg.dir(d), None => cfg.gate(None), }; let ctx = sona::Context::open(cfg).map_err(|e| e.to_string())?; // An avatar from its manifest. It loads over the next updates. let avatar = ctx.add_base64(&args[1], &sona::AddOptions::default()).map_err(|e| e.to_string())?; // No window: one picture, 1.5 s into the wave. let h = sona::Headless::open(&ctx, &sona::HeadlessOptions::default()).map_err(|e| e.to_string())?; let mut ro = sona::RenderOptions::default(); ro.width = 480; ro.height = 600; ro.clip = Some("GenericWave".into()); ro.time = 1.5; let rgba = h.render(&avatar, &ro).map_err(|e| e.to_string())?; // A PNG with the credit in its text chunks. let png = ctx.png_encode(&rgba, ro.width, ro.height).map_err(|e| e.to_string())?; std::fs::write(&path, png).map_err(|e| format!("cannot write {}: {}", path, e))?; Ok(format!("saved {} ({} x {})", path, ro.width, ro.height)) } fn main() -> ExitCode { match run() { Ok(line) => { println!("{}", line); ExitCode::SUCCESS } Err(e) => { eprintln!("{}", e); ExitCode::FAILURE } } }