Simplified all loading methods to take path, stream, or pointers.

This commit is contained in:
NathanSweet 2013-03-25 12:26:30 +01:00
parent 6e7e8c5512
commit 75fc36eab6
12 changed files with 34 additions and 69 deletions

View File

@ -19,8 +19,8 @@ bool ExampleScene::init() {
Atlas *atlas = new Atlas("../data/spineboy.atlas");
SkeletonJson json(atlas);
SkeletonData *skeletonData = json.readSkeletonDataFile("../data/spineboy-skeleton.json");
Animation *animation = json.readAnimationFile("../data/spineboy-walk.json", skeletonData);
SkeletonData *skeletonData = json.readSkeletonData("../data/spineboy-skeleton.json");
Animation *animation = json.readAnimation("../data/spineboy-walk.json", skeletonData);
CCSkeleton* skeletonNode = new CCSkeleton(skeletonData);
skeletonNode->state->setAnimation(animation, true);

View File

@ -51,6 +51,7 @@ public:
class Atlas: public BaseAtlas {
public:
Atlas (const std::string &path);
Atlas (std::istream &input);
Atlas (const char *begin, const char *end);
AtlasRegion* findRegion (const std::string &name);

View File

@ -38,8 +38,8 @@ public:
/** The SkeletonJson owns the attachmentLoader. */
SkeletonJson (BaseAttachmentLoader *attachmentLoader);
SkeletonData* readSkeletonDataFile (const std::string &path) const;
Animation* readAnimationFile (const std::string &path, const SkeletonData *skeletonData) const;
SkeletonData* readSkeletonData (const std::string &path) const;
Animation* readAnimation (const std::string &path, const SkeletonData *skeletonData) const;
};
} /* namespace spine */

View File

@ -45,6 +45,10 @@ Atlas::Atlas (const std::string &path) {
load(data, data + size);
}
Atlas::Atlas (std::istream &input) {
load(input);
}
Atlas::Atlas (const char *begin, const char *end) {
load(begin, end);
}

View File

@ -41,20 +41,20 @@ SkeletonJson::SkeletonJson (Atlas *atlas) :
BaseSkeletonJson(new AtlasAttachmentLoader(atlas)) {
}
SkeletonData* SkeletonJson::readSkeletonDataFile (const std::string &path) const {
SkeletonData* SkeletonJson::readSkeletonData (const std::string &path) const {
unsigned long size;
char* data = reinterpret_cast<char*>(CCFileUtils::sharedFileUtils()->getFileData(
CCFileUtils::sharedFileUtils()->fullPathForFilename(path.c_str()).c_str(), "r", &size));
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;
char* data = reinterpret_cast<char*>(CCFileUtils::sharedFileUtils()->getFileData(
CCFileUtils::sharedFileUtils()->fullPathForFilename(path.c_str()).c_str(), "r", &size));
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 */

View File

@ -40,15 +40,15 @@ public:
std::vector<BaseAtlasPage*> pages;
std::vector<BaseAtlasRegion*> regions;
virtual BaseAtlasRegion* findRegion (const std::string &name);
protected:
virtual ~BaseAtlas ();
void load (std::ifstream &file);
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);
virtual BaseAtlasRegion* findRegion (const std::string &name);
private:
virtual BaseAtlasPage* newAtlasPage (const std::string &name) = 0;
virtual BaseAtlasRegion* newAtlasRegion (BaseAtlasPage *page) = 0;

View File

@ -44,16 +44,12 @@ public:
BaseSkeletonJson (BaseAttachmentLoader *attachmentLoader);
virtual ~BaseSkeletonJson ();
SkeletonData* readSkeletonDataFile (const std::string &path) const;
SkeletonData* readSkeletonData (std::ifstream &file) const;
SkeletonData* readSkeletonData (std::istream &file) const;
SkeletonData* readSkeletonData (const std::string &json) const;
SkeletonData* readSkeletonData (const std::string &path) const;
SkeletonData* readSkeletonData (std::istream &input) const;
SkeletonData* readSkeletonData (const char *begin, const char *end) const;
Animation* readAnimationFile (const std::string &path, const SkeletonData *skeletonData) const;
Animation* readAnimation (std::ifstream &file, 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 std::string &path, const SkeletonData *skeletonData) const;
Animation* readAnimation (std::istream &input, const SkeletonData *skeletonData) const;
Animation* readAnimation (const char *begin, const char *end, const SkeletonData *skeletonData) const;
};

