Sfoglia il codice sorgente

[cpp] Fixed memory allocations at runtime.

badlogic 7 anni fa
parent
commit
bceee601ad

+ 1 - 1
spine-cpp/spine-cpp/include/spine/PathConstraint.h

@@ -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);
         

+ 12 - 1
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 {

+ 6 - 1
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) {

+ 1 - 1
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<float> frames = rotateTimeline->_frames;
+	Vector<float>& frames = rotateTimeline->_frames;
 	if (time < frames[0]) {
 		if (pose == MixPose_Setup) {
 			bone->_rotation = bone->_data._rotation;

+ 2 - 2
spine-cpp/spine-cpp/src/spine/PathConstraint.cpp

@@ -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;

+ 10 - 2
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;
 	}
 }