mirror of
https://github.com/EsotericSoftware/spine-runtimes.git
synced 2025-12-21 09:46:02 +08:00
Simplified all loading methods to take path, stream, or pointers.
This commit is contained in:
parent
6e7e8c5512
commit
75fc36eab6
@ -19,8 +19,8 @@ bool ExampleScene::init() {
|
|||||||
|
|
||||||
Atlas *atlas = new Atlas("../data/spineboy.atlas");
|
Atlas *atlas = new Atlas("../data/spineboy.atlas");
|
||||||
SkeletonJson json(atlas);
|
SkeletonJson json(atlas);
|
||||||
SkeletonData *skeletonData = json.readSkeletonDataFile("../data/spineboy-skeleton.json");
|
SkeletonData *skeletonData = json.readSkeletonData("../data/spineboy-skeleton.json");
|
||||||
Animation *animation = json.readAnimationFile("../data/spineboy-walk.json", skeletonData);
|
Animation *animation = json.readAnimation("../data/spineboy-walk.json", skeletonData);
|
||||||
|
|
||||||
CCSkeleton* skeletonNode = new CCSkeleton(skeletonData);
|
CCSkeleton* skeletonNode = new CCSkeleton(skeletonData);
|
||||||
skeletonNode->state->setAnimation(animation, true);
|
skeletonNode->state->setAnimation(animation, true);
|
||||||
|
|||||||
@ -51,6 +51,7 @@ public:
|
|||||||
class Atlas: public BaseAtlas {
|
class Atlas: public BaseAtlas {
|
||||||
public:
|
public:
|
||||||
Atlas (const std::string &path);
|
Atlas (const std::string &path);
|
||||||
|
Atlas (std::istream &input);
|
||||||
Atlas (const char *begin, const char *end);
|
Atlas (const char *begin, const char *end);
|
||||||
|
|
||||||
AtlasRegion* findRegion (const std::string &name);
|
AtlasRegion* findRegion (const std::string &name);
|
||||||
|
|||||||
@ -38,8 +38,8 @@ public:
|
|||||||
/** The SkeletonJson owns the attachmentLoader. */
|
/** The SkeletonJson owns the attachmentLoader. */
|
||||||
SkeletonJson (BaseAttachmentLoader *attachmentLoader);
|
SkeletonJson (BaseAttachmentLoader *attachmentLoader);
|
||||||
|
|
||||||
SkeletonData* readSkeletonDataFile (const std::string &path) const;
|
SkeletonData* readSkeletonData (const std::string &path) const;
|
||||||
Animation* readAnimationFile (const std::string &path, const SkeletonData *skeletonData) const;
|
Animation* readAnimation (const std::string &path, const SkeletonData *skeletonData) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
} /* namespace spine */
|
} /* namespace spine */
|
||||||
|
|||||||
@ -45,6 +45,10 @@ Atlas::Atlas (const std::string &path) {
|
|||||||
load(data, data + size);
|
load(data, data + size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Atlas::Atlas (std::istream &input) {
|
||||||
|
load(input);
|
||||||
|
}
|
||||||
|
|
||||||
Atlas::Atlas (const char *begin, const char *end) {
|
Atlas::Atlas (const char *begin, const char *end) {
|
||||||
load(begin, end);
|
load(begin, end);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -41,20 +41,20 @@ SkeletonJson::SkeletonJson (Atlas *atlas) :
|
|||||||
BaseSkeletonJson(new AtlasAttachmentLoader(atlas)) {
|
BaseSkeletonJson(new AtlasAttachmentLoader(atlas)) {
|
||||||
}
|
}
|
||||||
|
|
||||||
SkeletonData* SkeletonJson::readSkeletonDataFile (const std::string &path) const {
|
SkeletonData* SkeletonJson::readSkeletonData (const std::string &path) const {
|
||||||
unsigned long size;
|
unsigned long size;
|
||||||
char* data = reinterpret_cast<char*>(CCFileUtils::sharedFileUtils()->getFileData(
|
char* data = reinterpret_cast<char*>(CCFileUtils::sharedFileUtils()->getFileData(
|
||||||
CCFileUtils::sharedFileUtils()->fullPathForFilename(path.c_str()).c_str(), "r", &size));
|
CCFileUtils::sharedFileUtils()->fullPathForFilename(path.c_str()).c_str(), "r", &size));
|
||||||
if (!data) throw runtime_error("Error reading skeleton file: " + path);
|
if (!data) throw runtime_error("Error reading skeleton file: " + path);
|
||||||
return readSkeletonData(data, data + size);
|
return BaseSkeletonJson::readSkeletonData(data, data + size);
|
||||||
}
|
}
|
||||||
|
|
||||||
Animation* SkeletonJson::readAnimationFile (const std::string &path, const SkeletonData *skeletonData) const {
|
Animation* SkeletonJson::readAnimation (const std::string &path, const SkeletonData *skeletonData) const {
|
||||||
unsigned long size;
|
unsigned long size;
|
||||||
char* data = reinterpret_cast<char*>(CCFileUtils::sharedFileUtils()->getFileData(
|
char* data = reinterpret_cast<char*>(CCFileUtils::sharedFileUtils()->getFileData(
|
||||||
CCFileUtils::sharedFileUtils()->fullPathForFilename(path.c_str()).c_str(), "r", &size));
|
CCFileUtils::sharedFileUtils()->fullPathForFilename(path.c_str()).c_str(), "r", &size));
|
||||||
if (!data) throw runtime_error("Error reading animation file: " + path);
|
if (!data) throw runtime_error("Error reading animation file: " + path);
|
||||||
return readAnimation(data, data + size, skeletonData);
|
return BaseSkeletonJson::readAnimation(data, data + size, skeletonData);
|
||||||
}
|
}
|
||||||
|
|
||||||
} /* namespace spine */
|
} /* namespace spine */
|
||||||
|
|||||||
@ -40,15 +40,15 @@ public:
|
|||||||
std::vector<BaseAtlasPage*> pages;
|
std::vector<BaseAtlasPage*> pages;
|
||||||
std::vector<BaseAtlasRegion*> regions;
|
std::vector<BaseAtlasRegion*> regions;
|
||||||
|
|
||||||
|
virtual BaseAtlasRegion* findRegion (const std::string &name);
|
||||||
|
|
||||||
|
protected:
|
||||||
virtual ~BaseAtlas ();
|
virtual ~BaseAtlas ();
|
||||||
|
|
||||||
void load (std::ifstream &file);
|
|
||||||
void load (std::istream &input);
|
void load (std::istream &input);
|
||||||
void load (const std::string &text);
|
void load (const std::string &path);
|
||||||
void load (const char *begin, const char *end);
|
void load (const char *begin, const char *end);
|
||||||
|
|
||||||
virtual BaseAtlasRegion* findRegion (const std::string &name);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
virtual BaseAtlasPage* newAtlasPage (const std::string &name) = 0;
|
virtual BaseAtlasPage* newAtlasPage (const std::string &name) = 0;
|
||||||
virtual BaseAtlasRegion* newAtlasRegion (BaseAtlasPage *page) = 0;
|
virtual BaseAtlasRegion* newAtlasRegion (BaseAtlasPage *page) = 0;
|
||||||
|
|||||||
@ -44,16 +44,12 @@ public:
|
|||||||
BaseSkeletonJson (BaseAttachmentLoader *attachmentLoader);
|
BaseSkeletonJson (BaseAttachmentLoader *attachmentLoader);
|
||||||
virtual ~BaseSkeletonJson ();
|
virtual ~BaseSkeletonJson ();
|
||||||
|
|
||||||
SkeletonData* readSkeletonDataFile (const std::string &path) const;
|
SkeletonData* readSkeletonData (const std::string &path) const;
|
||||||
SkeletonData* readSkeletonData (std::ifstream &file) const;
|
SkeletonData* readSkeletonData (std::istream &input) const;
|
||||||
SkeletonData* readSkeletonData (std::istream &file) const;
|
|
||||||
SkeletonData* readSkeletonData (const std::string &json) const;
|
|
||||||
SkeletonData* readSkeletonData (const char *begin, const char *end) const;
|
SkeletonData* readSkeletonData (const char *begin, const char *end) const;
|
||||||
|
|
||||||
Animation* readAnimationFile (const std::string &path, const SkeletonData *skeletonData) const;
|
Animation* readAnimation (const std::string &path, const SkeletonData *skeletonData) const;
|
||||||
Animation* readAnimation (std::ifstream &file, const SkeletonData *skeletonData) const;
|
Animation* readAnimation (std::istream &input, const SkeletonData *skeletonData) const;
|
||||||
Animation* readAnimation (std::istream &file, const SkeletonData *skeletonData) const;
|
|
||||||
Animation* readAnimation (const std::string &json, const SkeletonData *skeletonData) const;
|
|
||||||
Animation* readAnimation (const char *begin, const char *end, const SkeletonData *skeletonData) const;
|
Animation* readAnimation (const char *begin, const char *end, const SkeletonData *skeletonData) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -101,11 +101,10 @@ BaseAtlas::~BaseAtlas () {
|
|||||||
delete regions[i];
|
delete regions[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
void BaseAtlas::load (std::ifstream &file) {
|
void BaseAtlas::load (const std::string &path) {
|
||||||
if (!file) throw invalid_argument("file cannot be null.");
|
std::ifstream file(path.c_str());
|
||||||
if (!file.is_open()) throw runtime_error("Atlas file is not open.");
|
if (!file) throw std::invalid_argument("Error reading atlas file: " + path);
|
||||||
|
load(file);
|
||||||
load((std::istream&)file);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void BaseAtlas::load (std::istream &input) {
|
void BaseAtlas::load (std::istream &input) {
|
||||||
@ -118,12 +117,6 @@ void BaseAtlas::load (std::istream &input) {
|
|||||||
load(begin, end);
|
load(begin, end);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BaseAtlas::load (const string &text) {
|
|
||||||
const char *begin = text.c_str();
|
|
||||||
const char *end = begin + text.length();
|
|
||||||
load(begin, end);
|
|
||||||
}
|
|
||||||
|
|
||||||
void BaseAtlas::load (const char *current, const char *end) {
|
void BaseAtlas::load (const char *current, const char *end) {
|
||||||
if (!current) throw invalid_argument("current cannot be null.");
|
if (!current) throw invalid_argument("current cannot be null.");
|
||||||
if (!end) throw invalid_argument("end cannot be null.");
|
if (!end) throw invalid_argument("end cannot be null.");
|
||||||
|
|||||||
@ -65,27 +65,17 @@ BaseSkeletonJson::~BaseSkeletonJson () {
|
|||||||
delete attachmentLoader;
|
delete attachmentLoader;
|
||||||
}
|
}
|
||||||
|
|
||||||
SkeletonData* BaseSkeletonJson::readSkeletonDataFile (const string &path) const {
|
SkeletonData* BaseSkeletonJson::readSkeletonData (const string &path) const {
|
||||||
std::ifstream file(path.c_str());
|
std::ifstream file(path.c_str());
|
||||||
|
if (!file) throw std::invalid_argument("Error reading skeleton file: " + path);
|
||||||
return readSkeletonData(file);
|
return readSkeletonData(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
SkeletonData* BaseSkeletonJson::readSkeletonData (std::ifstream &file) const {
|
|
||||||
if (!file) throw invalid_argument("file cannot be null.");
|
|
||||||
if (!file.is_open()) throw runtime_error("Skeleton file is not open.");
|
|
||||||
|
|
||||||
return readSkeletonData((std::istream&)file);
|
|
||||||
}
|
|
||||||
|
|
||||||
SkeletonData* BaseSkeletonJson::readSkeletonData (std::istream &input) const {
|
SkeletonData* BaseSkeletonJson::readSkeletonData (std::istream &input) const {
|
||||||
if (!input) throw invalid_argument("input cannot be null.");
|
if (!input) throw invalid_argument("input cannot be null.");
|
||||||
|
|
||||||
string json;
|
string json;
|
||||||
std::getline(input, json, (char)EOF);
|
std::getline(input, json, (char)EOF);
|
||||||
return readSkeletonData(json);
|
|
||||||
}
|
|
||||||
|
|
||||||
SkeletonData* BaseSkeletonJson::readSkeletonData (const string &json) const {
|
|
||||||
const char *begin = json.c_str();
|
const char *begin = json.c_str();
|
||||||
const char *end = begin + json.length();
|
const char *end = begin + json.length();
|
||||||
return readSkeletonData(begin, end);
|
return readSkeletonData(begin, end);
|
||||||
@ -209,27 +199,17 @@ SkeletonData* BaseSkeletonJson::readSkeletonData (const char *begin, const char
|
|||||||
return skeletonData;
|
return skeletonData;
|
||||||
}
|
}
|
||||||
|
|
||||||
Animation* BaseSkeletonJson::readAnimationFile (const string &path, const SkeletonData *skeletonData) const {
|
Animation* BaseSkeletonJson::readAnimation (const string &path, const SkeletonData *skeletonData) const {
|
||||||
std::ifstream file(path.c_str());
|
std::ifstream file(path.c_str());
|
||||||
|
if (!file) throw std::invalid_argument("Error reading animation file: " + path);
|
||||||
return readAnimation(file, skeletonData);
|
return readAnimation(file, skeletonData);
|
||||||
}
|
}
|
||||||
|
|
||||||
Animation* BaseSkeletonJson::readAnimation (std::ifstream &file, const SkeletonData *skeletonData) const {
|
|
||||||
if (!file) throw invalid_argument("file cannot be null.");
|
|
||||||
if (!file.is_open()) throw runtime_error("Animation file is not open.");
|
|
||||||
|
|
||||||
return readAnimation((std::istream&)file, skeletonData);
|
|
||||||
}
|
|
||||||
|
|
||||||
Animation* BaseSkeletonJson::readAnimation (std::istream &input, const SkeletonData *skeletonData) const {
|
Animation* BaseSkeletonJson::readAnimation (std::istream &input, const SkeletonData *skeletonData) const {
|
||||||
if (!input) throw invalid_argument("input cannot be null.");
|
if (!input) throw invalid_argument("input cannot be null.");
|
||||||
|
|
||||||
string json;
|
string json;
|
||||||
std::getline(input, json, (char)EOF);
|
std::getline(input, json, (char)EOF);
|
||||||
return readAnimation(json, skeletonData);
|
|
||||||
}
|
|
||||||
|
|
||||||
Animation* BaseSkeletonJson::readAnimation (const string &json, const SkeletonData *skeletonData) const {
|
|
||||||
const char *begin = json.c_str();
|
const char *begin = json.c_str();
|
||||||
const char *end = begin + json.length();
|
const char *end = begin + json.length();
|
||||||
return readAnimation(begin, end, skeletonData);
|
return readAnimation(begin, end, skeletonData);
|
||||||
|
|||||||
@ -49,9 +49,8 @@ public:
|
|||||||
|
|
||||||
class Atlas: public BaseAtlas {
|
class Atlas: public BaseAtlas {
|
||||||
public:
|
public:
|
||||||
Atlas (std::ifstream &file);
|
|
||||||
Atlas (std::istream &input);
|
|
||||||
Atlas (const std::string &path);
|
Atlas (const std::string &path);
|
||||||
|
Atlas (std::istream &input);
|
||||||
Atlas (const char *begin, const char *end);
|
Atlas (const char *begin, const char *end);
|
||||||
|
|
||||||
AtlasRegion* findRegion (const std::string &name);
|
AtlasRegion* findRegion (const std::string &name);
|
||||||
|
|||||||
@ -24,7 +24,6 @@
|
|||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <fstream>
|
|
||||||
#include <spine-sfml/spine.h>
|
#include <spine-sfml/spine.h>
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
|
|
||||||
@ -36,8 +35,8 @@ int main () {
|
|||||||
try {
|
try {
|
||||||
Atlas *atlas = new Atlas("../data/spineboy.atlas");
|
Atlas *atlas = new Atlas("../data/spineboy.atlas");
|
||||||
SkeletonJson json(atlas);
|
SkeletonJson json(atlas);
|
||||||
SkeletonData *skeletonData = json.readSkeletonDataFile("../data/spineboy-skeleton.json");
|
SkeletonData *skeletonData = json.readSkeletonData("../data/spineboy-skeleton.json");
|
||||||
Animation *animation = json.readAnimationFile("../data/spineboy-walk.json", skeletonData);
|
Animation *animation = json.readAnimation("../data/spineboy-walk.json", skeletonData);
|
||||||
|
|
||||||
Skeleton *skeleton = new Skeleton(skeletonData);
|
Skeleton *skeleton = new Skeleton(skeletonData);
|
||||||
skeleton->flipX = false;
|
skeleton->flipX = false;
|
||||||
|
|||||||
@ -25,8 +25,6 @@
|
|||||||
|
|
||||||
#include <SFML/Graphics/Texture.hpp>
|
#include <SFML/Graphics/Texture.hpp>
|
||||||
#include <spine-sfml/Atlas.h>
|
#include <spine-sfml/Atlas.h>
|
||||||
#include <iostream>
|
|
||||||
#include <fstream>
|
|
||||||
|
|
||||||
namespace spine {
|
namespace spine {
|
||||||
|
|
||||||
@ -36,19 +34,14 @@ AtlasPage::~AtlasPage () {
|
|||||||
|
|
||||||
//
|
//
|
||||||
|
|
||||||
Atlas::Atlas (std::ifstream &file) {
|
Atlas::Atlas (const std::string &path) {
|
||||||
load(file);
|
load(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
Atlas::Atlas (std::istream &input) {
|
Atlas::Atlas (std::istream &input) {
|
||||||
load(input);
|
load(input);
|
||||||
}
|
}
|
||||||
|
|
||||||
Atlas::Atlas (const std::string &path) {
|
|
||||||
std::ifstream file("../data/spineboy.atlas");
|
|
||||||
load(file);
|
|
||||||
}
|
|
||||||
|
|
||||||
Atlas::Atlas (const char *begin, const char *end) {
|
Atlas::Atlas (const char *begin, const char *end) {
|
||||||
load(begin, end);
|
load(begin, end);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user