[sdl] Switch to SkeletonRenderer.

This commit is contained in:
Mario Zechner 2024-07-01 15:09:42 +02:00
parent 9db5f90ccb
commit dcaa4f935e
4 changed files with 75 additions and 150 deletions

View File

@ -39,8 +39,8 @@ namespace spine {
struct SP_API RenderCommand { struct SP_API RenderCommand {
float *positions; float *positions;
float *uvs; float *uvs;
int32_t *colors; uint32_t *colors;
int32_t *darkColors; uint32_t *darkColors;
int32_t numVertices; int32_t numVertices;
uint16_t *indices; uint16_t *indices;
int32_t numIndices; int32_t numIndices;

View File

@ -55,8 +55,8 @@ static RenderCommand *createRenderCommand(BlockAllocator &allocator, int numVert
RenderCommand *cmd = allocator.allocate<RenderCommand>(1); RenderCommand *cmd = allocator.allocate<RenderCommand>(1);
cmd->positions = allocator.allocate<float>(numVertices << 1); cmd->positions = allocator.allocate<float>(numVertices << 1);
cmd->uvs = allocator.allocate<float>(numVertices << 1); cmd->uvs = allocator.allocate<float>(numVertices << 1);
cmd->colors = allocator.allocate<int32_t>(numVertices); cmd->colors = allocator.allocate<uint32_t>(numVertices);
cmd->darkColors = allocator.allocate<int32_t>(numVertices); cmd->darkColors = allocator.allocate<uint32_t>(numVertices);
cmd->numVertices = numVertices; cmd->numVertices = numVertices;
cmd->indices = allocator.allocate<uint16_t>(numIndices); cmd->indices = allocator.allocate<uint16_t>(numIndices);
cmd->numIndices = numIndices; cmd->numIndices = numIndices;
@ -70,8 +70,8 @@ static RenderCommand *batchSubCommands(BlockAllocator &allocator, Vector<RenderC
RenderCommand *batched = createRenderCommand(allocator, numVertices, numIndices, commands[first]->blendMode, commands[first]->texture); RenderCommand *batched = createRenderCommand(allocator, numVertices, numIndices, commands[first]->blendMode, commands[first]->texture);
float *positions = batched->positions; float *positions = batched->positions;
float *uvs = batched->uvs; float *uvs = batched->uvs;
int32_t *colors = batched->colors; uint32_t *colors = batched->colors;
int32_t *darkColors = batched->darkColors; uint32_t *darkColors = batched->darkColors;
uint16_t *indices = batched->indices; uint16_t *indices = batched->indices;
int indicesOffset = 0; int indicesOffset = 0;
for (int i = first; i <= last; i++) { for (int i = first; i <= last; i++) {

View File

@ -36,6 +36,8 @@
using namespace spine; using namespace spine;
SkeletonRenderer skeletonRenderer;
SkeletonDrawable::SkeletonDrawable(SkeletonData *skeletonData, AnimationStateData *animationStateData) { SkeletonDrawable::SkeletonDrawable(SkeletonData *skeletonData, AnimationStateData *animationStateData) {
Bone::setYDown(true); Bone::setYDown(true);
skeleton = new (__FILE__, __LINE__) Skeleton(skeletonData); skeleton = new (__FILE__, __LINE__) Skeleton(skeletonData);
@ -58,151 +60,76 @@ void SkeletonDrawable::update(float delta, Physics physics) {
skeleton->updateWorldTransform(physics); skeleton->updateWorldTransform(physics);
} }
inline void toSDLColor(uint32_t color, SDL_Color *sdlColor) {
sdlColor->r = (color >> 24) & 0xFF;
sdlColor->g = (color >> 16) & 0xFF;
sdlColor->b = (color >> 8) & 0xFF;
sdlColor->a = color & 0xFF;
}
void SkeletonDrawable::draw(SDL_Renderer *renderer) { void SkeletonDrawable::draw(SDL_Renderer *renderer) {
Vector<unsigned short> quadIndices; RenderCommand *command = skeletonRenderer.render(*skeleton);
quadIndices.add(0); while(command) {
quadIndices.add(1); float *positions = command->positions;
quadIndices.add(2); float *uvs = command->uvs;
quadIndices.add(2); uint32_t *colors = command->colors;
quadIndices.add(3); sdlVertices.clear();
quadIndices.add(0); for (int ii = 0; ii < command->numVertices << 1; ii += 2) {
SDL_Texture *texture; SDL_Vertex sdlVertex;
SDL_Vertex sdlVertex; sdlVertex.position.x = positions[ii];
for (unsigned i = 0; i < skeleton->getSlots().size(); ++i) { sdlVertex.position.y = positions[ii + 1];
Slot &slot = *skeleton->getDrawOrder()[i]; sdlVertex.tex_coord.x = uvs[ii];
Attachment *attachment = slot.getAttachment(); sdlVertex.tex_coord.y = uvs[ii + 1];
if (!attachment) { toSDLColor(colors[ii >> 1], &sdlVertex.color);
clipper.clipEnd(slot); sdlVertices.add(sdlVertex);
continue; }
} sdlIndices.clear();
uint16_t *indices = command->indices;
for (int ii = 0; ii < command->numIndices; ii++)
sdlIndices.add(indices[ii]);
// Early out if the slot color is 0 or the bone is not active BlendMode blendMode = command->blendMode;
if (slot.getColor().a == 0 || !slot.getBone().isActive()) { SDL_Texture *texture = (SDL_Texture *)command->texture;
clipper.clipEnd(slot); if (!usePremultipliedAlpha) {
continue; switch (blendMode) {
} case BlendMode_Normal:
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
break;
case BlendMode_Multiply:
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_MOD);
break;
case BlendMode_Additive:
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_ADD);
break;
case BlendMode_Screen:
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
break;
}
} else {
SDL_BlendMode target;
switch (blendMode) {
case BlendMode_Normal:
target = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD, SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD);
SDL_SetTextureBlendMode(texture, target);
break;
case BlendMode_Multiply:
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_MOD);
break;
case BlendMode_Additive:
target = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_ADD, SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_ADD);
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_ADD);
break;
case BlendMode_Screen:
target = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD, SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD);
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
break;
}
}
Vector<float> *vertices = &worldVertices; SDL_RenderGeometry(renderer, texture, sdlVertices.buffer(), sdlVertices.size(), sdlIndices.buffer(),
int verticesCount = 0; command->numIndices);
Vector<float> *uvs = NULL; command = command->next;
Vector<unsigned short> *indices; }
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);
verticesCount = 4;
uvs = &regionAttachment->getUVs();
indices = &quadIndices;
indicesCount = 6;
texture = (SDL_Texture *) regionAttachment->getRegion()->rendererObject;
} 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);
texture = (SDL_Texture *) mesh->getRegion()->rendererObject;
verticesCount = mesh->getWorldVerticesLength() >> 1;
uvs = &mesh->getUVs();
indices = &mesh->getTriangles();
indicesCount = indices->size();
} 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);
sdlVertex.color.r = r;
sdlVertex.color.g = g;
sdlVertex.color.b = b;
sdlVertex.color.a = a;
if (clipper.isClipping()) {
clipper.clipTriangles(worldVertices, *indices, *uvs, 2);
vertices = &clipper.getClippedVertices();
verticesCount = clipper.getClippedVertices().size() >> 1;
uvs = &clipper.getClippedUVs();
indices = &clipper.getClippedTriangles();
indicesCount = clipper.getClippedTriangles().size();
}
sdlVertices.clear();
for (int ii = 0; ii < verticesCount << 1; ii += 2) {
sdlVertex.position.x = (*vertices)[ii];
sdlVertex.position.y = (*vertices)[ii + 1];
sdlVertex.tex_coord.x = (*uvs)[ii];
sdlVertex.tex_coord.y = (*uvs)[ii + 1];
sdlVertices.add(sdlVertex);
}
sdlIndices.clear();
for (int ii = 0; ii < (int) indices->size(); ii++)
sdlIndices.add((*indices)[ii]);
if (!usePremultipliedAlpha) {
switch (slot.getData().getBlendMode()) {
case BlendMode_Normal:
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
break;
case BlendMode_Multiply:
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_MOD);
break;
case BlendMode_Additive:
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_ADD);
break;
case BlendMode_Screen:
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
break;
}
} else {
SDL_BlendMode target;
switch (slot.getData().getBlendMode()) {
case BlendMode_Normal:
target = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD, SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD);
SDL_SetTextureBlendMode(texture, target);
break;
case BlendMode_Multiply:
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_MOD);
break;
case BlendMode_Additive:
target = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_ADD, SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_ADD);
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_ADD);
break;
case BlendMode_Screen:
target = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD, SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD);
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
break;
}
}
SDL_RenderGeometry(renderer, texture, sdlVertices.buffer(), sdlVertices.size(), sdlIndices.buffer(),
indicesCount);
clipper.clipEnd(slot);
}
clipper.clipEnd();
} }
SDL_Texture *loadTexture(SDL_Renderer *renderer, const String &path) { SDL_Texture *loadTexture(SDL_Renderer *renderer, const String &path) {

View File

@ -50,8 +50,6 @@ namespace spine {
private: private:
bool ownsAnimationStateData; bool ownsAnimationStateData;
SkeletonClipping clipper;
Vector<float> worldVertices;
Vector<SDL_Vertex> sdlVertices; Vector<SDL_Vertex> sdlVertices;
Vector<int> sdlIndices; Vector<int> sdlIndices;
}; };