Formatting

This commit is contained in:
Mario Zechner 2024-05-21 13:17:23 +02:00
parent 5abed8a7f6
commit aaa9b81628
9 changed files with 138 additions and 134 deletions

View File

@ -1525,7 +1525,7 @@ spine_bounds spine_skeleton_get_bounds(spine_skeleton skeleton) {
if (skeleton == nullptr) return (spine_bounds) bounds; if (skeleton == nullptr) return (spine_bounds) bounds;
Skeleton *_skeleton = (Skeleton *) skeleton; Skeleton *_skeleton = (Skeleton *) skeleton;
Vector<float> vertices; Vector<float> vertices;
SkeletonClipping clipper; SkeletonClipping clipper;
_skeleton->getBounds(bounds->x, bounds->y, bounds->width, bounds->height, vertices, &clipper); _skeleton->getBounds(bounds->x, bounds->y, bounds->width, bounds->height, vertices, &clipper);
return (spine_bounds) bounds; return (spine_bounds) bounds;

View File

@ -458,8 +458,8 @@ Skeleton::findPhysicsConstraint(const String &constraintName) {
} }
void Skeleton::getBounds(float &outX, float &outY, float &outWidth, void Skeleton::getBounds(float &outX, float &outY, float &outWidth,
float &outHeight, Vector<float> &outVertexBuffer) { float &outHeight, Vector<float> &outVertexBuffer) {
getBounds(outX, outY, outWidth, outHeight, outVertexBuffer, NULL); getBounds(outX, outY, outWidth, outHeight, outVertexBuffer, NULL);
} }
static unsigned short quadIndices[] = {0, 1, 2, 2, 3, 0}; static unsigned short quadIndices[] = {0, 1, 2, 2, 3, 0};
@ -477,8 +477,8 @@ void Skeleton::getBounds(float &outX, float &outY, float &outWidth,
continue; continue;
size_t verticesLength = 0; size_t verticesLength = 0;
Attachment *attachment = slot->getAttachment(); Attachment *attachment = slot->getAttachment();
unsigned short *triangles = NULL; unsigned short *triangles = NULL;
size_t trianglesLength = 0; size_t trianglesLength = 0;
if (attachment != NULL && if (attachment != NULL &&
attachment->getRTTI().instanceOf(RegionAttachment::rtti)) { attachment->getRTTI().instanceOf(RegionAttachment::rtti)) {
@ -490,8 +490,8 @@ void Skeleton::getBounds(float &outX, float &outY, float &outWidth,
outVertexBuffer.setSize(8, 0); outVertexBuffer.setSize(8, 0);
} }
regionAttachment->computeWorldVertices(*slot, outVertexBuffer, 0); regionAttachment->computeWorldVertices(*slot, outVertexBuffer, 0);
triangles = quadIndices; triangles = quadIndices;
trianglesLength = 6; trianglesLength = 6;
} else if (attachment != NULL && } else if (attachment != NULL &&
attachment->getRTTI().instanceOf(MeshAttachment::rtti)) { attachment->getRTTI().instanceOf(MeshAttachment::rtti)) {
MeshAttachment *mesh = static_cast<MeshAttachment *>(attachment); MeshAttachment *mesh = static_cast<MeshAttachment *>(attachment);
@ -503,33 +503,33 @@ void Skeleton::getBounds(float &outX, float &outY, float &outWidth,
mesh->computeWorldVertices(*slot, 0, verticesLength, mesh->computeWorldVertices(*slot, 0, verticesLength,
outVertexBuffer.buffer(), 0); outVertexBuffer.buffer(), 0);
triangles = mesh->getTriangles().buffer(); triangles = mesh->getTriangles().buffer();
trianglesLength = mesh->getTriangles().size(); trianglesLength = mesh->getTriangles().size();
} else if (attachment != NULL && } else if (attachment != NULL &&
attachment->getRTTI().instanceOf(ClippingAttachment::rtti) && clipper != NULL) { attachment->getRTTI().instanceOf(ClippingAttachment::rtti) && clipper != NULL) {
clipper->clipStart(*slot, static_cast<ClippingAttachment *>(attachment)); clipper->clipStart(*slot, static_cast<ClippingAttachment *>(attachment));
} }
if (verticesLength > 0) { if (verticesLength > 0) {
float *vertices = outVertexBuffer.buffer(); float *vertices = outVertexBuffer.buffer();
if (clipper != NULL && clipper->isClipping()) { if (clipper != NULL && clipper->isClipping()) {
clipper->clipTriangles(outVertexBuffer.buffer(), triangles, trianglesLength); clipper->clipTriangles(outVertexBuffer.buffer(), triangles, trianglesLength);
vertices = clipper->getClippedVertices().buffer(); vertices = clipper->getClippedVertices().buffer();
verticesLength = clipper->getClippedVertices().size(); verticesLength = clipper->getClippedVertices().size();
} }
for (size_t ii = 0; ii < verticesLength; ii += 2) { for (size_t ii = 0; ii < verticesLength; ii += 2) {
float vx = vertices[ii]; float vx = vertices[ii];
float vy = vertices[ii + 1]; float vy = vertices[ii + 1];
minX = MathUtil::min(minX, vx); minX = MathUtil::min(minX, vx);
minY = MathUtil::min(minY, vy); minY = MathUtil::min(minY, vy);
maxX = MathUtil::max(maxX, vx); maxX = MathUtil::max(maxX, vx);
maxY = MathUtil::max(maxY, vy); maxY = MathUtil::max(maxY, vy);
} }
} }
if (clipper != NULL) clipper->clipEnd(*slot); if (clipper != NULL) clipper->clipEnd(*slot);
} }
if (clipper != NULL) clipper->clipEnd(); if (clipper != NULL) clipper->clipEnd();
outX = minX; outX = minX;
outY = minY; outY = minY;

