Adds Binary support to cocos2d-x (#716)

- it simplifies the constructors/initializors
- the binary API is ::createWithBinaryFile()
- the JSON API is :: createWithJsonFile()
- the old API ::createWithDataFile() is deprecated and calls
createWithJsonFile()
This commit is contained in:
Ricardo Quesada 2016-10-03 04:37:34 -07:00 committed by Mario Zechner
parent ea0c2ae704
commit efe0b55dbb
3 changed files with 63 additions and 46 deletions

View File

@ -72,24 +72,46 @@ void disposeTrackEntry (spTrackEntry* entry) {
//
SkeletonAnimation* SkeletonAnimation::createWithData (spSkeletonData* skeletonData, bool ownsSkeletonData) {
SkeletonAnimation* node = new SkeletonAnimation(skeletonData, ownsSkeletonData);
SkeletonAnimation* node = new SkeletonAnimation();
node->initWithData(skeletonData, ownsSkeletonData);
node->autorelease();
return node;
}
SkeletonAnimation* SkeletonAnimation::createWithFile (const std::string& skeletonDataFile, spAtlas* atlas, float scale) {
SkeletonAnimation* node = new SkeletonAnimation(skeletonDataFile, atlas, scale);
SkeletonAnimation* SkeletonAnimation::createWithJsonFile (const std::string& skeletonJsonFile, spAtlas* atlas, float scale) {
SkeletonAnimation* node = new SkeletonAnimation();
node->initWithJsonFile(skeletonJsonFile, atlas, scale);
node->autorelease();
return node;
}
SkeletonAnimation* SkeletonAnimation::createWithFile (const std::string& skeletonDataFile, const std::string& atlasFile, float scale) {
SkeletonAnimation* node = new SkeletonAnimation(skeletonDataFile, atlasFile, scale);
SkeletonAnimation* SkeletonAnimation::createWithJsonFile (const std::string& skeletonJsonFile, const std::string& atlasFile, float scale) {
SkeletonAnimation* node = new SkeletonAnimation();
spAtlas* atlas = spAtlas_createFromFile(atlasFile.c_str(), 0);
node->initWithJsonFile(skeletonJsonFile, atlas, scale);
node->autorelease();
return node;
}
SkeletonAnimation* SkeletonAnimation::createWithBinaryFile (const std::string& skeletonBinaryFile, spAtlas* atlas, float scale) {
SkeletonAnimation* node = new SkeletonAnimation();
node->initWithBinaryFile(skeletonBinaryFile, atlas, scale);
node->autorelease();
return node;
}
SkeletonAnimation* SkeletonAnimation::createWithBinaryFile (const std::string& skeletonBinaryFile, const std::string& atlasFile, float scale) {
SkeletonAnimation* node = new SkeletonAnimation();
spAtlas* atlas = spAtlas_createFromFile(atlasFile.c_str(), 0);
node->initWithBinaryFile(skeletonBinaryFile, atlas, scale);
node->autorelease();
return node;
}
void SkeletonAnimation::initialize () {
super::initialize();
_ownsAnimationStateData = true;
_state = spAnimationState_create(spAnimationStateData_create(_skeleton->data));
_state->rendererObject = this;
@ -103,21 +125,6 @@ SkeletonAnimation::SkeletonAnimation ()
: SkeletonRenderer() {
}
SkeletonAnimation::SkeletonAnimation (spSkeletonData *skeletonData, bool ownsSkeletonData)
: SkeletonRenderer(skeletonData, ownsSkeletonData) {
initialize();
}
SkeletonAnimation::SkeletonAnimation (const std::string& skeletonDataFile, spAtlas* atlas, float scale)
: SkeletonRenderer(skeletonDataFile, atlas, scale) {
initialize();
}
SkeletonAnimation::SkeletonAnimation (const std::string& skeletonDataFile, const std::string& atlasFile, float scale)
: SkeletonRenderer(skeletonDataFile, atlasFile, scale) {
initialize();
}
SkeletonAnimation::~SkeletonAnimation () {
if (_ownsAnimationStateData) spAnimationStateData_dispose(_state->data);
spAnimationState_dispose(_state);
@ -165,9 +172,9 @@ spTrackEntry* SkeletonAnimation::addAnimation (int trackIndex, const std::string
}
return spAnimationState_addAnimation(_state, trackIndex, animation, loop, delay);
}
spAnimation* SkeletonAnimation::findAnimation(const std::string& name) const {
return spSkeletonData_findAnimation(_skeleton->data, name.c_str());
return spSkeletonData_findAnimation(_skeleton->data, name.c_str());
}
spTrackEntry* SkeletonAnimation::getCurrent (int trackIndex) {

View File

@ -49,17 +49,30 @@ class SkeletonAnimation: public SkeletonRenderer {
public:
CREATE_FUNC(SkeletonAnimation);
static SkeletonAnimation* createWithData (spSkeletonData* skeletonData, bool ownsSkeletonData = false);
static SkeletonAnimation* createWithFile (const std::string& skeletonDataFile, spAtlas* atlas, float scale = 1);
static SkeletonAnimation* createWithFile (const std::string& skeletonDataFile, const std::string& atlasFile, float scale = 1);
static SkeletonAnimation* createWithJsonFile (const std::string& skeletonJsonFile, spAtlas* atlas, float scale = 1);
static SkeletonAnimation* createWithJsonFile (const std::string& skeletonJsonFile, const std::string& atlasFile, float scale = 1);
static SkeletonAnimation* createWithBinaryFile (const std::string& skeletonBinaryFile, spAtlas* atlas, float scale = 1);
static SkeletonAnimation* createWithBinaryFile (const std::string& skeletonBinaryFile, const std::string& atlasFile, float scale = 1);
virtual void update (float deltaTime);
// Use createWithJsonFile instead
CC_DEPRECATED_ATTRIBUTE static SkeletonAnimation* createWithFile (const std::string& skeletonJsonFile, spAtlas* atlas, float scale = 1)
{
return SkeletonAnimation::createWithJsonFile(skeletonJsonFile, atlas, scale);
}
// Use createWithJsonFile instead
CC_DEPRECATED_ATTRIBUTE static SkeletonAnimation* createWithile (const std::string& skeletonJsonFile, const std::string& atlasFile, float scale = 1)
{
return SkeletonAnimation::createWithJsonFile(skeletonJsonFile, atlasFile, scale);
}
virtual void update (float deltaTime) override;
void setAnimationStateData (spAnimationStateData* stateData);
void setMix (const std::string& fromAnimation, const std::string& toAnimation, float duration);
spTrackEntry* setAnimation (int trackIndex, const std::string& name, bool loop);
spTrackEntry* addAnimation (int trackIndex, const std::string& name, bool loop, float delay = 0);
spAnimation* findAnimation(const std::string& name) const;
spAnimation* findAnimation(const std::string& name) const;
spTrackEntry* getCurrent (int trackIndex = 0);
void clearTracks ();
void clearTrack (int trackIndex = 0);
@ -81,11 +94,8 @@ public:
CC_CONSTRUCTOR_ACCESS:
SkeletonAnimation ();
SkeletonAnimation (spSkeletonData* skeletonData, bool ownsSkeletonData = false);
SkeletonAnimation (const std::string&skeletonDataFile, spAtlas* atlas, float scale = 1);
SkeletonAnimation (const std::string& skeletonDataFile, const std::string& atlasFile, float scale = 1);
virtual ~SkeletonAnimation ();
void initialize ();
virtual void initialize () override;
protected:
spAnimationState* _state;

View File

@ -38,34 +38,34 @@ USING_NS_CC;
using std::max;
namespace spine {
static SkeletonBatch* instance = nullptr;
SkeletonBatch* SkeletonBatch::getInstance () {
if (!instance) instance = new SkeletonBatch();
return instance;
}
void SkeletonBatch::destroyInstance () {
if (instance) {
delete instance;
instance = nullptr;
}
}
SkeletonBatch::SkeletonBatch ()
{
_firstCommand = new Command();
_command = _firstCommand;
Director::getInstance()->getEventDispatcher()->addCustomEventListener(EVENT_AFTER_DRAW_RESET_POSITION, [this](EventCustom* eventCustom){
this->update(0);
});;
}
SkeletonBatch::~SkeletonBatch () {
Director::getInstance()->getEventDispatcher()->removeCustomEventListeners(EVENT_AFTER_DRAW_RESET_POSITION);
Command* command = _firstCommand;
while (command) {
Command* next = command->next;
@ -73,37 +73,37 @@ namespace spine {
command = next;
}
}
void SkeletonBatch::update (float delta) {
_command = _firstCommand;
}
void SkeletonBatch::addCommand (cocos2d::Renderer* renderer, float globalZOrder, GLuint textureID, GLProgramState* glProgramState,
BlendFunc blendFunc, const TrianglesCommand::Triangles& triangles, const Mat4& transform, uint32_t transformFlags
) {
_command->triangles->verts = triangles.verts;
_command->triangles->vertCount = triangles.vertCount;
_command->triangles->indexCount = triangles.indexCount;
_command->triangles->indices = triangles.indices;
_command->trianglesCommand->init(globalZOrder, textureID, glProgramState, blendFunc, *_command->triangles, transform);
renderer->addCommand(_command->trianglesCommand);
if (!_command->next) _command->next = new Command();
_command = _command->next;
}
SkeletonBatch::Command::Command () :
next(nullptr)
{
trianglesCommand = new TrianglesCommand();
triangles = new TrianglesCommand::Triangles();
}
SkeletonBatch::Command::~Command () {
delete triangles;
delete trianglesCommand;
}
}
}