# Xbox 360 avatars in Rust

To use Xbox 360 avatars in Rust, depend on the `sona360` crate in the SDK download (`use sona;` in code), open a
`sona::Context` with your licence and add each avatar from its manifest. The crate wraps the C library safely, and
`sona360-sys` (`sona_sys`) holds the raw calls.

![A Rust program's run, and the avatar.png it saved: an avatar mid-wave](../img/one-tutorial-rust.webp)

## Steps

1. Download the SDK for your platform ([getting it](../web-api.md#sdk-download)) and unpack it into `sdk/`
   beside your `Cargo.toml`. You need Rust 1.77 or newer.
2. Set `SONA_LIB_DIR` to the folder that holds the library when you build.
3. Add the crate as a dependency.
4. Open a context with `gate(None)` (the item files from 360sona) or `dir(folder)` (a folder of your own), then add
   the avatar with `add_base64`.

```toml
[dependencies]
# the crate in your download, unpacked into sdk/ beside this file
sona360 = { path = "sdk/bindings/rust/sona" }
```

## Open a context

```rust
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())?;
```

Then `ctx.add_base64` adds the avatar, and `sona::Headless::render` waits for it to load, poses it in a clip and
returns RGBA bytes with no window.

## More

- [The Rust tutorial](../tutorials/rust.md) builds the whole program, which saves a picture of the wave.
- The crate's docs (`cargo doc --open`) list its types; [the C reference](../reference.md) has the rules behind
  every call.
- Credit: show "Powered by 360sona" where your users see the avatars.
- 360sona is not affiliated with or endorsed by Microsoft.
