[cpp] Fixed memory allocations at runtime.

This commit is contained in:
badlogic 2018-04-17 12:30:35 +02:00
parent 906c0b4b60
commit bceee601ad
6 changed files with 32 additions and 8 deletions

View File

@ -97,7 +97,7 @@ namespace Spine {
Vector<float> _lengths;
Vector<float> _segments;
Vector<float> computeWorldPositions(PathAttachment& path, int spacesCount, bool tangents, bool percentPosition, bool percentSpacing);
Vector<float>& computeWorldPositions(PathAttachment& path, int spacesCount, bool tangents, bool percentPosition, bool percentSpacing);
static void addBeforePosition(float p, Vector<float>& temp, int i, Vector<float>& output, int o);

View File

@ -52,7 +52,10 @@ public:
int _slotIndex;
String _name;
explicit AttachmentKey(int slotIndex = 0, const String &name = "");
explicit AttachmentKey(int slotIndex = 0, const String &name = 0);
// Used in Skin::getAttachment to avoid allocation of temporary string
explicit AttachmentKey(int slotIndex, const char* name);
AttachmentKey(const AttachmentKey &other) {
this->_slotIndex = other._slotIndex;
@ -60,6 +63,14 @@ public:
}
bool operator==(const AttachmentKey &other) const;
int getSlotIndex() {
return _slotIndex;
}
String& getName() {
return _name;
}
};
struct HashAttachmentKey : public SpineObject {

View File

@ -100,13 +100,18 @@ public:
if (!chars) {
_length = 0;
_buffer = 0;
_buffer = NULL;
} else {
_length = strlen(chars);
_buffer = (char *) chars;
}
}
void unown() {
_length = 0;
_buffer = NULL;
}
String &operator=(const String &other) {
if (this == &other) return *this;
if (_buffer) {

View File

@ -676,7 +676,7 @@ void AnimationState::applyRotateTimeline(RotateTimeline *rotateTimeline, Skeleto
}
Bone *bone = skeleton._bones[rotateTimeline->_boneIndex];
Vector<float> frames = rotateTimeline->_frames;
Vector<float>& frames = rotateTimeline->_frames;
if (time < frames[0]) {
if (pose == MixPose_Setup) {
bone->_rotation = bone->_data._rotation;

View File

@ -125,7 +125,7 @@ void PathConstraint::update() {
}
}
Vector<float> positions = computeWorldPositions(*attachment, spacesCount, tangents,
Vector<float>& positions = computeWorldPositions(*attachment, spacesCount, tangents,
data.getPositionMode() == PositionMode_Percent,
spacingMode == SpacingMode_Percent);
float boneX = positions[0];
@ -254,7 +254,7 @@ PathConstraintData &PathConstraint::getData() {
return _data;
}
Vector<float>
Vector<float>&
PathConstraint::computeWorldPositions(PathAttachment &path, int spacesCount, bool tangents, bool percentPosition,
bool percentSpacing) {
Slot &target = *_target;

View File

@ -39,6 +39,11 @@
using namespace Spine;
Skin::AttachmentKey::AttachmentKey(int slotIndex, const char* name) :
_slotIndex(slotIndex),
_name(name, true) {
}
Skin::AttachmentKey::AttachmentKey(int slotIndex, const String &name) :
_slotIndex(slotIndex),
_name(name) {
@ -71,10 +76,13 @@ void Skin::addAttachment(int slotIndex, const String &name, Attachment *attachme
}
Attachment *Skin::getAttachment(int slotIndex, const String &name) {
AttachmentKey key(slotIndex, name);
AttachmentKey key(slotIndex, name.buffer());
if (_attachments.containsKey(key)) {
return _attachments[key];
Attachment *attachment = _attachments[key];
key.getName().unown();
return attachment;
} else {
key.getName().unown();
return NULL;
}
}