85 lines
2.1 KiB
C++
85 lines
2.1 KiB
C++
/*
|
|
* ParseSprite.cpp
|
|
*
|
|
* Created on: Dec 1, 2016
|
|
* Author: tibi
|
|
*/
|
|
|
|
#include <storage/parsers/ParseSprite.h>
|
|
#include <resources/Resources.h>
|
|
|
|
namespace farmlands {
|
|
namespace storage {
|
|
|
|
template <>
|
|
base::Frame* parse<base::Frame> (boost::property_tree::ptree& root)
|
|
{
|
|
// Ensure we are on the scene node (property tree seems to add root of its own)
|
|
if (root.front().first == "Frame")
|
|
root = root.front().second;
|
|
|
|
base::Frame* frame = new base::Frame();
|
|
|
|
// Obtine tile set id
|
|
std::string tileSetPath = root.get<std::string>("<xmlattr>.tileSet");
|
|
frame->tileSetId = resources::ResourceManager::instance().getId(tileSetPath);
|
|
|
|
// Set properties
|
|
frame->tileSetCell = root.get<int>("<xmlattr>.cell");
|
|
frame->width = root.get<uint32_t>("<xmlattr>.w", 1u);
|
|
frame->height = root.get<uint32_t>("<xmlattr>.h", 1u);
|
|
frame->duration = root.get<uint32_t>("<xmlattr>.duration");
|
|
|
|
return frame;
|
|
}
|
|
|
|
template <>
|
|
base::SpriteState* parse<base::SpriteState> (boost::property_tree::ptree& root)
|
|
{
|
|
// Ensure we are on the scene node (property tree seems to add root of its own)
|
|
if (root.front().first == "SpriteState")
|
|
root = root.front().second;
|
|
|
|
base::SpriteState* spriteState = new base::SpriteState();
|
|
spriteState->name = root.get<std::string>("<xmlattr>.name");
|
|
|
|
for (auto child : root)
|
|
{
|
|
if (child.first == "Frame")
|
|
{
|
|
base::Frame* frame = parse<base::Frame>(child.second);
|
|
spriteState->frames.push_back(*frame);
|
|
delete frame;
|
|
}
|
|
}
|
|
|
|
return spriteState;
|
|
}
|
|
|
|
template <>
|
|
base::Sprite* parse<base::Sprite> (boost::property_tree::ptree& root)
|
|
{
|
|
// Ensure we are on the scene node (property tree seems to add root of its own)
|
|
if (root.front().first == "Sprite")
|
|
root = root.front().second;
|
|
|
|
base::Sprite* sprite = new base::Sprite();
|
|
sprite->anchorX = root.get<float>("<xmlattr>.anchorX");
|
|
sprite->anchorY = root.get<float>("<xmlattr>.anchorY");
|
|
|
|
for (auto child : root)
|
|
{
|
|
if (child.first == "State")
|
|
{
|
|
base::SpriteState* state = parse<base::SpriteState>(child.second);
|
|
sprite->addState(*state);
|
|
delete state;
|
|
}
|
|
}
|
|
|
|
return sprite;
|
|
}
|
|
|
|
}
|
|
}
|