This commit is contained in:
Stephen Gowen 2017-11-15 19:07:09 -05:00
parent 0b6c8411b7
commit 949eb15944
5 changed files with 253 additions and 99 deletions

View File

@ -0,0 +1,107 @@
/******************************************************************************
* Spine Runtimes Software License v2.5
*
* Copyright (c) 2013-2016, Esoteric Software
* All rights reserved.
*
* You are granted a perpetual, non-exclusive, non-sublicensable, and
* non-transferable license to use, install, execute, and perform the Spine
* Runtimes software and derivative works solely for personal or internal
* use. Without the written permission of Esoteric Software (see Section 2 of
* the Spine Software License Agreement), you may not (a) modify, translate,
* adapt, or develop new applications using the Spine Runtimes or otherwise
* create derivative works or improvements of the Spine Runtimes or (b) remove,
* delete, alter, or obscure any trademarks or any copyright, trademark, patent,
* or other intellectual property or proprietary rights notices on or in the
* Software, including any copy thereof. Redistributions in binary or source
* form must include this license and terms.
*
* THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS INTERRUPTION, OR LOSS OF
* USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
#ifndef Spine_Atlas_h
#define Spine_Atlas_h
#include <spine/Vector.h>
#include <string>
namespace Spine
{
enum Format
{
Format_Alpha,
Format_Intensity,
Format_LuminanceAlpha,
Format_RGB565,
Format_RGBA4444,
Format_RGB888,
Format_RGBA8888
};
enum TextureFilter
{
TextureFilter_Nearest,
TextureFilter_Linear,
TextureFilter_MipMap,
TextureFilter_MipMapNearestNearest,
TextureFilter_MipMapLinearNearest,
TextureFilter_MipMapNearestLinear,
TextureFilter_MipMapLinearLinear
};
enum TextureWrap
{
TextureWrap_MirroredRepeat,
TextureWrap_ClampToEdge,
TextureWrap_Repeat
};
class AtlasPage
{
public:
std::string name;
Format format;
TextureFilter minFilter;
TextureFilter magFilter;
TextureWrap uWrap;
TextureWrap vWrap;
void* rendererObject;
int width, height;
};
class AtlasRegion
{
public:
AtlasPage page;
std::string name;
int x, y, width, height;
float u, v, u2, v2;
float offsetX, offsetY;
int originalWidth, originalHeight;
int index;
bool rotate;
Vector<int> splits;
Vector<int> pads;
};
class Atlas
{
public:
/// Returns the first region found with the specified name. This method uses string comparison to find the region, so the result
/// should be cached rather than calling this method multiple times.
/// @return The region, or NULL.
AtlasRegion* findRegion(std::string name);
};
}
#endif /* Spine_Atlas_h */

View File

