[sfml] Switch to SkeletonRenderer

This commit is contained in:
Mario Zechner 2024-07-01 16:03:56 +02:00
parent d3c8ccbfc1
commit e12b71bb1a
3 changed files with 92 additions and 217 deletions

View File

@ -773,6 +773,7 @@ void test(SkeletonData *skeletonData, Atlas *atlas) {
} }
DebugExtension dbgExtension(SpineExtension::getInstance()); DebugExtension dbgExtension(SpineExtension::getInstance());
extern spine::SkeletonRenderer *skeletonRenderer;
int main() { int main() {
SpineExtension::setInstance(&dbgExtension); SpineExtension::setInstance(&dbgExtension);
@ -794,6 +795,7 @@ int main() {
testcase(goblins, "data/goblins-pro.json", "data/goblins-pro.skel", "data/goblins-pma.atlas", 1.4f); testcase(goblins, "data/goblins-pro.json", "data/goblins-pro.skel", "data/goblins-pma.atlas", 1.4f);
testcase(stretchyman, "data/stretchyman-pro.json", "data/stretchyman-pro.skel", "data/stretchyman-pma.atlas", 0.6f); testcase(stretchyman, "data/stretchyman-pro.json", "data/stretchyman-pro.skel", "data/stretchyman-pma.atlas", 0.6f);
delete skeletonRenderer;
dbgExtension.reportLeaks(); dbgExtension.reportLeaks();
return 0; return 0;
} }

View File

@ -34,211 +34,90 @@
#endif #endif
using namespace sf; using namespace sf;
using namespace spine;
sf::BlendMode normal = sf::BlendMode(sf::BlendMode::SrcAlpha, sf::BlendMode::OneMinusSrcAlpha); sf::BlendMode blendModes[] = {
sf::BlendMode additive = sf::BlendMode(sf::BlendMode::SrcAlpha, sf::BlendMode::One); sf::BlendMode(sf::BlendMode::SrcAlpha, sf::BlendMode::OneMinusSrcAlpha),
sf::BlendMode multiply = sf::BlendMode(sf::BlendMode::DstColor, sf::BlendMode::OneMinusSrcAlpha); sf::BlendMode(sf::BlendMode::SrcAlpha, sf::BlendMode::One),
sf::BlendMode screen = sf::BlendMode(sf::BlendMode::One, sf::BlendMode::OneMinusSrcColor); sf::BlendMode(sf::BlendMode::DstColor, sf::BlendMode::OneMinusSrcAlpha),
sf::BlendMode(sf::BlendMode::One, sf::BlendMode::OneMinusSrcColor)
};
sf::BlendMode normalPma = sf::BlendMode(sf::BlendMode::One, sf::BlendMode::OneMinusSrcAlpha); sf::BlendMode blendModesPma[] = {
sf::BlendMode additivePma = sf::BlendMode(sf::BlendMode::One, sf::BlendMode::One); sf::BlendMode(sf::BlendMode::One, sf::BlendMode::OneMinusSrcAlpha),
sf::BlendMode multiplyPma = sf::BlendMode(sf::BlendMode::DstColor, sf::BlendMode::OneMinusSrcAlpha); sf::BlendMode(sf::BlendMode::One, sf::BlendMode::One),
sf::BlendMode screenPma = sf::BlendMode(sf::BlendMode::One, sf::BlendMode::OneMinusSrcColor); sf::BlendMode(sf::BlendMode::DstColor, sf::BlendMode::OneMinusSrcAlpha),
sf::BlendMode(sf::BlendMode::One, sf::BlendMode::OneMinusSrcColor),
};
namespace spine { SkeletonRenderer *skeletonRenderer = nullptr;
SkeletonDrawable::SkeletonDrawable(SkeletonData *skeletonData, AnimationStateData *stateData) : timeScale(1), SkeletonDrawable::SkeletonDrawable(SkeletonData *skeletonData, AnimationStateData *stateData) : timeScale(1),
vertexArray(new VertexArray(Triangles, skeletonData->getBones().size() * 4)), usePremultipliedAlpha(false),
worldVertices(), clipper(), usePremultipliedAlpha(false) { vertexArray(new VertexArray(Triangles, skeletonData->getBones().size() * 4)) {
Bone::setYDown(true); Bone::setYDown(true);
worldVertices.ensureCapacity(SPINE_MESH_VERTEX_COUNT_MAX);
skeleton = new (__FILE__, __LINE__) Skeleton(skeletonData); skeleton = new (__FILE__, __LINE__) Skeleton(skeletonData);
tempUvs.ensureCapacity(16);
tempColors.ensureCapacity(16);
ownsAnimationStateData = stateData == 0; ownsAnimationStateData = stateData == 0;
if (ownsAnimationStateData) stateData = new (__FILE__, __LINE__) AnimationStateData(skeletonData); if (ownsAnimationStateData) stateData = new (__FILE__, __LINE__) AnimationStateData(skeletonData);
state = new (__FILE__, __LINE__) AnimationState(stateData); state = new (__FILE__, __LINE__) AnimationState(stateData);
}
quadIndices.add(0); SkeletonDrawable::~SkeletonDrawable() {
quadIndices.add(1);
quadIndices.add(2);
quadIndices.add(2);
quadIndices.add(3);
quadIndices.add(0);
}
SkeletonDrawable::~SkeletonDrawable() {
delete vertexArray; delete vertexArray;
if (ownsAnimationStateData) delete state->getData(); if (ownsAnimationStateData) delete state->getData();
delete state; delete state;
delete skeleton; delete skeleton;
} }
void SkeletonDrawable::update(float deltaTime, Physics physics) { void SkeletonDrawable::update(float deltaTime, Physics physics) {
state->update(deltaTime * timeScale); state->update(deltaTime * timeScale);
state->apply(*skeleton); state->apply(*skeleton);
skeleton->update(deltaTime * timeScale); skeleton->update(deltaTime * timeScale);
skeleton->updateWorldTransform(physics); skeleton->updateWorldTransform(physics);
} }
void SkeletonDrawable::draw(RenderTarget &target, RenderStates states) const { inline void toSFMLColor(uint32_t color, sf::Color *sfmlColor) {
vertexArray->clear(); sfmlColor->a = (color >> 24) & 0xFF;
sfmlColor->r = (color >> 16) & 0xFF;
sfmlColor->g = (color >> 8) & 0xFF;
sfmlColor->b = color & 0xFF;
}
void SkeletonDrawable::draw(RenderTarget &target, RenderStates states) const {
states.texture = NULL; states.texture = NULL;
// Early out if skeleton is invisible
if (skeleton->getColor().a == 0) return;
sf::Vertex vertex;
Texture *texture = NULL;
for (unsigned i = 0; i < skeleton->getSlots().size(); ++i) {
Slot &slot = *skeleton->getDrawOrder()[i];
Attachment *attachment = slot.getAttachment();
if (!attachment) {
clipper.clipEnd(slot);
continue;
}
// Early out if the slot color is 0 or the bone is not active
if (slot.getColor().a == 0 || !slot.getBone().isActive()) {
clipper.clipEnd(slot);
continue;
}
Vector<float> *vertices = &worldVertices;
Vector<float> *uvs = NULL;
Vector<unsigned short> *indices = NULL;
int indicesCount = 0;
Color *attachmentColor;
if (attachment->getRTTI().isExactly(RegionAttachment::rtti)) {
RegionAttachment *regionAttachment = (RegionAttachment *) attachment;
attachmentColor = &regionAttachment->getColor();
// Early out if the slot color is 0
if (attachmentColor->a == 0) {
clipper.clipEnd(slot);
continue;
}
worldVertices.setSize(8, 0);
regionAttachment->computeWorldVertices(slot, worldVertices, 0, 2);
uvs = &regionAttachment->getUVs();
indices = &quadIndices;
indicesCount = 6;
texture = (Texture *) ((AtlasRegion *) regionAttachment->getRegion())->page->texture;
} else if (attachment->getRTTI().isExactly(MeshAttachment::rtti)) {
MeshAttachment *mesh = (MeshAttachment *) attachment;
attachmentColor = &mesh->getColor();
// Early out if the slot color is 0
if (attachmentColor->a == 0) {
clipper.clipEnd(slot);
continue;
}
worldVertices.setSize(mesh->getWorldVerticesLength(), 0);
mesh->computeWorldVertices(slot, 0, mesh->getWorldVerticesLength(), worldVertices.buffer(), 0, 2);
uvs = &mesh->getUVs();
indices = &mesh->getTriangles();
indicesCount = mesh->getTriangles().size();
texture = (Texture *) ((AtlasRegion *) mesh->getRegion())->page->texture;
} else if (attachment->getRTTI().isExactly(ClippingAttachment::rtti)) {
ClippingAttachment *clip = (ClippingAttachment *) slot.getAttachment();
clipper.clipStart(slot, clip);
continue;
} else
continue;
Uint8 r = static_cast<Uint8>(skeleton->getColor().r * slot.getColor().r * attachmentColor->r * 255);
Uint8 g = static_cast<Uint8>(skeleton->getColor().g * slot.getColor().g * attachmentColor->g * 255);
Uint8 b = static_cast<Uint8>(skeleton->getColor().b * slot.getColor().b * attachmentColor->b * 255);
Uint8 a = static_cast<Uint8>(skeleton->getColor().a * slot.getColor().a * attachmentColor->a * 255);
vertex.color.r = r;
vertex.color.g = g;
vertex.color.b = b;
vertex.color.a = a;
Color light;
light.r = r / 255.0f;
light.g = g / 255.0f;
light.b = b / 255.0f;
light.a = a / 255.0f;
sf::BlendMode blend;
if (!usePremultipliedAlpha) {
switch (slot.getData().getBlendMode()) {
case BlendMode_Normal:
blend = normal;
break;
case BlendMode_Additive:
blend = additive;
break;
case BlendMode_Multiply:
blend = multiply;
break;
case BlendMode_Screen:
blend = screen;
break;
default:
blend = normal;
}
} else {
switch (slot.getData().getBlendMode()) {
case BlendMode_Normal:
blend = normalPma;
break;
case BlendMode_Additive:
blend = additivePma;
break;
case BlendMode_Multiply:
blend = multiplyPma;
break;
case BlendMode_Screen:
blend = screenPma;
break;
default:
blend = normalPma;
}
}
if (states.texture == 0) states.texture = texture;
if (states.blendMode != blend || states.texture != texture) {
target.draw(*vertexArray, states);
vertexArray->clear(); vertexArray->clear();
states.blendMode = blend;
states.texture = texture;
}
if (clipper.isClipping()) {
clipper.clipTriangles(worldVertices, *indices, *uvs, 2);
vertices = &clipper.getClippedVertices();
uvs = &clipper.getClippedUVs();
indices = &clipper.getClippedTriangles();
indicesCount = clipper.getClippedTriangles().size();
}
if (!skeletonRenderer) skeletonRenderer = new (__FILE__, __LINE__) SkeletonRenderer();
RenderCommand *command = skeletonRenderer->render(*skeleton);
while (command) {
Texture *texture = (Texture *)command->texture;
Vector2u size = texture->getSize(); Vector2u size = texture->getSize();
Vertex vertex;
for (int ii = 0; ii < indicesCount; ++ii) { float *positions = command->positions;
int index = (*indices)[ii] << 1; float *uvs = command->uvs;
vertex.position.x = (*vertices)[index]; uint32_t *colors = command->colors;
vertex.position.y = (*vertices)[index + 1]; uint16_t *indices = command->indices;
vertex.texCoords.x = (*uvs)[index] * size.x; for (int i = 0, n = command->numIndices; i < n; ++i) {
vertex.texCoords.y = (*uvs)[index + 1] * size.y; int ii = indices[i];
int index = ii << 1;
vertex.position.x = positions[index];
vertex.position.y = positions[index + 1];
vertex.texCoords.x = uvs[index] * size.x;
vertex.texCoords.y = uvs[index + 1] * size.y;
toSFMLColor(colors[ii], &vertex.color);
vertexArray->append(vertex); vertexArray->append(vertex);
} }
clipper.clipEnd(slot); BlendMode blendMode = command->blendMode;
} states.blendMode = usePremultipliedAlpha ? blendModesPma[blendMode] : blendModes[blendMode];
states.texture = texture;
target.draw(*vertexArray, states); target.draw(*vertexArray, states);
clipper.clipEnd(); vertexArray->clear();
}
void SFMLTextureLoader::load(AtlasPage &page, const String &path) { command = command->next;
}
}
void SFMLTextureLoader::load(AtlasPage &page, const String &path) {
Texture *texture = new Texture(); Texture *texture = new Texture();
if (!texture->loadFromFile(path.buffer())) return; if (!texture->loadFromFile(path.buffer())) return;
@ -249,13 +128,12 @@ namespace spine {
Vector2u size = texture->getSize(); Vector2u size = texture->getSize();
page.width = size.x; page.width = size.x;
page.height = size.y; page.height = size.y;
} }
void SFMLTextureLoader::unload(void *texture) { void SFMLTextureLoader::unload(void *texture) {
delete (Texture *) texture; delete (Texture *) texture;
} }
SpineExtension *getDefaultExtension() { SpineExtension *spine::getDefaultExtension() {
return new DefaultSpineExtension(); return new DefaultSpineExtension();
} }
}// namespace spine

View File

@ -45,7 +45,6 @@ namespace spine {
Skeleton *skeleton; Skeleton *skeleton;
AnimationState *state; AnimationState *state;
float timeScale; float timeScale;
sf::VertexArray *vertexArray;
SkeletonDrawable(SkeletonData *skeleton, AnimationStateData *stateData = 0); SkeletonDrawable(SkeletonData *skeleton, AnimationStateData *stateData = 0);
@ -61,13 +60,9 @@ namespace spine {
bool getUsePremultipliedAlpha() { return usePremultipliedAlpha; }; bool getUsePremultipliedAlpha() { return usePremultipliedAlpha; };
private: private:
mutable bool ownsAnimationStateData; bool ownsAnimationStateData;
mutable Vector<float> worldVertices;
mutable Vector<float> tempUvs;
mutable Vector<Color> tempColors;
mutable Vector<unsigned short> quadIndices;
mutable SkeletonClipping clipper;
mutable bool usePremultipliedAlpha; mutable bool usePremultipliedAlpha;
sf::VertexArray *vertexArray;
}; };
class SFMLTextureLoader : public TextureLoader { class SFMLTextureLoader : public TextureLoader {