1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
#include "fsCAnimatingSpriteSpine.h"

#include "fsCAnimationDataSpine.h"

#include <algorithm>

#include <fsCore/fsPoint.h>
#include <fsCore/fsSColour.h>
#include <fsCore/fsVec2d.h>
#include <fsCore/src/debug/fsLog.h>

#include <fsRenderer/fsIRenderData.h>
#include <fsRenderer/src/fsCBrush.h>
#include <fsRenderer/src/fsCMaterial.h>
#include <fsRenderer/src/fsCTexturedMesh.h>

#include <spine/Animation.h>
#include <spine/AnimationState.h>
#include <spine/AnimationStateData.h>
#include <spine/Attachment.h>
#include <spine/Bone.h>
#include <spine/ClippingAttachment.h>
#include <spine/Event.h>
#include <spine/EventData.h>
#include <spine/MeshAttachment.h>
#include <spine/Physics.h>
#include <spine/RegionAttachment.h>
#include <spine/Skeleton.h>
#include <spine/SkeletonClipping.h>
#include <spine/SkeletonData.h>
#include <spine/Slot.h>
#include <spine/SlotData.h>

#include "fsCSpineTextureLoader.h"

class fsCSpineStateListener final : public spine::AnimationStateListenerObject
{
public:
	explicit fsCSpineStateListener(fsCAnimatingSpriteSpine* pOwner) : mOwner(pOwner)
	{
	}

	void callback(spine::AnimationState*, spine::EventType pType,
		spine::TrackEntry* pEntry, spine::Event* pEvent) override
	{
		if (!mOwner || pType == spine::EventType_Dispose)
		{
			return;
		}

		fsSAnimationEvent event;
		switch (pType)
		{
		case spine::EventType_Start: event.mType = fsSAnimationEvent::eSTART; break;
		case spine::EventType_Interrupt: event.mType = fsSAnimationEvent::eINTERRUPT; break;
		case spine::EventType_End: event.mType = fsSAnimationEvent::eEND; break;
		case spine::EventType_Complete: event.mType = fsSAnimationEvent::eCOMPLETE; break;
		case spine::EventType_Event: event.mType = fsSAnimationEvent::eEVENT; break;
		case spine::EventType_Dispose: return;
		}

		if (pEntry)
		{
			event.mTrackIndex = pEntry->getTrackIndex();
			if (pEntry->getAnimation())
			{
				event.mAnimation = pEntry->getAnimation()->getName().buffer();
			}
		}
		if (pEvent)
		{
			event.mName = pEvent->getData().getName().buffer();
			event.mTimeMs = static_cast<fsS32>(pEvent->getTime() * 1000.0f);
			event.mIntValue = pEvent->getIntValue();
			event.mFloatValue = pEvent->getFloatValue();
			event.mStringValue = pEvent->getStringValue().buffer();
			event.mAudioPath = pEvent->getData().getAudioPath().buffer();
			event.mVolume = pEvent->getVolume();
			event.mBalance = pEvent->getBalance();
		}
		mOwner->animationEventDispatchSpine(event);
	}

private:
	fsCAnimatingSpriteSpine* mOwner;
};

namespace
{
	struct fsSSpineBounds
	{
		float mX = 0.0f;
		float mTop = 0.0f;
		float mWidth = 0.0f;
		float mHeight = 0.0f;
	};

	fsBool boundsGet(spine::Skeleton* pSkeleton, fsSSpineBounds& pBounds)
	{
		if (!pSkeleton)
		{
			return false;
		}

		spine::Vector<float> scratch;
		spine::SkeletonClipping clipper;
		float spineY = 0.0f;
		pSkeleton->getBounds(pBounds.mX, spineY, pBounds.mWidth, pBounds.mHeight, scratch, &clipper);
		pBounds.mTop = -(spineY + pBounds.mHeight);
		return pBounds.mWidth > 0.0f && pBounds.mHeight > 0.0f;
	}

	fsU8 colourByteGet(float pChannel)
	{
		return static_cast<fsU8>(std::clamp(pChannel, 0.0f, 1.0f) * 255.0f);
	}

