Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Example code for parsing a YAML file, as seen in http://www.buildandgun.com/2014/02/yaml-yaml-aint-markup-language.html

View example.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
/* *
* 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
Something went wrong with that request. Please try again.