Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
SpineRuntime.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
30#pragma once
31
32#include <cstdint>
33#include <memory>
34#include <string>
35
36#include "./SkeletalModule.hpp"
37
38// The namespace this release's backend lives in. A web side module carries one
39// release and keeps the plain name; a native host carries every release, so its
40// build compiles each backend with its own (spine38, spine42, …), the same way
41// each spine-c release gets its own C names (native/spine/SpineCRename*.hpp).
42#ifndef ES_SPINE_NS
43#define ES_SPINE_NS spine
44#endif
45
46namespace es::ES_SPINE_NS {
47
49struct Skeleton;
50
52struct Instance;
53
54void destroy(Skeleton* skeleton);
55void destroy(Instance* instance);
56
59 void operator()(Skeleton* p) const { destroy(p); }
60};
62 void operator()(Instance* p) const { destroy(p); }
63};
64using SkeletonPtr = std::unique_ptr<Skeleton, SkeletonDeleter>;
65using InstancePtr = std::unique_ptr<Instance, InstanceDeleter>;
66
68
76 float rotate = 0.0f, x = 0.0f, y = 0.0f;
77 float scaleX = 0.0f, scaleY = 0.0f, shearY = 0.0f;
78};
79
80struct PathMix {
81 float position = 0.0f, spacing = 0.0f;
82 float rotate = 0.0f, x = 0.0f, y = 0.0f;
83};
84
91enum class EventKind {
92 Start = 0,
94 End = 2,
97 Event = 5,
98};
99
100struct Event {
101 const void* source = nullptr;
103 int track = 0;
104 float floatValue = 0.0f;
105 int intValue = 0;
107 const char* animation = nullptr;
108 const char* name = nullptr;
109 const char* stringValue = nullptr;
110};
111
113using EventSink = void (*)(const Event&);
114
115const void* eventSource(Instance* instance);
116
132
154inline bool blendHasPremultipliedTwin(int slotBlend) {
155 return slotBlend == 0 || slotBlend == 1; // normal, additive
156}
157
159inline int blendForPage(int slotBlend, bool pagePremultiplied) {
160 return pagePremultiplied && blendHasPremultipliedTwin(slotBlend) ? slotBlend + 4 : slotBlend;
161}
162
165inline void premultiplyTint(float rgba[4], bool pagePremultiplied) {
166 if (!pagePremultiplied) return;
167 rgba[0] *= rgba[3];
168 rgba[1] *= rgba[3];
169 rgba[2] *= rgba[3];
170}
171
172// --- resources -------------------------------------------------------------
173
175Skeleton* loadSkeleton(const void* skeletonData, int length,
176 const char* atlasText, int atlasLength,
177 bool binary, std::string& error);
178
179int atlasPageCount(const Skeleton* skeleton);
180const char* atlasPageName(const Skeleton* skeleton, int page);
181void setAtlasPageTexture(Skeleton* skeleton, int page, uint32_t texture, int width, int height);
182
183void setDefaultMix(Skeleton* skeleton, float seconds);
184void setMixDuration(Skeleton* skeleton, const char* from, const char* to, float seconds);
185
186// --- instances -------------------------------------------------------------
187
188Instance* createInstance(Skeleton* skeleton);
194void advanceAndApply(Instance* instance, float dt);
195
202void materializeWorldPose(Instance* instance, float dt);
203
205void update(Instance* instance, float dt);
206
213bool requiresContinuousWorldPose(const Skeleton* skeleton);
214
218float animationDuration(const Skeleton* skeleton, const char* animation);
219
227void setToSetupPose(Instance* instance);
228
229bool playAnimation(Instance* instance, const char* animation, bool loop, int track);
230bool addAnimation(Instance* instance, const char* animation, bool loop, float delay, int track);
232void setSkin(Instance* instance, const char* skin);
233void setTrackAlpha(Instance* instance, int track, float alpha);
234
235void setEventSink(EventSink sink);
236void enableEvents(Instance* instance);
237
238// --- queries ---------------------------------------------------------------
239
240int animationCount(const Instance* instance);
241const char* animationName(const Instance* instance, int index);
242int skinCount(const Instance* instance);
243const char* skinName(const Instance* instance, int index);
244
245bool bonePosition(const Instance* instance, const char* bone, float* x, float* y);
246bool boneRotation(const Instance* instance, const char* bone, float* degrees);
247
253int boneCount(const Instance* instance);
254const char* boneName(const Instance* instance, int index);
255int boneParent(const Instance* instance, int index);
256
261bool bonePose(const Instance* instance, int index, float* x, float* y, float* degrees, float* length);
262void bounds(const Instance* instance, float* x, float* y, float* width, float* height);
263
270bool skeletonBounds(const Skeleton* skeleton, float* x, float* y, float* width, float* height);
271
272int constraintCount(const Instance* instance, ConstraintKind kind);
273const char* constraintName(const Instance* instance, ConstraintKind kind, int index);
274bool transformMix(const Instance* instance, const char* name, TransformMix* out);
275bool setTransformMix(Instance* instance, const char* name, const TransformMix& mix);
276bool pathMix(const Instance* instance, const char* name, PathMix* out);
277bool setPathMix(Instance* instance, const char* name, const PathMix& mix);
278
279// --- posing ----------------------------------------------------------------
280
281bool setAttachment(Instance* instance, const char* slot, const char* attachment);
282bool setIkTarget(Instance* instance, const char* constraint, float x, float y, float mix);
283bool setSlotColor(Instance* instance, const char* slot, float r, float g, float b, float a);
284bool setSkeletonColor(Instance* instance, float r, float g, float b, float a);
285
286// --- geometry --------------------------------------------------------------
287
289void render(Instance* instance, TriangleSink& sink, bool clipping);
290
303
306 std::uint32_t slots;
307 std::uint32_t regionAttachments;
308 std::uint32_t meshAttachments;
309 std::uint32_t clipStarts;
310 std::uint32_t clippedEmits;
311 std::uint32_t verticesGenerated;
312 std::uint32_t verticesEmitted;
313 std::uint32_t indicesEmitted;
314 std::uint32_t emits;
318 std::uint32_t clipPolygons;
319 std::uint32_t clipPolygonVertices;
322 std::uint32_t clipPolygonEdges;
323 std::uint32_t clipInputTriangles;
324 std::uint32_t clipOutputTriangles;
327 std::uint32_t clipBoundsRejects;
328 std::uint32_t clipInsideAccepts;
329
338 std::uint32_t clipEdgeWork;
339 std::uint32_t clipInputVertices;
340 std::uint32_t clipOutputVertices;
344 std::uint32_t clipConvexBypasses;
345 std::uint32_t clipDecompositions;
346};
347
351 std::uint32_t polygon;
352 std::uint32_t output;
353 std::uint32_t vertices;
354 std::uint32_t uvs;
355 std::uint32_t triangles;
356 std::uint32_t scratch;
357};
358
360bool clipStorage(ClipStorage* out);
361
374
377 std::uint32_t rawVertices;
379 std::uint32_t pieces;
380 std::uint32_t effectiveEdges;
383 std::uint32_t triangulatorScratch;
384 float minX, minY, maxX, maxY;
385};
386
391bool clipStartStage(Instance* instance, int stage, ClipStartCounts* counts);
392
398bool renderCounted(Instance* instance, TriangleSink& sink, bool clipping, ProbeCounts* counts);
399
406bool renderStage(Instance* instance, TriangleSink& sink, bool clipping,
407 int stage, ProbeCounts* counts);
408
419
422 std::uint32_t tracks;
423 std::uint32_t entries;
424 std::uint32_t timelines;
425 std::uint32_t bones;
426 std::uint32_t ikConstraints;
427 std::uint32_t transformConstraints;
428 std::uint32_t pathConstraints;
429 std::uint32_t physicsConstraints;
430 std::uint32_t events;
431};
432
438bool poseStage(Instance* instance, float dt, int stage, PoseCounts* counts);
439
441int version();
442
443} // namespace es::ES_SPINE_NS
What every 2D skeletal-animation side module needs, none of which is about any particular one.
bool renderCounted(Instance *instance, TriangleSink &sink, bool clipping, ProbeCounts *counts)
void(*)(const Event &) EventSink
The module is a singleton, so its collector is process-wide rather than per instance.
const void * eventSource(Instance *instance)
void premultiplyTint(float rgba[4], bool pagePremultiplied)
bool clipStorage(ClipStorage *out)
@ CLIP_START_RESET
The scratch sized for the polygon.
@ CLIP_START_PUBLISH
…and this module's bounds and edges captured.
@ CLIP_START_WORLD
…and its world vertices computed.
@ CLIP_START_WINDING
…made clockwise.
@ CLIP_START_TRIANGULATE
…triangulated.
@ CLIP_START_DECOMPOSE
…decomposed into convex pieces.
@ CLIP_START_PIECES
…each piece wound and closed. spine ends here.
int blendForPage(int slotBlend, bool pagePremultiplied)
The blend code to emit for a slot's mode on a page of the given kind (see above).
bool blendHasPremultipliedTwin(int slotBlend)
es::skeletal::TriangleSink TriangleSink
bool poseStage(Instance *instance, float dt, int stage, PoseCounts *counts)
@ POSE_SETUP
The instance lookup; nothing advances.
@ POSE_WORLD
…and resolve world transforms and constraints — a whole pose.
@ POSE_APPLY
…and evaluate their timelines onto the bones.
@ POSE_ADVANCE
Advance the tracks: time, mixing, queued entries.
std::unique_ptr< Skeleton, SkeletonDeleter > SkeletonPtr
void destroy(Skeleton *skeleton)
@ STAGE_EMIT
…and hand them to the sink. The full extraction.
@ STAGE_VERTICES
…and compute world vertices and tint.
@ STAGE_TRAVERSE
Walk the draw order, resolving what each slot holds.
@ STAGE_CLIP
…and cut the triangles against those pieces.
@ STAGE_SETUP
The instance and the clipper; the loop does not run.
bool clipStartStage(Instance *instance, int stage, ClipStartCounts *counts)
std::unique_ptr< Instance, InstanceDeleter > InstancePtr
bool renderStage(Instance *instance, TriangleSink &sink, bool clipping, int stage, ProbeCounts *counts)
Where a runtime pours the triangles it posed.
std::uint32_t triangulationTriangles
const char * name
const void * source
const char * stringValue
const char * animation
Borrowed from the runtime's own data — valid until the skeleton unloads.
void operator()(Instance *p) const
std::uint32_t pathConstraints
std::uint32_t entries
Track entries applied, mixingFrom chains included.
std::uint32_t transformConstraints
std::uint32_t physicsConstraints
std::uint32_t ikConstraints
std::uint32_t indicesEmitted
std::uint32_t clipInputTriangles
std::uint32_t regionAttachments
std::uint32_t clipCandidateTriangles
std::uint32_t verticesGenerated
std::uint32_t clipOutputVertices
std::uint32_t clipRejectedTriangles
std::uint32_t meshAttachments
std::uint32_t clipPolygonVertices
std::uint32_t clipPolygonEdges
std::uint32_t clipBypassedTriangles
std::uint32_t clipConvexBypasses
std::uint32_t clipInsideAccepts
std::uint32_t clipDecompositions
std::uint32_t verticesEmitted
std::uint32_t clipOutputTriangles
std::uint32_t clipBoundsRejects
std::uint32_t clipInputVertices
Owning handles, so every error path that bails mid-construct still frees.
void operator()(Skeleton *p) const