# Concepts

The model every language and engine follows: the context, avatars, renderers, the frame, where files come from, units,
threads, memory and errors. The [C reference](reference.md) states each rule exactly. Each idea has a page of its own:

- [Manifests](concepts/manifests.md): the 1,000 bytes of one avatar.
- [Items and slots](concepts/items.md): what an avatar wears and holds.
- [Clips and animation](concepts/clips.md): how an avatar moves.
- [Item files, the gate and the licence](concepts/sources.md): where the items' files come from.
- [The credit](concepts/credit.md): "Powered by 360sona".
- [Glossary](glossary.md): the words, one line each.

## The pieces

- **The context** (`sona_ctx`) holds the licence, the sources avatar files come from, and every avatar. A game
  usually has one. Every call on it and its avatars belongs to the thread that opened it (the owner thread).
- **An avatar** (`sona_avatar`) is one person, made from a manifest. It loads in the background and becomes `READY`;
  until then it is drawn as nothing, or as a neutral figure with [placeholders](guides/placeholder.md) on.
- **A renderer** (`sona_renderer`) draws the context's avatars on one graphics device: Vulkan, Direct3D 11,
  Direct3D 12, OpenGL, OpenGL ES, Metal, or Xenos on an Xbox 360. A game can instead read posed meshes and draw them
  itself: that is how the Unity, Godot and Unreal packages work.

## The manifest

An avatar is 1,000 bytes, the same description an Xbox 360 stores: the body, its height and weight, nine colours,
the face, and the items worn in each slot. Text APIs carry it as base64. 360sona.com makes one, the gamertag lookup
returns the one on an Xbox account, and `sona_make_random` makes one from a seed.

## The frame

Each frame:

1. `sona_update(ctx, dt, view, stats)` moves every clock, loads what arrived, and poses the avatars in view.
2. `sona_draw(renderer, view, target, options)` draws them into your target, with the credit badge in a corner.
3. Your own code presents the frame.

Clips play on each avatar's clock: `sona_avatar_play`, `_crossfade` and `_queue`, with layers, masks, blend
spaces and state machines above them ([layers](guides/layers.md)).

## Where avatar files come from

Sources are asked in the order you list them, and the first that has a file wins:

- the 360sona gate, which streams files over https for your app id (the usual source; your download's licence opens it);
- an asset folder: a folder of item files you own, laid out as the gate serves them;
- a console's Content tree.

What is read is kept in a memory cache. The licence file from your download is checked when the context opens.

A new machine has no avatar files. A program, or an agent, then needs a folder the person owns (`--dir` in the
tools and samples), or the gate (`--gate` in the tools; the samples use it when given no `--dir`), which needs the
network and your app's licence. With no network the tools and samples fail at once and say so rather than wait for
files that cannot come.

## Units and axes

Metres, Y up, left-handed. Avatars stand about 1.8 m tall and face -Z at turn 0. Matrices are row-major and
row-vector (`v' = v * M`), so a matrix's position is its last row. Times are seconds.

## Placement: position, turn and scale

Each avatar stands where its placement puts it: `position` in metres, `turn` in degrees about Y (0 faces -Z) and
`scale`, a size factor (1 is the avatar's own size, about 1.8 m tall; 2 is twice that). In C it is
[`sona_placement`](reference/fields.md#sona_placement), given in `sona_add_options.place` when the avatar is added and
to `sona_avatar_move` after; the web SDK's `add()` and `move()` take the same three
([Placement](reference/web.md#placement)).

## Threads

One thread owns a context: the thread that opened it makes every call on it and on its avatars. Reads, decodes and
avatar builds run on worker threads (`config.worker_threads`, or your own job system through `config.jobs`), and
their results are committed on the owner thread inside `sona_update`, within `config.build_budget_ms` a frame. With
no workers the same work runs inside that budget. Two contexts are independent and may live on two threads.

## Memory

Every allocation goes through the config's allocator (yours, if you set one) and counts against `config.budget`.
`sona_stats` reports what is held in `cpu_mb` and `gpu_mb`. Items no avatar wears any more are kept for reuse and
evicted oldest first; an item an avatar needs is never refused for being over a cache's cap, and `budget.total` is
the hard limit: past it an allocation fails with `SONA_E_MEMORY`. What the library hands you (`sona_png_encode`'s
bytes, an exported file) you free with `sona_free`.

## Results and errors

Every call returns a `sona_result`. `SONA_OK` is success, `SONA_PENDING` means still working (keep updating), and the
`SONA_E_*` codes are errors. `sona_last_error(ctx)` gives the code, a message and a fix in words; a failed avatar
raises `SONA_EVENT_FAILED` with its reason. [Troubleshooting](troubleshooting.md) lists every code.

## Credit

Every SDK draws a small "Powered by 360sona" badge where avatars appear, and writes the credit into the files it
exports. You may turn the badge off if you put the line in your credits, about page or store page.
