aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2017-09-25 21:15:03 +0100
committerYann Herklotz <ymherklotz@gmail.com>2017-09-25 21:15:03 +0100
commitba733040afb85d4c287a2ec464db05cb86a53fca (patch)
tree4b385a291bb6ded2685d0e7996dd0d4cb92da74d
parent5e6067f74e7ac072656f11ead0f22ec7f8e9c525 (diff)
downloadYAGE-ba733040afb85d4c287a2ec464db05cb86a53fca.tar.gz
YAGE-ba733040afb85d4c287a2ec464db05cb86a53fca.zip
Changing sax parser to dom parser as it is simpler
-rw-r--r--.clang-format1
-rw-r--r--yage/base/spritesheet.cpp155
-rw-r--r--yage/base/spritesheet.h37
3 files changed, 22 insertions, 171 deletions
diff --git a/.clang-format b/.clang-format
index 6008495f..a4489c53 100644
--- a/.clang-format
+++ b/.clang-format
@@ -9,6 +9,7 @@ AllowShortFunctionsOnASingleLine: Inline
AlwaysBreakTemplateDeclarations: true
BreakBeforeBraces: Linux
SortIncludes: true
+SpaceBeforeParens: ControlStatements
UseTab: Never
...
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 <sstream>
#include <stdexcept>
-#include <rapidjson/reader.h>
+#include <rapidjson/document.h>
#include <yage/base/imageloader.h>
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<int>(u));
-}
-
-bool SpriteSheetHandler::Int64(int64_t i)
-{
- return handleNumber(static_cast<int>(i));
-}
-
-bool SpriteSheetHandler::Uint64(uint64_t u)
-{
- return handleNumber(static_cast<int>(u));
-}
-
-bool SpriteSheetHandler::Double(double d)
-{
- return handleNumber(static_cast<int>(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<std::string, details::Coordinate> SpriteMap;
-class SpriteSheetHandler
- : public rapidjson::BaseReaderHandler<rapidjson::UTF8<>, 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