	fsSColour4B colourGet(const spine::Color& pSkeletonColour,
						 const spine::Color& pSlotColour,
						 const spine::Color& pAttachmentColour,
						 fsBool pPremultipliedAlpha)
	{
		const float alpha = pSkeletonColour.a * pSlotColour.a * pAttachmentColour.a;
		const float rgbMultiplier = pPremultipliedAlpha ? alpha : 1.0f;
		return fsSColour4B(
			colourByteGet(pSkeletonColour.r * pSlotColour.r * pAttachmentColour.r * rgbMultiplier),
			colourByteGet(pSkeletonColour.g * pSlotColour.g * pAttachmentColour.g * rgbMultiplier),
			colourByteGet(pSkeletonColour.b * pSlotColour.b * pAttachmentColour.b * rgbMultiplier),
			colourByteGet(alpha));
	}

	fsS32 blendModeGet(spine::BlendMode pBlendMode, fsBool pPremultipliedAlpha)
	{
		switch (pBlendMode)
		{
		case spine::BlendMode_Additive:
			return pPremultipliedAlpha
				? fsCMaterial::eALPHA_ADD_PREMULTIPLIED
				: fsCMaterial::eALPHA_ADD;
		case spine::BlendMode_Multiply:
			return fsCMaterial::eALPHA_MULTIPLY;
		case spine::BlendMode_Screen:
			return fsCMaterial::eALPHA_SCREEN;
		case spine::BlendMode_Normal:
		default:
			return pPremultipliedAlpha
				? fsCMaterial::eALPHA_BLEND_PREMULTIPLIED
				: fsCMaterial::eALPHA_BLEND;
		}
	}

	void verticesSet(fsIRenderData::fsSPolyInfo& pMesh,
					 const float* pVertices, const float* pUVs, size_t pVertexCount,
					 const fsSColour4B& pColour)
	{
		pMesh.mTris.resize(pVertexCount);
		for (size_t vertexIndex = 0; vertexIndex < pVertexCount; ++vertexIndex)
		{
			auto& vertex = pMesh.mTris[vertexIndex];
			vertex.vertices = fsVec3d(pVertices[vertexIndex * 2], -pVertices[vertexIndex * 2 + 1], 0.0f);
			vertex.texCoords = fsIRenderData::Tex2F(pUVs[vertexIndex * 2], pUVs[vertexIndex * 2 + 1]);
			vertex.colors = pColour;
		}
	}

	void trianglesSet(fsIRenderData::fsSPolyInfo& pMesh,
					  const unsigned short* pTriangles, size_t pTriangleIndexCount)
	{
		pMesh.mTriangles.reserve(pTriangleIndexCount);
		for (size_t index = 0; index + 2 < pTriangleIndexCount; index += 3)
		{
			// Fission's screen space points down, so reverse the Spine winding
			// after negating Y.
			pMesh.mTriangles.push_back(pTriangles[index]);
			pMesh.mTriangles.push_back(pTriangles[index + 2]);
			pMesh.mTriangles.push_back(pTriangles[index + 1]);
		}
	}

	fsBool barycentricGet(const fsVec2d& pPoint,
		const fsVec3d& pA, const fsVec3d& pB, const fsVec3d& pC,
		fsF32& pWeightA, fsF32& pWeightB, fsF32& pWeightC)
	{
		const fsF32 denominator = (pB.y - pC.y) * (pA.x - pC.x)
			+ (pC.x - pB.x) * (pA.y - pC.y);
		if (denominator == 0.0f)
		{
			return false;
		}

		pWeightA = ((pB.y - pC.y) * (pPoint.x - pC.x)
			+ (pC.x - pB.x) * (pPoint.y - pC.y)) / denominator;
		pWeightB = ((pC.y - pA.y) * (pPoint.x - pC.x)
			+ (pA.x - pC.x) * (pPoint.y - pC.y)) / denominator;
		pWeightC = 1.0f - pWeightA - pWeightB;
		return pWeightA >= 0.0f && pWeightB >= 0.0f && pWeightC >= 0.0f;
	}
}

void fsCAnimatingSpriteSpine::activeSet(fsBool pVisibility)
{
	if (mSkeleton)
	{
		mActive = pVisibility;
	}
}

void fsCAnimatingSpriteSpine::animationPlayDo(const fsStr& pAnimation)
{
	animationPlayDo(pAnimation, false);
}

void fsCAnimatingSpriteSpine::animationPlayDo(const fsStr& pAnimation, fsBool pLooping)
{
	animationTrackPlayDo(0, pAnimation, pLooping);
}

fsBool fsCAnimatingSpriteSpine::animationQueueDo(const fsStr& pAnimation, fsBool pLooping)
{
	return animationTrackQueueDo(0, pAnimation, pLooping);
}

