Skip to content

Commit

Permalink
[sfml] Added premultiplied alpha support to SkeletonDrawable. Closes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
badlogic committed Aug 17, 2017
1 parent 5947520 commit 34086c1
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -70,6 +70,7 @@
* Fixed renderer to work with 3.6 changes. Sadly, two color tinting does not work, as the vertex format in SFML is fixed.
* Added support for clipping.
* Added support for vertex effects. See raptor example.
* Added premultiplied alpha support to `SkeletonDrawable`.

### Unreal Engine 4
* Fixed renderer to work with 3.6 changes
Expand Down
54 changes: 44 additions & 10 deletions spine-sfml/src/spine/spine-sfml.cpp
Expand Up @@ -36,6 +36,16 @@

using namespace sf;

sf::BlendMode normal = sf::BlendMode(sf::BlendMode::SrcAlpha, sf::BlendMode::OneMinusSrcAlpha);
sf::BlendMode additive = sf::BlendMode(sf::BlendMode::SrcAlpha, sf::BlendMode::One);
sf::BlendMode multiply = sf::BlendMode(sf::BlendMode::DstColor, sf::BlendMode::OneMinusSrcAlpha);
sf::BlendMode screen = sf::BlendMode(sf::BlendMode::One, sf::BlendMode::OneMinusSrcColor);

sf::BlendMode normalPma = sf::BlendMode(sf::BlendMode::One, sf::BlendMode::OneMinusSrcAlpha);
sf::BlendMode additivePma = sf::BlendMode(sf::BlendMode::One, sf::BlendMode::One);
sf::BlendMode multiplyPma = sf::BlendMode(sf::BlendMode::DstColor, sf::BlendMode::OneMinusSrcAlpha);
sf::BlendMode screenPma = sf::BlendMode(sf::BlendMode::One, sf::BlendMode::OneMinusSrcColor);

_SP_ARRAY_IMPLEMENT_TYPE(spColorArray, spColor)

void _AtlasPage_createTexture (AtlasPage* self, const char* path){
Expand Down Expand Up @@ -163,16 +173,40 @@ void SkeletonDrawable::draw (RenderTarget& target, RenderStates states) const {
light.a = a / 255.0f;

sf::BlendMode blend;
switch (slot->data->blendMode) {
case BLEND_MODE_ADDITIVE:
blend = BlendAdd;
break;
case BLEND_MODE_MULTIPLY:
blend = BlendMultiply;
break;
case BLEND_MODE_SCREEN: // Unsupported, fall through.
default:
blend = BlendAlpha;
if (!usePremultipliedAlpha) {
switch (slot->data->blendMode) {
case BLEND_MODE_NORMAL:
blend = normal;
break;
case BLEND_MODE_ADDITIVE:
blend = additive;
break;
case BLEND_MODE_MULTIPLY:
blend = multiply;
break;
case BLEND_MODE_SCREEN:
blend = screen;
break;
default:
blend = normal;
}
} else {
switch (slot->data->blendMode) {
case BLEND_MODE_NORMAL:
blend = normalPma;
break;
case BLEND_MODE_ADDITIVE:
blend = additivePma;
break;
case BLEND_MODE_MULTIPLY:
blend = multiplyPma;
break;
case BLEND_MODE_SCREEN:
blend = screenPma;
break;
default:
blend = normalPma;
}
}

if (states.texture == 0) states.texture = texture;
Expand Down
4 changes: 4 additions & 0 deletions spine-sfml/src/spine/spine-sfml.h
Expand Up @@ -58,12 +58,16 @@ class SkeletonDrawable: public sf::Drawable {
void update (float deltaTime);

virtual void draw (sf::RenderTarget& target, sf::RenderStates states) const;

void setUsePremultipliedAlpha(bool usePMA) { usePremultipliedAlpha = usePMA; };
bool getUsePremultipliedAlpha() { return usePremultipliedAlpha; };
private:
bool ownsAnimationStateData;
float* worldVertices;
spFloatArray* tempUvs;
spColorArray* tempColors;
spSkeletonClipping* clipper;
bool usePremultipliedAlpha;
};

} /* namespace spine */
Expand Down

0 comments on commit 34086c1

Please sign in to comment.