# Examples

Every example here is a whole program in the SDK: its picture is what it draws, its code is cut from its source,
and the page after it says how to build and run it. The programs are in your download (`samples/`, `starters/`,
`templates/`) or beside the tutorials.

## One avatar, waving

One avatar from its manifest, drawn with no window 1.5 seconds into a wave and saved as a PNG with the credit in it.
The same program in each language.

### C

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

```c
/* An avatar from its manifest. It loads over the next updates. */
r = sona_avatar_add_base64(ctx, argv[2], NULL, &avatar);
if (r != SONA_OK) return fail("sona_avatar_add_base64", ctx, r);

/* No window: one picture, 1.5 s into the wave. */
r = sona_headless_open(ctx, NULL, &h);
if (r != SONA_OK) return fail("sona_headless_open", ctx, r);
sona_render_defaults(&ro);
ro.width = 480;
ro.height = 600;
ro.clip = "GenericWave";
ro.time = 1.5f;
rgba = malloc((size_t)ro.width * ro.height * 4);
r = rgba ? sona_headless_render(h, avatar, &ro, rgba) : SONA_E_MEMORY;
if (r != SONA_OK) return fail("sona_headless_render", ctx, r);
```

The whole program, `hello.c`, and how to build it: [your first avatar in C](tutorials/c.md).

### C++

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

```cpp
sona::Context ctx(cfg);
sona::Avatar avatar = ctx.add_base64(argv[2]);

// The headless renderer is plain C: sona.hpp hands over the raw handles.
sona_headless *h = 0;
if (sona_headless_open(ctx.raw(), 0, &h) != SONA_OK) { std::fprintf(stderr, "no device to draw with\n"); return 1; }
sona_render_options ro;
sona_render_defaults(&ro);
ro.width = 480;
ro.height = 600;
ro.clip = "GenericWave";
ro.time = 1.5f;
```

The whole program and its build: [your first avatar in C++](tutorials/cpp.md).

### C#

