From bceee601adef495e58f0ea43efdfa138858d9a7c Mon Sep 17 00:00:00 2001 From: badlogic Date: Tue, 17 Apr 2018 12:30:35 +0200 Subject: [PATCH] [cpp] Fixed memory allocations at runtime. --- spine-cpp/spine-cpp/include/spine/PathConstraint.h | 2 +- spine-cpp/spine-cpp/include/spine/Skin.h | 13 ++++++++++++- spine-cpp/spine-cpp/include/spine/String.h | 7 ++++++- spine-cpp/spine-cpp/src/spine/AnimationState.cpp | 2 +- spine-cpp/spine-cpp/src/spine/PathConstraint.cpp | 4 ++-- spine-cpp/spine-cpp/src/spine/Skin.cpp | 12 ++++++++++-- 6 files changed, 32 insertions(+), 8 deletions(-) diff --git a/spine-cpp/spine-cpp/include/spine/PathConstraint.h b/spine-cpp/spine-cpp/include/spine/PathConstraint.h index 9bc041010..bfaadbdf8 100644 --- a/spine-cpp/spine-cpp/include/spine/PathConstraint.h +++ b/spine-cpp/spine-cpp/include/spine/PathConstraint.h @@ -97,7 +97,7 @@ namespace Spine { Vector _lengths; Vector _segments; - Vector computeWorldPositions(PathAttachment& path, int spacesCount, bool tangents, bool percentPosition, bool percentSpacing); + Vector& computeWorldPositions(PathAttachment& path, int spacesCount, bool tangents, bool percentPosition, bool percentSpacing); static void addBeforePosition(float p, Vector& temp, int i, Vector& output, int o); diff --git a/spine-cpp/spine-cpp/include/spine/Skin.h b/spine-cpp/spine-cpp/include/spine/Skin.h index b23730bb5..5f141d3f4 100644 --- a/spine-cpp/spine-cpp/include/spine/Skin.h +++ b/spine-cpp/spine-cpp/include/spine/Skin.h @@ -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 { diff --git a/spine-cpp/spine-cpp/include/spine/String.h b/spine-cpp/spine-cpp/include/spine/String.h index fda962329..189726ba3 100644 --- a/spine-cpp/spine-cpp/include/spine/String.h +++ b/spine-cpp/spine-cpp/include/spine/String.h @@ -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) { diff --git a/spine-cpp/spine-cpp/src/spine/AnimationState.cpp b/spine-cpp/spine-cpp/src/spine/AnimationState.cpp index 9394b5fd8..1c49a36d3 100644 --- a/spine-cpp/spine-cpp/src/spine/AnimationState.cpp +++ b/spine-cpp/spine-cpp/src/spine/AnimationState.cpp @@ -676,7 +676,7 @@ void AnimationState::applyRotateTimeline(RotateTimeline *rotateTimeline, Skeleto } Bone *bone = skeleton._bones[rotateTimeline->_boneIndex]; - Vector frames = rotateTimeline->_frames; + Vector& frames = rotateTimeline->_frames; if (time < frames[0]) { if (pose == MixPose_Setup) { bone->_rotation = bone->_data._rotation; diff --git a/spine-cpp/spine-cpp/src/spine/PathConstraint.cpp b/spine-cpp/spine-cpp/src/spine/PathConstraint.cpp index e01a7a8c1..64fd8e66d 100644 --- a/spine-cpp/spine-cpp/src/spine/PathConstraint.cpp +++ b/spine-cpp/spine-cpp/src/spine/PathConstraint.cpp @@ -125,7 +125,7 @@ void PathConstraint::update() { } } - Vector positions = computeWorldPositions(*attachment, spacesCount, tangents, + Vector& 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 +Vector& PathConstraint::computeWorldPositions(PathAttachment &path, int spacesCount, bool tangents, bool percentPosition, bool percentSpacing) { Slot &target = *_target; diff --git a/spine-cpp/spine-cpp/src/spine/Skin.cpp b/spine-cpp/spine-cpp/src/spine/Skin.cpp index efe600e88..33e40f6cb 100644 --- a/spine-cpp/spine-cpp/src/spine/Skin.cpp +++ b/spine-cpp/spine-cpp/src/spine/Skin.cpp @@ -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; } }