diff options
author | Yann Herklotz <ymherklotz@gmail.com> | 2017-10-12 14:57:26 +0100 |
---|---|---|
committer | Yann Herklotz <ymherklotz@gmail.com> | 2017-10-12 14:57:26 +0100 |
commit | 1bb0ef8960c71ef505a351702bec54c01ba15e22 (patch) | |
tree | 365811364237f1e55e66f421b93cad8adcf51eda /yage/base/spritesheet.cpp | |
parent | ba733040afb85d4c287a2ec464db05cb86a53fca (diff) | |
download | YAGE-1bb0ef8960c71ef505a351702bec54c01ba15e22.tar.gz YAGE-1bb0ef8960c71ef505a351702bec54c01ba15e22.zip |
Fixing spritesheet and fixed #12
Diffstat (limited to 'yage/base/spritesheet.cpp')
-rw-r--r-- | yage/base/spritesheet.cpp | 33 |
1 files changed, 29 insertions, 4 deletions
diff --git a/yage/base/spritesheet.cpp b/yage/base/spritesheet.cpp index d53f64aa..5c3499cc 100644 --- a/yage/base/spritesheet.cpp +++ b/yage/base/spritesheet.cpp @@ -6,6 +6,9 @@ * ---------------------------------------------------------------------------- */ +/** @file + */ + #include "spritesheet.h" #include <cassert> @@ -25,7 +28,15 @@ namespace yage SpriteSheet::SpriteSheet(string pngFileName, string jsonFileName) { - string fileContents = fileContent(jsonFileName); + int jsonWidth, jsonHeight; + fileLocations_ = + parseJson(jsonWidth, jsonHeight, fileContent(jsonFileName)); + texture_ = ImageLoader::loadPng(pngFileName); + + if (texture_.width != jsonWidth) + throw runtime_error("JSON width does not match texture width"); + if (texture_.height != jsonHeight) + throw runtime_error("JSON height does not match texture height"); } string SpriteSheet::fileContent(string jsonFileName) const @@ -38,7 +49,8 @@ string SpriteSheet::fileContent(string jsonFileName) const return stream.str(); } -SpriteMap SpriteSheet::parseJson(int &width, int &height, const string &jsonContent) const +SpriteMap SpriteSheet::parseJson(int &width, int &height, + string jsonContent) const { SpriteMap spriteMap; Document jsonAtlas; @@ -48,10 +60,23 @@ SpriteMap SpriteSheet::parseJson(int &width, int &height, const string &jsonCont height = jsonAtlas["height"].GetInt(); for (auto &texture : jsonAtlas["sprites"].GetObject()) { - spriteMap[texture.name.GetString()] = Coordinate(); + Coordinate coord; for (auto &value : texture.value.GetObject()) { - /// @todo add the coordinate to the map + 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 runtime_error("JSON key incorrect: " + keyName); + } } + spriteMap[texture.name.GetString()] = coord; } return spriteMap; |