aboutsummaryrefslogtreecommitdiffstats
path: root/yage/base/spritesheet.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'yage/base/spritesheet.cpp')
-rw-r--r--yage/base/spritesheet.cpp33
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;