[c][cpp] Ported flipX/flipY -> scaleX/scaleY change. See #1142.

This commit is contained in:
badlogic 2018-07-23 15:16:21 +02:00
parent 172bf22d84
commit 5aab588928
8 changed files with 52 additions and 102 deletions

View File

@ -66,7 +66,7 @@ typedef struct spSkeleton {
spSkin* const skin;
spColor color;
float time;
int/*bool*/flipX, flipY;
float scaleX, scaleY;
float x, y;
#ifdef __cplusplus
@ -88,8 +88,8 @@ typedef struct spSkeleton {
skin(0),
color(),
time(0),
flipX(0),
flipY(0),
scaleX(1),
scaleY(1),
x(0), y(0) {
}
#endif

View File

@ -65,6 +65,8 @@ void spBone_updateWorldTransformWith (spBone* self, float x, float y, float rota
float cosine, sine;
float pa, pb, pc, pd;
spBone* parent = self->parent;
float sx = self->skeleton->scaleX;
float sy = self->skeleton->scaleY * (spBone_isYDown() ? -1 : 1);
self->ax = x;
self->ay = y;
@ -77,26 +79,12 @@ void spBone_updateWorldTransformWith (spBone* self, float x, float y, float rota
if (!parent) { /* Root bone. */
float rotationY = rotation + 90 + shearY;
float la = COS_DEG(rotation + shearX) * scaleX;
float lb = COS_DEG(rotationY) * scaleY;
float lc = SIN_DEG(rotation + shearX) * scaleX;
float ld = SIN_DEG(rotationY) * scaleY;
if (self->skeleton->flipX) {
x = -x;
la = -la;
lb = -lb;
}
if (self->skeleton->flipY != yDown) {
y = -y;
lc = -lc;
ld = -ld;
}
CONST_CAST(float, self->a) = la;
CONST_CAST(float, self->b) = lb;
CONST_CAST(float, self->c) = lc;
CONST_CAST(float, self->d) = ld;
CONST_CAST(float, self->worldX) = x + self->skeleton->x;
CONST_CAST(float, self->worldY) = y + self->skeleton->y;
CONST_CAST(float, self->a) = COS_DEG(rotation + shearX) * scaleX * sx;
CONST_CAST(float, self->b) = COS_DEG(rotationY) * scaleY * sy;
CONST_CAST(float, self->c) = SIN_DEG(rotation + shearX) * scaleX * sx;
CONST_CAST(float, self->d) = SIN_DEG(rotationY) * scaleY * sy;
CONST_CAST(float, self->worldX) = x * sx + self->skeleton->x;
CONST_CAST(float, self->worldY) = y * sy + self->skeleton->y;
return;
}
@ -159,8 +147,8 @@ void spBone_updateWorldTransformWith (spBone* self, float x, float y, float rota
float za, zc, s;
float r, zb, zd, la, lb, lc, ld;
cosine = COS_DEG(rotation); sine = SIN_DEG(rotation);
za = pa * cosine + pb * sine;
zc = pc * cosine + pd * sine;
za = (pa * cosine + pb * sine) / sx;
zc = (pc * cosine + pd * sine) / sy;
s = SQRT(za * za + zc * zc);
if (s > 0.00001f) s = 1 / s;
za *= s;
@ -173,25 +161,18 @@ void spBone_updateWorldTransformWith (spBone* self, float x, float y, float rota
lb = COS_DEG(90 + shearY) * scaleY;
lc = SIN_DEG(shearX) * scaleX;
ld = SIN_DEG(90 + shearY) * scaleY;
if (self->data->transformMode != SP_TRANSFORMMODE_NOSCALEORREFLECTION ? pa * pd - pb * pc < 0 : self->skeleton->flipX != self->skeleton->flipY) {
zb = -zb;
zd = -zd;
}
CONST_CAST(float, self->a) = za * la + zb * lc;
CONST_CAST(float, self->b) = za * lb + zb * ld;
CONST_CAST(float, self->c) = zc * la + zd * lc;
CONST_CAST(float, self->d) = zc * lb + zd * ld;
return;
break;
}
}
if (self->skeleton->flipX) {
CONST_CAST(float, self->a) = -self->a;
CONST_CAST(float, self->b) = -self->b;
}
if (self->skeleton->flipY != yDown) {
CONST_CAST(float, self->c) = -self->c;
CONST_CAST(float, self->d) = -self->d;
}
CONST_CAST(float, self->a) *= sx;
CONST_CAST(float, self->b) *= sx;
CONST_CAST(float, self->c) *= sy;
CONST_CAST(float, self->d) *= sy;
}
void spBone_setToSetupPose (spBone* self) {

View File

@ -119,6 +119,9 @@ spSkeleton* spSkeleton_create (spSkeletonData* data) {
spColor_setFromFloats(&self->color, 1, 1, 1, 1);
self->scaleX = 1;
self->scaleY = 1;
spSkeleton_updateCache(self);
FREE(childrenCounts);

View File

@ -204,13 +204,13 @@ public:
void setY(float inValue);
bool getFlipX();
float getScaleX();
void setFlipX(bool inValue);
void setScaleX(float inValue);
bool getFlipY();
float getScaleY();
void setFlipY(bool inValue);
void setScaleY(float inValue);
private:
SkeletonData *_data;
@ -225,7 +225,7 @@ private:
Skin *_skin;
Color _color;
float _time;
bool _flipX, _flipY;
float _scaleX, _scaleY;
float _x, _y;
void sortIkConstraint(IkConstraint *constraint);

View File

@ -101,26 +101,14 @@ Bone::updateWorldTransform(float x, float y, float rotation, float scaleX, float
if (!parent) { /* Root bone. */
float rotationY = rotation + 90 + shearY;
float la = MathUtil::cosDeg(rotation + shearX) * scaleX;
float lb = MathUtil::cosDeg(rotationY) * scaleY;
float lc = MathUtil::sinDeg(rotation + shearX) * scaleX;
float ld = MathUtil::sinDeg(rotationY) * scaleY;
if (_skeleton.getFlipX()) {
x = -x;
la = -la;
lb = -lb;
}
if (_skeleton.getFlipY() != yDown) {
y = -y;
lc = -lc;
ld = -ld;
}
_a = la;
_b = lb;
_c = lc;
_d = ld;
_worldX = x + _skeleton.getX();
_worldY = y + _skeleton.getY();
float sx = _skeleton.getScaleX();
float sy = _skeleton.getScaleY();
_a = MathUtil::cosDeg(rotation + shearX) * scaleX * sx;
_b = MathUtil::cosDeg(rotationY) * scaleY * sy;
_c = MathUtil::sinDeg(rotation + shearX) * scaleX * sx;
_d = MathUtil::sinDeg(rotationY) * scaleY * sy;
_worldX = x * sx + _skeleton.getX();
_worldY = y * sy + _skeleton.getY();
return;
}
@ -184,8 +172,8 @@ Bone::updateWorldTransform(float x, float y, float rotation, float scaleX, float
float r, zb, zd, la, lb, lc, ld;
cosine = MathUtil::cosDeg(rotation);
sine = MathUtil::sinDeg(rotation);
za = pa * cosine + pb * sine;
zc = pc * cosine + pd * sine;
za = (pa * cosine + pb * sine) / _skeleton.getScaleX();
zc = (pc * cosine + pd * sine) / _skeleton.getScaleY();
s = MathUtil::sqrt(za * za + zc * zc);
if (s > 0.00001f) s = 1 / s;
za *= s;
@ -198,26 +186,17 @@ Bone::updateWorldTransform(float x, float y, float rotation, float scaleX, float
lb = MathUtil::cosDeg(90 + shearY) * scaleY;
lc = MathUtil::sinDeg(shearX) * scaleX;
ld = MathUtil::sinDeg(90 + shearY) * scaleY;
if (getData().getTransformMode() != TransformMode_NoScaleOrReflection ? pa * pd - pb * pc < 0 :
_skeleton.getFlipX() != _skeleton.getFlipY()) {
zb = -zb;
zd = -zd;
}
_a = za * la + zb * lc;
_b = za * lb + zb * ld;
_c = zc * la + zd * lc;
_d = zc * lb + zd * ld;
return;
break;
}
}
if (_skeleton.getFlipX()) {
_a = -_a;
_b = -_b;
}
if (_skeleton.getFlipY() != yDown) {
_c = -_c;
_d = -_d;
}
_a *= _skeleton.getScaleX();
_b *= _skeleton.getScaleX();
_c *= _skeleton.getScaleY();
_d *= _skeleton.getScaleY();
}
void Bone::setToSetupPose() {

View File

@ -57,8 +57,8 @@ Skeleton::Skeleton(SkeletonData *skeletonData) :
_skin(NULL),
_color(1, 1, 1, 1),
_time(0),
_flipX(false),
_flipY(false),
_scaleX(1),
_scaleY(1),
_x(0),
_y(0) {
_bones.ensureCapacity(_data->getBones().size());
@ -513,20 +513,20 @@ void Skeleton::setY(float inValue) {
_y = inValue;
}
bool Skeleton::getFlipX() {
return _flipX;
float Skeleton::getScaleX() {
return _scaleX;
}
void Skeleton::setFlipX(bool inValue) {
_flipX = inValue;
void Skeleton::setScaleX(float inValue) {
_scaleX = inValue;
}
bool Skeleton::getFlipY() {
return _flipY;
float Skeleton::getScaleY() {
return _scaleY * (Bone::isYDown() ? -1 : 1);
}
void Skeleton::setFlipY(bool inValue) {
_flipY = inValue;
void Skeleton::setScaleY(float inValue) {
_scaleY = inValue;
}
void Skeleton::sortIkConstraint(IkConstraint *constraint) {

View File

@ -119,8 +119,6 @@ void spineboy (SkeletonData* skeletonData, Atlas* atlas) {
drawable->timeScale = 1;
Skeleton* skeleton = drawable->skeleton;
skeleton->flipX = false;
skeleton->flipY = false;
Skeleton_setToSetupPose(skeleton);
skeleton->x = 320;
@ -170,8 +168,6 @@ void goblins (SkeletonData* skeletonData, Atlas* atlas) {
drawable->timeScale = 1;
Skeleton* skeleton = drawable->skeleton;
skeleton->flipX = false;
skeleton->flipY = false;
Skeleton_setSkinByName(skeleton, "goblin");
Skeleton_setSlotsToSetupPose(skeleton);
//Skeleton_setAttachment(skeleton, "left hand item", "dagger");
@ -307,9 +303,6 @@ void stretchyman (SkeletonData* skeletonData, Atlas* atlas) {
drawable->timeScale = 1;
Skeleton* skeleton = drawable->skeleton;
skeleton->flipX = false;
skeleton->flipY = false;
skeleton->x = 100;
skeleton->y = 590;
Skeleton_updateWorldTransform(skeleton);

View File

@ -118,8 +118,6 @@ void spineboy (SkeletonData* skeletonData, Atlas* atlas) {
drawable->timeScale = 1;
Skeleton* skeleton = drawable->skeleton;
skeleton->setFlipX(false);
skeleton->setFlipY(false);
skeleton->setToSetupPose();
skeleton->setPosition(320, 590);
@ -168,8 +166,6 @@ void goblins (SkeletonData* skeletonData, Atlas* atlas) {
drawable->timeScale = 1;
Skeleton* skeleton = drawable->skeleton;
skeleton->setFlipX(false);
skeleton->setFlipY(false);
skeleton->setSkin("goblin");
skeleton->setSlotsToSetupPose();
@ -309,8 +305,6 @@ void stretchyman (SkeletonData* skeletonData, Atlas* atlas) {
drawable->timeScale = 1;
Skeleton* skeleton = drawable->skeleton;
skeleton->setFlipX(false);
skeleton->setFlipY(false);
skeleton->setPosition(100, 590);
skeleton->updateWorldTransform();