@ -33,8 +33,12 @@
#include <spine/AttachmentLoader.h> #include <spine/AttachmentLoader.h>
#include <spine/Vector.h>
namespace Spine namespace Spine
{ {
class Atlas;
/// ///
/// An AttachmentLoader that configures attachments using texture regions from an Atlas. /// An AttachmentLoader that configures attachments using texture regions from an Atlas.
/// See http://esotericsoftware.com/spine-loading-skeleton-data#JSON-and-binary-data about Loading Skeleton Data in the Spine Runtimes Guide. /// See http://esotericsoftware.com/spine-loading-skeleton-data#JSON-and-binary-data about Loading Skeleton Data in the Spine Runtimes Guide.
@ -44,16 +48,12 @@ namespace Spine
RTTI_DECL; RTTI_DECL;
public: public:
private: AtlasAttachmentLoader (Vector<Atlas*>& inAtlasArray) : _atlasArray(inAtlasArray)
Vector<Atlas> atlasArray;
public AtlasAttachmentLoader (params Atlas[] atlasArray)
{ {
if (atlasArray == NULL) throw new ArgumentNullException("atlas array cannot be NULL."); // Empty
_atlasArray = atlasArray;
} }
public RegionAttachment newRegionAttachment(Skin skin, std::string name, std::string path) RegionAttachment newRegionAttachment(Skin skin, std::string name, std::string path)
{ {
AtlasRegion* region = findRegion(path); AtlasRegion* region = findRegion(path);
if (region == NULL) throw new ArgumentException(std::string.Format("Region not found in atlas: {0} (region attachment: {1})", path, name)); if (region == NULL) throw new ArgumentException(std::string.Format("Region not found in atlas: {0} (region attachment: {1})", path, name));
@ -69,7 +69,7 @@ namespace Spine
return attachment; return attachment;
} }
public MeshAttachment newMeshAttachment(Skin skin, std::string name, std::string path) MeshAttachment newMeshAttachment(Skin skin, std::string name, std::string path)
{ {
AtlasRegion region = findRegion(path); AtlasRegion region = findRegion(path);
if (region == NULL) throw new ArgumentException(std::string.Format("Region not found in atlas: {0} (region attachment: {1})", path, name)); if (region == NULL) throw new ArgumentException(std::string.Format("Region not found in atlas: {0} (region attachment: {1})", path, name));
@ -91,41 +91,44 @@ namespace Spine
return attachment; return attachment;
} }
public BoundingBoxAttachment NewBoundingBoxAttachment (Skin skin, std::string name) BoundingBoxAttachment NewBoundingBoxAttachment (Skin skin, std::string name)
{ {
return new BoundingBoxAttachment(name); return new BoundingBoxAttachment(name);
} }
public PathAttachment newPathAttachment(Skin skin, std::string name) PathAttachment newPathAttachment(Skin skin, std::string name)
{ {
return new PathAttachment(name); return new PathAttachment(name);
} }
public PointAttachment newPointAttachment(Skin skin, std::string name) PointAttachment newPointAttachment(Skin skin, std::string name)
{ {
return new PointAttachment(name); return new PointAttachment(name);
} }
public ClippingAttachment newClippingAttachment(Skin skin, std::string name) ClippingAttachment newClippingAttachment(Skin skin, std::string name)
{ {
return new ClippingAttachment(name); return new ClippingAttachment(name);
} }
public AtlasRegion findRegion (std::string name) AtlasRegion* findRegion(std::string name)
{ {
AtlasRegion region; AtlasRegion* ret;
for (int i = 0; i < atlasArray.Length; i++) for (int i = 0; i < _atlasArray.size(); i++)
{ {
region = atlasArray[i].findRegion(name); ret = _atlasArray[i]->findRegion(name);
if (region != NULL) if (ret != NULL)
{ {
return region; return ret;
} }
} }
return NULL; return NULL;
} }
private:
Vector<Atlas*> _atlasArray;
} }
} }

View File

