support premultiplied alpha (#2523)

* Update spine-sdl-cpp.h

* Update spine-sdl-cpp.cpp
This commit is contained in:
oldeviloldevil 2024-05-21 17:38:58 +08:00 committed by GitHub
parent 25d55851e9
commit 4cf43903c8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 1 deletions

View File

@ -162,6 +162,7 @@ void SkeletonDrawable::draw(SDL_Renderer *renderer) {
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);
@ -176,6 +177,27 @@ void SkeletonDrawable::draw(SDL_Renderer *renderer) {
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);

View File

@ -47,12 +47,16 @@ namespace spine {
Skeleton *skeleton;
AnimationState *animationState;
void setUsePremultipliedAlpha(bool usePMA) { usePremultipliedAlpha = usePMA; };
bool getUsePremultipliedAlpha() { return usePremultipliedAlpha; };
private:
bool ownsAnimationStateData;
SkeletonClipping clipper;
Vector<float> worldVertices;
Vector<SDL_Vertex> sdlVertices;
Vector<int> sdlIndices;
bool usePremultipliedAlpha;
};
class SDLTextureLoader : public spine::TextureLoader {