Last active
October 21, 2018 16:55
-
-
Save dejaime/9129611 to your computer and use it in GitHub Desktop.
Example code for parsing a YAML file, as seen in http://www.buildandgun.com/2014/02/yaml-yaml-aint-markup-language.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* * | |
* example.h | |
* | |
* "No" Copyright 2014 Dejaime <dejaime@Funny-Unix> | |
* | |
* http://www.buildandgun.com/2014/02/yaml-yaml-aint-markup-language.html | |
* Released under Public Domain. | |
* More information here: | |
* http://creativecommons.org/publicdomain/zero/1.0/ | |
* | |
*/ | |
#ifndef EXAMPLE_H_INCLUDED_CIKF798GX67UMRO2F5SAF55F5CLXVJ | |
#define EXAMPLE_H_INCLUDED_CIKF798GX67UMRO2F5SAF55F5CLXVJ | |
//128 bit tail, not to conflict with any possible example.h guards. | |
#include <string> | |
#include <vector> | |
#include <inttypes.h> | |
#include "yaml-cpp/yaml.h" | |
struct v2 { | |
unsigned int x; | |
unsigned int y; | |
v2 ():x(0), y(0){} | |
v2 (const v2& cp):x(cp.x), y(cp.y){} | |
v2 & operator = (v2 cp) { | |
x = cp.x; | |
y = cp.y; | |
return *this; | |
} | |
}; | |
struct Animation { | |
typedef std::vector<Animation*> p_vector; | |
v2 m_Offset; | |
v2 m_Size; | |
std::string m_psName; | |
std::vector<uint32_t> m_fvDurations; | |
Animation () {} | |
Animation (std::string p_name, v2 p_offset, v2 p_size) { | |
m_psName = p_name; | |
m_Offset = p_offset; | |
m_Size = p_size; | |
} | |
}; | |
class Sprite { | |
std::string m_psName; | |
std::string m_psSpritesheetPath; | |
Animation::p_vector m_pvAnimations; | |
bool m_pbIsLoaded; | |
public: | |
typedef std::vector<Sprite*> p_vector; | |
Sprite(){ m_pbIsLoaded = false; } | |
~Sprite(); | |
bool IsLoaded () const { return m_pbIsLoaded; } | |
bool Load (std::string file, std::string name); | |
static bool LoadAll (std::string file, p_vector *target); | |
}; | |
bool Sprite::Load (std::string file, std::string name) { | |
if (m_pbIsLoaded) return false; //Already loaded. | |
YAML::Node baseNode = YAML::Load(file); | |
if (baseNode.IsNull()) return false; //File Not Found? | |
YAML::Node spriteNode = baseNode[name]; | |
if (spriteNode.IsNull()) return false; //Sprite Not Found? | |
//Set the name, that we know exists in the file. | |
m_psName = name; | |
//Set the SSheet path by casting the value of the SpriteSheet field | |
m_psSpritesheetPath = spriteNode["SpriteSheet"].as<std::string>(); | |
m_pbIsLoaded = true; //point of no return | |
//Now, we need to parse the info on the animations | |
short int totalAnimations = spriteNode["Anim_Names"].size(); | |
for (unsigned short i = 0; i < totalAnimations; ++i) { | |
//We get the animation by looking up the string value of the | |
// i-th entry on the Anim_Names sequence. | |
std::string tmpName = spriteNode["Anim_Names"][i].as<std::string>(); | |
YAML::Node animNode = spriteNode[ tmpName ]; | |
Animation *tmpAnim = new Animation(); | |
tmpAnim->m_psName = tmpName; | |
tmpAnim->m_Offset.x = animNode["Offset"]["x"].as<unsigned int>(); | |
tmpAnim->m_Offset.y = animNode["Offset"]["y"].as<unsigned int>(); | |
tmpAnim->m_Size.x = animNode["Offset"]["w"].as<unsigned int>(); | |
tmpAnim->m_Size.y = animNode["Offset"]["h"].as<unsigned int>(); | |
unsigned short totalFrames = animNode["Frame_Durations"].size(); | |
for (unsigned short f = 0; f < totalFrames; ++f){ | |
tmpAnim->m_fvDurations.push_back( | |
animNode["Frame_Durations"][f].as<uint32_t>() | |
); | |
}//Finished! | |
} | |
return true; | |
} | |
bool Sprite::LoadAll (std::string file, p_vector *target) { | |
YAML::Node baseNode = YAML::Load(file); | |
if (baseNode.IsNull()) return false; //File Not Found? | |
Sprite *tmpSprite; | |
//For every item (sprite name) listed in the first sequence, we | |
// load it and add to the vector. | |
short int totalSprites = baseNode["Sprites_List"].size(); | |
for (unsigned short i = 0; i < totalSprites; ++i) { | |
tmpSprite = new Sprite(); | |
// TODO (dejaime#1#): Error Handling | |
tmpSprite->Load(file, baseNode["Sprites_List"][i].as<std::string>()); | |
target->push_back(tmpSprite); | |
} | |
//BadPractice warning: | |
// The code above is allocating memory dynamically but isn't catching | |
// any errors. | |
// This can and will lead to memory leaks and crashes in case | |
// the .yaml file has any discrepancies from the expected structure. | |
return true; | |
} | |
Sprite::~Sprite (){ | |
if (m_pbIsLoaded) | |
while (!m_pvAnimations.empty()) { | |
m_pvAnimations.back()->m_fvDurations.clear(); | |
m_pvAnimations.pop_back(); | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment