Skip to content

Commit

Permalink
[cpp] Fix SkeletinBinary, fix memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
badlogic committed Apr 1, 2024
1 parent ff4cfba commit 2dc7335
Show file tree
Hide file tree
Showing 12 changed files with 392 additions and 175 deletions.
2 changes: 2 additions & 0 deletions spine-cpp/spine-cpp/include/spine/ConstraintData.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ namespace spine {
/// The interface for all constraints.
class SP_API ConstraintData : public SpineObject {

friend class SkeletonBinary;

RTTI_DECL

public:
Expand Down
8 changes: 6 additions & 2 deletions spine-cpp/spine-cpp/include/spine/LinkedMesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,16 @@ namespace spine {
friend class SkeletonJson;

public:
LinkedMesh(MeshAttachment *mesh, const String &skin, size_t slotIndex, const String &parent,
LinkedMesh(MeshAttachment *mesh, const int skinIndex, size_t slotIndex, const String &parent,
bool inheritTimeline);

LinkedMesh(MeshAttachment *mesh, const String &skin, size_t slotIndex, const String &parent,
bool inheritTimeline);

private:
MeshAttachment *_mesh;
String _skin;
int _skinIndex;
String _skin;
size_t _slotIndex;
String _parent;
bool _inheritTimeline;
Expand Down
18 changes: 14 additions & 4 deletions spine-cpp/spine-cpp/include/spine/SkeletonBinary.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ namespace spine {
static const int BONE_SHEAR = 7;
static const int BONE_SHEARX = 8;
static const int BONE_SHEARY = 9;
static const int BONE_INHERIT = 10;

static const int SLOT_ATTACHMENT = 0;
static const int SLOT_RGBA = 1;
Expand All @@ -90,6 +91,15 @@ namespace spine {
static const int PATH_SPACING = 1;
static const int PATH_MIX = 2;

static const int PHYSICS_INERTIA = 0;
static const int PHYSICS_STRENGTH = 1;
static const int PHYSICS_DAMPING = 2;
static const int PHYSICS_MASS = 4;
static const int PHYSICS_WIND = 5;
static const int PHYSICS_GRAVITY = 6;
static const int PHYSICS_MIX = 7;
static const int PHYSICS_RESET = 8;

static const int CURVE_LINEAR = 0;
static const int CURVE_STEPPED = 1;
static const int CURVE_BEZIER = 2;
Expand Down Expand Up @@ -147,21 +157,21 @@ namespace spine {
Attachment *readAttachment(DataInput *input, Skin *skin, int slotIndex, const String &attachmentName,
SkeletonData *skeletonData, bool nonessential);

void readVertices(DataInput *input, Vector<float> &vertices, Vector<int> &bones, int vertexCount);
int readVertices(DataInput *input, Vector<float> &vertices, Vector<int> &bones, bool weighted);

void readFloatArray(DataInput *input, int n, float scale, Vector<float> &array);

void readShortArray(DataInput *input, Vector<unsigned short> &array);
void readShortArray(DataInput *input, Vector<unsigned short> &array, int n);

Animation *readAnimation(const String &name, DataInput *input, SkeletonData *skeletonData);

void
setBezier(DataInput *input, CurveTimeline *timeline, int bezier, int frame, int value, float time1, float time2,
float value1, float value2, float scale);

Timeline *readTimeline(DataInput *input, CurveTimeline1 *timeline, float scale);
void readTimeline(DataInput *input, Vector<Timeline*> &timelines, CurveTimeline1 *timeline, float scale);

Timeline *readTimeline2(DataInput *input, CurveTimeline2 *timeline, float scale);
void readTimeline2(DataInput *input, Vector<Timeline*> &timelines, CurveTimeline2 *timeline, float scale);
};
}

Expand Down
4 changes: 2 additions & 2 deletions spine-cpp/spine-cpp/include/spine/SpineString.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ namespace spine {
}

String substring(int startIndex, int length) const {
if (startIndex < 0 || startIndex >= _length || length < 0 || startIndex + length > _length) {
if (startIndex < 0 || startIndex >= (int)_length || length < 0 || startIndex + length > (int)_length) {
return String();
}
char* subStr = SpineExtension::calloc<char>(length + 1, __FILE__, __LINE__);
Expand All @@ -205,7 +205,7 @@ namespace spine {
}

String substring(int startIndex) const {
if (startIndex < 0 || startIndex >= _length) {
if (startIndex < 0 || startIndex >= (int)_length) {
return String();
}
int length = _length - startIndex;
Expand Down
7 changes: 4 additions & 3 deletions spine-cpp/spine-cpp/src/spine/IkConstraint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void IkConstraint::apply(Bone &bone, float targetX, float targetY, bool compress
float rotationIK = -bone._ashearX - bone._arotation;
float tx = 0, ty = 0;

switch (bone._data.getInherit()) {
switch (bone._inherit) {
case Inherit_OnlyTranslation:
tx = (targetX - bone._worldX) * MathUtil::sign(bone.getSkeleton().getScaleX());
ty = (targetY - bone._worldY) * MathUtil::sign(bone.getSkeleton().getScaleY());
Expand Down Expand Up @@ -77,7 +77,7 @@ void IkConstraint::apply(Bone &bone, float targetX, float targetY, bool compress
float sx = bone._ascaleX;
float sy = bone._ascaleY;
if (compress || stretch) {
switch (bone._data.getInherit()) {
switch (bone._inherit) {
case Inherit_NoScale:
case Inherit_NoScaleOrReflection:
tx = targetX - bone._worldX;
Expand Down Expand Up @@ -109,7 +109,8 @@ void IkConstraint::apply(Bone &parent, Bone &child, float targetX, float targetY
Bone *pp = parent.getParent();
float tx, ty, dx, dy, dd, l1, l2, a1, a2, r, td, sd, p;
float id, x, y;
px = parent._ax;
if (parent._inherit != Inherit_Normal || child._inherit != Inherit_Normal) return;
px = parent._ax;
py = parent._ay;
psx = parent._ascaleX;
psy = parent._ascaleY;
Expand Down
4 changes: 2 additions & 2 deletions spine-cpp/spine-cpp/src/spine/IkConstraintData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ RTTI_IMPL(IkConstraintData, ConstraintData)

IkConstraintData::IkConstraintData(const String &name) : ConstraintData(name),
_target(NULL),
_bendDirection(1),
_bendDirection(0),
_compress(false),
_stretch(false),
_uniform(false),
_mix(1),
_mix(0),
_softness(0) {
}

Expand Down
20 changes: 15 additions & 5 deletions spine-cpp/spine-cpp/src/spine/LinkedMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,20 @@

using namespace spine;

LinkedMesh::LinkedMesh(MeshAttachment *mesh, const int skinIndex, size_t slotIndex, const String &parent,
bool inheritTimeline) : _mesh(mesh),
_skinIndex(skinIndex),
_skin(""),
_slotIndex(slotIndex),
_parent(parent),
_inheritTimeline(inheritTimeline) {
}

LinkedMesh::LinkedMesh(MeshAttachment *mesh, const String &skin, size_t slotIndex, const String &parent,
bool inheritTimeline) : _mesh(mesh),
_skin(skin),
_slotIndex(slotIndex),
_parent(parent),
_inheritTimeline(inheritTimeline) {
bool inheritTimeline) : _mesh(mesh),
_skinIndex(-1),
_skin(skin),
_slotIndex(slotIndex),
_parent(parent),
_inheritTimeline(inheritTimeline) {
}
7 changes: 5 additions & 2 deletions spine-cpp/spine-cpp/src/spine/Skeleton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ Skeleton::~Skeleton() {
ContainerUtil::cleanUpVectorOfPointers(_ikConstraints);
ContainerUtil::cleanUpVectorOfPointers(_transformConstraints);
ContainerUtil::cleanUpVectorOfPointers(_pathConstraints);
ContainerUtil::cleanUpVectorOfPointers(_physicsConstraints);
}

void Skeleton::updateCache() {
Expand Down Expand Up @@ -195,7 +196,7 @@ void Skeleton::updateCache() {
}
}

for (size_t ii = 0; ii < pathCount; ++ii) {
for (size_t ii = 0; ii < physicsCount; ++ii) {
PhysicsConstraint *constraint = _physicsConstraints[ii];
if (constraint->getData().getOrder() == i) {
sortPhysicsConstraint(constraint);
Expand All @@ -222,7 +223,9 @@ void Skeleton::printUpdateCache() {
printf("ik constraint %s\n", ((IkConstraint *) updatable)->getData().getName().buffer());
} else if (updatable->getRTTI().isExactly(PathConstraint::rtti)) {
printf("path constraint %s\n", ((PathConstraint *) updatable)->getData().getName().buffer());
}
} else if (updatable->getRTTI().isExactly(PhysicsConstraint::rtti)) {
printf("physics constraint %s\n", ((PhysicsConstraint *) updatable)->getData().getName().buffer());
}
}
}

Expand Down
Loading

0 comments on commit 2dc7335

Please sign in to comment.