# Get started with C / C++

A static library and one header. You give it a window (or a layer, or a surface) and call two functions a frame; it renders through **DirectX 12 on Windows, Metal on macOS and Vulkan on Linux**.

> [!TIP]
>
> **You do not need to know what wgpu is**
>
> wgpu is how one library covers three graphics APIs, and that is the whole of its relevance here: **no wgpu type crosses this ABI**, and none appears in the header. What you hand over is a platform window handle, and what comes back is `SS_OK` or an error code.
>
> One consequence: this face **owns its own graphics device**, built on the window you name. It does not draw into a `ID3D12CommandList` or `MTLCommandBuffer` of yours. Sharing an engine's device through a C ABI is a [deliberate non-goal](<https://cri-middleware.github.io/SSPlayerForWgpu/limitations/index.md#no-device-sharing-across-the-c-abi>), and a scene draws only to the window it was created on, not into a texture for your engine to composite.

## What you get

|  |  |
| --- | --- |
| `include/ssplayer_wgpu.h` | one header, C99, no dependencies. Generated by cbindgen |
| `lib/libssplayer_wgpu.a` (`ssplayer_wgpu.lib` on MSVC) | the static library |
| `lib/libssplayer_wgpu.dylib` (macOS only) | the shared library, signed and notarized, for a host that loads the library rather than linking it |
| `lib/VERSION` | the SpriteStudio-SDK version the library links, written as its release tag — what `ss_sdk_version()` answers |
| `LICENSE.md` | BSD-3-Clause |
| `LICENSE.ja.md` | a reference translation of it |
| `THIRD_PARTY_NOTICES.md` | the licences of the Rust crates compiled into the library — what to carry into your acknowledgements |
| `README.md`, `docs/` | this documentation as Markdown, for the release the zip came from, readable offline |

## Install

**Released**

