瀏覽代碼

[sfml] Switch to SkeletonRenderer

Mario Zechner 1 年之前
父節點
當前提交
e12b71bb1a
共有 3 個文件被更改,包括 107 次插入232 次删除
  1. 2 0
      spine-sfml/cpp/example/main.cpp
  2. 103 225
      spine-sfml/cpp/src/spine/spine-sfml.cpp
  3. 2 7
      spine-sfml/cpp/src/spine/spine-sfml.h

+ 2 - 0
spine-sfml/cpp/example/main.cpp

@@ -773,6 +773,7 @@ void test(SkeletonData *skeletonData, Atlas *atlas) {
 }
 
 DebugExtension dbgExtension(SpineExtension::getInstance());
+extern spine::SkeletonRenderer *skeletonRenderer;
 
 int main() {
 	SpineExtension::setInstance(&dbgExtension);
@@ -794,6 +795,7 @@ int main() {
 	testcase(goblins, "data/goblins-pro.json", "data/goblins-pro.skel", "data/goblins-pma.atlas", 1.4f);
 	testcase(stretchyman, "data/stretchyman-pro.json", "data/stretchyman-pro.skel", "data/stretchyman-pma.atlas", 0.6f);
 
+    delete skeletonRenderer;
 	dbgExtension.reportLeaks();
 	return 0;
 }

+ 103 - 225
spine-sfml/cpp/src/spine/spine-sfml.cpp

@@ -34,228 +34,106 @@
 #endif
 
 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);
