From 4cf43903c8feac88cd2e4b43869337f0ee086951 Mon Sep 17 00:00:00 2001 From: oldeviloldevil <83639351+oldeviloldevil@users.noreply.github.com> Date: Tue, 21 May 2024 17:38:58 +0800 Subject: [PATCH] support premultiplied alpha (#2523) * Update spine-sdl-cpp.h * Update spine-sdl-cpp.cpp --- spine-sdl/src/spine-sdl-cpp.cpp | 24 +++++++++++++++++++++++- spine-sdl/src/spine-sdl-cpp.h | 4 ++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/spine-sdl/src/spine-sdl-cpp.cpp b/spine-sdl/src/spine-sdl-cpp.cpp index 8bb11661e..91b03c2ed 100644 --- a/spine-sdl/src/spine-sdl-cpp.cpp +++ b/spine-sdl/src/spine-sdl-cpp.cpp @@ -162,7 +162,8 @@ void SkeletonDrawable::draw(SDL_Renderer *renderer) { for (int ii = 0; ii < (int) indices->size(); ii++) sdlIndices.add((*indices)[ii]); - switch (slot.getData().getBlendMode()) { + if (!usePremultipliedAlpha) { + switch (slot.getData().getBlendMode()) { case BlendMode_Normal: SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND); break; @@ -175,6 +176,27 @@ void SkeletonDrawable::draw(SDL_Renderer *renderer) { 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(), diff --git a/spine-sdl/src/spine-sdl-cpp.h b/spine-sdl/src/spine-sdl-cpp.h index e9760dfcb..4185d352c 100644 --- a/spine-sdl/src/spine-sdl-cpp.h +++ b/spine-sdl/src/spine-sdl-cpp.h @@ -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 worldVertices; Vector sdlVertices; Vector sdlIndices; + bool usePremultipliedAlpha; }; class SDLTextureLoader : public spine::TextureLoader {