View File

@ -101,11 +101,10 @@ BaseAtlas::~BaseAtlas () {
delete regions[i];
}
void BaseAtlas::load (std::ifstream &file) {
if (!file) throw invalid_argument("file cannot be null.");
if (!file.is_open()) throw runtime_error("Atlas file is not open.");
load((std::istream&)file);
void BaseAtlas::load (const std::string &path) {
std::ifstream file(path.c_str());
if (!file) throw std::invalid_argument("Error reading atlas file: " + path);
load(file);
}
void BaseAtlas::load (std::istream &input) {
@ -118,12 +117,6 @@ void BaseAtlas::load (std::istream &input) {
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) {
if (!current) throw invalid_argument("current cannot be null.");
if (!end) throw invalid_argument("end cannot be null.");

View File

@ -65,27 +65,17 @@ BaseSkeletonJson::~BaseSkeletonJson () {
delete attachmentLoader;
}
SkeletonData* BaseSkeletonJson::readSkeletonDataFile (const string &path) const {
SkeletonData* BaseSkeletonJson::readSkeletonData (const string &path) const {
std::ifstream file(path.c_str());
if (!file) throw std::invalid_argument("Error reading skeleton file: " + path);
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 {
if (!input) throw invalid_argument("input cannot be null.");
string json;
std::getline(input, json, (char)EOF);
return readSkeletonData(json);
}
SkeletonData* BaseSkeletonJson::readSkeletonData (const string &json) const {
const char *begin = json.c_str();
const char *end = begin + json.length();
return readSkeletonData(begin, end);
@ -209,27 +199,17 @@ SkeletonData* BaseSkeletonJson::readSkeletonData (const char *begin, const char
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());
if (!file) throw std::invalid_argument("Error reading animation file: " + path);
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 {
if (!input) throw invalid_argument("input cannot be null.");
string json;
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 *end = begin + json.length();
return readAnimation(begin, end, skeletonData);

View File

@ -49,9 +49,8 @@ public:
class Atlas: public BaseAtlas {
public:
Atlas (std::ifstream &file);
Atlas (std::istream &input);
Atlas (const std::string &path);
Atlas (std::istream &input);
Atlas (const char *begin, const char *end);
AtlasRegion* findRegion (const std::string &name);

View File

@ -24,7 +24,6 @@
******************************************************************************/
#include <iostream>
#include <fstream>
#include <spine-sfml/spine.h>
#include <SFML/Graphics.hpp>
@ -36,8 +35,8 @@ int main () {
try {
Atlas *atlas = new Atlas("../data/spineboy.atlas");
SkeletonJson json(atlas);
SkeletonData *skeletonData = json.readSkeletonDataFile("../data/spineboy-skeleton.json");
Animation *animation = json.readAnimationFile("../data/spineboy-walk.json", skeletonData);
SkeletonData *skeletonData = json.readSkeletonData("../data/spineboy-skeleton.json");
Animation *animation = json.readAnimation("../data/spineboy-walk.json", skeletonData);
Skeleton *skeleton = new Skeleton(skeletonData);
skeleton->flipX = false;

View File

@ -25,8 +25,6 @@
#include <SFML/Graphics/Texture.hpp>
#include <spine-sfml/Atlas.h>
#include <iostream>
#include <fstream>
namespace spine {
@ -36,19 +34,14 @@ AtlasPage::~AtlasPage () {
//
Atlas::Atlas (std::ifstream &file) {
load(file);
Atlas::Atlas (const std::string &path) {
load(path);
}
Atlas::Atlas (std::istream &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) {
load(begin, end);
}