-
-namespace spine {
-
-	SkeletonDrawable::SkeletonDrawable(SkeletonData *skeletonData, AnimationStateData *stateData) : timeScale(1),
-																									vertexArray(new VertexArray(Triangles, skeletonData->getBones().size() * 4)),
-																									worldVertices(), clipper(), usePremultipliedAlpha(false) {
-		Bone::setYDown(true);
-		worldVertices.ensureCapacity(SPINE_MESH_VERTEX_COUNT_MAX);
-		skeleton = new (__FILE__, __LINE__) Skeleton(skeletonData);
-		tempUvs.ensureCapacity(16);
-		tempColors.ensureCapacity(16);
-
-		ownsAnimationStateData = stateData == 0;
-		if (ownsAnimationStateData) stateData = new (__FILE__, __LINE__) AnimationStateData(skeletonData);
-
-		state = new (__FILE__, __LINE__) AnimationState(stateData);
-
-		quadIndices.add(0);
-		quadIndices.add(1);
-		quadIndices.add(2);
-		quadIndices.add(2);
-		quadIndices.add(3);
-		quadIndices.add(0);
-	}
-
-	SkeletonDrawable::~SkeletonDrawable() {
-		delete vertexArray;
-		if (ownsAnimationStateData) delete state->getData();
-		delete state;
-		delete skeleton;
-	}
-
-	void SkeletonDrawable::update(float deltaTime, Physics physics) {
-		state->update(deltaTime * timeScale);
-		state->apply(*skeleton);
-		skeleton->update(deltaTime * timeScale);
-		skeleton->updateWorldTransform(physics);
-	}
-
-	void SkeletonDrawable::draw(RenderTarget &target, RenderStates states) const {
-		vertexArray->clear();
-		states.texture = NULL;
-
-		// Early out if skeleton is invisible
-		if (skeleton->getColor().a == 0) return;
-
-		sf::Vertex vertex;
-		Texture *texture = NULL;
-		for (unsigned i = 0; i < skeleton->getSlots().size(); ++i) {
-			Slot &slot = *skeleton->getDrawOrder()[i];
-			Attachment *attachment = slot.getAttachment();
-			if (!attachment) {
-				clipper.clipEnd(slot);
-				continue;
-			}
-
-			// Early out if the slot color is 0 or the bone is not active
-			if (slot.getColor().a == 0 || !slot.getBone().isActive()) {
-				clipper.clipEnd(slot);
-				continue;
-			}
-
-			Vector<float> *vertices = &worldVertices;
-			Vector<float> *uvs = NULL;
-			Vector<unsigned short> *indices = NULL;
-			int indicesCount = 0;
-			Color *attachmentColor;
-
-			if (attachment->getRTTI().isExactly(RegionAttachment::rtti)) {
-				RegionAttachment *regionAttachment = (RegionAttachment *) attachment;
-				attachmentColor = &regionAttachment->getColor();
-
-				// Early out if the slot color is 0
-				if (attachmentColor->a == 0) {
-					clipper.clipEnd(slot);
-					continue;
-				}
-
-				worldVertices.setSize(8, 0);
-				regionAttachment->computeWorldVertices(slot, worldVertices, 0, 2);
-				uvs = &regionAttachment->getUVs();
-				indices = &quadIndices;
-				indicesCount = 6;
-				texture = (Texture *) ((AtlasRegion *) regionAttachment->getRegion())->page->texture;
-
-			} else if (attachment->getRTTI().isExactly(MeshAttachment::rtti)) {
-				MeshAttachment *mesh = (MeshAttachment *) attachment;
-				attachmentColor = &mesh->getColor();
-
-				// Early out if the slot color is 0
-				if (attachmentColor->a == 0) {
-					clipper.clipEnd(slot);
-					continue;
-				}
-
-				worldVertices.setSize(mesh->getWorldVerticesLength(), 0);
-				mesh->computeWorldVertices(slot, 0, mesh->getWorldVerticesLength(), worldVertices.buffer(), 0, 2);
-				uvs = &mesh->getUVs();
-				indices = &mesh->getTriangles();
-				indicesCount = mesh->getTriangles().size();
-				texture = (Texture *) ((AtlasRegion *) mesh->getRegion())->page->texture;
-
-			} else if (attachment->getRTTI().isExactly(ClippingAttachment::rtti)) {
-				ClippingAttachment *clip = (ClippingAttachment *) slot.getAttachment();
-				clipper.clipStart(slot, clip);
-				continue;
-			} else
-				continue;
-
-			Uint8 r = static_cast<Uint8>(skeleton->getColor().r * slot.getColor().r * attachmentColor->r * 255);
-			Uint8 g = static_cast<Uint8>(skeleton->getColor().g * slot.getColor().g * attachmentColor->g * 255);
-			Uint8 b = static_cast<Uint8>(skeleton->getColor().b * slot.getColor().b * attachmentColor->b * 255);
-			Uint8 a = static_cast<Uint8>(skeleton->getColor().a * slot.getColor().a * attachmentColor->a * 255);
-			vertex.color.r = r;
-			vertex.color.g = g;
-			vertex.color.b = b;
-			vertex.color.a = a;
-
-			Color light;
-			light.r = r / 255.0f;
-			light.g = g / 255.0f;
-			light.b = b / 255.0f;
-			light.a = a / 255.0f;
-
-			sf::BlendMode blend;
-			if (!usePremultipliedAlpha) {
-				switch (slot.getData().getBlendMode()) {
-					case BlendMode_Normal:
-						blend = normal;
-						break;
-					case BlendMode_Additive:
-						blend = additive;
-						break;
-					case BlendMode_Multiply:
-						blend = multiply;
-						break;
-					case BlendMode_Screen:
-						blend = screen;
-						break;
-					default:
-						blend = normal;
-				}
-			} else {
-				switch (slot.getData().getBlendMode()) {
-					case BlendMode_Normal:
-						blend = normalPma;
-						break;
-					case BlendMode_Additive:
-						blend = additivePma;
-						break;
-					case BlendMode_Multiply:
-						blend = multiplyPma;
-						break;
-					case BlendMode_Screen:
-						blend = screenPma;
-						break;
-					default:
-						blend = normalPma;
-				}
-			}
-
-			if (states.texture == 0) states.texture = texture;
-
-			if (states.blendMode != blend || states.texture != texture) {
-				target.draw(*vertexArray, states);
-				vertexArray->clear();
-				states.blendMode = blend;
-				states.texture = texture;
-			}
-
-			if (clipper.isClipping()) {
-				clipper.clipTriangles(worldVertices, *indices, *uvs, 2);
-				vertices = &clipper.getClippedVertices();
-				uvs = &clipper.getClippedUVs();
-				indices = &clipper.getClippedTriangles();
-				indicesCount = clipper.getClippedTriangles().size();
-			}
-
-			Vector2u size = texture->getSize();
-
-			for (int ii = 0; ii < indicesCount; ++ii) {
-				int index = (*indices)[ii] << 1;
-				vertex.position.x = (*vertices)[index];
-				vertex.position.y = (*vertices)[index + 1];
-				vertex.texCoords.x = (*uvs)[index] * size.x;
-				vertex.texCoords.y = (*uvs)[index + 1] * size.y;
-				vertexArray->append(vertex);
-			}
-			clipper.clipEnd(slot);
-		}
-		target.draw(*vertexArray, states);
-		clipper.clipEnd();
-	}
-
-	void SFMLTextureLoader::load(AtlasPage &page, const String &path) {
-		Texture *texture = new Texture();
-		if (!texture->loadFromFile(path.buffer())) return;
-
-		if (page.magFilter == TextureFilter_Linear) texture->setSmooth(true);
-		if (page.uWrap == TextureWrap_Repeat && page.vWrap == TextureWrap_Repeat) texture->setRepeated(true);
-
-		page.texture = texture;
-		Vector2u size = texture->getSize();
-		page.width = size.x;
-		page.height = size.y;
-	}
-
-	void SFMLTextureLoader::unload(void *texture) {
-		delete (Texture *) texture;
-	}
-
-	SpineExtension *getDefaultExtension() {
-		return new DefaultSpineExtension();
-	}
-}// namespace spine
+using namespace spine;
+
+sf::BlendMode blendModes[] = {
+        sf::BlendMode(sf::BlendMode::SrcAlpha, sf::BlendMode::OneMinusSrcAlpha),
+        sf::BlendMode(sf::BlendMode::SrcAlpha, sf::BlendMode::One),
+        sf::BlendMode(sf::BlendMode::DstColor, sf::BlendMode::OneMinusSrcAlpha),
+        sf::BlendMode(sf::BlendMode::One, sf::BlendMode::OneMinusSrcColor)
+};
+
+sf::BlendMode blendModesPma[] = {
+        sf::BlendMode(sf::BlendMode::One, sf::BlendMode::OneMinusSrcAlpha),
+        sf::BlendMode(sf::BlendMode::One, sf::BlendMode::One),
+        sf::BlendMode(sf::BlendMode::DstColor, sf::BlendMode::OneMinusSrcAlpha),
+        sf::BlendMode(sf::BlendMode::One, sf::BlendMode::OneMinusSrcColor),
+};
+
+SkeletonRenderer *skeletonRenderer = nullptr;
+
+SkeletonDrawable::SkeletonDrawable(SkeletonData *skeletonData, AnimationStateData *stateData) : timeScale(1),
+                                                                                                usePremultipliedAlpha(false),
+                                                                                                vertexArray(new VertexArray(Triangles, skeletonData->getBones().size() * 4)) {
+    Bone::setYDown(true);
+    skeleton = new (__FILE__, __LINE__) Skeleton(skeletonData);
+    ownsAnimationStateData = stateData == 0;
+    if (ownsAnimationStateData) stateData = new (__FILE__, __LINE__) AnimationStateData(skeletonData);
+    state = new (__FILE__, __LINE__) AnimationState(stateData);
+}
+
+SkeletonDrawable::~SkeletonDrawable() {
+    delete vertexArray;
+    if (ownsAnimationStateData) delete state->getData();
+    delete state;
+    delete skeleton;
+}
+
+void SkeletonDrawable::update(float deltaTime, Physics physics) {
+    state->update(deltaTime * timeScale);
+    state->apply(*skeleton);
+    skeleton->update(deltaTime * timeScale);
+    skeleton->updateWorldTransform(physics);
+}
+
+inline void toSFMLColor(uint32_t color, sf::Color *sfmlColor) {
+    sfmlColor->a = (color >> 24) & 0xFF;
+    sfmlColor->r = (color >> 16) & 0xFF;
+    sfmlColor->g = (color >> 8) & 0xFF;
+    sfmlColor->b = color & 0xFF;
+}
+
+void SkeletonDrawable::draw(RenderTarget &target, RenderStates states) const {
+    states.texture = NULL;
+    vertexArray->clear();
+
+    if (!skeletonRenderer) skeletonRenderer = new (__FILE__, __LINE__) SkeletonRenderer();
+    RenderCommand *command = skeletonRenderer->render(*skeleton);
+    while (command) {
+        Texture *texture = (Texture *)command->texture;
+        Vector2u size = texture->getSize();
+        Vertex vertex;
+        float *positions = command->positions;
+        float *uvs = command->uvs;
+        uint32_t *colors = command->colors;
+        uint16_t *indices = command->indices;
+        for (int i = 0, n = command->numIndices; i < n; ++i) {
+            int ii = indices[i];
+            int index = ii << 1;
+            vertex.position.x = positions[index];
+            vertex.position.y = positions[index + 1];
+            vertex.texCoords.x = uvs[index] * size.x;
+            vertex.texCoords.y = uvs[index + 1] * size.y;
+            toSFMLColor(colors[ii], &vertex.color);
+            vertexArray->append(vertex);
+        }
+        BlendMode blendMode = command->blendMode;
+        states.blendMode = usePremultipliedAlpha ? blendModesPma[blendMode] : blendModes[blendMode];
+        states.texture = texture;
+        target.draw(*vertexArray, states);
+        vertexArray->clear();
+
+        command = command->next;
+    }
+}
+
+void SFMLTextureLoader::load(AtlasPage &page, const String &path) {
+    Texture *texture = new Texture();
+    if (!texture->loadFromFile(path.buffer())) return;
+
+    if (page.magFilter == TextureFilter_Linear) texture->setSmooth(true);
+    if (page.uWrap == TextureWrap_Repeat && page.vWrap == TextureWrap_Repeat) texture->setRepeated(true);
+
+    page.texture = texture;
+    Vector2u size = texture->getSize();
+    page.width = size.x;
+    page.height = size.y;
+}
+
+void SFMLTextureLoader::unload(void *texture) {
+    delete (Texture *) texture;
+}
+
+SpineExtension *spine::getDefaultExtension() {
+    return new DefaultSpineExtension();
+}

+ 2 - 7
spine-sfml/cpp/src/spine/spine-sfml.h

@@ -45,7 +45,6 @@ namespace spine {
 		Skeleton *skeleton;
 		AnimationState *state;
 		float timeScale;
-		sf::VertexArray *vertexArray;
 
 		SkeletonDrawable(SkeletonData *skeleton, AnimationStateData *stateData = 0);
 
@@ -61,13 +60,9 @@ namespace spine {
 		bool getUsePremultipliedAlpha() { return usePremultipliedAlpha; };
 
 	private:
-		mutable bool ownsAnimationStateData;
-		mutable Vector<float> worldVertices;
-		mutable Vector<float> tempUvs;
-		mutable Vector<Color> tempColors;
-		mutable Vector<unsigned short> quadIndices;
-		mutable SkeletonClipping clipper;
+		bool ownsAnimationStateData;
 		mutable bool usePremultipliedAlpha;
+        sf::VertexArray *vertexArray;
 	};
 
 	class SFMLTextureLoader : public TextureLoader {