From 75fc36eab61118110d04f5b05b27e9fd2e964075 Mon Sep 17 00:00:00 2001 From: NathanSweet Date: Mon, 25 Mar 2013 12:26:30 +0100 Subject: [PATCH] Simplified all loading methods to take path, stream, or pointers. --- .../spine-cocos2dx/example/ExampleScene.cpp | 4 +-- .../include/spine-cocos2dx/Atlas.h | 1 + .../include/spine-cocos2dx/SkeletonJson.h | 4 +-- .../src/spine-cocos2dx/Atlas.cpp | 4 +++ .../src/spine-cocos2dx/SkeletonJson.cpp | 8 +++--- spine-cpp/include/spine/BaseAtlas.h | 8 +++--- spine-cpp/include/spine/BaseSkeletonJson.h | 12 +++----- spine-cpp/src/spine/BaseAtlas.cpp | 15 +++------- spine-cpp/src/spine/BaseSkeletonJson.cpp | 28 +++---------------- spine-sfml/include/spine-sfml/Atlas.h | 3 +- spine-sfml/src/main.cpp | 5 ++-- spine-sfml/src/spine-sfml/Atlas.cpp | 11 ++------ 12 files changed, 34 insertions(+), 69 deletions(-) diff --git a/spine-cocos2dx/spine-cocos2dx/example/ExampleScene.cpp b/spine-cocos2dx/spine-cocos2dx/example/ExampleScene.cpp index 115e21416..38f6f0e93 100644 --- a/spine-cocos2dx/spine-cocos2dx/example/ExampleScene.cpp +++ b/spine-cocos2dx/spine-cocos2dx/example/ExampleScene.cpp @@ -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); diff --git a/spine-cocos2dx/spine-cocos2dx/include/spine-cocos2dx/Atlas.h b/spine-cocos2dx/spine-cocos2dx/include/spine-cocos2dx/Atlas.h index a87220253..755a817ef 100644 --- a/spine-cocos2dx/spine-cocos2dx/include/spine-cocos2dx/Atlas.h +++ b/spine-cocos2dx/spine-cocos2dx/include/spine-cocos2dx/Atlas.h @@ -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); diff --git a/spine-cocos2dx/spine-cocos2dx/include/spine-cocos2dx/SkeletonJson.h b/spine-cocos2dx/spine-cocos2dx/include/spine-cocos2dx/SkeletonJson.h index 8911e092f..576d2c55c 100644 --- a/spine-cocos2dx/spine-cocos2dx/include/spine-cocos2dx/SkeletonJson.h +++ b/spine-cocos2dx/spine-cocos2dx/include/spine-cocos2dx/SkeletonJson.h @@ -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 */ diff --git a/spine-cocos2dx/spine-cocos2dx/src/spine-cocos2dx/Atlas.cpp b/spine-cocos2dx/spine-cocos2dx/src/spine-cocos2dx/Atlas.cpp index 2eaf75fbd..966fb7bf9 100644 --- a/spine-cocos2dx/spine-cocos2dx/src/spine-cocos2dx/Atlas.cpp +++ b/spine-cocos2dx/spine-cocos2dx/src/spine-cocos2dx/Atlas.cpp @@ -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); } diff --git a/spine-cocos2dx/spine-cocos2dx/src/spine-cocos2dx/SkeletonJson.cpp b/spine-cocos2dx/spine-cocos2dx/src/spine-cocos2dx/SkeletonJson.cpp index 468d1dc18..b0425e94a 100644 --- a/spine-cocos2dx/spine-cocos2dx/src/spine-cocos2dx/SkeletonJson.cpp +++ b/spine-cocos2dx/spine-cocos2dx/src/spine-cocos2dx/SkeletonJson.cpp @@ -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(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(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 */ diff --git a/spine-cpp/include/spine/BaseAtlas.h b/spine-cpp/include/spine/BaseAtlas.h index a49ed7bed..e11e24ac3 100644 --- a/spine-cpp/include/spine/BaseAtlas.h +++ b/spine-cpp/include/spine/BaseAtlas.h @@ -40,15 +40,15 @@ public: std::vector pages; std::vector 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; diff --git a/spine-cpp/include/spine/BaseSkeletonJson.h b/spine-cpp/include/spine/BaseSkeletonJson.h index 3f30c1002..d7212c3e4 100644 --- a/spine-cpp/include/spine/BaseSkeletonJson.h +++ b/spine-cpp/include/spine/BaseSkeletonJson.h @@ -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; }; diff --git a/spine-cpp/src/spine/BaseAtlas.cpp b/spine-cpp/src/spine/BaseAtlas.cpp index 960ea965b..4e4d79fc3 100644 --- a/spine-cpp/src/spine/BaseAtlas.cpp +++ b/spine-cpp/src/spine/BaseAtlas.cpp @@ -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."); diff --git a/spine-cpp/src/spine/BaseSkeletonJson.cpp b/spine-cpp/src/spine/BaseSkeletonJson.cpp index 0a14e02fa..b81e0b1ab 100644 --- a/spine-cpp/src/spine/BaseSkeletonJson.cpp +++ b/spine-cpp/src/spine/BaseSkeletonJson.cpp @@ -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); diff --git a/spine-sfml/include/spine-sfml/Atlas.h b/spine-sfml/include/spine-sfml/Atlas.h index 688476a10..c618b4715 100644 --- a/spine-sfml/include/spine-sfml/Atlas.h +++ b/spine-sfml/include/spine-sfml/Atlas.h @@ -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); diff --git a/spine-sfml/src/main.cpp b/spine-sfml/src/main.cpp index 587113812..c01407206 100644 --- a/spine-sfml/src/main.cpp +++ b/spine-sfml/src/main.cpp @@ -24,7 +24,6 @@ ******************************************************************************/ #include -#include #include #include @@ -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; diff --git a/spine-sfml/src/spine-sfml/Atlas.cpp b/spine-sfml/src/spine-sfml/Atlas.cpp index 4ecf431fc..9c7cd67c8 100644 --- a/spine-sfml/src/spine-sfml/Atlas.cpp +++ b/spine-sfml/src/spine-sfml/Atlas.cpp @@ -25,8 +25,6 @@ #include #include -#include -#include 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); }