![Hello.cs's run, and the avatar.png it saved: an avatar mid-wave](img/one-tutorial-csharp.webp)

```csharp
var avatar = ctx.AddBase64(args[1]);
while (avatar.State != SonaAvatarState.Ready && avatar.State != SonaAvatarState.Failed) ctx.Update(1.0 / 60);
if (avatar.State != SonaAvatarState.Ready) { Console.Error.WriteLine("the avatar did not load"); return 1; }

avatar.Play("GenericWave");
for (var i = 0; i < 60; i++) ctx.Update(1.0 / 60);
```

The whole program and its project: [your first avatar in C#](tutorials/csharp.md).

### Rust

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

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

The whole program and its `Cargo.toml`: [your first avatar in Rust](tutorials/rust.md).

### Python

![hello.py's run, and the avatar.png it saved: an avatar mid-wave](img/one-tutorial-python.webp)

```python
avatar = ctx.add_base64(manifest)
while not avatar.settled:
    ctx.update(1 / 60)
if not avatar.ready:
    sys.exit("the avatar did not load")

avatar.play("GenericWave")
for _ in range(60):
    ctx.update(1 / 60)
```

The whole program: [avatars as data in Python](tutorials/python.md).

## In an engine

One avatar from a manifest file, waving, in each engine's own terms: a component on an object.

### Unity

```csharp
void Start()
{
    var avatar = new GameObject("Avatar").AddComponent<SonaAvatar>();
    avatar.manifestAsset = manifest;
    avatar.clip = "GenericWave";
}
```

`HelloAvatar.cs` is in the package's Components sample: [your first avatar in Unity](tutorials/unity.md).

### Godot

![The Godot demo's hello scene](img/godot-hello.webp)

```gdscript
func _ready() -> void:
	var avatar = ClassDB.instantiate("SonaAvatar3D")
	var text = FileAccess.get_file_as_string("res://hello/manifest.txt")
	avatar.manifest = text.strip_edges()
	avatar.clip = "GenericWave"
	add_child(avatar)
	avatar.avatar_ready.connect(func(): avatar.frame_camera($Camera3D))
```

The addon's demo project, scene `hello`: [your first avatar in Godot](tutorials/godot.md).

### Unreal

```cpp
AHelloAvatar::AHelloAvatar()
{
	Avatar = CreateDefaultSubobject<USonaAvatarComponent>(TEXT("Avatar"));
	RootComponent = Avatar;
	Avatar->Clip = TEXT("GenericWave");
}
```

`AHelloAvatar` is in the starter project's module: [your first avatar in Unreal](tutorials/unreal.md).

## The plaza

![The plaza starter in C: costumed avatars walking about a plaza](img/starter-plaza.webp)

Twelve avatars, every one a different costume, walking loops about a plaza. The same scene is a starter on every
platform (C, C++, C#, Rust, Python, the web, Unity, Godot, Unreal and Android), the bare start of a game.

```c
for (i = 0; i < (int)count; i++) {   /* all start at once, each a little further into the walk */
  sona_play_options po = { 1, 1, (float)i * 0.13f };
  sona_avatar_play_stock(walkers[i].a, sona_stock_clip_find("walk_forward"), &po, 0);
}
```

Each platform's starter and how to run it: [the plaza starter](tutorials/starter-plaza.md).

## A crowd on a web page

![The web crowd: a thousand different avatars on one canvas](img/web-crowd.webp)

A thousand avatars in one canvas, each in one of 506 costumes on a face, hair and build of its own, standing out of
step.

```js
for (const f of crowd) f.avatar = sona.add(f.manifest, { position: f.at, turn: f.turn, clip: f.clip, startAt: 'random' });
```

`examples/crowd.html` in the web download: [the web crowd template](tutorials/template-web-crowd.md).

## Templates

Each template is a small game or tool to start from, built with CMake and run as it comes.

### Bot and server

![A gamerpic the bot template drew](img/template-bot-gamerpic.webp)

Gamerpics and outfit cards over HTTP, for chat bots and sites: [the bot and server template](tutorials/template-bot.md).

### Lobby

![Two players in the lobby template](img/template-lobby.webp)

Sign in with 360sona, the gamertag lookup, network sync and emotes with root motion: [the lobby template](tutorials/template-lobby.md).

### VTuber kit

![The VTuber kit's window](img/template-vtuber.webp)

A webcam and a microphone drive the avatar's face, in a transparent window for OBS: [the VTuber kit](tutorials/template-vtuber.md).

### VR

![The VR template's mirror](img/template-vr-mirror.webp)

OpenXR, a first-person body, a mirror and other players: [the VR template](tutorials/template-vr.md).

### C++ starter

![The C++ starter project](img/template-cpp-starter.webp)

A project made by `sona project new`: your avatar and eleven others walking about a plaza, in C++: [the C++ starter project](tutorials/template-cpp-starter.md).

### NPC kit

![An NPC from the kit](img/npc-kit.webp)

An avatar your game's agent drives: it says a line with lip sync, emotes what the words say and looks at the player: [the NPC kit](guides/npc.md).

## Looks and outputs

### Art styles

![Toon with an outline](img/styles-toon.webp)

Toon, flat, silhouette and promo looks, each with or without an outline: [art styles](guides/styles.md).

### Lights and shadows

![Avatars under the SDK's lighting](img/lighting.webp)

Lights, an environment and shadows, or your engine's own shadow map: [lighting](guides/lighting.md).

### Two avatars, one moment

![A high five](img/interact-five.webp)

Hand holds, high fives, handshakes, hugs and seats, fitted to both bodies: [two-avatar interactions](guides/interactions.md).

### A sprite sheet

![A sprite sheet for a 2D game](img/sprite-sheet.webp)

An avatar's clips as frames for a 2D game, with stickers and pixel art: [2D outputs](guides/sprites2d.md).

### A 3D print

![A print-ready avatar](img/print3d.webp)

A posed avatar as one watertight solid, STL or 3MF with colour, on the CPU: [3D print](guides/print3d.md).

### An avatar in Blender

![An avatar imported into Blender](img/blender-import.webp)

Rigged, with its clips, and exported back: [Blender](tutorials/blender.md).

### Ten thousand avatars

![A stadium of 10,000 different avatars](img/crowd-10000.webp)

Every one different, with level of detail: [crowds](guides/crowd.md).

More, one recipe a game moment: [the cookbook](cookbook.md).
