Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 44 additions & 25 deletions Documentation/Contracts/Sprites.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Sprites

**Status:** a design, approved on 2026-12-02. Nothing here is
implemented. What was decided is at the end. `Scenes.md` is the 3D twin and
**Status:** a design, approved on 2026-12-02. swift-sprite's side is
done ([What swift-sprite needs](#what-swift-sprite-needs)); `DynamicSprite`
isn't started. What was decided is at the end. `Scenes.md` is the 3D twin and
shares the rules in [How it runs](#how-it-runs).

Module `DynamicSprite`: 2D games in Embedded Swift, with
Expand Down Expand Up @@ -46,9 +47,10 @@ plays). It has no contract of its own.
points are where AppRuntime plugs in: `SpriteRenderBackend` for
drawing, `TextShaper` and `BitmapFont` for text, `SceneAudioOutput` for
sound. No wrapper layer to keep in step, and one object per node.
- **swift-sprite is nearly Embedded already.** `SpriteCore` and
`SpriteGraph` import only the C library and `SpritePhysics`. What's
left is small ([What swift-sprite needs](#what-swift-sprite-needs)).
- **swift-sprite builds Embedded.** `SpriteMath`, `SpriteCore`,
`Physics2D`, `SpritePhysics` and `SpriteGraph` build with Embedded
Swift for wasm32, Linux and bare metal, checked in its CI
([What swift-sprite needs](#what-swift-sprite-needs)).
- **GL ES 2.0 under WebGL 1 rules** is the one graphics API every host
has, because `Surface` needs it. swift-sprite's renderers are SDL's GPU
renderer and GL 3.3; this adds one over `CGLES2`.
Expand Down Expand Up @@ -269,24 +271,39 @@ swift-scene (`Scenes.md`).
### What swift-sprite needs

Upstream changes, each its own pull request, none of which changes what
swift-sprite does on its current platforms:

1. **Physics in Swift.** `SpritePhysics` moves from the C Box2D to
`Physics2D`, a port of it ([Physics in Swift](#physics-in-swift)).
2. **`SpriteCore`, `SpritePhysics` and `SpriteGraph` build Embedded.** No
`@MainActor` (Embedded has none: `Implementation.md`), typed throws
in place of untyped, no casts to existentials that aren't
class-bound, and `Duration` only where Embedded has it. A CI job builds
them with the wasm Embedded SDK.
3. **No weak references,** which Embedded Swift lacks: children keep an
`unowned(unsafe)` parent, cleared when they're removed.
swift-sprite does on its current platforms. All are done:

1. **Physics in Swift.** `SpritePhysics` moved from the C Box2D to
`Physics2D`, a port of it ([Physics in Swift](#physics-in-swift)):
swift-sprite #61 to #64.
2. **`SpriteCore`, `SpritePhysics` and `SpriteGraph` build Embedded**
(#60, #65, #67), with `SpriteMath`'s elementary functions (#59) in
place of the C library's. `Scripts/build-embedded.sh` builds them, and
CI runs it, for wasm32, Linux and armv7 and x86_64 bare metal (#66,
#68).
- **The main actor:** Embedded Swift has none, so `SpriteGraph` no
longer writes `@MainActor`: it's the module's default isolation
(`.defaultIsolation(MainActor.self)`), which other builds keep and
an Embedded build leaves out. Its public API is the same.
- **No `Regex`:** an Embedded build matches node-name patterns with a
small matcher of the ICU syntax names use, tested against `Regex`.
- **No run-time conformance lookups:** a warp action finds `Sprite`
and `EffectNode` by class.
- `runAndWait`, an `async` method, and action tracing are left out of
an Embedded build.
3. **No weak references,** which Embedded Swift lacks. A node keeps a
lifetime that it marks when it's freed, and its parent, a scene's
listener, an emitter's target and a constraint's reference node read
it, so they turn `nil` as weak ones do. A freed node lets go of its
physics body.
4. **Nothing in `SpriteGraph` needs SDL or FFmpeg.** `AudioNode` and
`VideoNode` already talk through `SceneAudioOutput` and
`SceneVideoOutput`; this keeps it so.
5. **A hook for being shown,** `Scene.didMove(toViewOfSize:)` and
`willMoveFromView()`, called by whatever presents the scene (an SDL
window, a `SpriteView`), with the size it's shown at. It names no
view type, so `SpriteGraph` stays free of any toolkit.
`VideoNode` talk through `SceneAudioOutput` and `SceneVideoOutput`.
5. **A hook for being shown** (#68): `Scene.didMove(toViewOfSize:)` and
`willMoveFromView()`, called through `moveToView(ofSize:)` and
`moveFromView()` by whatever presents the scene (an SDL window, a
`SpriteView`), with the size it's shown at. A `.resizeFill` scene has
that size before it hears. It names no view type, so `SpriteGraph`
stays free of any toolkit.

### Physics in Swift

Expand Down Expand Up @@ -330,9 +347,11 @@ So Box2D is ported to Swift, as `Physics2D`, in swift-sprite:
Once the port matches, `CBox2D` leaves swift-sprite's library targets and
stays only in its tests.

Physics is the largest piece of this design. Until `Physics2D` is done,
`SpriteGraph` builds in a guest without a physics world (bodies are kept
but don't move), so the rest of the work isn't held up.
**Where it stands.** The port steps worlds the same as Box2D 3.2, bit
for bit, in its tests against the C library, which is now only in
swift-sprite's tests. Box2D's 8-wide AVX2 solver isn't ported; the
4-wide one runs on every target. The benchmark, natively and under
WasmKit and wasm2c, is still to be run.

## Not in this design

Expand Down
14 changes: 8 additions & 6 deletions Documentation/Implementation.md
Original file line number Diff line number Diff line change
Expand Up @@ -1899,12 +1899,14 @@ Each of these needs a decision, or is verified only as far as noted:
need it first.
31. Sprites (`Documentation/Contracts/Sprites.md`), approved as a design:
swift-sprite's own API in a guest, its engine running there and
drawing on `Surface`. Upstream first: `SpriteCore`, `SpritePhysics`
and `SpriteGraph` building Embedded, and `Physics2D`, Box2D v3 ported
to Swift and checked against the C library. Then `Packages/Games` with
`DynamicSprite` (`SpriteView`, input in scene space, the GLES 2
backend, `BitmapFont.typeface`, sound through `Audio`), atlases and
tile maps packed by `build-bundle.sh`, and `examples/ExampleSprites`.
drawing on `Surface`. Upstream, done: `SpriteCore`, `SpritePhysics`
and `SpriteGraph` building Embedded, for wasm32 among others, and
`Physics2D`, Box2D v3 ported to Swift and checked against the C
library; `Physics2D`'s benchmark is still to be run. Next:
`Packages/Games` with `DynamicSprite` (`SpriteView`, input in scene
space, the GLES 2 backend, `BitmapFont.typeface`, sound through
`Audio`), atlases and tile maps packed by `build-bundle.sh`, and
`examples/ExampleSprites`.
32. Scenes (`Documentation/Contracts/Scenes.md`), approved as a design:
swift-scene's `SceneGraph` in a guest, the same way. Upstream:
`SceneCore` and `SceneGraph` building Embedded, the renderer moved to
Expand Down
Loading