[sdl] Use PMA asset in example, formatting, port to C backend

This commit is contained in:
Mario Zechner 2024-05-21 11:55:53 +02:00
parent 4cf43903c8
commit cb3aa4ec7c
11 changed files with 75 additions and 53 deletions

View File

@ -225,8 +225,8 @@ cp -f ../celestial-circus/export/celestial-circus.png "$ROOT/spine-godot/example
echo "spine-sdl"
rm -f "$ROOT/spine-sdl/data/"*
cp -f ../spineboy/export/spineboy-pro.json "$ROOT/spine-sdl/data/"
cp -f ../spineboy/export/spineboy.atlas "$ROOT/spine-sdl/data/"
cp -f ../spineboy/export/spineboy.png "$ROOT/spine-sdl/data/"
cp -f ../spineboy/export/spineboy-pma.atlas "$ROOT/spine-sdl/data/"
cp -f ../spineboy/export/spineboy-pma.png "$ROOT/spine-sdl/data/"
echo "spine-sfml-c"
rm "$ROOT/spine-sfml/c/data/"*

View File

@ -20,7 +20,7 @@ For the official legal terms governing the Spine Runtimes, please read the [Spin
spine-sdl works with data exported from Spine 4.2.xx.
spine-sdl supports all Spine features except premultiplied alpha, screen blend mode, and two color tinting.
spine-sdl supports all Spine features except screen blend mode and two color tinting.
## Usage

View File

@ -1,6 +1,7 @@
spineboy.png
spineboy-pma.png
size: 1024, 256
filter: Linear, Linear
pma: true
scale: 0.5
crosshair
bounds: 352, 7, 45, 45

Binary file not shown.

After

Width:  |  Height:  |  Size: 239 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 240 KiB

View File

@ -47,13 +47,14 @@ int main() {
return -1;
}
spAtlas *atlas = spAtlas_createFromFile("data/spineboy.atlas", renderer);
spAtlas *atlas = spAtlas_createFromFile("data/spineboy-pma.atlas", renderer);
spSkeletonJson *json = spSkeletonJson_create(atlas);
json->scale = 0.5f;
spSkeletonData *skeletonData = spSkeletonJson_readSkeletonDataFile(json, "data/spineboy-pro.json");
spAnimationStateData *animationStateData = spAnimationStateData_create(skeletonData);
animationStateData->defaultMix = 0.2f;
spSkeletonDrawable *drawable = spSkeletonDrawable_create(skeletonData, animationStateData);
drawable->usePremultipliedAlpha = -1;
drawable->skeleton->x = 400;
drawable->skeleton->y = 500;
spSkeleton_setToSetupPose(drawable->skeleton);

View File

@ -48,12 +48,13 @@ int main(int argc, char **argv) {
}
spine::SDLTextureLoader textureLoader(renderer);
spine::Atlas atlas("data/spineboy.atlas", &textureLoader);
spine::Atlas atlas("data/spineboy-pma.atlas", &textureLoader);
spine::AtlasAttachmentLoader attachmentLoader(&atlas);
spine::SkeletonJson json(&attachmentLoader);
json.setScale(0.5f);
spine::SkeletonData *skeletonData = json.readSkeletonDataFile("data/spineboy-pro.json");
spine::SkeletonDrawable drawable(skeletonData);
drawable.usePremultipliedAlpha = true;
drawable.animationState->getData()->setDefaultMix(0.2f);
drawable.skeleton->setPosition(400, 500);
drawable.skeleton->setToSetupPose();

View File

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

View File

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

View File

@ -177,8 +177,7 @@ void SkeletonDrawable::draw(SDL_Renderer *renderer) {
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
break;
}
}
else {
} else {
SDL_BlendMode target;
switch (slot.getData().getBlendMode()) {
case BlendMode_Normal:

View File

@ -46,9 +46,7 @@ namespace spine {
Skeleton *skeleton;
AnimationState *animationState;
void setUsePremultipliedAlpha(bool usePMA) { usePremultipliedAlpha = usePMA; };
bool getUsePremultipliedAlpha() { return usePremultipliedAlpha; };
bool usePremultipliedAlpha;
private:
bool ownsAnimationStateData;
@ -56,7 +54,6 @@ namespace spine {
Vector<float> worldVertices;
Vector<SDL_Vertex> sdlVertices;
Vector<int> sdlIndices;
bool usePremultipliedAlpha;
};
class SDLTextureLoader : public spine::TextureLoader {