From 9a404e5c4956b21619dd3d7ca2b0148a45ade0cd Mon Sep 17 00:00:00 2001 From: NathanSweet Date: Sat, 2 Apr 2016 15:23:15 +0200 Subject: [PATCH] AttachmentLoader now has create, configure, dispose. Now the attachment loader should not be disposed until loaded attachments are disposed. --- spine-c/include/spine/Attachment.h | 3 ++ spine-c/include/spine/AttachmentLoader.h | 13 +++++-- spine-c/include/spine/MeshAttachment.h | 2 +- spine-c/include/spine/SkeletonJson.h | 2 ++ .../include/spine/WeightedMeshAttachment.h | 2 +- spine-c/include/spine/extension.h | 35 +++++++++++-------- spine-c/src/spine/AtlasAttachmentLoader.c | 4 +-- spine-c/src/spine/Attachment.c | 1 + spine-c/src/spine/AttachmentLoader.c | 33 ++++++++++++----- spine-c/src/spine/SkeletonJson.c | 13 ++++--- 10 files changed, 73 insertions(+), 35 deletions(-) diff --git a/spine-c/include/spine/Attachment.h b/spine-c/include/spine/Attachment.h index 71eccd3cb..f4a31b174 100644 --- a/spine-c/include/spine/Attachment.h +++ b/spine-c/include/spine/Attachment.h @@ -36,6 +36,8 @@ extern "C" { #endif +struct spAttachmentLoader; + typedef enum { SP_ATTACHMENT_REGION, SP_ATTACHMENT_BOUNDING_BOX, SP_ATTACHMENT_MESH, SP_ATTACHMENT_WEIGHTED_MESH } spAttachmentType; @@ -44,6 +46,7 @@ typedef struct spAttachment { const char* const name; const spAttachmentType type; const void* const vtable; + struct spAttachmentLoader* attachmentLoader; #ifdef __cplusplus spAttachment() : diff --git a/spine-c/include/spine/AttachmentLoader.h b/spine-c/include/spine/AttachmentLoader.h index 7c86e9af9..c1179ae8c 100644 --- a/spine-c/include/spine/AttachmentLoader.h +++ b/spine-c/include/spine/AttachmentLoader.h @@ -55,14 +55,21 @@ typedef struct spAttachmentLoader { void spAttachmentLoader_dispose (spAttachmentLoader* self); -/* Returns 0 to not load an attachment. If 0 is returned and spAttachmentLoader.error1 is set, an error occurred. */ -spAttachment* spAttachmentLoader_newAttachment (spAttachmentLoader* self, spSkin* skin, spAttachmentType type, const char* name, +/* Called to create each attachment. Returns 0 to not load an attachment. If 0 is returned and _spAttachmentLoader_setError was + * called, an error occurred. */ +spAttachment* spAttachmentLoader_createAttachment (spAttachmentLoader* self, spSkin* skin, spAttachmentType type, const char* name, const char* path); +/* Called after the attachment has been fully configured. */ +void spAttachmentLoader_configureAttachment (spAttachmentLoader* self, spAttachment* attachment); +/* Called just before the attachment is disposed. This can release allocations made in spAttachmentLoader_configureAttachment. */ +void spAttachmentLoader_disposeAttachment (spAttachmentLoader* self, spAttachment* attachment); #ifdef SPINE_SHORT_NAMES typedef spAttachmentLoader AttachmentLoader; #define AttachmentLoader_dispose(...) spAttachmentLoader_dispose(__VA_ARGS__) -#define AttachmentLoader_newAttachment(...) spAttachmentLoader_newAttachment(__VA_ARGS__) +#define AttachmentLoader_createAttachment(...) spAttachmentLoader_createAttachment(__VA_ARGS__) +#define AttachmentLoader_configureAttachment(...) spAttachmentLoader_configureAttachment(__VA_ARGS__) +#define AttachmentLoader_disposeAttachment(...) spAttachmentLoader_disposeAttachment(__VA_ARGS__) #endif #ifdef __cplusplus diff --git a/spine-c/include/spine/MeshAttachment.h b/spine-c/include/spine/MeshAttachment.h index f80033508..9793b794b 100644 --- a/spine-c/include/spine/MeshAttachment.h +++ b/spine-c/include/spine/MeshAttachment.h @@ -52,7 +52,7 @@ typedef struct spMeshAttachment { float* uvs; int trianglesCount; - int* triangles; + unsigned short* triangles; float r, g, b, a; diff --git a/spine-c/include/spine/SkeletonJson.h b/spine-c/include/spine/SkeletonJson.h index 3eb7fb758..ab954ef7f 100644 --- a/spine-c/include/spine/SkeletonJson.h +++ b/spine-c/include/spine/SkeletonJson.h @@ -42,6 +42,8 @@ extern "C" { #endif +struct spAtlasAttachmentLoader; + typedef struct spSkeletonJson { float scale; spAttachmentLoader* attachmentLoader; diff --git a/spine-c/include/spine/WeightedMeshAttachment.h b/spine-c/include/spine/WeightedMeshAttachment.h index b133a95b9..71a54e0f9 100644 --- a/spine-c/include/spine/WeightedMeshAttachment.h +++ b/spine-c/include/spine/WeightedMeshAttachment.h @@ -50,7 +50,7 @@ typedef struct spWeightedMeshAttachment { float* weights; int trianglesCount; - int* triangles; + unsigned short* triangles; int uvsCount; float* regionUVs; diff --git a/spine-c/include/spine/extension.h b/spine-c/include/spine/extension.h index 9da6dd7c3..62027b167 100644 --- a/spine-c/include/spine/extension.h +++ b/spine-c/include/spine/extension.h @@ -11,8 +11,8 @@ - Classes intended for inheritance provide init/deinit functions which subclasses must call in their create/dispose functions. - - Polymorphism is done by a base class providing function pointers in its init function. The public API delegates to this - function. + - Polymorphism is done by a base class providing function pointers in its init function. The public API delegates to these + function pointers. - Subclasses do not provide a dispose function, instead the base class' dispose function should be used, which will delegate to a dispose function pointer. @@ -146,11 +146,16 @@ void _spTrackEntry_dispose (spTrackEntry* self); /**/ -void _spAttachmentLoader_init (spAttachmentLoader* self, /**/ -void (*dispose) (spAttachmentLoader* self), /**/ - spAttachment* (*newAttachment) (spAttachmentLoader* self, spSkin* skin, spAttachmentType type, const char* name, - const char* path)); +/* configureAttachment and disposeAttachment may be 0. */ +void _spAttachmentLoader_init (spAttachmentLoader* self, + void (*dispose) (spAttachmentLoader* self), + spAttachment* (*createAttachment) (spAttachmentLoader* self, spSkin* skin, spAttachmentType type, const char* name, + const char* path), + void (*configureAttachment) (spAttachmentLoader* self, spAttachment*), + void (*disposeAttachment) (spAttachmentLoader* self, spAttachment*) +); void _spAttachmentLoader_deinit (spAttachmentLoader* self); +/* Can only be called from createAttachment. */ void _spAttachmentLoader_setError (spAttachmentLoader* self, const char* error1, const char* error2); void _spAttachmentLoader_setUnknownTypeError (spAttachmentLoader* self, spAttachmentType type); @@ -163,7 +168,7 @@ void _spAttachmentLoader_setUnknownTypeError (spAttachmentLoader* self, spAttach /**/ -void _spAttachment_init (spAttachment* self, const char* name, spAttachmentType type, /**/ +void _spAttachment_init (spAttachment* self, const char* name, spAttachmentType type, void (*dispose) (spAttachment* self)); void _spAttachment_deinit (spAttachment* self); @@ -174,10 +179,10 @@ void _spAttachment_deinit (spAttachment* self); /**/ -void _spTimeline_init (spTimeline* self, spTimelineType type, /**/ -void (*dispose) (spTimeline* self), /**/ - void (*apply) (const spTimeline* self, spSkeleton* skeleton, float lastTime, float time, spEvent** firedEvents, - int* eventsCount, float alpha)); +void _spTimeline_init (spTimeline* self, spTimelineType type, + void (*dispose) (spTimeline* self), + void (*apply) (const spTimeline* self, spSkeleton* skeleton, float lastTime, float time, spEvent** firedEvents, + int* eventsCount, float alpha)); void _spTimeline_deinit (spTimeline* self); #ifdef SPINE_SHORT_NAMES @@ -187,10 +192,10 @@ void _spTimeline_deinit (spTimeline* self); /**/ -void _spCurveTimeline_init (spCurveTimeline* self, spTimelineType type, int framesCount, /**/ -void (*dispose) (spTimeline* self), /**/ - void (*apply) (const spTimeline* self, spSkeleton* skeleton, float lastTime, float time, spEvent** firedEvents, - int* eventsCount, float alpha)); +void _spCurveTimeline_init (spCurveTimeline* self, spTimelineType type, int framesCount, + void (*dispose) (spTimeline* self), + void (*apply) (const spTimeline* self, spSkeleton* skeleton, float lastTime, float time, spEvent** firedEvents, + int* eventsCount, float alpha)); void _spCurveTimeline_deinit (spCurveTimeline* self); #ifdef SPINE_SHORT_NAMES diff --git a/spine-c/src/spine/AtlasAttachmentLoader.c b/spine-c/src/spine/AtlasAttachmentLoader.c index 0c88ed08a..4d5a81caf 100644 --- a/spine-c/src/spine/AtlasAttachmentLoader.c +++ b/spine-c/src/spine/AtlasAttachmentLoader.c @@ -32,7 +32,7 @@ #include #include -spAttachment* _spAtlasAttachmentLoader_newAttachment (spAttachmentLoader* loader, spSkin* skin, spAttachmentType type, +spAttachment* _spAtlasAttachmentLoader_createAttachment (spAttachmentLoader* loader, spSkin* skin, spAttachmentType type, const char* name, const char* path) { spAtlasAttachmentLoader* self = SUB_CAST(spAtlasAttachmentLoader, loader); switch (type) { @@ -108,7 +108,7 @@ spAttachment* _spAtlasAttachmentLoader_newAttachment (spAttachmentLoader* loader spAtlasAttachmentLoader* spAtlasAttachmentLoader_create (spAtlas* atlas) { spAtlasAttachmentLoader* self = NEW(spAtlasAttachmentLoader); - _spAttachmentLoader_init(SUPER(self), _spAttachmentLoader_deinit, _spAtlasAttachmentLoader_newAttachment); + _spAttachmentLoader_init(SUPER(self), _spAttachmentLoader_deinit, _spAtlasAttachmentLoader_createAttachment, 0, 0); self->atlas = atlas; return self; } diff --git a/spine-c/src/spine/Attachment.c b/spine-c/src/spine/Attachment.c index c920907aa..e920f3219 100644 --- a/spine-c/src/spine/Attachment.c +++ b/spine-c/src/spine/Attachment.c @@ -48,6 +48,7 @@ void _spAttachment_init (spAttachment* self, const char* name, spAttachmentType } void _spAttachment_deinit (spAttachment* self) { + spAttachmentLoader_disposeAttachment(self->attachmentLoader, self); FREE(self->vtable); FREE(self->name); } diff --git a/spine-c/src/spine/AttachmentLoader.c b/spine-c/src/spine/AttachmentLoader.c index c211dc883..9c5f132fa 100644 --- a/spine-c/src/spine/AttachmentLoader.c +++ b/spine-c/src/spine/AttachmentLoader.c @@ -34,18 +34,25 @@ #include typedef struct _spAttachmentLoaderVtable { - spAttachment* (*newAttachment) (spAttachmentLoader* self, spSkin* skin, spAttachmentType type, const char* name, + spAttachment* (*createAttachment) (spAttachmentLoader* self, spSkin* skin, spAttachmentType type, const char* name, const char* path); + void (*configureAttachment) (spAttachmentLoader* self, spAttachment*); + void (*disposeAttachment) (spAttachmentLoader* self, spAttachment*); void (*dispose) (spAttachmentLoader* self); } _spAttachmentLoaderVtable; -void _spAttachmentLoader_init (spAttachmentLoader* self, /**/ -void (*dispose) (spAttachmentLoader* self), /**/ - spAttachment* (*newAttachment) (spAttachmentLoader* self, spSkin* skin, spAttachmentType type, const char* name, - const char* path)) { +void _spAttachmentLoader_init (spAttachmentLoader* self, + void (*dispose) (spAttachmentLoader* self), + spAttachment* (*createAttachment) (spAttachmentLoader* self, spSkin* skin, spAttachmentType type, const char* name, + const char* path), + void (*configureAttachment) (spAttachmentLoader* self, spAttachment*), + void (*disposeAttachment) (spAttachmentLoader* self, spAttachment*) +) { CONST_CAST(_spAttachmentLoaderVtable*, self->vtable) = NEW(_spAttachmentLoaderVtable); VTABLE(spAttachmentLoader, self)->dispose = dispose; - VTABLE(spAttachmentLoader, self)->newAttachment = newAttachment; + VTABLE(spAttachmentLoader, self)->createAttachment = createAttachment; + VTABLE(spAttachmentLoader, self)->configureAttachment = configureAttachment; + VTABLE(spAttachmentLoader, self)->disposeAttachment = disposeAttachment; } void _spAttachmentLoader_deinit (spAttachmentLoader* self) { @@ -59,13 +66,23 @@ void spAttachmentLoader_dispose (spAttachmentLoader* self) { FREE(self); } -spAttachment* spAttachmentLoader_newAttachment (spAttachmentLoader* self, spSkin* skin, spAttachmentType type, const char* name, +spAttachment* spAttachmentLoader_createAttachment (spAttachmentLoader* self, spSkin* skin, spAttachmentType type, const char* name, const char* path) { FREE(self->error1); FREE(self->error2); self->error1 = 0; self->error2 = 0; - return VTABLE(spAttachmentLoader, self)->newAttachment(self, skin, type, name, path); + return VTABLE(spAttachmentLoader, self)->createAttachment(self, skin, type, name, path); +} + +void spAttachmentLoader_configureAttachment (spAttachmentLoader* self, spAttachment* attachment) { + if (!VTABLE(spAttachmentLoader, self)->configureAttachment) return; + VTABLE(spAttachmentLoader, self)->configureAttachment(self, attachment); +} + +void spAttachmentLoader_disposeAttachment (spAttachmentLoader* self, spAttachment* attachment) { + if (!VTABLE(spAttachmentLoader, self)->disposeAttachment) return; + VTABLE(spAttachmentLoader, self)->disposeAttachment(self, attachment); } void _spAttachmentLoader_setError (spAttachmentLoader* self, const char* error1, const char* error2) { diff --git a/spine-c/src/spine/SkeletonJson.c b/spine-c/src/spine/SkeletonJson.c index 0535f9829..061fcd6e3 100644 --- a/spine-c/src/spine/SkeletonJson.c +++ b/spine-c/src/spine/SkeletonJson.c @@ -622,7 +622,7 @@ spSkeletonData* spSkeletonJson_readSkeletonData (spSkeletonJson* self, const cha return 0; } - attachment = spAttachmentLoader_newAttachment(self->attachmentLoader, skin, type, attachmentName, path); + attachment = spAttachmentLoader_createAttachment(self->attachmentLoader, skin, type, attachmentName, path); if (!attachment) { if (self->attachmentLoader->error1) { spSkeletonData_dispose(skeletonData); @@ -631,6 +631,7 @@ spSkeletonData* spSkeletonJson_readSkeletonData (spSkeletonJson* self, const cha } continue; } + attachment->attachmentLoader = self->attachmentLoader; switch (attachment->type) { case SP_ATTACHMENT_REGION: { @@ -668,9 +669,9 @@ spSkeletonData* spSkeletonJson_readSkeletonData (spSkeletonJson* self, const cha entry = Json_getItem(attachmentMap, "triangles"); mesh->trianglesCount = entry->size; - mesh->triangles = MALLOC(int, entry->size); + mesh->triangles = MALLOC(unsigned short, entry->size); for (entry = entry->child, i = 0; entry; entry = entry->next, ++i) - mesh->triangles[i] = entry->valueInt; + mesh->triangles[i] = (unsigned short)entry->valueInt; entry = Json_getItem(attachmentMap, "uvs"); mesh->regionUVs = MALLOC(float, entry->size); @@ -744,9 +745,9 @@ spSkeletonData* spSkeletonJson_readSkeletonData (spSkeletonJson* self, const cha entry = Json_getItem(attachmentMap, "triangles"); mesh->trianglesCount = entry->size; - mesh->triangles = MALLOC(int, entry->size); + mesh->triangles = MALLOC(unsigned short, entry->size); for (entry = entry->child, i = 0; entry; entry = entry->next, ++i) - mesh->triangles[i] = entry->valueInt; + mesh->triangles[i] = (unsigned short)entry->valueInt; spWeightedMeshAttachment_updateUVs(mesh); @@ -783,6 +784,8 @@ spSkeletonData* spSkeletonJson_readSkeletonData (spSkeletonJson* self, const cha } } + spAttachmentLoader_configureAttachment(self->attachmentLoader, attachment); + spSkin_addAttachment(skin, slotIndex, skinAttachmentName, attachment); } }