fsBool fsCAnimatingSpriteSpine::animationTrackPlayDo(fsS32 pTrackIndex,
	const fsStr& pAnimation, fsBool pLooping)
{
	if (!mState || !mAnimationData || pTrackIndex < 0)
	{
		return false;
	}

	spine::Animation* const animation = mAnimationData->mSkeletonData->findAnimation(pAnimation.utf8Get());
	if (!animation)
	{
		fsLogError("Spine animation not found: %s", pAnimation.utf8Get());
		return false;
	}

	mState->setAnimation(static_cast<size_t>(pTrackIndex), animation, pLooping);
	if (pTrackIndex == 0)
	{
		resetDo();
	}
	else
	{
		poseApply(spine::Physics_Pose);
		geometryUpdate();
		mActive = true;
	}
	return true;
}

fsBool fsCAnimatingSpriteSpine::animationTrackQueueDo(fsS32 pTrackIndex,
	const fsStr& pAnimation, fsBool pLooping)
{
	if (!mState || !mAnimationData || pTrackIndex < 0)
	{
		return false;
	}

	spine::Animation* const animation = mAnimationData->mSkeletonData->findAnimation(pAnimation.utf8Get());
	if (!animation)
	{
		fsLogError("Spine animation not found: %s", pAnimation.utf8Get());
		return false;
	}

	mState->addAnimation(static_cast<size_t>(pTrackIndex), animation, pLooping, 0.0f);
	mActive = true;
	return true;
}

fsBool fsCAnimatingSpriteSpine::animationTrackClearDo(fsS32 pTrackIndex)
{
	if (!mState || !mSkeleton || pTrackIndex < 0
		|| static_cast<size_t>(pTrackIndex) >= mState->getTracks().size())
	{
		return false;
	}

	mState->clearTrack(static_cast<size_t>(pTrackIndex));
	mSkeleton->setToSetupPose();
	poseApply(spine::Physics_Pose);
	geometryUpdate();
	activeUpdate();
	return true;
}

void fsCAnimatingSpriteSpine::animationTracksClearDo()
{
	if (!mState || !mSkeleton)
	{
		return;
	}

	mState->clearTracks();
	mSkeleton->setToSetupPose();
	mSkeleton->updateWorldTransform(spine::Physics_Pose);
	geometryUpdate();
	mActive = false;
}

void fsCAnimatingSpriteSpine::animationMixDurationSetDo(fsS32 pDurationMs)
{
	if (mStateData)
	{
		mStateData->setDefaultMix(static_cast<float>(std::max<fsS32>(0, pDurationMs)) / 1000.0f);
	}
}

fsBool fsCAnimatingSpriteSpine::skinSetDo(const fsStr& pSkin)
{
	if (!mSkeleton || !mAnimationData)
	{
		return false;
	}

	spine::Skin* const skin = pSkin.emptyGet()
		? mAnimationData->mSkeletonData->getDefaultSkin()
		: mAnimationData->mSkeletonData->findSkin(pSkin.utf8Get());
	if (!skin)
	{
		fsLogError("Spine skin not found: %s", pSkin.utf8Get());
		return false;
	}

	mSkeleton->setSkin(skin);
	mSkeleton->setSlotsToSetupPose();
	poseApply(spine::Physics_Pose);
	geometryUpdate();
	return true;
}

fsBool fsCAnimatingSpriteSpine::attachmentSetDo(const fsStr& pSlot, const fsStr& pAttachment)
{
	if (!mSkeleton || pSlot.emptyGet())
	{
		return false;
	}
	if (!mSkeleton->findSlot(pSlot.utf8Get()))
	{
		fsLogError("Spine slot not found: %s", pSlot.utf8Get());
		return false;
	}
	if (!pAttachment.emptyGet()
		&& !mSkeleton->getAttachment(pSlot.utf8Get(), pAttachment.utf8Get()))
	{
		fsLogError("Spine attachment not found: %s/%s", pSlot.utf8Get(), pAttachment.utf8Get());
		return false;
	}

	mSkeleton->setAttachment(pSlot.utf8Get(), pAttachment.utf8Get());
	geometryUpdate();
	return true;
}

void fsCAnimatingSpriteSpine::colourApplyToSpriteFrames(const fsSColour4B& pColour)
{
	if (mSkeleton)
	{
		constexpr fsF32 BYTE_TO_FLOAT = 1.0f / 255.0f;
		mSkeleton->getColor().set(pColour.mR * BYTE_TO_FLOAT,
								  pColour.mG * BYTE_TO_FLOAT,
								  pColour.mB * BYTE_TO_FLOAT,
								  pColour.mA * BYTE_TO_FLOAT);
		geometryUpdate();
	}
}

