Просмотр исходного кода

support premultiplied alpha (#2523)

* Update spine-sdl-cpp.h

* Update spine-sdl-cpp.cpp
oldeviloldevil 1 год назад
Родитель
Сommit
4cf43903c8
2 измененных файлов с 27 добавлено и 1 удалено
  1. 23 1
      spine-sdl/src/spine-sdl-cpp.cpp
  2. 4 0
      spine-sdl/src/spine-sdl-cpp.h

+ 23 - 1
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++)
 		for (int ii = 0; ii < (int) indices->size(); ii++)
 			sdlIndices.add((*indices)[ii]);
 			sdlIndices.add((*indices)[ii]);
 
 
-		switch (slot.getData().getBlendMode()) {
+		if (!usePremultipliedAlpha) {
+			switch (slot.getData().getBlendMode()) {
 			case BlendMode_Normal:
 			case BlendMode_Normal:
 				SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
 				SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
 				break;
 				break;
@@ -175,6 +176,27 @@ void SkeletonDrawable::draw(SDL_Renderer *renderer) {
 			case BlendMode_Screen:
 			case BlendMode_Screen:
 				SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
 				SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
 				break;
 				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(),
 		SDL_RenderGeometry(renderer, texture, sdlVertices.buffer(), sdlVertices.size(), sdlIndices.buffer(),

+ 4 - 0
spine-sdl/src/spine-sdl-cpp.h

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