diff options
author | Yann Herklotz <ymherklotz@gmail.com> | 2018-04-05 15:05:36 +0100 |
---|---|---|
committer | Yann Herklotz <ymherklotz@gmail.com> | 2018-04-05 15:05:36 +0100 |
commit | aeeca45ae53c2382354f26fb4dae02fbda62b314 (patch) | |
tree | 04e94114d58b2fa726943c590d3261ed472507ea /yage/core/spritesheet.cpp | |
parent | 0d10ba12062144a270a368ef9bffd88517a6c27c (diff) | |
download | YAGE-aeeca45ae53c2382354f26fb4dae02fbda62b314.tar.gz YAGE-aeeca45ae53c2382354f26fb4dae02fbda62b314.zip |
Adding test abstraction and fixing tests
Diffstat (limited to 'yage/core/spritesheet.cpp')
-rw-r--r-- | yage/core/spritesheet.cpp | 37 |
1 files changed, 16 insertions, 21 deletions
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 <sstream> #include <stdexcept> +#include <iostream> + 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; } |