AttachmentLoader now has create, configure, dispose.

Now the attachment loader should not be disposed until loaded attachments are disposed.
This commit is contained in:
NathanSweet 2016-04-02 15:23:15 +02:00
parent 39f588daeb
commit 9a404e5c49
10 changed files with 73 additions and 35 deletions

View File

@ -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() :

View File

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

View File

@ -52,7 +52,7 @@ typedef struct spMeshAttachment {
float* uvs;
int trianglesCount;
int* triangles;
unsigned short* triangles;
float r, g, b, a;

View File

@ -42,6 +42,8 @@
extern "C" {
#endif
struct spAtlasAttachmentLoader;
typedef struct spSkeletonJson {
float scale;
spAttachmentLoader* attachmentLoader;

View File

@ -50,7 +50,7 @@ typedef struct spWeightedMeshAttachment {
float* weights;
int trianglesCount;
int* triangles;
unsigned short* triangles;
int uvsCount;
float* regionUVs;

View File

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

View File

@ -32,7 +32,7 @@
#include <spine/AtlasAttachmentLoader.h>
#include <spine/extension.h>
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;
}

View File

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

View File

@ -34,18 +34,25 @@
#include <spine/extension.h>
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) {

View File

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