From aeeca45ae53c2382354f26fb4dae02fbda62b314 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Thu, 5 Apr 2018 15:05:36 +0100 Subject: Adding test abstraction and fixing tests --- yage/core/spritesheet.cpp | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) (limited to 'yage/core/spritesheet.cpp') diff --git a/yage/core/spritesheet.cpp b/yage/core/spritesheet.cpp index 4ecdcf71..f3f99619 100644 --- a/yage/core/spritesheet.cpp +++ b/yage/core/spritesheet.cpp @@ -18,24 +18,30 @@ #include #include +#include + using rapidjson::Document; using yage::details::Coordinate; using yage::details::SpriteMap; +using std::cout; + namespace yage { SpriteSheet::SpriteSheet(std::string pngFileName, std::string jsonFileName) { int jsonWidth, jsonHeight; + fileLocations_ = parseJson(jsonWidth, jsonHeight, fileContent(jsonFileName)); texture_ = ImageLoader::loadPng(pngFileName); - - if (texture_.width != jsonWidth) + if (texture_.width != jsonWidth) { throw std::runtime_error("JSON width does not match texture width"); - if (texture_.height != jsonHeight) + } + if (texture_.height != jsonHeight) { throw std::runtime_error("JSON height does not match texture height"); + } } std::string SpriteSheet::fileContent(std::string jsonFileName) const @@ -54,27 +60,16 @@ SpriteMap SpriteSheet::parseJson(int &width, int &height, SpriteMap spriteMap; Document jsonAtlas; jsonAtlas.Parse(jsonContent.c_str()); - width = jsonAtlas["width"].GetInt(); height = jsonAtlas["height"].GetInt(); - for (auto &texture : jsonAtlas["sprites"].GetObject()) { - Coordinate coord; - for (auto &value : texture.value.GetObject()) { - std::string keyName{value.value.GetString()}; - int keyValue{value.value.GetInt()}; - if (keyName == "x") { - coord.x = keyValue; - } else if (keyName == "y") { - coord.y = keyValue; - } else if (keyName == "width") { - coord.width = keyValue; - } else if (keyName == "height") { - coord.height = keyValue; - } else { - throw std::runtime_error("JSON key incorrect: " + keyName); - } - } + Coordinate coord; + for(auto &texture : jsonAtlas["sprites"].GetObject()) { + auto texVal = texture.value.GetObject(); + coord.x = texVal["x"].GetInt(); + coord.y = texVal["y"].GetInt(); + coord.width = texVal["width"].GetInt(); + coord.height = texVal["height"].GetInt(); spriteMap[texture.name.GetString()] = coord; } -- cgit