void fsCAnimatingSpriteSpine::animationDataSetDo(std::shared_ptr<fsIAnimationData> pAnimData)
{
	instanceDestroy();
	mAnimationData = std::dynamic_pointer_cast<fsCAnimationDataSpine>(pAnimData);
	instanceCreate();
}

fsS32 fsCAnimatingSpriteSpine::currentFrameHeightGetDo() const
{
	fsSSpineBounds bounds;
	return boundsGet(mSkeleton, bounds) ? static_cast<fsS32>(bounds.mHeight) : 0;
}

fsPoint fsCAnimatingSpriteSpine::currentFramePosGetDo() const
{
	fsSSpineBounds bounds;
	return boundsGet(mSkeleton, bounds)
		? fsPoint(static_cast<fsS32>(bounds.mX), static_cast<fsS32>(bounds.mTop))
		: fsPoint(0, 0);
}

std::shared_ptr<fsCBrush> fsCAnimatingSpriteSpine::brushGetDo() const
{
	return mRenderableCount > 0 ? mMeshes[0]->brushGet() : nullptr;
}

fsBool fsCAnimatingSpriteSpine::hitTestDo(const fsPoint& pPoint, fsBool pRequireHitMap) const
{
	if (!mActive || !mSkeleton)
	{
		return false;
	}

	fsVec2d localPoint;
	if (!mRenderMtx.fsVec2DInverseTransform(
		fsVec2d(static_cast<fsF32>(pPoint.x), static_cast<fsF32>(pPoint.y)), &localPoint))
	{
		return false;
	}

	if (pRequireHitMap)
	{
		for (size_t reverseIndex = mRenderableCount; reverseIndex > 0; --reverseIndex)
		{
			const fsCTexturedMesh& renderMesh = *mMeshes[reverseIndex - 1];
			const std::shared_ptr<fsCBrush> brush = renderMesh.brushGet();
			if (!brush || !brush->hasHitMapGet())
			{
				continue;
			}

			const auto& mesh = renderMesh.meshGet();
			for (size_t triangleIndex = 0; triangleIndex + 2 < mesh.mTriangles.size(); triangleIndex += 3)
			{
				const fsU16 indexA = mesh.mTriangles[triangleIndex];
				const fsU16 indexB = mesh.mTriangles[triangleIndex + 1];
				const fsU16 indexC = mesh.mTriangles[triangleIndex + 2];
				if (indexA >= mesh.mTris.size() || indexB >= mesh.mTris.size() || indexC >= mesh.mTris.size())
				{
					continue;
				}

				fsF32 weightA;
				fsF32 weightB;
				fsF32 weightC;
				const auto& vertexA = mesh.mTris[indexA];
				const auto& vertexB = mesh.mTris[indexB];
				const auto& vertexC = mesh.mTris[indexC];
				if (!barycentricGet(localPoint, vertexA.vertices, vertexB.vertices, vertexC.vertices,
					weightA, weightB, weightC))
				{
					continue;
				}

				const fsF32 u = vertexA.texCoords.mU * weightA
					+ vertexB.texCoords.mU * weightB + vertexC.texCoords.mU * weightC;
				const fsF32 v = vertexA.texCoords.mV * weightA
					+ vertexB.texCoords.mV * weightB + vertexC.texCoords.mV * weightC;
				const fsPoint& dimensions = brush->dimensionsGet();
				if (dimensions.x <= 0 || dimensions.y <= 0)
				{
					continue;
				}
				const fsS32 pixelX = std::clamp(static_cast<fsS32>(u * dimensions.x), 0, dimensions.x - 1);
				const fsS32 pixelY = std::clamp(static_cast<fsS32>(v * dimensions.y), 0, dimensions.y - 1);
				if (brush->hitTest(fsPoint(pixelX, pixelY)))
				{
					return true;
				}
			}
		}
		return false;
	}

	fsSSpineBounds bounds;
	return boundsGet(mSkeleton, bounds)
		&& localPoint.x >= bounds.mX && localPoint.x < bounds.mX + bounds.mWidth
		&& localPoint.y >= bounds.mTop && localPoint.y < bounds.mTop + bounds.mHeight;
}

