Browse Source

Broken beginnings of ref counting attachments in C/C++.

badlogic 6 years ago
parent
commit
28b4694dbe

+ 3 - 1
spine-c/spine-c/include/spine/Attachment.h

@@ -52,13 +52,15 @@ typedef struct spAttachment {
 	const char* const name;
 	const spAttachmentType type;
 	const void* const vtable;
+	int refCount;
 	struct spAttachmentLoader* attachmentLoader;
 
 #ifdef __cplusplus
 	spAttachment() :
 		name(0),
 		type(SP_ATTACHMENT_REGION),
-		vtable(0) {
+		vtable(0),
+		refCount(0) {
 	}
 #endif
 } spAttachment;

+ 2 - 2
spine-c/spine-c/include/spine/Skin.h

@@ -77,7 +77,7 @@ SP_API spSkin* spSkin_create (const char* name);
 SP_API void spSkin_dispose (spSkin* self);
 
 /* The Skin owns the attachment. */
-SP_API void spSkin_addAttachment (spSkin* self, int slotIndex, const char* name, spAttachment* attachment);
+SP_API void spSkin_setAttachment (spSkin* self, int slotIndex, const char* name, spAttachment* attachment);
 /* Returns 0 if the attachment was not found. */
 SP_API spAttachment* spSkin_getAttachment (const spSkin* self, int slotIndex, const char* name);
 
@@ -91,7 +91,7 @@ SP_API void spSkin_attachAll (const spSkin* self, struct spSkeleton* skeleton, c
 typedef spSkin Skin;
 #define Skin_create(...) spSkin_create(__VA_ARGS__)
 #define Skin_dispose(...) spSkin_dispose(__VA_ARGS__)
-#define Skin_addAttachment(...) spSkin_addAttachment(__VA_ARGS__)
+#define Skin_setAttachment(...) spSkin_addAttachment(__VA_ARGS__)
 #define Skin_getAttachment(...) spSkin_getAttachment(__VA_ARGS__)
 #define Skin_getAttachmentName(...) spSkin_getAttachmentName(__VA_ARGS__)
 #define Skin_attachAll(...) spSkin_attachAll(__VA_ARGS__)

+ 5 - 1
spine-c/spine-c/src/spine/Attachment.c

@@ -43,6 +43,8 @@ void _spAttachment_init (spAttachment* self, const char* name, spAttachmentType
 
 	MALLOC_STR(self->name, name);
 	CONST_CAST(spAttachmentType, self->type) = type;
+
+	self->refCount++;
 }
 
 void _spAttachment_deinit (spAttachment* self) {
@@ -52,5 +54,7 @@ void _spAttachment_deinit (spAttachment* self) {
 }
 
 void spAttachment_dispose (spAttachment* self) {
-	VTABLE(spAttachment, self) ->dispose(self);
+	self->refCount--;
+	if (self->refCount == 0)
+		VTABLE(spAttachment, self) ->dispose(self);
 }

+ 1 - 1
spine-c/spine-c/src/spine/SkeletonBinary.c

@@ -831,7 +831,7 @@ spSkin* spSkeletonBinary_readSkin(spSkeletonBinary* self, _dataInput* input,
 		for (ii = 0, nn = readVarint(input, 1); ii < nn; ++ii) {
 			const char* name = readString(input);
 			spAttachment* attachment = spSkeletonBinary_readAttachment(self, input, skin, slotIndex, name, skeletonData, nonessential);
-			if (attachment) spSkin_addAttachment(skin, slotIndex, name, attachment);
+			if (attachment) spSkin_setAttachment(skin, slotIndex, name, attachment);
 			FREE(name);
 		}
 	}

+ 1 - 1
spine-c/spine-c/src/spine/SkeletonJson.c

@@ -1054,7 +1054,7 @@ spSkeletonData* spSkeletonJson_readSkeletonData (spSkeletonJson* self, const cha
 					}
 					}
 
-					spSkin_addAttachment(skin, slotIndex, skinAttachmentName, attachment);
+					spSkin_setAttachment(skin, slotIndex, skinAttachmentName, attachment);
 				}
 			}
 		}

+ 1 - 1
spine-c/spine-c/src/spine/Skin.c

@@ -90,7 +90,7 @@ void spSkin_dispose (spSkin* self) {
 	FREE(self);
 }
 
-void spSkin_addAttachment (spSkin* self, int slotIndex, const char* name, spAttachment* attachment) {
+void spSkin_setAttachment (spSkin* self, int slotIndex, const char* name, spAttachment* attachment) {
 	_Entry* newEntry = _Entry_create(slotIndex, name, attachment);
 	newEntry->next = SUB_CAST(_spSkin, self)->entries;
 	SUB_CAST(_spSkin, self)->entries = newEntry;