diff options
author | Yann Herklotz <ymherklotz@gmail.com> | 2017-09-25 17:46:30 +0100 |
---|---|---|
committer | Yann Herklotz <ymherklotz@gmail.com> | 2017-09-25 17:46:30 +0100 |
commit | 5e6067f74e7ac072656f11ead0f22ec7f8e9c525 (patch) | |
tree | ca9e3758832d68ea1d9cf531c48d1962532d8dfa /yage/base/spritesheet.cpp | |
parent | 619746d51e91f02e7c5327f0c65ebb5952632bc7 (diff) | |
download | YAGE-5e6067f74e7ac072656f11ead0f22ec7f8e9c525.tar.gz YAGE-5e6067f74e7ac072656f11ead0f22ec7f8e9c525.zip |
Testing spritesheet
Diffstat (limited to 'yage/base/spritesheet.cpp')
-rw-r--r-- | yage/base/spritesheet.cpp | 81 |
1 files changed, 68 insertions, 13 deletions
diff --git a/yage/base/spritesheet.cpp b/yage/base/spritesheet.cpp index 748cdb5c..311bb955 100644 --- a/yage/base/spritesheet.cpp +++ b/yage/base/spritesheet.cpp @@ -8,6 +8,18 @@ #include "spritesheet.h" +#include <cassert> +#include <fstream> +#include <sstream> +#include <stdexcept> + +#include <rapidjson/reader.h> +#include <yage/base/imageloader.h> + +using namespace std; +using namespace rapidjson; +using namespace yage::details; + namespace yage { @@ -49,14 +61,14 @@ bool SpriteSheetHandler::Double(double d) return handleNumber(static_cast<int>(d)); } -bool SpriteSheetHandler::String(const char *, rapidjson::SizeType, bool) +bool SpriteSheetHandler::String(const char *, SizeType, bool) { return true; } -bool SpriteSheetHandler::Key(const char *str, rapidjson::SizeType length, bool) +bool SpriteSheetHandler::Key(const char *str, SizeType length, bool) { - current_key_ = std::string(str, length); + current_key_ = string(str, length); return true; } @@ -64,16 +76,16 @@ bool SpriteSheetHandler::StartObject() { depth_++; - if(depth_ == 3) { + if (depth_ == 3) { current_image_ = current_key_; } return true; } -bool SpriteSheetHandler::EndObject(rapidjson::SizeType) +bool SpriteSheetHandler::EndObject(SizeType) { - if(depth_ == 3) { + if (depth_ == 3) { map_[current_image_] = coord_; } depth_--; @@ -85,7 +97,7 @@ bool SpriteSheetHandler::StartArray() return true; } -bool SpriteSheetHandler::EndArray(rapidjson::SizeType) +bool SpriteSheetHandler::EndArray(SizeType) { return true; } @@ -95,23 +107,33 @@ SpriteMap SpriteSheetHandler::spriteMap() const return map_; } +int SpriteSheetHandler::imageWidth() const +{ + return image_width_; +} + +int SpriteSheetHandler::imageHeight() const +{ + return image_height_; +} + bool SpriteSheetHandler::handleNumber(int i) { - if(current_key_ == "width") { - if(depth_ == 1) { + if (current_key_ == "width") { + if (depth_ == 1) { image_width_ = i; } else { coord_.width = i; } - } else if(current_key_ == "height") { - if(depth_ == 1) { + } else if (current_key_ == "height") { + if (depth_ == 1) { image_height_ = i; } else { coord_.height = i; } - } else if(current_key_ == "x") { + } else if (current_key_ == "x") { coord_.x = i; - } else if(current_key_ == "y") { + } else if (current_key_ == "y") { coord_.y = i; } return true; @@ -119,4 +141,37 @@ bool SpriteSheetHandler::handleNumber(int i) } // 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(); +} + } // namespace yage |