Repository navigation
feat: add atlases, sprites, animations and a batch drawn into a Canvas - #16
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4
Summary
Sprites and atlases for the 2D stage of nsgo-dev/nsgo#1047, drawn into a
core.Canvaswith NSGo's public API only.Atlas: an image (core.ImageSource), itsFramesby name and itsAnimationsby name.LoadAtlas(img, data)reads TexturePacker's "JSON (Hash)" and "JSON (Array)" and Aseprite's json-hash and json-array exports withencoding/json. Rectangles are divided bymeta.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 anAtlasliteral, so there is no YAML loader.Animations: each frame takes the export's duration, in the tag's direction (forward,reverse,pingpong,pingpong_reverse). A tag without arepeatloops; 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) andLoop;Update(dt)advances it with aLoop's fixed steps, and a long step skips whole rounds.Frame,Index,DoneandResetread 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),AlphaandZ.NewSprite(f)returns one at full scale and opacity.Batch{Atlas, Sprites}.Draw(d)sorts the sprites byZ(stable, so equal Z keeps list order, and it does not reorderSprites) and makes oneDrawImageFromcall per sprite. A sprite that isn't rotated or mirrored draws as a moved and scaled rectangle with no transform. Otherwise it draws under oneTransformbetweenSaveandRestore, and alpha is set inside the sameSave. A later change swaps this for Canvas: measure many sprites per frame, and add a batched DrawAtlas nsgo#1053'sDrawAtlas.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:nsgotestas hand-writtenDrawImageFromcalls. 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 cameraTranslate.game.Loopat 100 Hz.ubuild mise exec [email protected] -- go tool nsgo check -target macos,windows,linux,android,webrbuild mise exec [email protected] -- go tool nsgo check -target ios,ios_simulatorgofmt -l .is clean--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