2013-04-24 13:57:34 +08:00
|
|
|
/*******************************************************************************
|
|
|
|
* Copyright (c) 2013, Esoteric Software
|
|
|
|
* All rights reserved.
|
2014-10-15 11:23:02 +08:00
|
|
|
*
|
2013-04-24 13:57:34 +08:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
2014-10-15 11:23:02 +08:00
|
|
|
*
|
2013-04-24 13:57:34 +08:00
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
2014-10-15 11:23:02 +08:00
|
|
|
*
|
2013-04-24 13:57:34 +08:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include "SpineTest.h"
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <string.h>
|
2014-10-15 11:23:02 +08:00
|
|
|
#include "spine/spine.h"
|
2013-04-24 13:57:34 +08:00
|
|
|
|
|
|
|
using namespace cocos2d;
|
|
|
|
using namespace std;
|
2013-10-15 18:00:03 +08:00
|
|
|
using namespace spine;
|
2013-04-24 13:57:34 +08:00
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// SpineTestScene
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
2014-10-15 11:23:02 +08:00
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
SpineTests::SpineTests()
|
2014-10-15 11:23:02 +08:00
|
|
|
{
|
2017-02-09 16:57:42 +08:00
|
|
|
ADD_TEST_CASE(BatchingExample);
|
2017-11-02 09:06:13 +08:00
|
|
|
ADD_TEST_CASE(CoinExample);
|
2017-02-09 16:57:42 +08:00
|
|
|
ADD_TEST_CASE(GoblinsExample);
|
|
|
|
ADD_TEST_CASE(RaptorExample);
|
|
|
|
ADD_TEST_CASE(SpineboyExample);
|
|
|
|
ADD_TEST_CASE(TankExample);
|
2014-10-15 11:23:02 +08:00
|
|
|
}
|
|
|
|
|
2017-02-09 16:57:42 +08:00
|
|
|
SpineTestLayer::SpineTestLayer()
|
|
|
|
: _title("")
|
|
|
|
{}
|
|
|
|
|
|
|
|
std::string SpineTestLayer::title() const
|
|
|
|
{
|
|
|
|
return _title;
|
|
|
|
}
|
|
|
|
|
|
|
|
// BatchingExample
|
|
|
|
bool BatchingExample::init () {
|
2015-04-03 14:31:03 +08:00
|
|
|
if (!SpineTestLayer::init()) return false;
|
2013-12-19 21:35:43 +08:00
|
|
|
|
2017-02-09 16:57:42 +08:00
|
|
|
_title = "BatchingExample";
|
2013-12-19 21:35:43 +08:00
|
|
|
|
2017-02-09 16:57:42 +08:00
|
|
|
// Load the texture atlas.
|
|
|
|
_atlas = spAtlas_createFromFile("spine/spineboy.atlas", 0);
|
|
|
|
CCASSERT(_atlas, "Error reading atlas file.");
|
2013-12-19 21:35:43 +08:00
|
|
|
|
2017-02-09 16:57:42 +08:00
|
|
|
// This attachment loader configures attachments with data needed for cocos2d-x rendering.
|
|
|
|
// Do not dispose the attachment loader until the skeleton data is disposed!
|
|
|
|
_attachmentLoader = (spAttachmentLoader*)Cocos2dAttachmentLoader_create(_atlas);
|
2013-12-19 21:35:43 +08:00
|
|
|
|
2017-02-09 16:57:42 +08:00
|
|
|
// Load the skeleton data.
|
|
|
|
spSkeletonJson* json = spSkeletonJson_createWithLoader(_attachmentLoader);
|
|
|
|
json->scale = 0.6f; // Resizes skeleton data to 60% of the size it was in Spine.
|
2017-11-02 09:06:13 +08:00
|
|
|
_skeletonData = spSkeletonJson_readSkeletonDataFile(json, "spine/spineboy-ess.json");
|
2017-02-09 16:57:42 +08:00
|
|
|
CCASSERT(_skeletonData, json->error ? json->error : "Error reading skeleton data file.");
|
|
|
|
spSkeletonJson_dispose(json);
|
2013-12-19 21:35:43 +08:00
|
|
|
|
2017-02-09 16:57:42 +08:00
|
|
|
// Setup mix times.
|
|
|
|
_stateData = spAnimationStateData_create(_skeletonData);
|
|
|
|
spAnimationStateData_setMixByName(_stateData, "walk", "jump", 0.2f);
|
|
|
|
spAnimationStateData_setMixByName(_stateData, "jump", "run", 0.2f);
|
2014-10-15 11:23:02 +08:00
|
|
|
|
2017-02-09 16:57:42 +08:00
|
|
|
int xMin = _contentSize.width * 0.10f, xMax = _contentSize.width * 0.90f;
|
|
|
|
int yMin = 0, yMax = _contentSize.height * 0.7f;
|
|
|
|
for (int i = 0; i < 50; i++) {
|
|
|
|
// Each skeleton node shares the same atlas, skeleton data, and mix times.
|
|
|
|
SkeletonAnimation* skeletonNode = SkeletonAnimation::createWithData(_skeletonData, false);
|
|
|
|
skeletonNode->setAnimationStateData(_stateData);
|
|
|
|
|
|
|
|
skeletonNode->setAnimation(0, "walk", true);
|
|
|
|
skeletonNode->addAnimation(0, "jump", false, 3);
|
|
|
|
skeletonNode->addAnimation(0, "run", true);
|
|
|
|
|
|
|
|
skeletonNode->setPosition(Vec2(
|
|
|
|
RandomHelper::random_int(xMin, xMax),
|
|
|
|
RandomHelper::random_int(yMin, yMax)
|
|
|
|
));
|
2017-03-22 09:21:59 +08:00
|
|
|
skeletonNode->setScale(0.8);
|
2017-02-09 16:57:42 +08:00
|
|
|
addChild(skeletonNode);
|
|
|
|
}
|
2013-12-19 21:35:43 +08:00
|
|
|
|
2017-02-09 16:57:42 +08:00
|
|
|
return true;
|
2013-04-24 13:57:34 +08:00
|
|
|
}
|
|
|
|
|
2017-02-09 16:57:42 +08:00
|
|
|
BatchingExample::~BatchingExample () {
|
|
|
|
// SkeletonAnimation instances are cocos2d-x nodes and are disposed of automatically as normal, but the data created
|
|
|
|
// manually to be shared across multiple SkeletonAnimations needs to be disposed of manually.
|
|
|
|
spSkeletonData_dispose(_skeletonData);
|
|
|
|
spAnimationStateData_dispose(_stateData);
|
|
|
|
spAttachmentLoader_dispose(_attachmentLoader);
|
|
|
|
spAtlas_dispose(_atlas);
|
2013-12-19 21:35:43 +08:00
|
|
|
}
|
|
|
|
|
2017-11-02 09:06:13 +08:00
|
|
|
// CoinExample
|
|
|
|
|
|
|
|
bool CoinExample::init () {
|
|
|
|
if (!SpineTestLayer::init()) return false;
|
|
|
|
|
|
|
|
_title = "CoinExample";
|
|
|
|
|
|
|
|
skeletonNode = SkeletonAnimation::createWithJsonFile("spine/coin-pro.json", "spine/coin.atlas", 1.f);
|
|
|
|
skeletonNode->setAnimation(0, "rotate", true);
|
|
|
|
|
|
|
|
skeletonNode->setPosition(Vec2(_contentSize.width / 2, 100));
|
|
|
|
addChild(skeletonNode);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-02-09 16:57:42 +08:00
|
|
|
// GoblinsExample
|
2014-10-15 11:23:02 +08:00
|
|
|
|
2017-02-09 16:57:42 +08:00
|
|
|
bool GoblinsExample::init () {
|
2015-04-03 14:31:03 +08:00
|
|
|
if (!SpineTestLayer::init()) return false;
|
2014-10-15 11:23:02 +08:00
|
|
|
|
2017-11-02 09:06:13 +08:00
|
|
|
_title = "GoblinsExample";
|
|
|
|
|
|
|
|
skeletonNode = SkeletonAnimation::createWithJsonFile("spine/goblins-pro.json", "spine/goblins.atlas", 1.5f);
|
2017-02-09 16:57:42 +08:00
|
|
|
skeletonNode->setAnimation(0, "walk", true);
|
|
|
|
skeletonNode->setSkin("goblin");
|
2014-10-15 11:23:02 +08:00
|
|
|
|
2017-02-09 16:57:42 +08:00
|
|
|
skeletonNode->setPosition(Vec2(_contentSize.width / 2, 20));
|
2017-03-22 09:21:59 +08:00
|
|
|
skeletonNode->setScale(0.6);
|
2017-02-09 16:57:42 +08:00
|
|
|
addChild(skeletonNode);
|
|
|
|
return true;
|
2014-10-15 11:23:02 +08:00
|
|
|
}
|
|
|
|
|
2017-02-09 16:57:42 +08:00
|
|
|
// RaptorExample
|
|
|
|
|
|
|
|
bool RaptorExample::init () {
|
2015-04-14 16:14:51 +08:00
|
|
|
if (!SpineTestLayer::init()) return false;
|
|
|
|
|
2017-11-02 09:06:13 +08:00
|
|
|
_title = "RaptorExample";
|
|
|
|
skeletonNode = SkeletonAnimation::createWithJsonFile("spine/raptor-pro.json", "spine/raptor.atlas", 0.5f);
|
2017-02-09 16:57:42 +08:00
|
|
|
|
2015-04-14 16:14:51 +08:00
|
|
|
skeletonNode->setAnimation(0, "walk", true);
|
|
|
|
skeletonNode->setAnimation(1, "empty", false);
|
|
|
|
skeletonNode->addAnimation(1, "gungrab", false, 2);
|
|
|
|
|
2017-02-09 16:57:42 +08:00
|
|
|
skeletonNode->setPosition(Vec2(_contentSize.width / 2, 20));
|
2017-03-22 09:21:59 +08:00
|
|
|
skeletonNode->setScale(0.6);
|
2015-04-14 16:14:51 +08:00
|
|
|
addChild(skeletonNode);
|
|
|
|
return true;
|
|
|
|
}
|
2016-10-12 01:56:14 +08:00
|
|
|
|
2017-02-09 16:57:42 +08:00
|
|
|
// SpineboyExample
|
2016-10-12 01:56:14 +08:00
|
|
|
|
2017-02-09 16:57:42 +08:00
|
|
|
bool SpineboyExample::init () {
|
|
|
|
if (!SpineTestLayer::init()) return false;
|
|
|
|
|
2017-11-02 09:06:13 +08:00
|
|
|
_title = "SpineboyExample";
|
|
|
|
skeletonNode = SkeletonAnimation::createWithJsonFile("spine/spineboy-ess.json", "spine/spineboy.atlas", 0.6f);
|
2017-02-09 16:57:42 +08:00
|
|
|
skeletonNode->setStartListener( [] (spTrackEntry* entry) {
|
|
|
|
log("%d start: %s", entry->trackIndex, entry->animation->name);
|
2016-10-12 01:56:14 +08:00
|
|
|
});
|
2017-02-09 16:57:42 +08:00
|
|
|
skeletonNode->setInterruptListener( [] (spTrackEntry* entry) {
|
|
|
|
log("%d interrupt", entry->trackIndex);
|
2016-10-12 01:56:14 +08:00
|
|
|
});
|
2017-02-09 16:57:42 +08:00
|
|
|
skeletonNode->setEndListener( [] (spTrackEntry* entry) {
|
|
|
|
log("%d end", entry->trackIndex);
|
2016-10-12 01:56:14 +08:00
|
|
|
});
|
2017-02-09 16:57:42 +08:00
|
|
|
skeletonNode->setCompleteListener( [] (spTrackEntry* entry) {
|
|
|
|
log("%d complete", entry->trackIndex);
|
2016-10-12 01:56:14 +08:00
|
|
|
});
|
2017-02-09 16:57:42 +08:00
|
|
|
skeletonNode->setDisposeListener( [] (spTrackEntry* entry) {
|
|
|
|
log("%d dispose", entry->trackIndex);
|
|
|
|
});
|
|
|
|
skeletonNode->setEventListener( [] (spTrackEntry* entry, spEvent* event) {
|
|
|
|
log("%d event: %s, %d, %f, %s", entry->trackIndex, event->data->name, event->intValue, event->floatValue, event->stringValue);
|
|
|
|
});
|
|
|
|
|
|
|
|
skeletonNode->setMix("walk", "jump", 0.4);
|
|
|
|
skeletonNode->setMix("jump", "run", 0.4);
|
2016-10-12 01:56:14 +08:00
|
|
|
skeletonNode->setAnimation(0, "walk", true);
|
2017-02-09 16:57:42 +08:00
|
|
|
spTrackEntry* jumpEntry = skeletonNode->addAnimation(0, "jump", false, 1);
|
2016-10-12 01:56:14 +08:00
|
|
|
skeletonNode->addAnimation(0, "run", true);
|
2017-02-09 16:57:42 +08:00
|
|
|
|
|
|
|
skeletonNode->setTrackStartListener(jumpEntry, [] (spTrackEntry* entry) {
|
2016-10-12 01:56:14 +08:00
|
|
|
log("jumped!");
|
|
|
|
});
|
2017-02-09 16:57:42 +08:00
|
|
|
|
|
|
|
// skeletonNode->addAnimation(1, "test", true);
|
|
|
|
// skeletonNode->runAction(RepeatForever::create(Sequence::create(FadeOut::create(1), FadeIn::create(1), DelayTime::create(5), NULL)));
|
|
|
|
|
|
|
|
skeletonNode->setPosition(Vec2(_contentSize.width / 2, 20));
|
2017-03-22 09:21:59 +08:00
|
|
|
skeletonNode->setScale(0.8);
|
2016-10-12 01:56:14 +08:00
|
|
|
addChild(skeletonNode);
|
2017-02-09 16:57:42 +08:00
|
|
|
|
2016-10-12 01:56:14 +08:00
|
|
|
scheduleUpdate();
|
2017-02-09 16:57:42 +08:00
|
|
|
|
2016-10-12 01:56:14 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-02-09 16:57:42 +08:00
|
|
|
void SpineboyExample::update (float deltaTime) {
|
|
|
|
// Test releasing memory.
|
|
|
|
// Director::getInstance()->replaceScene(SpineboyExample::scene());
|
2016-10-12 01:56:14 +08:00
|
|
|
}
|
|
|
|
|
2017-02-09 16:57:42 +08:00
|
|
|
// TankExample
|
2016-10-12 01:56:14 +08:00
|
|
|
|
2017-02-09 16:57:42 +08:00
|
|
|
bool TankExample::init () {
|
2016-10-12 01:56:14 +08:00
|
|
|
if (!SpineTestLayer::init()) return false;
|
2017-02-09 16:57:42 +08:00
|
|
|
|
2017-11-02 09:06:13 +08:00
|
|
|
_title = "TankExample";
|
|
|
|
skeletonNode = SkeletonAnimation::createWithJsonFile("spine/tank-pro.json", "spine/tank.atlas", 0.5f);
|
2017-02-09 16:57:42 +08:00
|
|
|
skeletonNode->setAnimation(0, "drive", true);
|
|
|
|
|
|
|
|
skeletonNode->setPosition(Vec2(_contentSize.width / 2 + 400, 20));
|
2017-03-22 09:21:59 +08:00
|
|
|
skeletonNode->setScale(0.8);
|
2016-10-12 01:56:14 +08:00
|
|
|
addChild(skeletonNode);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|