From 36809166a2a199499594613f5649dc39317c456a Mon Sep 17 00:00:00 2001 From: NathanSweet Date: Mon, 12 May 2014 17:12:24 +0200 Subject: [PATCH] Make AnimationState TrackEntry create/dispose functions optional. closes #218 --- spine-c/include/spine/extension.h | 10 ++++- spine-c/src/spine/AnimationState.c | 37 +++++++++++++------ .../src/spine/SkeletonAnimation.cpp | 12 +++++- spine-cocos2dx/src/spine/spine-cocos2dx.cpp | 9 ----- 4 files changed, 43 insertions(+), 25 deletions(-) diff --git a/spine-c/include/spine/extension.h b/spine-c/include/spine/extension.h index 3820eaa00..cb0b78dcb 100644 --- a/spine-c/include/spine/extension.h +++ b/spine-c/include/spine/extension.h @@ -87,8 +87,6 @@ extern "C" { void _spAtlasPage_createTexture (spAtlasPage* self, const char* path); void _spAtlasPage_disposeTexture (spAtlasPage* self); char* _spUtil_readFile (const char* path, int* length); -spTrackEntry* _spAnimationState_createTrackEntry (spAnimationState* self); -void _spAnimationState_disposeTrackEntry (spAnimationState* self, spTrackEntry* entry); #ifdef SPINE_SHORT_NAMES #define _AtlasPage_createTexture(...) _spAtlasPage_createTexture(__VA_ARGS__) @@ -112,6 +110,14 @@ char* _readFile (const char* path, int* length); /**/ +typedef struct { + spAnimationState super; + spEvent** events; + + spTrackEntry* (*createTrackEntry) (spAnimationState* self); + void (*disposeTrackEntry) (spAnimationState* self, spTrackEntry* entry); +} _spAnimationState; + spTrackEntry* _spTrackEntry_create (); void _spTrackEntry_dispose (spTrackEntry* entry); diff --git a/spine-c/src/spine/AnimationState.c b/spine-c/src/spine/AnimationState.c index 77818c411..4e9042a75 100644 --- a/spine-c/src/spine/AnimationState.c +++ b/spine-c/src/spine/AnimationState.c @@ -47,12 +47,13 @@ void _spTrackEntry_dispose (spTrackEntry* entry) { /**/ -typedef struct { - spAnimationState super; - spEvent** events; -} _spAnimationState; +spTrackEntry* _spAnimationState_createTrackEntry (spAnimationState* self) { + return _spTrackEntry_create(); +} -void _spAnimationState_setCurrent (spAnimationState* self, int index, spTrackEntry* entry); +void _spAnimationState_disposeTrackEntry (spAnimationState* self, spTrackEntry* entry) { + _spTrackEntry_dispose(entry); +} spAnimationState* spAnimationState_create (spAnimationStateData* data) { _spAnimationState* internal = NEW(_spAnimationState); @@ -60,13 +61,16 @@ spAnimationState* spAnimationState_create (spAnimationStateData* data) { internal->events = MALLOC(spEvent*, 64); self->timeScale = 1; CONST_CAST(spAnimationStateData*, self->data) = data; + internal->createTrackEntry = _spAnimationState_createTrackEntry; + internal->disposeTrackEntry = _spAnimationState_disposeTrackEntry; return self; } void _spAnimationState_disposeAllEntries (spAnimationState* self, spTrackEntry* entry) { + _spAnimationState* internal = SUB_CAST(_spAnimationState, self); while (entry) { spTrackEntry* next = entry->next; - _spAnimationState_disposeTrackEntry(self, entry); + internal->disposeTrackEntry(self, entry); entry = next; } } @@ -81,6 +85,8 @@ void spAnimationState_dispose (spAnimationState* self) { FREE(self); } +void _spAnimationState_setCurrent (spAnimationState* self, int index, spTrackEntry* entry); + void spAnimationState_update (spAnimationState* self, float delta) { int i; float previousDelta; @@ -140,7 +146,7 @@ void spAnimationState_apply (spAnimationState* self, spSkeleton* skeleton) { if (alpha >= 1) { alpha = 1; - _spAnimationState_disposeTrackEntry(self, current->previous); + internal->disposeTrackEntry(self, current->previous); current->previous = 0; } spAnimation_mix(current->animation, skeleton, current->lastTime, time, @@ -193,6 +199,8 @@ void spAnimationState_clearTracks (spAnimationState* self) { } void spAnimationState_clearTrack (spAnimationState* self, int trackIndex) { + _spAnimationState* internal = SUB_CAST(_spAnimationState, self); + spTrackEntry* current; if (trackIndex >= self->trackCount) return; current = self->tracks[trackIndex]; @@ -203,7 +211,7 @@ void spAnimationState_clearTrack (spAnimationState* self, int trackIndex) { self->tracks[trackIndex] = 0; - if (current->previous) _spAnimationState_disposeTrackEntry(self, current->previous); + if (current->previous) internal->disposeTrackEntry(self, current->previous); _spAnimationState_disposeAllEntries(self, current); } @@ -219,6 +227,8 @@ spTrackEntry* _spAnimationState_expandToIndex (spAnimationState* self, int index } void _spAnimationState_setCurrent (spAnimationState* self, int index, spTrackEntry* entry) { + _spAnimationState* internal = SUB_CAST(_spAnimationState, self); + spTrackEntry* current = _spAnimationState_expandToIndex(self, index); if (current) { spTrackEntry* previous = current->previous; @@ -237,9 +247,9 @@ void _spAnimationState_setCurrent (spAnimationState* self, int index, spTrackEnt } else entry->previous = current; } else - _spAnimationState_disposeTrackEntry(self, current); + internal->disposeTrackEntry(self, current); - if (previous) _spAnimationState_disposeTrackEntry(self, previous); + if (previous) internal->disposeTrackEntry(self, previous); } self->tracks[index] = entry; @@ -258,11 +268,13 @@ spTrackEntry* spAnimationState_setAnimationByName (spAnimationState* self, int t } spTrackEntry* spAnimationState_setAnimation (spAnimationState* self, int trackIndex, spAnimation* animation, int/*bool*/loop) { + _spAnimationState* internal = SUB_CAST(_spAnimationState, self); + spTrackEntry* entry; spTrackEntry* current = _spAnimationState_expandToIndex(self, trackIndex); if (current) _spAnimationState_disposeAllEntries(self, current->next); - entry = _spAnimationState_createTrackEntry(self); + entry = internal->createTrackEntry(self); entry->animation = animation; entry->loop = loop; entry->endTime = animation->duration; @@ -278,9 +290,10 @@ spTrackEntry* spAnimationState_addAnimationByName (spAnimationState* self, int t spTrackEntry* spAnimationState_addAnimation (spAnimationState* self, int trackIndex, spAnimation* animation, int/*bool*/loop, float delay) { + _spAnimationState* internal = SUB_CAST(_spAnimationState, self); spTrackEntry* last; - spTrackEntry* entry = _spAnimationState_createTrackEntry(self); + spTrackEntry* entry = internal->createTrackEntry(self); entry->animation = animation; entry->loop = loop; entry->endTime = animation->duration; diff --git a/spine-cocos2dx/src/spine/SkeletonAnimation.cpp b/spine-cocos2dx/src/spine/SkeletonAnimation.cpp index 5d5f7cd59..7f20c5c92 100644 --- a/spine-cocos2dx/src/spine/SkeletonAnimation.cpp +++ b/spine-cocos2dx/src/spine/SkeletonAnimation.cpp @@ -40,14 +40,19 @@ using std::vector; namespace spine { -static void animationCallback (spAnimationState* state, int trackIndex, spEventType type, spEvent* event, int loopCount) { +void animationCallback (spAnimationState* state, int trackIndex, spEventType type, spEvent* event, int loopCount) { ((SkeletonAnimation*)state->rendererObject)->onAnimationStateEvent(trackIndex, type, event, loopCount); } -static void trackEntryCallback (spAnimationState* state, int trackIndex, spEventType type, spEvent* event, int loopCount) { +void trackEntryCallback (spAnimationState* state, int trackIndex, spEventType type, spEvent* event, int loopCount) { ((SkeletonAnimation*)state->rendererObject)->onTrackEntryEvent(trackIndex, type, event, loopCount); } +void disposeTrackEntry (spAnimationState* self, spTrackEntry* entry) { + if (entry->rendererObject) FREE(entry->rendererObject); + _spTrackEntry_dispose(entry); +} + SkeletonAnimation* SkeletonAnimation::createWithData (spSkeletonData* skeletonData) { SkeletonAnimation* node = new SkeletonAnimation(skeletonData); node->autorelease(); @@ -71,6 +76,9 @@ void SkeletonAnimation::initialize () { state = spAnimationState_create(spAnimationStateData_create(skeleton->data)); state->rendererObject = this; state->listener = animationCallback; + + _spAnimationState* stateInternal = (_spAnimationState*)state; + stateInternal->disposeTrackEntry = disposeTrackEntry; } SkeletonAnimation::SkeletonAnimation (spSkeletonData *skeletonData) diff --git a/spine-cocos2dx/src/spine/spine-cocos2dx.cpp b/spine-cocos2dx/src/spine/spine-cocos2dx.cpp index dea7e4003..ebee422ce 100644 --- a/spine-cocos2dx/src/spine/spine-cocos2dx.cpp +++ b/spine-cocos2dx/src/spine/spine-cocos2dx.cpp @@ -55,15 +55,6 @@ char* _spUtil_readFile (const char* path, int* length) { return bytes; } -spTrackEntry* _spAnimationState_createTrackEntry (spAnimationState* self) { - return _spTrackEntry_create(); -} - -void _spAnimationState_disposeTrackEntry (spAnimationState* self, spTrackEntry* entry) { - if (entry->rendererObject) FREE(entry->rendererObject); - _spTrackEntry_dispose(entry); -} - /**/ namespace spine {