Skip to content

Instantly share code, notes, and snippets.

@dejaime dejaime/example.h
Last active Oct 21, 2018

What would you like to do?
Example code for parsing a YAML file, as seen in
/* *
* example.h
* "No" Copyright 2014 Dejaime <dejaime@Funny-Unix>
* Released under Public Domain.
* More information here:
//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;
typedef std::vector<Sprite*> p_vector;
Sprite(){ m_pbIsLoaded = false; }
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){
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>());
//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()) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.