Skip to content

feat: add atlases, sprites, animations and a batch drawn into a Canvas - #16

Merged
houko merged 1 commit into
mainfrom
feat/issue-4-sprites
Nov 21, 2026
Merged

houko merged 1 commit into
mainfrom
feat/issue-4-sprites

Conversation

@houko

@houko houko commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Fixes #4

Summary

Sprites and atlases for the 2D stage of nsgo-dev/nsgo#1047, drawn into a core.Canvas with NSGo's public API only.

  • Atlas: an image (core.ImageSource), its Frames by name and its Animations by name. LoadAtlas(img, data) reads TexturePacker's "JSON (Hash)" and "JSON (Array)" and Aseprite's json-hash and json-array exports with encoding/json. Rectangles are divided by meta.scale, so a 2x sheet's frames come out in the image's points. Trimmed frames keep their offset and untrimmed size. Rotated frames are refused with an error asking for an export with rotation off. A hand-written table is an Atlas literal, so there is no YAML loader.
  • Aseprite frame tags become Animations: each frame takes the export's duration, in the tag's direction (forward, reverse, pingpong, pingpong_reverse). A tag without a repeat loops; a tag with one plays that many times and stops. Ping-pong counts each one-way pass as one repeat and never shows the frame it turns on twice, which matches Aseprite's exporter (src/app/doc_exporter.cpp) and playback (src/doc/playback.cpp).
  • Animation: Frames (frame plus duration) and Loop; Update(dt) advances it with a Loop's fixed steps, and a long step skips whole rounds. Frame, Index, Done and Reset read and restart it. It is a value that keeps its own position, so each sprite plays its own copy of an atlas's animation.
  • Sprite: Frame, X/Y, OriginX/OriginY, Rotation, ScaleX/ScaleY, FlipX/FlipY (which mirror the picture within its untrimmed box), Alpha and Z. NewSprite(f) returns one at full scale and opacity.
  • Batch{Atlas, Sprites}.Draw(d) sorts the sprites by Z (stable, so equal Z keeps list order, and it does not reorder Sprites) and makes one DrawImageFrom call per sprite. A sprite that isn't rotated or mirrored draws as a moved and scaled rectangle with no transform. Otherwise it draws under one Transform between Save and Restore, and alpha is set inside the same Save. A later change swaps this for Canvas: measure many sprites per frame, and add a batched DrawAtlas nsgo#1053's DrawAtlas.

No tint. Canvas has no transparency layer or offscreen target to recolor a single image in. A composite mode such as multiply would mix with everything already drawn beneath the sprite, so sprites have no tint field rather than a fake one. This can come back once Canvas can tint an image or draw into a layer.

Validation

  • ubuild mise exec [email protected] -- go tool nsgo test ./...: 18 passed. New tests:
    • A batch records the same canvas ops under nsgotest as hand-written DrawImageFrom calls. The cases cover plain, scaled about the center, a quarter turn, flipped and trimmed, faded, Z order, and sprites skipped for alpha 0 or an empty frame, all under a camera Translate.
    • Hand-written Aseprite fixtures in both the hash and array variants load with their four tags as animations (forward, pingpong, reverse ×1, pingpong_reverse ×3), including a trimmed frame.
    • A TexturePacker hash at scale 2 loads in points.
    • Malformed exports are refused.
    • Animations advance, loop, stop, skip whole rounds and stay put with no time or no frames, both by direct steps and inside a game.Loop at 100 Hz.
  • ubuild mise exec [email protected] -- go tool nsgo check -target macos,windows,linux,android,web
  • rbuild mise exec [email protected] -- go tool nsgo check -target ios,ios_simulator
  • gofmt -l . is clean
  • 1000 moving sprites at 60 fps on macOS with --nsgo-fps: needs a display and a real app, so it is left for the example game (Example: a playable sample game that meets the performance targets #8), not claimed here.

Screenshots

None

Atlas is an image and its named frames, loaded from TexturePacker's and Aseprite's JSON (hash and array), with Aseprite's frame tags as animations in their direction and repeat count. Sprite places a frame by position, origin, rotation, scale, flip, alpha and Z; Animation plays frames with their own durations, looping or once, on a Loop's fixed steps; Batch draws an atlas's sprites in Z order with one DrawImageFrom each.

Sprites have no tint: a Canvas has no layer to recolor one image in, and a composite mode would mix with everything drawn under it.
@houko
houko merged commit fdd37d5 into main Nov 21, 2026
4 checks passed
@houko
houko deleted the feat/issue-4-sprites branch November 21, 2026 01:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sprites: atlases, frames, animations and a batch drawn into a Canvas

1 participant