diff --git a/source/main.cpp b/source/main.cpp index 5006214..291af6f 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -8,10 +8,14 @@ #include "Game.h" #include "util/Loader.h" +#include "util/Yaml.h" + /** * Creates Game object. */ int main(int argc, char* argv[]) { + Yaml::setFolder("resources/yaml/"); + Loader::i().setFolder("resources/"); Loader::i().setSubFolder("textures/"); diff --git a/source/util/Yaml.cpp b/source/util/Yaml.cpp new file mode 100644 index 0000000..c0ca64f --- /dev/null +++ b/source/util/Yaml.cpp @@ -0,0 +1,29 @@ +/* + * Yaml.cpp + * + * Created on: 06.10.2012 + * Author: Felix + */ + +#include "Yaml.h" + +String Yaml::mFolder = ""; + +/** + * Creates a readable object from a YAML file. The path must be relative to the directory + * set in setFolder(). + */ +Yaml::Yaml(const String& filename) { + std::ifstream file(mFolder+filename); + YAML::Parser parser(file); + parser.GetNextDocument(mNode); +} + +/** + * Sets the folder where YAML files are placed. Is added in front of each file name. Allows + * shorter strings as this does not have to be added everywhere. + */ +void +Yaml::setFolder(const String& folder) { + mFolder = folder; +} diff --git a/source/util/Yaml.h b/source/util/Yaml.h new file mode 100644 index 0000000..24dc914 --- /dev/null +++ b/source/util/Yaml.h @@ -0,0 +1,46 @@ +/* + * Yaml.h + * + * Created on: 06.10.2012 + * Author: Felix + */ + +#ifndef DG_YAML_H_ +#define DG_YAML_H_ + +#include + +#include + +#include "String.h" + +/** + * Interface to a YAML file. + */ +class Yaml { +// Public functions. +public: + Yaml(const String& filename); + + static void setFolder(const String& folder); + /** + * Gets a value of a specified type by key. Throws exception if key not found. + * + * @param key The string by which to select the return value. + * @tparam T The type of the return value. + * @return The value of the specified key. + */ + template + T get(const String& key) const { + T tmp; + mNode[key] >> tmp; + return tmp; + }; + +// Private variables. +private: + static String mFolder; + YAML::Node mNode; +}; + +#endif /* DG_YAML_H_ */