fsS32 fsCAnimatingSpriteSpine::currentFrameGetDo() const
{
	const float fps = mSkeleton ? mSkeleton->getData()->getFps() : 0.0f;
	spine::TrackEntry* const current = mState ? mState->getCurrent(0) : nullptr;
	return current && fps > 0.0f
		? static_cast<fsS32>(current->getAnimationTime() * fps)
		: 0;
}

void fsCAnimatingSpriteSpine::updateDo(fsS32 pDeltaMS)
{
	if (!mActive || !mState || !mSkeleton)
	{
		return;
	}

	pDeltaMS = std::max<fsS32>(0, pDeltaMS);
	const float deltaSeconds = static_cast<float>(pDeltaMS) / 1000.0f;
	mState->update(deltaSeconds);
	mState->apply(*mSkeleton);
	mSkeleton->update(deltaSeconds);
	mSkeleton->updateWorldTransform(spine::Physics_Update);
	geometryUpdate();

	activeUpdate();
}

fsBool fsCAnimatingSpriteSpine::loopingGetDo() const
{
	spine::TrackEntry* const current = mState ? mState->getCurrent(0) : nullptr;
	return current && current->getLoop();
}

void fsCAnimatingSpriteSpine::resetDo()
{
	if (!mState || !mSkeleton)
	{
		mActive = false;
		return;
	}

	spine::TrackEntry* const current = mState->getCurrent(0);
	if (current)
	{
		current->setTrackTime(0.0f);
	}
	mSkeleton->setToSetupPose();
	poseApply(spine::Physics_Reset);
	geometryUpdate();
	activeUpdate();
}

void fsCAnimatingSpriteSpine::matrixApplyDo(const fsAffineMtx2d& pRenderMtx)
{
	for (size_t meshIndex = 0; meshIndex < mRenderableCount; ++meshIndex)
	{
		mMeshes[meshIndex]->matrixApply(pRenderMtx);
	}
}

void fsCAnimatingSpriteSpine::renderDo()
{
	for (size_t meshIndex = 0; meshIndex < mRenderableCount; ++meshIndex)
	{
		mMeshes[meshIndex]->render();
	}
}

void fsCAnimatingSpriteSpine::poseApply(spine::Physics pPhysics)
{
	mState->apply(*mSkeleton);
	mSkeleton->updateWorldTransform(pPhysics);
}

void fsCAnimatingSpriteSpine::animationEventDispatchSpine(const fsSAnimationEvent& pEvent) const
{
	animationEventDispatch(pEvent);
}

void fsCAnimatingSpriteSpine::activeUpdate()
{
	mActive = false;
	if (!mState)
	{
		return;
	}

	spine::Vector<spine::TrackEntry*>& tracks = mState->getTracks();
	for (size_t trackIndex = 0; trackIndex < tracks.size(); ++trackIndex)
	{
		spine::TrackEntry* const entry = tracks[trackIndex];
		if (entry && (entry->getLoop() || !entry->isComplete() || entry->getNext()))
		{
			mActive = true;
			return;
		}
	}
}

