From 5e6067f74e7ac072656f11ead0f22ec7f8e9c525 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Mon, 25 Sep 2017 17:46:30 +0100 Subject: Testing spritesheet --- yage/base/spritesheet.cpp | 81 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 68 insertions(+), 13 deletions(-) (limited to 'yage/base/spritesheet.cpp') 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 +#include +#include +#include + +#include +#include + +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(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 -- cgit