From ba733040afb85d4c287a2ec464db05cb86a53fca Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Mon, 25 Sep 2017 21:15:03 +0100 Subject: Changing sax parser to dom parser as it is simpler --- yage/base/spritesheet.cpp | 155 ++++++---------------------------------------- yage/base/spritesheet.h | 37 +---------- 2 files changed, 21 insertions(+), 171 deletions(-) (limited to 'yage') diff --git a/yage/base/spritesheet.cpp b/yage/base/spritesheet.cpp index 311bb955..d53f64aa 100644 --- a/yage/base/spritesheet.cpp +++ b/yage/base/spritesheet.cpp @@ -13,7 +13,7 @@ #include #include -#include +#include #include using namespace std; @@ -23,155 +23,38 @@ using namespace yage::details; namespace yage { -namespace details -{ - -bool SpriteSheetHandler::Null() -{ - return true; -} - -bool SpriteSheetHandler::Bool(bool) -{ - return true; -} - -bool SpriteSheetHandler::Int(int i) -{ - return handleNumber(i); -} - -bool SpriteSheetHandler::Uint(unsigned u) -{ - return handleNumber(static_cast(u)); -} - -bool SpriteSheetHandler::Int64(int64_t i) -{ - return handleNumber(static_cast(i)); -} - -bool SpriteSheetHandler::Uint64(uint64_t u) -{ - return handleNumber(static_cast(u)); -} - -bool SpriteSheetHandler::Double(double d) -{ - return handleNumber(static_cast(d)); -} - -bool SpriteSheetHandler::String(const char *, SizeType, bool) -{ - return true; -} - -bool SpriteSheetHandler::Key(const char *str, SizeType length, bool) -{ - current_key_ = string(str, length); - return true; -} - -bool SpriteSheetHandler::StartObject() -{ - depth_++; - - if (depth_ == 3) { - current_image_ = current_key_; - } - - return true; -} - -bool SpriteSheetHandler::EndObject(SizeType) +SpriteSheet::SpriteSheet(string pngFileName, string jsonFileName) { - if (depth_ == 3) { - map_[current_image_] = coord_; - } - depth_--; - return true; + string fileContents = fileContent(jsonFileName); } -bool SpriteSheetHandler::StartArray() +string SpriteSheet::fileContent(string jsonFileName) const { - return true; -} + ifstream inputFile(jsonFileName); -bool SpriteSheetHandler::EndArray(SizeType) -{ - return true; -} + stringstream stream; + stream << inputFile.rdbuf(); -SpriteMap SpriteSheetHandler::spriteMap() const -{ - return map_; + return stream.str(); } -int SpriteSheetHandler::imageWidth() const +SpriteMap SpriteSheet::parseJson(int &width, int &height, const string &jsonContent) const { - return image_width_; -} + SpriteMap spriteMap; + Document jsonAtlas; + jsonAtlas.Parse(jsonContent.c_str()); -int SpriteSheetHandler::imageHeight() const -{ - return image_height_; -} + width = jsonAtlas["width"].GetInt(); + height = jsonAtlas["height"].GetInt(); -bool SpriteSheetHandler::handleNumber(int i) -{ - if (current_key_ == "width") { - if (depth_ == 1) { - image_width_ = i; - } else { - coord_.width = i; + for (auto &texture : jsonAtlas["sprites"].GetObject()) { + spriteMap[texture.name.GetString()] = Coordinate(); + for (auto &value : texture.value.GetObject()) { + /// @todo add the coordinate to the map } - } else if (current_key_ == "height") { - if (depth_ == 1) { - image_height_ = i; - } else { - coord_.height = i; - } - } else if (current_key_ == "x") { - coord_.x = i; - } else if (current_key_ == "y") { - coord_.y = i; } - return true; -} -} // namespace details - -SpriteSheet::SpriteSheet(string pngFileName, string jsonFileName) -{ - string fileContents = fileContent(jsonFileName); - - SpriteSheetHandler ssHandler; - Reader reader; - StringStream ss(fileContents.c_str()); - reader.Parse(ss, ssHandler); - - fileLocations_ = ssHandler.spriteMap(); - texture_ = ImageLoader::loadPng(pngFileName); - - if (texture_.width != ssHandler.imageWidth()) - throw runtime_error("Texture width not equal: " + - to_string(texture_.width) + - " != " + to_string(ssHandler.imageWidth())); - if (texture_.height != ssHandler.imageHeight()) - throw runtime_error( - "Texture height not equal: " + to_string(texture_.height) + - " != " + to_string(ssHandler.imageHeight())); - assert(texture_.height == ssHandler.imageHeight()); -} - -string SpriteSheet::fileContent(string jsonFileName) const -{ - ifstream inputFile(jsonFileName); - - stringstream stream; - stream << inputFile.rdbuf(); - - return stream.str(); + return spriteMap; } } // namespace yage diff --git a/yage/base/spritesheet.h b/yage/base/spritesheet.h index 3dca6b57..d44e1dc0 100644 --- a/yage/base/spritesheet.h +++ b/yage/base/spritesheet.h @@ -38,41 +38,6 @@ struct Coordinate { typedef std::map SpriteMap; -class SpriteSheetHandler - : public rapidjson::BaseReaderHandler, SpriteSheetHandler> -{ -public: - bool Null(); - bool Bool(bool b); - bool Int(int i); - bool Uint(unsigned u); - bool Int64(int64_t i); - bool Uint64(uint64_t u); - bool Double(double d); - bool String(const char *str, rapidjson::SizeType length, bool copy); - - bool Key(const char *str, rapidjson::SizeType length, bool copy); - bool StartObject(); - bool EndObject(rapidjson::SizeType memberCount); - bool StartArray(); - bool EndArray(rapidjson::SizeType memberCount); - - SpriteMap spriteMap() const; - int imageWidth() const; - int imageHeight() const; - -private: - std::string current_key_; - std::string current_image_; - Coordinate coord_; - int depth_; - int image_width_; - int image_height_; - SpriteMap map_; - - bool handleNumber(int i); -}; - } // namespace details class SpriteSheet @@ -86,6 +51,8 @@ public: private: Texture texture_; details::SpriteMap fileLocations_; + + details::SpriteMap parseJson(int &width, int &height, const std::string &jsonContent) const; }; } // namespace yage -- cgit