void fsCAnimatingSpriteSpine::geometryUpdate()
{
	mRenderableCount = 0;
	if (!mSkeleton || !fsCTexturedMesh::supportedGet())
	{
		return;
	}

	auto& drawOrder = mSkeleton->getDrawOrder();
	while (mMeshes.size() < drawOrder.size())
	{
		mMeshes.emplace_back(new fsCTexturedMesh());
	}

	static unsigned short QUAD_TRIANGLES[] = {0, 1, 2, 2, 3, 0};
	spine::SkeletonClipping clipper;
	for (size_t slotIndex = 0; slotIndex < drawOrder.size(); ++slotIndex)
	{
		spine::Slot* const slot = drawOrder[slotIndex];
		spine::Attachment* const attachment = slot->getAttachment();
		if (!attachment || !slot->getBone().isActive() || !slot->getData().isVisible())
		{
			clipper.clipEnd(*slot);
			continue;
		}

		spine::TextureRegion* region = nullptr;<--- Variable 'region' can be declared as pointer to const
		const spine::Color* attachmentColour = nullptr;
		float* uvs = nullptr;
		unsigned short* triangles = nullptr;
		size_t triangleIndexCount = 0;
		std::vector<float> worldVertices;
		if (attachment->getRTTI().isExactly(spine::RegionAttachment::rtti))
		{
			auto* const regionAttachment = static_cast<spine::RegionAttachment*>(attachment);
			worldVertices.resize(8);
			regionAttachment->computeWorldVertices(*slot, worldVertices.data(), 0, 2);
			region = regionAttachment->getRegion();
			attachmentColour = &regionAttachment->getColor();
			uvs = regionAttachment->getUVs().buffer();
			triangles = QUAD_TRIANGLES;
			triangleIndexCount = 6;
		}
		else if (attachment->getRTTI().isExactly(spine::MeshAttachment::rtti))
		{
			auto* const meshAttachment = static_cast<spine::MeshAttachment*>(attachment);
			worldVertices.resize(meshAttachment->getWorldVerticesLength());
			meshAttachment->computeWorldVertices(*slot, 0, worldVertices.size(), worldVertices.data(), 0, 2);
			region = meshAttachment->getRegion();
			attachmentColour = &meshAttachment->getColor();
			uvs = meshAttachment->getUVs().buffer();
			triangles = meshAttachment->getTriangles().buffer();
			triangleIndexCount = meshAttachment->getTriangles().size();
		}
		else if (attachment->getRTTI().isExactly(spine::ClippingAttachment::rtti))
		{
			clipper.clipStart(*slot, static_cast<spine::ClippingAttachment*>(attachment));
			continue;
		}
		else
		{
			clipper.clipEnd(*slot);
			continue;
		}

		if (clipper.isClipping())
		{
			clipper.clipTriangles(worldVertices.data(), triangles, triangleIndexCount, uvs, 2);
			if (clipper.getClippedVertices().size() > 0)
			{
				worldVertices.assign(clipper.getClippedVertices().buffer(),
					clipper.getClippedVertices().buffer() + clipper.getClippedVertices().size());
			}
			else
			{
				worldVertices.clear();
			}
			uvs = clipper.getClippedUVs().buffer();
			triangles = clipper.getClippedTriangles().buffer();
			triangleIndexCount = clipper.getClippedTriangles().size();
		}

		fsIRenderData::fsSPolyInfo mesh;
		const fsBool premultipliedAlpha = fsCSpineTextureLoader::premultipliedAlphaGet(region);
		verticesSet(mesh, worldVertices.data(), uvs, worldVertices.size() / 2,
			colourGet(mSkeleton->getColor(), slot->getColor(), *attachmentColour, premultipliedAlpha));
		trianglesSet(mesh, triangles, triangleIndexCount);
		clipper.clipEnd(*slot);

		std::shared_ptr<fsCBrush> brush = fsCSpineTextureLoader::brushGet(region);
		if (!brush || mesh.mTriangles.empty())
		{
			continue;
		}

		fsCTexturedMesh& renderMesh = *mMeshes[mRenderableCount++];
		if (renderMesh.brushGet() != brush)
		{
			renderMesh.brushSet(brush);
		}
		renderMesh.alphaBlendModeSet(blendModeGet(slot->getData().getBlendMode(), premultipliedAlpha));
		renderMesh.meshSet(mesh);
		renderMesh.matrixApply(mRenderMtx);
	}
	clipper.clipEnd();
}

void fsCAnimatingSpriteSpine::instanceCreate()
{
	if (!mAnimationData || !mAnimationData->mSkeletonData)
	{
		return;
	}
	if (!fsCTexturedMesh::supportedGet())
	{
		fsLogError("Spine requires textured-mesh support from the active renderer.");
		return;
	}

	mSkeleton = new spine::Skeleton(mAnimationData->mSkeletonData);
	mStateData = new spine::AnimationStateData(mAnimationData->mSkeletonData);
	mState = new spine::AnimationState(mStateData);
	mStateListener.reset(new fsCSpineStateListener(this));
	mState->setListener(mStateListener.get());

	auto& animations = mAnimationData->mSkeletonData->getAnimations();
	if (animations.size() > 0)
	{
		mState->setAnimation(0, animations[0], false);
	}
	resetDo();
}

void fsCAnimatingSpriteSpine::instanceDestroy()
{
	mActive = false;
	mRenderableCount = 0;
	mMeshes.clear();
	delete mState;
	mStateListener.reset();
	delete mStateData;
	delete mSkeleton;
	mState = nullptr;
	mStateData = nullptr;
	mSkeleton = nullptr;
}

fsCAnimatingSpriteSpine::fsCAnimatingSpriteSpine(fsCSprite* pAnimatingRootSprt) :
	fsIAnimatingSprite(pAnimatingRootSprt),
	mSkeleton(nullptr),
	mStateData(nullptr),
	mState(nullptr),
	mRenderableCount(0)
{
}

fsCAnimatingSpriteSpine::~fsCAnimatingSpriteSpine()
{
	instanceDestroy();
}