View File

@ -83,76 +83,76 @@ void SkeletonClipping::clipEnd() {
} }
void SkeletonClipping::clipTriangles(float *vertices, unsigned short *triangles, void SkeletonClipping::clipTriangles(float *vertices, unsigned short *triangles,
size_t trianglesLength) { size_t trianglesLength) {
Vector<float> &clipOutput = _clipOutput; Vector<float> &clipOutput = _clipOutput;
Vector<float> &clippedVertices = _clippedVertices; Vector<float> &clippedVertices = _clippedVertices;
Vector<unsigned short> &clippedTriangles = _clippedTriangles; Vector<unsigned short> &clippedTriangles = _clippedTriangles;
Vector<Vector<float> *> &polygons = *_clippingPolygons; Vector<Vector<float> *> &polygons = *_clippingPolygons;
size_t polygonsCount = (*_clippingPolygons).size(); size_t polygonsCount = (*_clippingPolygons).size();
size_t index = 0; size_t index = 0;
clippedVertices.clear(); clippedVertices.clear();
_clippedUVs.clear(); _clippedUVs.clear();
clippedTriangles.clear(); clippedTriangles.clear();
int stride = 2; int stride = 2;
size_t i = 0; size_t i = 0;
continue_outer: continue_outer:
for (; i < trianglesLength; i += 3) { for (; i < trianglesLength; i += 3) {
int vertexOffset = triangles[i] * stride; int vertexOffset = triangles[i] * stride;
float x1 = vertices[vertexOffset], y1 = vertices[vertexOffset + 1]; float x1 = vertices[vertexOffset], y1 = vertices[vertexOffset + 1];
vertexOffset = triangles[i + 1] * stride; vertexOffset = triangles[i + 1] * stride;
float x2 = vertices[vertexOffset], y2 = vertices[vertexOffset + 1]; float x2 = vertices[vertexOffset], y2 = vertices[vertexOffset + 1];
vertexOffset = triangles[i + 2] * stride; vertexOffset = triangles[i + 2] * stride;
float x3 = vertices[vertexOffset], y3 = vertices[vertexOffset + 1]; float x3 = vertices[vertexOffset], y3 = vertices[vertexOffset + 1];
for (size_t p = 0; p < polygonsCount; p++) { for (size_t p = 0; p < polygonsCount; p++) {
size_t s = clippedVertices.size(); size_t s = clippedVertices.size();
if (clip(x1, y1, x2, y2, x3, y3, &(*polygons[p]), &clipOutput)) { if (clip(x1, y1, x2, y2, x3, y3, &(*polygons[p]), &clipOutput)) {
size_t clipOutputLength = clipOutput.size(); size_t clipOutputLength = clipOutput.size();
if (clipOutputLength == 0) continue; if (clipOutputLength == 0) continue;
size_t clipOutputCount = clipOutputLength >> 1; size_t clipOutputCount = clipOutputLength >> 1;
clippedVertices.setSize(s + clipOutputCount * 2, 0); clippedVertices.setSize(s + clipOutputCount * 2, 0);
for (size_t ii = 0; ii < clipOutputLength; ii += 2) { for (size_t ii = 0; ii < clipOutputLength; ii += 2) {
float x = clipOutput[ii], y = clipOutput[ii + 1]; float x = clipOutput[ii], y = clipOutput[ii + 1];
clippedVertices[s] = x; clippedVertices[s] = x;
clippedVertices[s + 1] = y; clippedVertices[s + 1] = y;
s += 2; s += 2;
} }
s = clippedTriangles.size(); s = clippedTriangles.size();
clippedTriangles.setSize(s + 3 * (clipOutputCount - 2), 0); clippedTriangles.setSize(s + 3 * (clipOutputCount - 2), 0);
clipOutputCount--; clipOutputCount--;
for (size_t ii = 1; ii < clipOutputCount; ii++) { for (size_t ii = 1; ii < clipOutputCount; ii++) {
clippedTriangles[s] = (unsigned short) (index); clippedTriangles[s] = (unsigned short) (index);
clippedTriangles[s + 1] = (unsigned short) (index + ii); clippedTriangles[s + 1] = (unsigned short) (index + ii);
clippedTriangles[s + 2] = (unsigned short) (index + ii + 1); clippedTriangles[s + 2] = (unsigned short) (index + ii + 1);
s += 3; s += 3;
} }
index += clipOutputCount + 1; index += clipOutputCount + 1;
} else { } else {
clippedVertices.setSize(s + 3 * 2, 0); clippedVertices.setSize(s + 3 * 2, 0);
clippedVertices[s] = x1; clippedVertices[s] = x1;
clippedVertices[s + 1] = y1; clippedVertices[s + 1] = y1;
clippedVertices[s + 2] = x2; clippedVertices[s + 2] = x2;
clippedVertices[s + 3] = y2; clippedVertices[s + 3] = y2;
clippedVertices[s + 4] = x3; clippedVertices[s + 4] = x3;
clippedVertices[s + 5] = y3; clippedVertices[s + 5] = y3;
s = clippedTriangles.size(); s = clippedTriangles.size();
clippedTriangles.setSize(s + 3, 0); clippedTriangles.setSize(s + 3, 0);
clippedTriangles[s] = (unsigned short) index; clippedTriangles[s] = (unsigned short) index;
clippedTriangles[s + 1] = (unsigned short) (index + 1); clippedTriangles[s + 1] = (unsigned short) (index + 1);
clippedTriangles[s + 2] = (unsigned short) (index + 2); clippedTriangles[s + 2] = (unsigned short) (index + 2);
index += 3; index += 3;
i += 3; i += 3;
goto continue_outer; goto continue_outer;
} }
} }
} }
} }
void SkeletonClipping::clipTriangles(Vector<float> &vertices, Vector<unsigned short> &triangles, Vector<float> &uvs, void SkeletonClipping::clipTriangles(Vector<float> &vertices, Vector<unsigned short> &triangles, Vector<float> &uvs,

View File

@ -9,7 +9,11 @@
"request": "launch", "request": "launch",
"name": "debug scene v4", "name": "debug scene v4",
"program": "godot/bin/godot.windows.editor.dev.x86_64.exe", "program": "godot/bin/godot.windows.editor.dev.x86_64.exe",
"args": ["--path", "example-v4", "examples/07-slot-node/slot-node.tscn"], "args": [
"--path",
"example-v4",
"examples/03-mix-and-match/mix-and-match.tscn"
],
"cwd": "${workspaceFolder}", "cwd": "${workspaceFolder}",
"preLaunchTask": "build-v4", "preLaunchTask": "build-v4",
"linux": { "linux": {

View File

@ -54,7 +54,7 @@ int main() {
spAnimationStateData *animationStateData = spAnimationStateData_create(skeletonData); spAnimationStateData *animationStateData = spAnimationStateData_create(skeletonData);
animationStateData->defaultMix = 0.2f; animationStateData->defaultMix = 0.2f;
spSkeletonDrawable *drawable = spSkeletonDrawable_create(skeletonData, animationStateData); spSkeletonDrawable *drawable = spSkeletonDrawable_create(skeletonData, animationStateData);
drawable->usePremultipliedAlpha = -1; drawable->usePremultipliedAlpha = -1;
drawable->skeleton->x = 400; drawable->skeleton->x = 400;
drawable->skeleton->y = 500; drawable->skeleton->y = 500;
spSkeleton_setToSetupPose(drawable->skeleton); spSkeleton_setToSetupPose(drawable->skeleton);

View File

@ -54,13 +54,13 @@ int main(int argc, char **argv) {
json.setScale(0.5f); json.setScale(0.5f);
spine::SkeletonData *skeletonData = json.readSkeletonDataFile("data/spineboy-pro.json"); spine::SkeletonData *skeletonData = json.readSkeletonDataFile("data/spineboy-pro.json");
spine::SkeletonDrawable drawable(skeletonData); spine::SkeletonDrawable drawable(skeletonData);
drawable.usePremultipliedAlpha = true; drawable.usePremultipliedAlpha = true;
drawable.animationState->getData()->setDefaultMix(0.2f); drawable.animationState->getData()->setDefaultMix(0.2f);
drawable.skeleton->setPosition(400, 500); drawable.skeleton->setPosition(400, 500);
drawable.skeleton->setToSetupPose(); drawable.skeleton->setToSetupPose();
drawable.animationState->setAnimation(0, "portal", true); drawable.animationState->setAnimation(0, "portal", true);
drawable.animationState->addAnimation(0, "run", true, 0); drawable.animationState->addAnimation(0, "run", true, 0);
drawable.update(0, spine::Physics_Update); drawable.update(0, spine::Physics_Update);
bool quit = false; bool quit = false;
uint64_t lastFrameTime = SDL_GetPerformanceCounter(); uint64_t lastFrameTime = SDL_GetPerformanceCounter();

View File

@ -42,7 +42,7 @@ spSkeletonDrawable *spSkeletonDrawable_create(spSkeletonData *skeletonData, spAn
spSkeletonDrawable *self = NEW(spSkeletonDrawable); spSkeletonDrawable *self = NEW(spSkeletonDrawable);
self->skeleton = spSkeleton_create(skeletonData); self->skeleton = spSkeleton_create(skeletonData);
self->animationState = spAnimationState_create(animationStateData); self->animationState = spAnimationState_create(animationStateData);
self->usePremultipliedAlpha = 0; self->usePremultipliedAlpha = 0;
self->sdlIndices = spIntArray_create(12); self->sdlIndices = spIntArray_create(12);
self->sdlVertices = spSdlVertexArray_create(12); self->sdlVertices = spSdlVertexArray_create(12);
self->worldVertices = spFloatArray_create(12); self->worldVertices = spFloatArray_create(12);
@ -164,41 +164,41 @@ void spSkeletonDrawable_draw(spSkeletonDrawable *self, struct SDL_Renderer *rend
for (int ii = 0; ii < (int) indicesCount; ii++) for (int ii = 0; ii < (int) indicesCount; ii++)
spIntArray_add(self->sdlIndices, indices[ii]); spIntArray_add(self->sdlIndices, indices[ii]);
if (!self->usePremultipliedAlpha) { if (!self->usePremultipliedAlpha) {
switch (slot->data->blendMode) { switch (slot->data->blendMode) {
case SP_BLEND_MODE_NORMAL: case SP_BLEND_MODE_NORMAL:
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND); SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
break; break;
case SP_BLEND_MODE_MULTIPLY: case SP_BLEND_MODE_MULTIPLY:
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_MOD); SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_MOD);
break; break;
case SP_BLEND_MODE_ADDITIVE: case SP_BLEND_MODE_ADDITIVE:
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_ADD); SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_ADD);
break; break;
case SP_BLEND_MODE_SCREEN: case SP_BLEND_MODE_SCREEN:
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND); SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
break; break;
} }
} else { } else {
SDL_BlendMode target; SDL_BlendMode target;
switch (slot->data->blendMode) { switch (slot->data->blendMode) {
case SP_BLEND_MODE_NORMAL: case SP_BLEND_MODE_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); 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); SDL_SetTextureBlendMode(texture, target);
break; break;
case SP_BLEND_MODE_MULTIPLY: case SP_BLEND_MODE_MULTIPLY:
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_MOD); SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_MOD);
break; break;
case SP_BLEND_MODE_ADDITIVE: case SP_BLEND_MODE_ADDITIVE:
target = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_ADD, SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_ADD); 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); SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_ADD);
break; break;
case SP_BLEND_MODE_SCREEN: case SP_BLEND_MODE_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); 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); SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
break; break;
} }
} }
SDL_RenderGeometry(renderer, texture, self->sdlVertices->items, self->sdlVertices->size, self->sdlIndices->items, SDL_RenderGeometry(renderer, texture, self->sdlVertices->items, self->sdlVertices->size, self->sdlIndices->items,
indicesCount); indicesCount);

View File

@ -43,7 +43,7 @@ _SP_ARRAY_DECLARE_TYPE(spSdlVertexArray, struct SDL_Vertex)
typedef struct spSkeletonDrawable { typedef struct spSkeletonDrawable {
spSkeleton *skeleton; spSkeleton *skeleton;
spAnimationState *animationState; spAnimationState *animationState;
int usePremultipliedAlpha; int usePremultipliedAlpha;
spSkeletonClipping *clipper; spSkeletonClipping *clipper;
spFloatArray *worldVertices; spFloatArray *worldVertices;

View File

@ -46,7 +46,7 @@ namespace spine {
Skeleton *skeleton; Skeleton *skeleton;
AnimationState *animationState; AnimationState *animationState;
bool usePremultipliedAlpha; bool usePremultipliedAlpha;
private: private:
bool ownsAnimationStateData; bool ownsAnimationStateData;