Added Yaml class for runtime configuration
This commit is contained in:
parent
9ff5edbcd5
commit
76b074220d
3 changed files with 79 additions and 0 deletions
|
@ -8,10 +8,14 @@
|
||||||
#include "Game.h"
|
#include "Game.h"
|
||||||
#include "util/Loader.h"
|
#include "util/Loader.h"
|
||||||
|
|
||||||
|
#include "util/Yaml.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates Game object.
|
* Creates Game object.
|
||||||
*/
|
*/
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
|
Yaml::setFolder("resources/yaml/");
|
||||||
|
|
||||||
Loader::i().setFolder("resources/");
|
Loader::i().setFolder("resources/");
|
||||||
Loader::i().setSubFolder<sf::Texture>("textures/");
|
Loader::i().setSubFolder<sf::Texture>("textures/");
|
||||||
|
|
||||||
|
|
29
source/util/Yaml.cpp
Normal file
29
source/util/Yaml.cpp
Normal file
|
@ -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;
|
||||||
|
}
|
46
source/util/Yaml.h
Normal file
46
source/util/Yaml.h
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
/*
|
||||||
|
* Yaml.h
|
||||||
|
*
|
||||||
|
* Created on: 06.10.2012
|
||||||
|
* Author: Felix
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef DG_YAML_H_
|
||||||
|
#define DG_YAML_H_
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
|
#include <yaml-cpp/yaml.h>
|
||||||
|
|
||||||
|
#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 <typename T>
|
||||||
|
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_ */
|
Reference in a new issue