Each [`ssplayer-wgpu` Release](<https://github.com/cri-middleware/SSPlayerForWgpu/releases?expanded=true>) carries one zip per platform under **Assets**, each holding `include/`, `lib/`, `LICENSE.md`, `LICENSE.ja.md`, `THIRD_PARTY_NOTICES.md`, `README.md` and `docs/`:

| Zip | For |
| --- | --- |
| `ssplayer-wgpu-windows-x86_64.zip` | Windows, MSVC (DX12) |
| `ssplayer-wgpu-macos-universal.zip` | macOS, arm64 + x86\_64 in one library (Metal) |

**Link the static library** — it goes into your own binary, and there is no runtime dependency to ship beside it. The macOS zip also carries `lib/libssplayer_wgpu.dylib`, for a host that loads the library instead, such as an FFI from Python, .NET or Dart. It is signed with a Developer ID and notarized, so macOS loads it straight out of a download — Gatekeeper looks its ticket up online the first time — and its install name is `@rpath/libssplayer_wgpu.dylib`. For a DLL on Windows, build from source; `build.ps1` produces one. On Linux, build from source as well: no release zip carries a Linux library.

The Windows library links the C runtime as a DLL (`/MD`), which is what CMake and Visual Studio default to. A host built with the static runtime (`/MT`) builds the library from source, with the static runtime switched on — see *From source*.

**From source**

Needs a Rust toolchain (1.100+); nothing else. `cargo` also regenerates the header through cbindgen in the same pass, so the header always matches the library you just built.

```bash
git clone --recursive https://github.com/cri-middleware/SSPlayerForWgpu.git
cd SSPlayerForWgpu
cargo build --release -p ssplayer-wgpu-ffi
```

| Where | What |
| --- | --- |
| `target/release/` | `libssplayer_wgpu.a` **and** `libssplayer_wgpu.dylib` / `.so`; on MSVC `ssplayer_wgpu.lib`, `ssplayer_wgpu.dll` and its import library `ssplayer_wgpu.dll.lib` |
| `libs/ssplayer-wgpu-ffi/include/ssplayer_wgpu.h` | the header, regenerated by this build |

`./scripts/build.sh` (`build.ps1` on Windows) does the same and collects the result into `bin/` — `bin/include/`, `bin/libs/<platform>/<arch>/`, and the license files with `THIRD_PARTY_NOTICES.md` for your acknowledgements — which is the layout the release zips are made from. Add `arch=universal` on macOS for a two-slice library.

For a Windows host built with the static C runtime (`/MT`), build the library with it too:

```powershell
$env:RUSTFLAGS = "-C target-feature=+crt-static"
cargo build --release -p ssplayer-wgpu-ffi
```

## Linking

The library is Rust, so a **static** link needs the platform libraries the Rust toolchain would have named for you. Ask it, on the platform you are building for:

```bash
cargo rustc -p ssplayer-wgpu-ffi --release --crate-type staticlib -- --print native-static-libs
```

On macOS that answers `-lSystem -framework QuartzCore -framework CoreGraphics -liconv -framework Metal -framework Foundation -framework CoreFoundation -lobjc` (the rest of its output repeats these), to which you add whatever your own window code needs (`-framework Cocoa`). On Windows it answers `opengl32.lib kernel32.lib ntdll.lib userenv.lib ws2_32.lib dbghelp.lib /defaultlib:msvcrt`, which you link beside the release's `ssplayer_wgpu.lib`. A CMake project on macOS, where `SSPLAYER_DIR` is the folder you unzipped the release into:

```cmake
cmake_minimum_required(VERSION 3.20)
project(myapp LANGUAGES OBJCXX)               # the window code is AppKit, so Objective-C++
add_executable(myapp src/main.mm)
target_include_directories(myapp PRIVATE "${SSPLAYER_DIR}/include")
target_link_libraries(myapp PRIVATE
    "${SSPLAYER_DIR}/lib/libssplayer_wgpu.a"
    "-framework Cocoa" "-framework QuartzCore" "-framework CoreGraphics" "-framework Metal"
    "-framework Foundation" "-framework CoreFoundation" objc iconv)
```

The three sample viewers in this repository take the *from source* route and link the dynamic library instead, which needs none of this — read [`examples/viewer-metal-cpp/CMakeLists.txt`](<https://github.com/cri-middleware/SSPlayerForWgpu/blob/main/examples/viewer-metal-cpp/CMakeLists.txt>) for that shape.

Each viewer — Metal on macOS, DX12 on Windows, Vulkan on Linux — is self-contained enough to copy out whole. Inside this repository its CMake runs `cargo build -p ssplayer-wgpu-ffi` and links what that leaves in `target/<profile>/`, with the header from `libs/ssplayer-wgpu-ffi/include/`. A copied viewer drops that cargo step and points the two paths at what you have instead: `bin/libs/<platform>/<arch>/` and `bin/include/` from `scripts/build.sh`, or `lib/` and `include/` from a release zip. The macOS shared library is named `@rpath/libssplayer_wgpu.dylib`, so give the executable an rpath to the folder that holds it (`BUILD_RPATH` in CMake).

## Naming your window

`ss_window_handle_t` is the platform window, flattened for the ABI. Fill in the fields your `kind` uses and leave the rest null.

| `kind` | `window` | `display` |
| --- | --- | --- |
| `SS_WINDOW_WIN32` | `HWND` | `HINSTANCE` (may be null) |
| `SS_WINDOW_APPKIT` | `NSView *` | — |
| `SS_WINDOW_METAL_LAYER` | `CAMetalLayer *` | — |
| `SS_WINDOW_XLIB` | the `Window` XID, cast to a pointer | `Display *` (and `screen`) |
| `SS_WINDOW_WAYLAND` | `wl_surface *` | `wl_display *` |

The window — and its layer, on macOS — must outlive the scene.

> [!WARNING]
>
> **Under a Wayland compositor, an X11 window is still X11**
>
> Xwayland means a toolkit that opened an X11 window keeps `SS_WINDOW_XLIB`, whatever the desktop session is. Pick the kind by what you actually created, not by what the compositor is.

## A whole program

The samples load a `.ssab` converted ahead of time: [Preparing assets](<https://cri-middleware.github.io/SSPlayerForWgpu/start/assets/index.md>) shows how to make one from a `.sspj`, and the official sample data needs no project of your own.

```c
#include <stdio.h>
#include <ssplayer_wgpu.h>

int main(void) {
    void *my_layer = /* your CAMetalLayer* */ NULL;
    float delta_seconds = 1.0f / 60.0f;
    uint32_t width = 960, height = 540;

    ss_scene_desc_t desc = {
        .window      = { .kind = SS_WINDOW_METAL_LAYER, .window = my_layer },
        .backend     = SS_BACKEND_AUTO,       // or SS_BACKEND_DX12 / _METAL / _VULKAN
        .width       = 960, .height = 540,    // pixels, not points
        .clear_color = { 0.1f, 0.1f, 0.12f, 1.0f },
        .enable_mask = true,                  // allocate a depth/stencil so mask parts clip
        .y_down      = false,
    };

    ss_scene_t *scene = ss_scene_create(&desc);
    if (!scene) { fprintf(stderr, "%s\n", ss_last_error()); return 1; }

    ss_player_t *player = ss_scene_create_player(scene);
    if (ss_player_load(player, "assets/overall/Basic.ssab") != SS_OK) {
        fprintf(stderr, "%s\n", ss_last_error());
    }
    ss_player_set_animation(player, "anime_1");
    ss_placement_t placement;
    ss_placement_default(&placement);       // contain, centred; never zero-initialise it
    ss_player_fit_into(player, (float)width, (float)height, &placement, NULL);   // the whole canvas in view
    ss_player_play(player);

    // ... every frame, in this order:
    ss_scene_update(scene, delta_seconds);   // advance every player, build geometry
    ss_scene_render(scene);                  // one pass, the whole cast, then present

    // ... on a window resize (drawable pixels):
    ss_scene_resize(scene, width, height);
    ss_player_fit_into(player, (float)width, (float)height, &placement, NULL);

    // ... at the end. Destroying the scene destroys its players.
    ss_scene_destroy(scene);
    return 0;
}
```

`ss_player_fit_into` puts the animation's whole canvas in the window, and is called again on a resize. Without it the player draws at authored scale, which is what a game that places its characters itself wants; see [Fitting into a box](<https://cri-middleware.github.io/SSPlayerForWgpu/guide/placement/index.md>).

The example is C99. C++ before C++20 — MSVC's default is C++14 — has no designated initializers: zero the struct (`ss_scene_desc_t desc{};`) and set its fields one by one.

That is the entire per-frame API: **`ss_scene_update` then `ss_scene_render`**. One scene is one window; one player is one character; a scene draws every player it owns in a single pass, in ascending `ss_player_set_order`.

Two characters in one window is `ss_scene_create_player` twice — they share one device, one set of pipelines and one atlas upload. That handle split is why the two are separate types.

## Errors

Functions that can fail return `int32_t`: `SS_OK` (0), `SS_ERROR_INVALID_ARGUMENT` (-1), `SS_ERROR_GPU` (-2), or the runtime's own positive code when it refuses a request; test against `SS_OK`. `ss_last_error()` returns a human-readable message for the last failure on the calling thread — for logging, not for branching.

A load reports `SS_OK` as long as the `.ssab` itself was readable. **Anything else that did not arrive comes back from `ss_player_missing_count` / `ss_player_missing_at`** rather than as an error; see [Loading and assets](<https://cri-middleware.github.io/SSPlayerForWgpu/guide/loading/index.md>).

## Run the samples

Three hosts, one per platform, each self-contained and driving this exact API:

```bash
cd examples/viewer-metal-cpp   && cmake -B build && cmake --build build && ./build/ssviewer-metal
cd examples/viewer-dx12-cpp    # cmake -B build; cmake --build build --config Release
cd examples/viewer-vulkan-cpp  && cmake -B build && cmake --build build && ./build/ssviewer-vulkan
```

They run `cargo` themselves, so there is no separate build step — and `./scripts/deploy-examples.sh` once, first, for the animation data.

## Next

- [C API reference](<https://cri-middleware.github.io/SSPlayerForWgpu/reference/c/index.md>) — every entry point, grouped
- [Playback and transport](<https://cri-middleware.github.io/SSPlayerForWgpu/guide/playback/index.md>) — `play` vs `resume`, loops, sections
- [Loading and assets](<https://cri-middleware.github.io/SSPlayerForWgpu/guide/loading/index.md>) — supplying textures yourself, and what `missing` means