@ -39,13 +39,13 @@ namespace Spine
class Triangulator class Triangulator
{ {
public: public:
Vector<int> triangulate(Vector<float>& vertices); Vector<int>& triangulate(Vector<float>& vertices);
Vector<Vector<float> > decompose(Vector<float>& vertices, Vector<int>& triangles); Vector<Vector<float>* > decompose(Vector<float>& vertices, Vector<int>& triangles);
private: private:
Vector<Vector<float> > _convexPolygons; Vector<Vector<float>* > _convexPolygons;
Vector<Vector<int> > _convexPolygonsIndices; Vector<Vector<int>* > _convexPolygonsIndices;
Vector<int> _indices; Vector<int> _indices;
Vector<bool> _isConcaveArray; Vector<bool> _isConcaveArray;

View File

@ -0,0 +1,39 @@
/******************************************************************************
* Spine Runtimes Software License v2.5
*
* Copyright (c) 2013-2016, Esoteric Software
* All rights reserved.
*
* You are granted a perpetual, non-exclusive, non-sublicensable, and
* non-transferable license to use, install, execute, and perform the Spine
* Runtimes software and derivative works solely for personal or internal
* use. Without the written permission of Esoteric Software (see Section 2 of
* the Spine Software License Agreement), you may not (a) modify, translate,
* adapt, or develop new applications using the Spine Runtimes or otherwise
* create derivative works or improvements of the Spine Runtimes or (b) remove,
* delete, alter, or obscure any trademarks or any copyright, trademark, patent,
* or other intellectual property or proprietary rights notices on or in the
* Software, including any copy thereof. Redistributions in binary or source
* form must include this license and terms.
*
* THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS INTERRUPTION, OR LOSS OF
* USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
#include <spine/Atlas.h>
namespace Spine
{
AtlasRegion* Atlas::findRegion(std::string name)
{
return NULL;
}
}

View File

@ -34,7 +34,7 @@
namespace Spine namespace Spine
{ {
Vector<int> Triangulator::triangulate(Vector<float>& vertices) Vector<int>& Triangulator::triangulate(Vector<float>& vertices)
{ {
int vertexCount = static_cast<int>(vertices.size() >> 1); int vertexCount = static_cast<int>(vertices.size() >> 1);
@ -79,7 +79,7 @@ namespace Spine
} }
int v = indices[ii] << 1; int v = indices[ii] << 1;
float vx = vertices[v], vy = vertices[v + 1]; float& vx = vertices[v], vy = vertices[v + 1];
if (positiveArea(p3x, p3y, p1x, p1y, vx, vy)) if (positiveArea(p3x, p3y, p1x, p1y, vx, vy))
{ {
if (positiveArea(p1x, p1y, p2x, p2y, vx, vy)) if (positiveArea(p1x, p1y, p2x, p2y, vx, vy))
@ -117,8 +117,8 @@ namespace Spine
triangles.push_back(indices[(vertexCount + i - 1) % vertexCount]); triangles.push_back(indices[(vertexCount + i - 1) % vertexCount]);
triangles.push_back(indices[i]); triangles.push_back(indices[i]);
triangles.push_back(indices[(i + 1) % vertexCount]); triangles.push_back(indices[(i + 1) % vertexCount]);
indices.RemoveAt(i); indices.erase(i);
isConcaveArray.RemoveAt(i); isConcaveArray.erase(i);
vertexCount--; vertexCount--;
int previousIndex = (vertexCount + i - 1) % vertexCount; int previousIndex = (vertexCount + i - 1) % vertexCount;
@ -137,51 +137,52 @@ namespace Spine
return triangles; return triangles;
} }
Vector<Vector<float> > Triangulator::decompose(Vector<float>& vertices, Vector<int>& triangles) Vector<Vector<float>* > Triangulator::decompose(Vector<float>& vertices, Vector<int>& triangles)
{ {
Vector<Vector<float> >& convexPolygons = _convexPolygons; Vector<Vector<float>* >&convexPolygons = _convexPolygons;
for (int i = 0, n = convexPolygons.size(); i < n; ++i) for (size_t i = 0, n = convexPolygons.size(); i < n; ++i)
{ {
polygonPool.Free(convexPolygons[i]); _polygonPool.free(convexPolygons[i]);
} }
convexPolygons.Clear(); convexPolygons.clear();
Vector<Vector<int> >& convexPolygonsIndices = _convexPolygonsIndices; Vector<Vector<int>* > convexPolygonsIndices = _convexPolygonsIndices;
for (int i = 0, n = convexPolygonsIndices.size(); i < n; ++i) for (size_t i = 0, n = convexPolygonsIndices.size(); i < n; ++i)
{ {
_polygonIndicesPool.free(convexPolygonsIndices[i]); _polygonIndicesPool.free(convexPolygonsIndices[i]);
} }
convexPolygonsIndices.clear(); convexPolygonsIndices.clear();
var polygonIndices = _polygonIndicesPool.Obtain(); Vector<int>* polygonIndicesP = _polygonIndicesPool.obtain();
polygonIndices.Clear(); Vector<int>& polygonIndices = *polygonIndicesP;
polygonIndices.clear();
var polygon = polygonPool.Obtain(); Vector<float>* polygonP = _polygonPool.obtain();
polygon.Clear(); Vector<float>& polygon = *polygonP;
polygon.clear();
// Merge subsequent triangles if they form a triangle fan. // Merge subsequent triangles if they form a triangle fan.
int fanBaseIndex = -1, lastwinding = 0; int fanBaseIndex = -1, lastwinding = 0;
int[] trianglesItems = triangles.Items; for (size_t i = 0, n = triangles.size(); i < n; i += 3)
for (int i = 0, n = triangles.Count; i < n; i += 3)
{ {
int t1 = trianglesItems[i] << 1, t2 = trianglesItems[i + 1] << 1, t3 = trianglesItems[i + 2] << 1; int t1 = triangles[i] << 1, t2 = triangles[i + 1] << 1, t3 = triangles[i + 2] << 1;
float x1 = vertices[t1], y1 = vertices[t1 + 1]; float x1 = vertices[t1], y1 = vertices[t1 + 1];
float x2 = vertices[t2], y2 = vertices[t2 + 1]; float x2 = vertices[t2], y2 = vertices[t2 + 1];
float x3 = vertices[t3], y3 = vertices[t3 + 1]; float x3 = vertices[t3], y3 = vertices[t3 + 1];
// If the base of the last triangle is the same as this triangle, check if they form a convex polygon (triangle fan). // If the base of the last triangle is the same as this triangle, check if they form a convex polygon (triangle fan).
var merged = false; bool merged = false;
if (fanBaseIndex == t1) if (fanBaseIndex == t1)
{ {
int o = polygon.Count - 4; size_t o = polygon.size() - 4;
float[] p = polygon.Items; Vector<float>& p = polygon;
int winding1 = winding(p[o], p[o + 1], p[o + 2], p[o + 3], x3, y3); int winding1 = winding(p[o], p[o + 1], p[o + 2], p[o + 3], x3, y3);
int winding2 = winding(x3, y3, p[0], p[1], p[2], p[3]); int winding2 = winding(x3, y3, p[0], p[1], p[2], p[3]);
if (winding1 == lastwinding && winding2 == lastwinding) if (winding1 == lastwinding && winding2 == lastwinding)
{ {
polygon.Add(x3); polygon.push_back(x3);
polygon.Add(y3); polygon.push_back(y3);
polygonIndices.Add(t3); polygonIndices.push_back(t3);
merged = true; merged = true;
} }
} }
@ -189,57 +190,59 @@ namespace Spine
// Otherwise make this triangle the new base. // Otherwise make this triangle the new base.
if (!merged) if (!merged)
{ {
if (polygon.Count > 0) if (polygon.size() > 0)
{ {
convexPolygons.Add(polygon); convexPolygons.push_back(&polygon);
convexPolygonsIndices.Add(polygonIndices); convexPolygonsIndices.push_back(&polygonIndices);
} }
else else
{ {
polygonPool.Free(polygon); _polygonPool.free(&polygon);
_polygonIndicesPool.Free(polygonIndices); _polygonIndicesPool.free(&polygonIndices);
} }
polygon = polygonPool.Obtain(); polygon = *_polygonPool.obtain();
polygon.Clear(); polygon.clear();
polygon.Add(x1); polygon.push_back(x1);
polygon.Add(y1); polygon.push_back(y1);
polygon.Add(x2); polygon.push_back(x2);
polygon.Add(y2); polygon.push_back(y2);
polygon.Add(x3); polygon.push_back(x3);
polygon.Add(y3); polygon.push_back(y3);
polygonIndices = _polygonIndicesPool.obtain(); polygonIndices = *_polygonIndicesPool.obtain();
polygonIndices.Clear(); polygonIndices.clear();
polygonIndices.Add(t1); polygonIndices.push_back(t1);
polygonIndices.Add(t2); polygonIndices.push_back(t2);
polygonIndices.Add(t3); polygonIndices.push_back(t3);
lastwinding = winding(x1, y1, x2, y2, x3, y3); lastwinding = winding(x1, y1, x2, y2, x3, y3);
fanBaseIndex = t1; fanBaseIndex = t1;
} }
} }
if (polygon.Count > 0) if (polygon.size() > 0)
{ {
convexPolygons.Add(polygon); convexPolygons.push_back(&polygon);
convexPolygonsIndices.Add(polygonIndices); convexPolygonsIndices.push_back(&polygonIndices);
} }
// Go through the list of polygons and try to merge the remaining triangles with the found triangle fans. // Go through the list of polygons and try to merge the remaining triangles with the found triangle fans.
for (int i = 0, n = convexPolygons.Count; i < n; ++i) for (size_t i = 0, n = convexPolygons.size(); i < n; ++i)
{ {
polygonIndices = convexPolygonsIndices.Items[i]; polygonIndicesP = convexPolygonsIndices[i];
if (polygonIndices.Count == 0) continue; polygonIndices = *polygonIndicesP;
int firstIndex = polygonIndices.Items[0];
int lastIndex = polygonIndices.Items[polygonIndices.Count - 1];
polygon = convexPolygons.Items[i]; if (polygonIndices.size() == 0) continue;
int o = polygon.Count - 4; int firstIndex = polygonIndices[0];
float[] p = polygon.Items; int lastIndex = polygonIndices[polygonIndices.size() - 1];
polygon = *convexPolygons[i];
size_t o = polygon.size() - 4;
Vector<float>& p = polygon;
float prevPrevX = p[o], prevPrevY = p[o + 1]; float prevPrevX = p[o], prevPrevY = p[o + 1];
float prevX = p[o + 2], prevY = p[o + 3]; float prevX = p[o + 2], prevY = p[o + 3];
float firstX = p[0], firstY = p[1]; float firstX = p[0], firstY = p[1];
float secondX = p[2], secondY = p[3]; float secondX = p[2], secondY = p[3];
int winding = winding(prevPrevX, prevPrevY, prevX, prevY, firstX, firstY); int winding0 = winding(prevPrevX, prevPrevY, prevX, prevY, firstX, firstY);
for (int ii = 0; ii < n; ++ii) for (int ii = 0; ii < n; ++ii)
{ {
@ -248,19 +251,22 @@ namespace Spine
continue; continue;
} }
var otherIndices = convexPolygonsIndices.Items[ii]; Vector<int>* otherIndicesP = convexPolygonsIndices[ii];
Vector<int>& otherIndices = *otherIndicesP;
if (otherIndices.Count != 3) if (otherIndices.size() != 3)
{ {
continue; continue;
} }
int otherFirstIndex = otherIndices.Items[0]; int otherFirstIndex = otherIndices[0];
int otherSecondIndex = otherIndices.Items[1]; int otherSecondIndex = otherIndices[1];
int otherLastIndex = otherIndices.Items[2]; int otherLastIndex = otherIndices[2];
var otherPoly = convexPolygons.Items[ii]; Vector<float>* otherPolyP = convexPolygons[ii];
float x3 = otherPoly.Items[otherPoly.Count - 2], y3 = otherPoly.Items[otherPoly.Count - 1]; Vector<float>& otherPoly = *otherPolyP;
float x3 = otherPoly[otherPoly.size() - 2], y3 = otherPoly[otherPoly.size() - 1];
if (otherFirstIndex != firstIndex || otherSecondIndex != lastIndex) if (otherFirstIndex != firstIndex || otherSecondIndex != lastIndex)
{ {
@ -269,13 +275,13 @@ namespace Spine
int winding1 = winding(prevPrevX, prevPrevY, prevX, prevY, x3, y3); int winding1 = winding(prevPrevX, prevPrevY, prevX, prevY, x3, y3);
int winding2 = winding(x3, y3, firstX, firstY, secondX, secondY); int winding2 = winding(x3, y3, firstX, firstY, secondX, secondY);
if (winding1 == winding && winding2 == winding) if (winding1 == winding0 && winding2 == winding0)
{ {
otherPoly.Clear(); otherPoly.clear();
otherIndices.Clear(); otherIndices.clear();
polygon.Add(x3); polygon.push_back(x3);
polygon.Add(y3); polygon.push_back(y3);
polygonIndices.Add(otherLastIndex); polygonIndices.push_back(otherLastIndex);
prevPrevX = prevX; prevPrevX = prevX;
prevPrevY = prevY; prevPrevY = prevY;
prevX = x3; prevX = x3;
@ -286,16 +292,16 @@ namespace Spine
} }
// Remove empty polygons that resulted from the merge step above. // Remove empty polygons that resulted from the merge step above.
for (int i = convexPolygons.Count - 1; i >= 0; --i) for (int i = static_cast<int>(convexPolygons.size()) - 1; i >= 0; --i)
{ {
polygon = convexPolygons.Items[i]; polygon = *convexPolygons[i];
if (polygon.Count == 0) if (polygon.size() == 0)
{ {
convexPolygons.RemoveAt(i); convexPolygons.erase(i);
polygonPool.Free(polygon); _polygonPool.free(&polygon);
polygonIndices = convexPolygonsIndices.Items[i]; polygonIndices = *convexPolygonsIndices[i];
convexPolygonsIndices.RemoveAt(i); convexPolygonsIndices.erase(i);
_polygonIndicesPool.Free(polygonIndices); _polygonIndicesPool.free(&polygonIndices);
} }
} }
@ -308,8 +314,7 @@ namespace Spine
int current = indices[index] << 1; int current = indices[index] << 1;
int next = indices[(index + 1) % vertexCount] << 1; int next = indices[(index + 1) % vertexCount] << 1;
return !positiveArea(vertices[previous], vertices[previous + 1], vertices[current], vertices[current + 1], vertices[next], return !positiveArea(vertices[previous], vertices[previous + 1], vertices[current], vertices[current + 1], vertices[next], vertices[next + 1]);
vertices[next + 1]);
} }
bool Triangulator::positiveArea(float p1x, float p1y, float p2x, float p2y, float p3x, float p3y) bool Triangulator::positiveArea(float p1x, float p1y, float p2x, float p2y, float p3x, float p3y)