# Bindings: C++

81 public names in `bindings/cpp/sona.hpp`.

## Functions

| Declaration | What it does |
|---|---|
| `sona_result check_layout(std::string *what = 0)` | Compares the size of every public struct and the offset of every field with the library's; SONA_OK when they all match, else SONA_E_UNSUPPORTED with the first that differs named in *what.<br>Calls [`sona_struct_size`](../c/the-context.md#sona_struct_size), [`sona_struct_offset`](../c/the-context.md#sona_struct_offset). |
| `sona_result abi(sona_abi_info &out)` | The loaded library's ABI and version.<br>Calls [`sona_abi_query`](../c/the-context.md#sona_abi_query). |
| `sona_result emote_from_text(const char *text, sona_emote &out)` | A sentence to stock clips, a speed and a face; the same text gives the same answer.<br>Calls [`sona_emote_from_text`](../c/text-to-emote.md#sona_emote_from_text). |
| `sona_route slot_of(const sona_guid &id, uint32_t *mask = 0)` | Where an item id goes; its slot mask in *mask when mask is given.<br>Calls [`sona_slot_of`](../c/manifests.md#sona_slot_of). |
| `std::string format_guid(const sona_guid &g)` | The id in its lowercase 36-character form.<br>Calls [`sona_guid_format`](../c/manifests.md#sona_guid_format). |
| `View<uint8_t> shader_source(sona_shader_lang lang, sona_program prog, int stage)` | Text, or SPIR-V bytes; empty when that language is not in this build.<br>Calls [`sona_shader_source`](../c/the-data-api.md#sona_shader_source). |
| `void camera_orbit(sona_view &v, const double target[3], double yaw_deg, double pitch_deg, double distance)` | Places the eye on an orbit around target.<br>Calls [`sona_camera_orbit`](../c/camera-helpers.md#sona_camera_orbit). |
| `View<uint8_t> vertices(const sona_mesh &m)` | The 76-byte vertices and the indices of one mesh. |

## View

`class View`: A read-only view over memory the core owns: valid as long as the call that gave it says.

| Declaration | What it does |
|---|---|
| `View()`<br>`View(const T *p, size_t n)` | A read-only view over memory the core owns: valid as long as the call that gave it says. |

## Error

`class Error : public std::exception`: What a call throws when it fails: code (a sona_result), message and fix, as sona_last_error gives them.

| Declaration | What it does |
|---|---|
| `Error(sona_result c, const std::string &m, const std::string &f)` | Makes the exception from a code, its message and its fix; its message reads "<code name>: <message> (<fix>)".<br>Calls [`sona_result_name`](../c/results-and-errors.md#sona_result_name). |
| `~Error() throw()` | An empty destructor: an Error owns nothing of the library's. |

## Manifest

`class Manifest`: An AvatarDescription and its edit verbs (pure; no ctx).

| Declaration | What it does |
|---|---|
| `sona_result parse(const uint8_t bytes[SONA_MANIFEST_BYTES], Manifest &out)` | Reads 1000 bytes into a sona_manifest.<br>Calls [`sona_manifest_parse`](../c/manifests.md#sona_manifest_parse). |
| `sona_result parse_base64(const std::string &b64, Manifest &out)` | base64 text, as the web's add: at most 4096 characters.<br>Calls [`sona_base64_decode`](../c/manifests.md#sona_base64_decode). |
| `std::vector<uint8_t> bytes() const` | The manifest's SONA_MANIFEST_BYTES bytes, packed as the console stores them. |
| `sona_result wear(const sona_guid &item, const uint32_t colours[3], uint32_t flags = 0)` | Puts an item on, with its three colours.<br>Calls [`sona_manifest_wear`](../c/manifests.md#sona_manifest_wear). |

## Request

`class Request`: One read in flight.

| Declaration | What it does |
|---|---|
| `Request(sona_request *r = 0)` | One read in flight. |

## Source

`class Source`: A host-written source.

| Declaration | What it does |
|---|---|
| `~Source()` | A source is ended through the context; destroying it frees the binding's side. |
| `void cancel(Request)` | Optional. The C field sona_source.cancel (a function (NULL: none)). |
| `sona_source c_source()` | The sona_source that calls this object's read and cancel: put it in a source description of kind SONA_SOURCE_CUSTOM. |

## Config

`class Config`: sona_config with owned strings.

| Declaration | What it does |
|---|---|
| `Config()` | Sets abi and leaves everything else at its default.<br>Calls [`sona_config_defaults`](../c/the-context.md#sona_config_defaults). |
| `Config &licence(const void *bytes, size_t len)` | Sona-licence.json from the download, as bytes. The C field sona_config.licence (pointer). |
| `bool licence_file(const std::string &path)` | false when the file could not be read. |
| `const sona_config *get()` | The sona_config, its pointers into this object. |

## Pose

`class Pose`: A view over sona_pose_data: valid until the next Context::update.

| Declaration | What it does |
|---|---|
| `Pose()`<br>`Pose(const sona_pose_data *p)` | A view over sona_pose_data: valid until the next Context::update. |

## Avatar

`class Avatar`: A handle to one avatar.

| Declaration | What it does |
|---|---|
| `Avatar()`<br>`Avatar(sona_avatar *a, sona_ctx *ctx)` | A handle to one avatar. |
| `sona_result ragdoll_motors(std::vector<sona_ragdoll_motor> &out) const` | The joint motor targets of the last pose; SONA_E_NOT_READY before sona_avatar_ragdoll.<br>Calls [`sona_avatar_ragdoll_motors`](../c/ragdolls.md#sona_avatar_ragdoll_motors). |
| `sona_result play(const char *clip, const sona_play_options *opts = 0)` | Plays a clip.<br>Calls [`sona_avatar_play`](../c/avatars.md#sona_avatar_play). |
| `void remove()` | Removes the avatar and frees what only it used.<br>Calls [`sona_avatar_remove`](../c/avatars.md#sona_avatar_remove). |
| `sona_result pose(Pose &out) const`<br>`Pose pose() const` | The avatar's pose data, valid until the next sona_update.<br>Calls [`sona_pose`](../c/the-data-api.md#sona_pose). |
| `sona_result pose_vertices(uint32_t mesh, uint32_t space, float *positions, float *normals = 0, size_t stride = 3)` | CPU skinning of one mesh; positions (and normals) hold vertex_count * stride floats.<br>Calls [`sona_pose_vertices`](../c/the-data-api.md#sona_pose_vertices). |
| `sona_result camera_preset(sona_preset p, sona_view &view) const` | Sets view->eye and view->target to one of the console's framings on one ready avatar.<br>Calls [`sona_camera_preset`](../c/camera-helpers.md#sona_camera_preset). |
| `sona_result set_override(uint32_t joint, const float rotation[4], float weight = 1, uint32_t mode = SONA_OVERRIDE_REPLACE)` | rotation (x, y, z, w) after the clip; weight 0 or a NULL rotation clears the joint.<br>Calls [`sona_avatar_override`](../c/game-features.md#sona_avatar_override). |
| `sona_result crossfade(const char *clip, float seconds, const sona_play_options *opts = 0)` | sona_avatar_play, blending from the current pose over seconds.<br>Calls [`sona_avatar_crossfade`](../c/game-features.md#sona_avatar_crossfade). |
| `sona_result queue(const char *clip, float fade_seconds = 0, const sona_play_options *opts = 0)` | Plays clip when the current one ends; SONA_E_LIMIT past SONA_QUEUE_MAX.<br>Calls [`sona_avatar_queue`](../c/game-features.md#sona_avatar_queue). |
| `sona_result expression_counts(uint32_t counts[3]) const` | counts0 mouth, 1 eyebrows, 2 eyes.<br>Calls [`sona_avatar_expression_counts`](../c/game-features.md#sona_avatar_expression_counts). |
| `std::vector<sona_clip_event> clip_events()` | Every waiting clip event, oldest first.<br>Calls [`sona_avatar_clip_events`](../c/game-features.md#sona_avatar_clip_events). |
| `sona_result preview_view(uint32_t width, uint32_t height, sona_view &out)` | The framed camera for a width x height picture: the slot's view preset gives the angle, the item's box the target and the distance.<br>Calls [`sona_item_preview_view`](../c/item-previews.md#sona_item_preview_view). |

## Context

`class Context`: Owns a sona_ctx; move-only.

| Declaration | What it does |
|---|---|
| `Context()`<br>`Context(sona_ctx *adopt)` | Owns a sona_ctx; move-only. |
| `Context &operator=(Context &&o) noexcept` | Moves the handle from another object, which is left empty. |
| `sona_result open(Config &cfg, Context &out)` | Checks the struct layout first: a header that does not match the library gives SONA_E_UNSUPPORTED.<br>Calls [`sona_open`](../c/the-context.md#sona_open). |
| `void close()` | Closes a context.<br>Calls [`sona_close`](../c/the-context.md#sona_close). |
| `sona_result add(const uint8_t manifest[SONA_MANIFEST_BYTES], Avatar &out, const sona_add_options *opts = 0)`<br>`Avatar add(const uint8_t manifest[SONA_MANIFEST_BYTES], const sona_add_options &opts = add_defaults())`<br>`Avatar add(const Manifest &m, const sona_add_options &opts = add_defaults())` | Queues an avatar and returns at once.<br>Calls [`sona_avatar_add`](../c/avatars.md#sona_avatar_add). |
| `sona_result add_base64(const char *b64, Avatar &out, const sona_add_options *opts = 0)`<br>`Avatar add_base64(const std::string &b64, const sona_add_options &opts = add_defaults())` | The same, from base64 text of at most 4096 characters, as the web.<br>Calls [`sona_avatar_add_base64`](../c/avatars.md#sona_avatar_add_base64). |
| `sona_result item_colourways(const char *item_id, std::vector<std::array<uint32_t, 3> > &out)` | An item's colourways (0xFFRRGGBB); SONA_PENDING until it has loaded.<br>Calls [`sona_item_colourways`](../c/the-offline-catalogue.md#sona_item_colourways). |
| `sona_result custom_item_pack_mesh(const sona_mesh_item_spec &spec, std::vector<uint8_t> &out, std::string *why = 0)` | A.sonaitem (format 1.1) of your own skinned mesh; SONA_PENDING while the template loads; why gets the refusal.<br>Calls [`sona_custom_item_pack_mesh`](../c/custom-items.md#sona_custom_item_pack_mesh), [`sona_free`](../c/drawing-with-the-sdks-backends.md#sona_free). |
| `sona_result award_unlock(const char *player_token, const char *key, uint32_t &ticket)` | player_token: the signed-in player's access token (Sign in with 360sona).<br>Calls [`sona_award_unlock`](../c/award-items.md#sona_award_unlock). |
| `sona_result update(double dt_seconds, const sona_view *view = 0, sona_frame_stats *out = 0)` | view may be NULL: nothing is culled and every avatar poses.<br>Calls [`sona_update`](../c/the-frame.md#sona_update). |
| `bool poll(Event &out)` | True when an event was written to out, false when there are none; call it until it is false.<br>Calls [`sona_poll_event`](../c/events.md#sona_poll_event). |
| `std::vector<sona_clip_info> clips() const` | One clip by index, 0..<br>Calls [`sona_clip_get`](../c/clips.md#sona_clip_get). |
| `sona_scene_stats stats() const` | The scene statistics.<br>Calls [`sona_stats`](../c/the-frame.md#sona_stats). |
| `sona_result png_encode(const uint8_t *rgba, uint32_t w, uint32_t h, std::vector<uint8_t> &out)` | PNG bytes of an RGBA8 picture, with the web's three tEXt chunks after IHDR: Software "360sona", Source "https://360sona.com", Comment "Powered by 360sona".<br>Calls [`sona_png_encode`](../c/drawing-with-the-sdks-backends.md#sona_png_encode), [`sona_free`](../c/drawing-with-the-sdks-backends.md#sona_free). |
| `sona_result add_item_preview(const char *item_id, Avatar &out, const sona_preview_options *opts = 0)`<br>`Avatar add_item_preview(const std::string &item_id, const sona_preview_options &opts = preview_defaults())` | One item worn alone on a stand-in body; it reaches READY like an avatar and Avatar::remove frees it.<br>Calls [`sona_item_preview_add`](../c/item-previews.md#sona_item_preview_add). |
| `sona_lod_stats lod_stats() const` | The level-of-detail statistics: avatars in view at each step, those fading, the baked looks and the bakes of the last draw.<br>Calls [`sona_lod_stats_get`](../c/crowds-level-of-detail-and-sprites.md#sona_lod_stats_get). |

## Renderer

`class Renderer`: Owns a sona_renderer; move-only.

| Declaration | What it does |
|---|---|
| `Renderer()` | Owns a sona_renderer; move-only. |
| `Renderer &operator=(Renderer &&o) noexcept` | Moves the handle from another object, which is left empty. |
| `sona_result create(Context &ctx, const sona_device_desc &dev, Renderer &out)` | SONA_PENDING until the shading data has arrived through Context::update.<br>Calls [`sona_renderer_create`](../c/drawing-with-the-sdks-backends.md#sona_renderer_create). |
| `void destroy()` | Frees the renderer and its GPU objects.<br>Calls [`sona_renderer_destroy`](../c/drawing-with-the-sdks-backends.md#sona_renderer_destroy). |
| `sona_result draw(const sona_view &view, const sona_target &target, const sona_draw_options *opts = 0)` | Draws every visible ready avatar, as posed by the last sona_update, folding each palette with this view.<br>Calls [`sona_draw`](../c/drawing-with-the-sdks-backends.md#sona_draw). |
| `sona_result snapshot(const sona_view &view, uint32_t w, uint32_t h, std::vector<uint8_t> &rgba, const sona_draw_options *opts = 0)` | Straight RGBA8, row 0 at the top.<br>Calls [`sona_snapshot`](../c/drawing-with-the-sdks-backends.md#sona_snapshot). |
| `sona_result preview_draw(const Avatar &preview, const sona_target &target, const sona_view *view = 0, const sona_draw_options *opts = 0)` | Only this preview in the frame.<br>Calls [`sona_item_preview_draw`](../c/item-previews.md#sona_item_preview_draw). |
| `sona_result preview_snapshot(const Avatar &preview, uint32_t w, uint32_t h, std::vector<uint8_t> &rgba, const sona_draw_options *opts = 0)` | sona_snapshot of this preview alone, framed for width x height.<br>Calls [`sona_item_preview_snapshot`](../c/item-previews.md#sona_item_preview_snapshot). |

## Catalogue

`class Catalogue`: Every item id with its name, slots, game and source, from sona-catalogue.bin, so pickers need no web call. The C type sona_catalogue.

| Declaration | What it does |
|---|---|
| `Catalogue()` | Every item id with its name, slots, game and source, from sona-catalogue.bin, so pickers need no web call. The C type sona_catalogue. |
| `Catalogue &operator=(Catalogue &&o) noexcept` | Moves the handle from another object, which is left empty. |
| `sona_result load(const std::string &path, Catalogue &out, const Context *ctx = 0)` | ctx may be NULL (then malloc, and no message beyond the code).<br>Calls [`sona_catalogue_load`](../c/the-offline-catalogue.md#sona_catalogue_load). |
| `sona_result load_memory(const void *bytes, size_t len, Catalogue &out, const Context *ctx = 0)` | The same from bytes in memory.<br>Calls [`sona_catalogue_load_memory`](../c/the-offline-catalogue.md#sona_catalogue_load_memory). |
| `void free()` | Frees it and every string it handed out.<br>Calls [`sona_catalogue_free`](../c/the-offline-catalogue.md#sona_catalogue_free). |
| `sona_result search(const sona_catalogue_query *q, std::vector<uint32_t> &out, uint32_t max, uint32_t *total = 0) const` | Up to max matching indexes in id order, after q->offset; total (may be NULL) is every match.<br>Calls [`sona_catalogue_search`](../c/the-offline-catalogue.md#sona_catalogue_search). |
| `sona_result game(uint32_t index, const char *&name, uint32_t &items) const` | One game, sorted by name, and its item count.<br>Calls [`sona_catalogue_game`](../c/the-offline-catalogue.md#sona_catalogue_game). |
| `sona_result item_name(uint32_t index, const char *lang, const char *&name) const` | SONA_E_NOT_FOUND when the item has no name in lang (show sona_item_info.name); not thrown.<br>Calls [`sona_catalogue_item_name`](../c/the-offline-catalogue.md#sona_catalogue_item_name). |

## Types

| Declaration | What it does |
|---|---|
| `class Event` | The C type sona_event. |
